Login.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <div class="login login-box">
  3. <div class="login-theme"></div>
  4. <div class="login-body" @keyup.enter="submit('loginForm')">
  5. <div class="login-title">
  6. <img v-if="schoolLogo" :src="schoolLogo" alt="学校logo" />
  7. <h1 v-else>教务处平台</h1>
  8. </div>
  9. <div class="login-form">
  10. <el-form ref="loginForm" :model="loginModel" :rules="loginRules">
  11. <el-form-item prop="loginName">
  12. <el-input
  13. v-model.trim="loginModel.loginName"
  14. placeholder="请输入账号"
  15. name="username"
  16. clearable
  17. >
  18. <i class="icon icon-phone" slot="prefix"></i>
  19. </el-input>
  20. </el-form-item>
  21. <el-form-item prop="password">
  22. <el-input
  23. type="password"
  24. v-model.trim="loginModel.password"
  25. placeholder="请输入密码"
  26. clearable
  27. >
  28. <i class="icon icon-password" slot="prefix"></i>
  29. </el-input>
  30. </el-form-item>
  31. <el-form-item prop="code" v-if="smsCodeRequired">
  32. <div class="vlcode">
  33. <div class="vlcode-right">
  34. <el-button
  35. style="width:100%;"
  36. type="text"
  37. @click="fetchSmsCode"
  38. :disabled="isFetchingCode"
  39. >+{{ codeContent }}</el-button
  40. >
  41. </div>
  42. <div class="vlcode-left">
  43. <el-input
  44. v-model.trim="loginModel.code"
  45. placeholder="请输入手机验证码"
  46. name="code"
  47. clearable
  48. >
  49. <i class="icon icon-checkcode" slot="prefix"></i>
  50. </el-input>
  51. </div>
  52. </div>
  53. </el-form-item>
  54. <el-form-item prop="schoolCode"></el-form-item>
  55. <el-form-item>
  56. <el-button
  57. class="login-submit-btn"
  58. size="large"
  59. type="info"
  60. :disabled="isSubmit"
  61. round
  62. @click="submit('loginForm')"
  63. >登录</el-button
  64. >
  65. </el-form-item>
  66. </el-form>
  67. </div>
  68. </div>
  69. <!-- 修改密码 -->
  70. <reset-pwd
  71. :user-info="userInfo"
  72. ref="ResetPwd"
  73. @modified="resetPwdSuccess"
  74. ></reset-pwd>
  75. </div>
  76. </template>
  77. <script>
  78. import { password, smscode } from "@/plugins/formRules";
  79. import { login, getSmsCode, getSchoolInfo, getSysConfig } from "../api";
  80. import { Base64 } from "@/plugins/crypto";
  81. import ResetPwd from "@/modules/base/components/ResetPwd";
  82. import { ORG_CODE } from "@/constants/app";
  83. import fetchSmsMixins from "../fetchSmsMixins";
  84. export default {
  85. name: "login",
  86. components: { ResetPwd },
  87. mixins: [fetchSmsMixins],
  88. data() {
  89. return {
  90. nameWaitTime: "login",
  91. smsCodeRequired: false,
  92. loginModel: {
  93. schoolCode: ORG_CODE,
  94. loginName: "",
  95. code: "",
  96. password: ""
  97. },
  98. loginRules: {
  99. code: smscode,
  100. password,
  101. loginName: [
  102. {
  103. required: true,
  104. message: "请输入用户名",
  105. trigger: "change"
  106. }
  107. ],
  108. schoolCode: [
  109. {
  110. required: true,
  111. message: "学校编码缺失",
  112. trigger: "change"
  113. }
  114. ]
  115. },
  116. userInfo: {},
  117. roles: [],
  118. isSubmit: false,
  119. schoolLogo: ""
  120. };
  121. },
  122. mounted() {
  123. this.$ls.clear();
  124. this.setWaitingTime();
  125. this.getSchool();
  126. this.getSmsCodeRequired();
  127. },
  128. methods: {
  129. async getSmsCodeRequired() {
  130. const data = await getSysConfig("sys.code.enable");
  131. this.smsCodeRequired = data && data.configValue === "true";
  132. },
  133. async getSchool() {
  134. const data = await getSchoolInfo(ORG_CODE);
  135. this.$ls.set("schoolLogo", data.logo);
  136. this.$ls.set("schoolName", data.name);
  137. this.schoolLogo = data.logo;
  138. },
  139. async submit(name) {
  140. const valid = await this.$refs[name].validate().catch(() => {});
  141. if (!valid) return;
  142. if (this.isSubmit) return;
  143. this.isSubmit = true;
  144. const data = await login({
  145. loginName: this.loginModel.loginName,
  146. password: Base64(this.loginModel.password),
  147. code: this.smsCodeRequired ? this.loginModel.code : null,
  148. schoolCode: this.loginModel.schoolCode
  149. }).catch(() => {});
  150. this.isSubmit = false;
  151. if (!data) return;
  152. if (data.orgInfo)
  153. this.$ls.set("orgId", data.orgInfo.id, this.GLOBAL.authTimeout);
  154. if (data.schoolInfo)
  155. this.$ls.set("schoolId", data.schoolInfo.id, this.GLOBAL.authTimeout);
  156. this.$ls.set("user", data, this.GLOBAL.authTimeout);
  157. this.$ls.set("token", data.accessToken, this.GLOBAL.authTimeout);
  158. // 强制修改密码和绑定手机号
  159. if (
  160. data.userLoginCheckResult &&
  161. (!data.userLoginCheckResult.pwdCount ||
  162. !data.userLoginCheckResult.mobileNumber)
  163. ) {
  164. this.userInfo = {
  165. ...this.loginModel,
  166. ...data.userLoginCheckResult
  167. };
  168. this.$refs.ResetPwd.open();
  169. return;
  170. }
  171. if (data.roleList && data.roleList.includes("ADMIN")) {
  172. this.$router.push({
  173. name: "SelectSchool"
  174. });
  175. } else {
  176. this.$router.push({
  177. name: "Home"
  178. });
  179. }
  180. },
  181. resetPwdSuccess(data) {
  182. if (data.orgInfo)
  183. this.$ls.set("orgId", data.orgInfo.id, this.GLOBAL.authTimeout);
  184. if (data.schoolInfo)
  185. this.$ls.set("schoolId", data.schoolInfo.id, this.GLOBAL.authTimeout);
  186. this.$ls.set("user", data, this.GLOBAL.authTimeout);
  187. this.$ls.set("token", data.accessToken, this.GLOBAL.authTimeout);
  188. if (data.roleList && data.roleList.includes("ADMIN")) {
  189. this.$router.push({
  190. name: "SelectSchool"
  191. });
  192. } else {
  193. this.$router.push({
  194. name: "Home"
  195. });
  196. }
  197. },
  198. // code valid
  199. checkField(field) {
  200. return new Promise((resolve, reject) => {
  201. this.$refs.loginForm.validateField(field, unvalid => {
  202. if (unvalid) {
  203. reject();
  204. } else {
  205. resolve();
  206. }
  207. });
  208. });
  209. },
  210. async fetchSmsCode() {
  211. const validAll = [
  212. this.checkField("loginName"),
  213. this.checkField("password"),
  214. this.checkField("schoolCode")
  215. ];
  216. let result = true;
  217. await Promise.all(validAll).catch(() => {
  218. result = false;
  219. });
  220. if (!result) return;
  221. this.isFetchingCode = true;
  222. const data = await getSmsCode({
  223. loginName: this.loginModel.loginName,
  224. schoolCode: this.loginModel.schoolCode,
  225. password: Base64(this.loginModel.password)
  226. }).catch(() => {
  227. this.isFetchingCode = false;
  228. });
  229. if (!data) return;
  230. if (data.mobileNumber) {
  231. this.$message.success(
  232. `已向手机尾号【${data.mobileNumber.slice(
  233. -4
  234. )}】成功发送短信,请在2分钟内进行验证!`
  235. );
  236. this.changeContent();
  237. } else {
  238. this.isFetchingCode = false;
  239. this.$message.error("未绑定手机号,请先绑定!");
  240. this.userInfo = {
  241. ...data,
  242. loginName: this.loginModel.loginName,
  243. schoolCode: this.loginModel.schoolCode,
  244. password: this.loginModel.password
  245. };
  246. this.$refs.ResetPwd.open();
  247. }
  248. }
  249. }
  250. };
  251. </script>