Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
SunWuyuan committed Nov 9, 2024
1 parent a62cac6 commit 67a913e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
40 changes: 19 additions & 21 deletions src/pages/account/totp/add.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,37 @@
</template>

<script>
import QRCode from 'qrcode';
import QRCode from "qrcode";
import request from "@/axios/axios";
export default {
name: 'TotpGenerator',
name: "TotpGenerator",
data() {
return {
loading: false,
totpCode: '',
authotpUrl: '',
totp_id: '',
qrCodeUrl: '',
errorMessage: ''
totpCode: "",
authotpUrl: "",
totp_id: "",
qrCodeUrl: "",
errorMessage: "",
};
},
methods: {
// 请求 TOTP 生成 URL
async generateTOTP() {
this.loading = true;
this.errorMessage = '';
this.errorMessage = "";
try {
const response = await request.post('/account/totp/generate');
const response = await request.post("/account/totp/generate");
// 获取 otpauth_url
this.authotpUrl = response.data.otpauth_url;
this.totp_id = response.data.totp_id;
// 使用 QRCode 库生成二维码
this.qrCodeUrl = await QRCode.toDataURL(this.authotpUrl);
} catch (error) {
this.errorMessage = error.response?.data?.message || '发生未知错误';
this.errorMessage = error.response?.data?.message || "发生未知错误";
} finally {
this.loading = false;
}
Expand All @@ -101,37 +100,36 @@ export default {
// 激活 TOTP 验证码
async activateTOTP() {
if (!this.totpCode) {
this.errorMessage = '请输入验证码';
this.errorMessage = "请输入验证码";
return;
}
this.loading = true;
this.errorMessage = '';
this.errorMessage = "";
try {
const response = await request.post('/account/totp/activate', {
const response = await request.post("/account/totp/activate", {
totp_token: this.totpCode,
totp_id: this.totp_id
totp_id: this.totp_id,
});
if (response.data.status === 'success') {
if (response.data.status === "success") {
this.$toast.add({
severity: "success",
summary: "成功",
detail: "验证器激活成功",
life: 3000,
});
} else {
this.errorMessage = '验证码无效,请重试';
this.errorMessage = "验证码无效,请重试";
}
} catch (error) {
this.errorMessage = error.response?.data?.message || '发生未知错误';
this.errorMessage = error.response?.data?.message || "发生未知错误";
} finally {
this.loading = false;
}
}
}
},
},
};
</script>
Expand Down
29 changes: 12 additions & 17 deletions src/pages/account/totp/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
<v-container>
<!-- Add New TOTP Button -->

<v-btn
to="./totp/add"
color="primary"
class="mb-4"
>
<v-icon left>mdi-plus</v-icon>新的验证器
</v-btn>

<v-btn to="./totp/add" color="primary" class="mb-4">
<v-icon left>mdi-plus</v-icon>新的验证器
</v-btn>

<!-- TOTP List Cards -->
<v-row
Expand Down Expand Up @@ -40,20 +35,23 @@
>
</v-card-text>

<v-card-actions><v-btn color="error" variant="text" @click="deleteTOTP(totp.id)" border>删除</v-btn>
<v-card-actions
><v-btn
color="error"
variant="text"
@click="deleteTOTP(totp.id)"
border
>删除</v-btn
>
<v-spacer></v-spacer>

<v-btn
text="关闭"
@click="isActive.value = false"
></v-btn>
<v-btn text="关闭" @click="isActive.value = false"></v-btn>
</v-card-actions>
</v-card>
</template>
</v-dialog>
</v-col>
</v-row>

</v-container>
</template>

Expand Down Expand Up @@ -85,7 +83,6 @@ export default {
};
},
methods: {
// 获取TOTP列表
async fetchTOTPList() {
this.loading = true;
Expand Down Expand Up @@ -113,8 +110,6 @@ export default {
}
},
// 提交重命名
async renameTOTP(totpid, newname) {
if (newname == "") {
Expand Down

0 comments on commit 67a913e

Please sign in to comment.