index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <div class="container">
  3. <n-form ref="formRef" label-placement="left" :label-width="100" :model="form" :rules="rules" size="large" require-mark-placement="left">
  4. <n-form-item path="loginCode">
  5. <n-input v-model:value="form.loginCode" :placeholder="t('common.login.usernameTip')">
  6. <template #prefix>
  7. <span class="bqfl-iconfont icon-yonghuming"></span>
  8. </template>
  9. </n-input>
  10. </n-form-item>
  11. <n-form-item path="password">
  12. <n-input v-model:value="form.password" type="password" show-password-on="click" :placeholder="t('common.login.passwordTip')">
  13. <template #prefix>
  14. <span class="bqfl-iconfont icon-mima"></span> </template></n-input>
  15. <a href="#" class="forgot" @click="handleForgot"> {{t('common.login.forgetPassword')}}</a>
  16. </n-form-item>
  17. <n-form-item path="validCode">
  18. <n-input v-model:value="form.validCode" :placeholder="t('common.login.validCodeTip')">
  19. <template #prefix>
  20. <span class="bqfl-iconfont icon-yanzhengma"></span>
  21. </template>
  22. <template #suffix>
  23. <img :src="getValidCodeImg" @click="refreshValidCodeImg" class="cursor-pointer" width="100" />
  24. </template>
  25. </n-input>
  26. </n-form-item>
  27. </n-form>
  28. <n-button class="login-btn" attr-type="button" type="info" color="#18A058" :disabled="submitLoading" @click="handleLogin">
  29. {{ submitLoading ? t('common.login.submitting') : t('common.login.title') }}
  30. </n-button>
  31. <p class="register-btn">
  32. {{t('common.login.noAccount')}}
  33. <span @click="handleRegister">{{t('common.login.register')}}</span>
  34. </p>
  35. </div>
  36. </template>
  37. <script lang="ts" setup>
  38. import { useI18n } from "#imports";
  39. import { ref, reactive, onMounted } from "vue";
  40. import { createDiscreteApi } from "naive-ui";
  41. import { useUserStore } from "@/store/user";
  42. const config = useRuntimeConfig();
  43. const baseUrl = ref(config.public.apiBase);
  44. const emit = defineEmits(["closeSginDialog"]);
  45. const userStore = useUserStore();
  46. const { t, locale, setLocale } = useI18n();
  47. const temptime = ref();
  48. const formRef = ref();
  49. const submitLoading = ref(false);
  50. const form: Login = reactive({
  51. loginCode: "",
  52. password: "",
  53. });
  54. const rules: FormRules = {
  55. loginCode: [
  56. {
  57. required: true,
  58. message: t("common.login.usernameTip"),
  59. trigger: ["input", "blur"],
  60. },
  61. ],
  62. password: [
  63. {
  64. required: true,
  65. message: t("common.login.passwordTip"),
  66. trigger: ["input", "blur"],
  67. },
  68. ],
  69. validCode: [
  70. {
  71. required: true,
  72. message: t("common.login.validCodeTip"),
  73. trigger: ["input", "blur"],
  74. },
  75. ],
  76. };
  77. const getValidCodeImg = ref<string>("");
  78. const refreshValidCodeImg = () => {
  79. temptime.value = +new Date().getTime();
  80. getValidCodeImg.value =
  81. baseUrl.value + `/report/websiteUser/getValidCode?unTime=${temptime.value}`;
  82. };
  83. refreshValidCodeImg();
  84. const message = createDiscreteApi(["message"]);
  85. const handleLogin = () => {
  86. formRef.value?.validate(async (errors: any) => {
  87. if (!errors) {
  88. submitLoading.value = true;
  89. const params = JSON.parse(JSON.stringify(form));
  90. params.loginCode = encryptByBase64(params.loginCode);
  91. params.password = encryptByBase64(params.password);
  92. params.unTime = temptime.value;
  93. try {
  94. const { code, data } = await login_Api(params);
  95. if (code === 200) {
  96. userStore.setToken(data?.token);
  97. userStore.setUserInfo(data?.user);
  98. emit("closeSginDialog", "success");
  99. message.message.success(t("common.login.success"));
  100. } else {
  101. refreshValidCodeImg();
  102. form.validCode = "";
  103. }
  104. submitLoading.value = false;
  105. } catch (error) {
  106. submitLoading.value = false;
  107. }
  108. }
  109. });
  110. };
  111. const handleForgot = () => {
  112. emit("closeSginDialog", "forgot");
  113. };
  114. const handleRegister = () => {
  115. emit("closeSginDialog", "register");
  116. };
  117. </script>
  118. <style lang="scss" scoped>
  119. .container {
  120. width: 100%;
  121. padding: 30px 20px;
  122. .login-btn {
  123. width: 100%;
  124. height: 50px;
  125. color: #fff;
  126. font-size: 18px;
  127. border-radius: 8px;
  128. }
  129. .register-btn {
  130. font-size: 18px;
  131. color: #808080;
  132. text-align: center;
  133. span {
  134. color: #f0a226;
  135. cursor: pointer;
  136. }
  137. }
  138. .forgot {
  139. margin-left: 10px;
  140. font-size: 14px;
  141. color: #00c2ff;
  142. text-decoration: underline;
  143. flex-shrink: 0;
  144. }
  145. .cursor-pointer {
  146. cursor: pointer;
  147. }
  148. :deep(input::-ms-reveal) {
  149. display: none;
  150. }
  151. }
  152. </style>