bind.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <script setup lang="ts">
  2. import { reactive, ref } from "vue";
  3. import { message } from "@/utils/message";
  4. import type { FormInstance } from "element-plus";
  5. import { cloneDeep } from "@pureadmin/utils";
  6. import * as $api from "@/api/auth/index";
  7. defineOptions({ name: "templateEdit" });
  8. const isDetail = ref(false);
  9. const formRef = ref<FormInstance>();
  10. // 初始化数据
  11. const initData = {
  12. code: undefined
  13. };
  14. // 表单校验规则
  15. const formRules = reactive({
  16. code: [{ required : true, message: '验证码不能为空'}]
  17. });
  18. // 页面数据
  19. const pageData: any = reactive({
  20. code: "",
  21. // 表单数据
  22. formData: initData,
  23. formRules: {
  24. code: [{ required: true, message: "请选择验证码", trigger: "blur" }]
  25. }
  26. });
  27. // 暴露给父级调用
  28. const emits = defineEmits(["ok", "close"]);
  29. const qrcode = ref("");
  30. // 打开弹窗
  31. const open = (data: any, dataSource: any, mode: string) => {
  32. $api.createGoogleOtp().then((res: any) => {
  33. qrcode.value = res.message;
  34. });
  35. isDetail.value = false;
  36. pageData.dialogVisible = true;
  37. };
  38. // 关闭弹窗
  39. const handleClose = () => {
  40. pageData.dialogVisible = false;
  41. emits("close");
  42. };
  43. // 确定并关闭弹窗
  44. const handleConfirm = () => {
  45. formRef.value!.validate((isValid: boolean) => {
  46. if (isValid) {
  47. _save();
  48. } else {
  49. message("表单校验失败", { type: "warning" });
  50. }
  51. });
  52. };
  53. const _save = () => {
  54. pageData.formLoading = true;
  55. const _data = cloneDeep(pageData.formData);
  56. $api
  57. .save(_data)
  58. .then((res: any) => {
  59. if (res.success) {
  60. _confirm();
  61. } else {
  62. message(res.message, { type: "warning" });
  63. }
  64. })
  65. .finally(() => {
  66. pageData.formLoading = false;
  67. });
  68. };
  69. const _confirm = () => {
  70. pageData.dialogVisible = false;
  71. emits("ok");
  72. };
  73. defineExpose({ open });
  74. const showClose = ref(false);
  75. </script>
  76. <template>
  77. <el-dialog v-model="pageData.dialogVisible" :show-close="showClose" :width="500">
  78. <template #header>
  79. <el-text class="mx-1" type="primary" size="large">绑定谷歌验证器</el-text>
  80. </template>
  81. <div class="el-dialog-content">
  82. <el-form
  83. :model="pageData.formData"
  84. ref="formRef"
  85. style="width: 90%; margin: 20px auto 0"
  86. label-width="auto"
  87. :rules="pageData.formRules"
  88. >
  89. <el-form-item label="二维码">
  90. <img :src="qrcode" />
  91. </el-form-item>
  92. <el-form-item label="验证码" prop="code">
  93. <el-input v-model="pageData.formData.code"></el-input>
  94. </el-form-item>
  95. </el-form>
  96. </div>
  97. <template #footer>
  98. <el-button type="primary" @click="handleConfirm">确认</el-button>
  99. </template>
  100. </el-dialog>
  101. </template>