Login.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <div class="login">
  3. <main class="login-main">
  4. <div class="login-title">
  5. <p>欢迎登录</p>
  6. <p>题库管理系统</p>
  7. </div>
  8. <div class="login-footer">
  9. Copyright &copy; 2021
  10. <a href="https://www.qmth.com.cn" target="_block">启明泰和</a>.<a
  11. href="https://beian.miit.gov.cn/"
  12. target="_blank"
  13. >鄂ICP备12000033号-13</a
  14. >
  15. <span v-if="version" class="margin-left-10">v{{ version }}</span>
  16. </div>
  17. <div class="login-body" @keyup.enter="submit">
  18. <h1 class="login-body-title">题库管理系统</h1>
  19. <el-form ref="loginForm" :model="loginInfo" :rules="loginRules">
  20. <el-form-item prop="accountValue">
  21. <el-input
  22. v-model.trim="loginInfo.accountValue"
  23. placeholder="请输入账号"
  24. clearable
  25. >
  26. <i slot="prefix" class="icon icon-username"></i>
  27. </el-input>
  28. </el-form-item>
  29. <el-form-item prop="password">
  30. <el-input
  31. v-model.trim="loginInfo.password"
  32. type="password"
  33. placeholder="请输入密码"
  34. clearable
  35. >
  36. <i slot="prefix" class="icon icon-password"></i>
  37. </el-input>
  38. </el-form-item>
  39. <el-form-item v-if="dialogVisible" prop="smsCode">
  40. <el-input
  41. v-model.trim="loginInfo.smsCode"
  42. placeholder="请输入短信验证码"
  43. clearable=""
  44. >
  45. <i slot="prefix" class="icon icon-code"></i>
  46. </el-input>
  47. </el-form-item>
  48. <el-form-item>
  49. <el-button
  50. class="login-submit-btn"
  51. type="danger"
  52. :disabled="isSubmit"
  53. @click="submit"
  54. >登录</el-button
  55. >
  56. </el-form-item>
  57. </el-form>
  58. </div>
  59. </main>
  60. </div>
  61. </template>
  62. <script>
  63. import { mapActions, mapState } from "vuex";
  64. import { USER_SIGNIN } from "../store/user";
  65. import { QUESTION_API } from "@/constants/constants";
  66. export default {
  67. name: "Login",
  68. data() {
  69. return {
  70. isSubmit: false,
  71. dialogVisible: false,
  72. loginInfo: {
  73. rootOrgId: "",
  74. domain: "",
  75. code: "",
  76. accountType: "COMMON_LOGIN_NAME",
  77. accountValue: "",
  78. password: "",
  79. smsCode: null,
  80. },
  81. loginRules: {
  82. accountValue: [
  83. {
  84. required: true,
  85. message: "请输入用户名",
  86. trigger: "change",
  87. },
  88. ],
  89. password: [
  90. {
  91. validator: (rule, value, callback) => {
  92. if (
  93. value &&
  94. (/^([0-9]+|[a-z]+|[A-Z]+)$/.test(value) ||
  95. !/^[a-zA-Z0-9_]+$/.test(value))
  96. ) {
  97. callback(
  98. new Error("密码只能大写字母、小写字母、数字至少两种组成")
  99. );
  100. } else {
  101. callback();
  102. }
  103. },
  104. trigger: "change",
  105. },
  106. {
  107. required: true,
  108. min: 8,
  109. max: 16,
  110. message: "密码只能8-16个字符",
  111. trigger: "change",
  112. },
  113. ],
  114. // password: [
  115. // {
  116. // required: true,
  117. // pattern: /^[a-zA-Z0-9_]{1,20}$/,
  118. // message: "密码只能由数字、字母和下划线组成,长度1-20个字符",
  119. // trigger: "change",
  120. // },
  121. // ],
  122. smsCode: [
  123. {
  124. required: true,
  125. message: "请输入短信验证码",
  126. trigger: "change",
  127. },
  128. ],
  129. },
  130. };
  131. },
  132. computed: {
  133. ...mapState(["version"]),
  134. },
  135. watch: {
  136. $route(to) {
  137. this.loginInfo.rootOrgId = to.query.orgId;
  138. this.loginInfo.code = to.query.code;
  139. },
  140. },
  141. created() {
  142. if (this.$route.hash && this.$route.hash.startsWith("#/access?")) {
  143. this.$router.push(this.$route.hash.slice(1));
  144. return;
  145. }
  146. this.loginInfo.domain = window.location.hostname.split(".")[0];
  147. sessionStorage.clear();
  148. var params = this.$route.query;
  149. this.loginInfo.rootOrgId = params.orgId;
  150. this.loginInfo.code = params.code;
  151. },
  152. methods: {
  153. ...mapActions([USER_SIGNIN]),
  154. async submit() {
  155. const valid = await this.$refs.loginForm.validate().catch(() => {});
  156. if (!valid) return;
  157. var url = QUESTION_API + "/auth/login";
  158. this.$httpWithMsg
  159. .post(url, this.loginInfo)
  160. .then((response) => {
  161. var user = response.data;
  162. user.questionUnlock = false;
  163. this.USER_SIGNIN(user);
  164. this.$router.replace({ path: "/questions/tips" });
  165. this.$notify({
  166. message: "登录成功",
  167. type: "success",
  168. duration: 1000,
  169. });
  170. })
  171. .catch((error) => {
  172. if (error.response.data.code === "UQ-003100") {
  173. this.dialogVisible = true;
  174. }
  175. });
  176. },
  177. loginWithSms() {
  178. this.dialogVisible = false;
  179. this.submit();
  180. },
  181. },
  182. };
  183. </script>