LoginDialog.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <template>
  2. <el-dialog
  3. v-model="visible"
  4. :title="L['loginDialog']['登录']"
  5. width="500px"
  6. :before-close="handleClose"
  7. align-center
  8. modal-class="login-dialog-model"
  9. >
  10. <div class="login-form">
  11. <el-form
  12. ref="ruleFormRef"
  13. :model="ruleForm"
  14. :rules="rules"
  15. label-position="top"
  16. >
  17. <el-form-item class="login-contact" :label="L['loginDialog']['邮箱']" prop="email" v-if="contactType==='email'">
  18. <el-input v-model="ruleForm.email" clearable />
  19. <div v-if="contactExistMsg" class="login-contact-error">{{ contactExistMsg }}</div>
  20. </el-form-item>
  21. <el-form-item class="login-contact" :label="L['loginDialog']['手机']" prop="mobile" v-if="contactType==='mobile'">
  22. <el-input v-model="ruleForm.mobile" clearable />
  23. <div v-if="contactExistMsg" class="login-contact-error">{{ contactExistMsg }}</div>
  24. </el-form-item>
  25. <el-form-item
  26. class="item-password"
  27. :label="L['loginDialog']['密码']"
  28. prop="password"
  29. >
  30. <el-input v-model="ruleForm.password" show-password type="password" />
  31. <div class="actions">
  32. <el-button type="text" @click="onForgotPassword">{{
  33. L["忘记密码"]
  34. }}</el-button>
  35. </div>
  36. </el-form-item>
  37. </el-form>
  38. </div>
  39. <div class="login-bottom">
  40. <el-button
  41. class="login-btn"
  42. :loading="loginLoading"
  43. @click="onLogin"
  44. round
  45. >{{ L["loginDialog"]["登录"] }}</el-button
  46. >
  47. <el-button
  48. type="default"
  49. class="register-btn"
  50. @click="onRegister"
  51. round
  52. >{{ L["注册"] }}</el-button
  53. >
  54. </div>
  55. </el-dialog>
  56. </template>
  57. <script setup>
  58. import { ref, reactive, defineExpose } from "vue";
  59. import { getCurLanguage, checkEmail } from "@/composables/common.js";
  60. import { useFiltersStore } from "@/store/filter.js";
  61. import { getContactType } from "../utils/common";
  62. const filtersStore = useFiltersStore();
  63. const contactType = getContactType();
  64. const L = getCurLanguage();
  65. const emit = defineEmits(["login", "register", "forgot", "close"]);
  66. const visible = ref(false);
  67. const loginLoading = ref(false);
  68. const contactExistMsg = ref('')
  69. const ruleFormRef = ref();
  70. const ruleForm = reactive({
  71. email: "",
  72. mobile: "",
  73. password: "",
  74. });
  75. const checkEmailFun = (rule, value, callback) => {
  76. if (checkEmail(value) !== true) {
  77. callback(new Error(checkEmail(value)));
  78. }
  79. callback();
  80. };
  81. const checkMobileFun = (rule, value, callback) => {
  82. if (checkPhone(value) !== true) {
  83. callback(new Error(checkPhone(value)));
  84. }
  85. callback();
  86. };
  87. const rules = reactive({
  88. email: [
  89. { required: true, message: L["请输入邮箱"], trigger: "blur" },
  90. { validator: checkEmailFun, trigger: "blur" },
  91. ],
  92. mobile: [
  93. { required: true, message: L["请输入手机号"], trigger: "blur" },
  94. { validator: checkMobileFun, trigger: "blur" },
  95. ],
  96. password: [
  97. {
  98. required: true,
  99. message: L["请输入密码"],
  100. trigger: "change",
  101. },
  102. ],
  103. });
  104. const open = () => {
  105. visible.value = true;
  106. };
  107. const close = () => {
  108. visible.value = false;
  109. };
  110. // 关闭弹窗
  111. const handleClose = () => {
  112. close();
  113. emit("close");
  114. };
  115. const login = () => {
  116. loginLoading.value = true;
  117. post("v3/frontLogin/oauth/token", {
  118. username: contactType === 'email' ? ruleForm.email : ruleForm.mobile,
  119. password: ruleForm.password,
  120. loginType: 1,
  121. })
  122. .then((res) => {
  123. if (res.state == 200) {
  124. //将用户信息存缓存,并跳转到首页
  125. filtersStore.setLoginStatus(true);
  126. filtersStore.setToken(res.data.access_token);
  127. filtersStore.setRefreshToken(res.data.refresh_token);
  128. filtersStore.setTime(new Date().getTime().toString()); //存储refresh_token更新时间
  129. //获取用户信息,并同步信息到pinia
  130. contactExistMsg.value = ''
  131. return res;
  132. }
  133. contactExistMsg.value = res.msg;
  134. return Promise.reject();
  135. })
  136. .then(() => {
  137. return get("v3/member/front/member/getInfo");
  138. })
  139. .then((res) => {
  140. if (res.state == 200) {
  141. filtersStore.setMemberInfo(res.data);
  142. showMessage({
  143. message: L['loginDialog']['登录成功'],
  144. type: "success",
  145. });
  146. close()
  147. } else {
  148. showMessage({
  149. message: res.msg,
  150. type: "error",
  151. });
  152. }
  153. })
  154. .finally(() => {
  155. loginLoading.value = false;
  156. });
  157. };
  158. // 登陆操作
  159. const onLogin = async () => {
  160. if (!ruleFormRef.value) return;
  161. await ruleFormRef.value.validate((valid, fields) => {
  162. if (valid) {
  163. return login();
  164. }
  165. });
  166. };
  167. // 注册
  168. const onRegister = () => {
  169. emit("register");
  170. close();
  171. };
  172. // 忘记密码
  173. const onForgotPassword = () => {
  174. emit("forgot");
  175. };
  176. defineExpose({ open });
  177. </script>
  178. <style lang="scss">
  179. .login-dialog-model {
  180. .el-dialog__body {
  181. padding: 20px 40px;
  182. }
  183. }
  184. </style>
  185. <style lang="scss" scoped>
  186. .login-form {
  187. display: flex;
  188. justify-content: center;
  189. :deep(.el-input) {
  190. width: 300px;
  191. }
  192. :deep(.el-input__inner) {
  193. height: 35px;
  194. }
  195. .item-password {
  196. position: relative;
  197. .actions {
  198. position: absolute;
  199. right: 0;
  200. top: -35px;
  201. .el-button {
  202. color: #606266;
  203. }
  204. }
  205. }
  206. .login-contact {
  207. position: relative;
  208. margin-bottom: 25px;
  209. &-error {
  210. position: absolute;
  211. left: 0;
  212. bottom: -30px;
  213. color: red;
  214. }
  215. }
  216. }
  217. .login-bottom {
  218. display: flex;
  219. flex-direction: column;
  220. justify-content: center;
  221. align-items: center;
  222. :deep(.el-button) {
  223. width: 300px;
  224. height: 35px;
  225. margin-left: 0;
  226. margin-top: 10px;
  227. }
  228. .login-btn {
  229. background-color: $colorMain;
  230. color: #fff;
  231. }
  232. .register-btn {
  233. &:hover {
  234. color: $colorMain;
  235. }
  236. }
  237. }
  238. </style>