Login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <template>
  2. <div class="login login-box" @keyup.enter="submit('loginForm')">
  3. <el-tabs class="login-menu" v-model="loginType">
  4. <el-tab-pane label="密码登录" name="ACCOUNT"></el-tab-pane>
  5. <el-tab-pane
  6. v-if="schoolInfo.phoneLogin"
  7. label="短信登录"
  8. name="PHONE"
  9. ></el-tab-pane>
  10. </el-tabs>
  11. <div class="login-form">
  12. <el-form
  13. v-if="IS_USERNAME_TYPE"
  14. ref="loginForm"
  15. size="large"
  16. :model="loginModel"
  17. :rules="loginRules"
  18. >
  19. <el-form-item prop="loginName">
  20. <el-input
  21. v-model.trim="loginModel.loginName"
  22. placeholder="请输入你的账号"
  23. name="username"
  24. clearable
  25. >
  26. </el-input>
  27. </el-form-item>
  28. <el-form-item prop="password">
  29. <el-input
  30. v-model.trim="loginModel.password"
  31. type="password"
  32. placeholder="请输入你的密码"
  33. name="password"
  34. show-password
  35. clearable
  36. >
  37. </el-input>
  38. </el-form-item>
  39. <el-form-item v-if="schoolInfo.accountSmsVerify" prop="code">
  40. <el-input
  41. v-model.trim="loginModel.code"
  42. placeholder="请输入手机验证码"
  43. name="code"
  44. clearable
  45. class="code-input"
  46. >
  47. <template slot="suffix">
  48. <el-button
  49. class="code-btn"
  50. type="text"
  51. size="medium"
  52. :disabled="isFetchingCode"
  53. @click="fetchAccountSmsCode"
  54. >{{ codeContent }}</el-button
  55. >
  56. </template>
  57. </el-input>
  58. </el-form-item>
  59. <el-form-item>
  60. <el-button
  61. class="login-submit-btn"
  62. type="info"
  63. :disabled="isSubmit"
  64. @click="submit('loginForm')"
  65. >登录</el-button
  66. >
  67. </el-form-item>
  68. </el-form>
  69. <el-form
  70. v-else
  71. ref="loginForm"
  72. size="large"
  73. :model="loginModel"
  74. :rules="loginRules"
  75. >
  76. <el-form-item prop="mobileNumber">
  77. <el-input
  78. v-model.trim="loginModel.mobileNumber"
  79. placeholder="请输入你的手机号"
  80. name="mobileNumber"
  81. clearable
  82. >
  83. </el-input>
  84. </el-form-item>
  85. <el-form-item prop="code">
  86. <el-input
  87. v-model.trim="loginModel.code"
  88. placeholder="请输入你的验证码"
  89. name="code"
  90. clearable
  91. class="code-input"
  92. >
  93. <template slot="suffix">
  94. <el-button
  95. class="code-btn"
  96. type="text"
  97. size="medium"
  98. :disabled="isFetchingCode"
  99. @click.stop="fetchSmsCode"
  100. >{{ codeContent }}</el-button
  101. >
  102. </template>
  103. </el-input>
  104. </el-form-item>
  105. <el-form-item>
  106. <el-button
  107. class="login-submit-btn"
  108. type="info"
  109. :disabled="isSubmit"
  110. @click="submit('loginForm')"
  111. >登录</el-button
  112. >
  113. </el-form-item>
  114. </el-form>
  115. </div>
  116. <!-- 修改密码 -->
  117. <reset-pwd
  118. :user-info="userInfo"
  119. ref="ResetPwd"
  120. @modified="resetPwdSuccess"
  121. @cancel="resetCancel"
  122. ></reset-pwd>
  123. </div>
  124. </template>
  125. <script>
  126. import { phone, smscode } from "@/plugins/formRules";
  127. import { login, getSmsCode, getSchoolInfo, getAccountSmsCode } from "../api";
  128. import { Base64 } from "@/plugins/crypto";
  129. import { MD5 } from "@/plugins/md5";
  130. import ResetPwd from "@/modules/base/components/ResetPwd.vue";
  131. import { getOrgCode } from "@/constants/app";
  132. import fetchSmsMixins from "../fetchSmsMixins";
  133. export default {
  134. name: "login",
  135. components: { ResetPwd },
  136. mixins: [fetchSmsMixins],
  137. data() {
  138. return {
  139. nameWaitTime: "login",
  140. smsCodeRequired: false,
  141. loginModel: {
  142. schoolCode: "",
  143. loginName: "",
  144. password: "",
  145. code: "",
  146. mobileNumber: "",
  147. },
  148. loginRules: {
  149. code: smscode,
  150. password: [
  151. {
  152. required: true,
  153. message: "请输入密码",
  154. trigger: "change",
  155. },
  156. ],
  157. loginName: [
  158. {
  159. required: true,
  160. message: "请输入用户名",
  161. trigger: "change",
  162. },
  163. ],
  164. schoolCode: [
  165. {
  166. required: true,
  167. message: "学校编码缺失",
  168. trigger: "change",
  169. },
  170. ],
  171. mobileNumber: phone,
  172. },
  173. userInfo: {},
  174. roles: [],
  175. isSubmit: false,
  176. loginType: "ACCOUNT",
  177. schoolInfo: {
  178. accountSmsVerify: false,
  179. phoneLogin: false,
  180. },
  181. };
  182. },
  183. mounted() {
  184. window.sessionStorage.removeItem("routeDomainCode");
  185. window.sessionStorage.removeItem("paramDomainCode");
  186. this.$ls.clear();
  187. this.setWaitingTime();
  188. this.getSchool();
  189. },
  190. computed: {
  191. IS_USERNAME_TYPE() {
  192. return this.loginType === "ACCOUNT";
  193. },
  194. switchBtnName() {
  195. return this.loginType === "ACCOUNT" ? "短信登录" : "账号登录";
  196. },
  197. },
  198. methods: {
  199. async getSchool() {
  200. if (this.$route.params.code) {
  201. this.loginModel.schoolCode = this.$route.params.code;
  202. window.sessionStorage.setItem(
  203. "routeDomainCode",
  204. this.$route.params.code
  205. );
  206. } else {
  207. this.loginModel.schoolCode = getOrgCode();
  208. }
  209. const data = await getSchoolInfo(this.loginModel.schoolCode);
  210. this.$ls.set("schoolLogo", data.logo);
  211. this.$ls.set("schoolName", data.name);
  212. this.$parent.version = data.version || "";
  213. this.$parent.schoolInfo = data;
  214. this.schoolInfo = data;
  215. this.loginModel.schoolCode = data.schoolCode;
  216. this.$ls.set("schoolInfo", data);
  217. },
  218. async submit(name) {
  219. const valid = await this.$refs[name].validate().catch(() => {});
  220. if (!valid) return;
  221. if (this.isSubmit) return;
  222. this.isSubmit = true;
  223. let datas = {
  224. schoolCode: this.loginModel.schoolCode,
  225. type: this.loginType,
  226. };
  227. if (this.IS_USERNAME_TYPE) {
  228. datas = {
  229. ...datas,
  230. loginName: this.loginModel.loginName,
  231. password: Base64(this.loginModel.password),
  232. };
  233. if (this.schoolInfo.accountSmsVerify) datas.code = this.loginModel.code;
  234. } else {
  235. datas = {
  236. ...datas,
  237. code: this.loginModel.code,
  238. mobileNumber: this.loginModel.mobileNumber,
  239. };
  240. }
  241. const data = await login(datas).catch(() => {});
  242. this.isSubmit = false;
  243. if (!data) return;
  244. if (data.orgInfo)
  245. this.$ls.set("orgId", data.orgInfo.id, this.GLOBAL.authTimeout);
  246. if (data.schoolInfo && data.schoolInfo.length) {
  247. const curSchool = data.schoolInfo.find(
  248. (item) => item.code === this.loginModel.schoolCode
  249. );
  250. if (curSchool) {
  251. this.$ls.set("schoolId", curSchool.id, this.GLOBAL.authTimeout);
  252. this.$ls.set("schoolName", curSchool.name, this.GLOBAL.authTimeout);
  253. }
  254. }
  255. this.$ls.set(
  256. "user",
  257. { ...data, pwd: MD5(this.loginModel.password) },
  258. this.GLOBAL.authTimeout
  259. );
  260. this.$ls.set("token", data.accessToken, this.GLOBAL.authTimeout);
  261. // 强制修改密码和绑定手机号
  262. if (
  263. data.userLoginCheckResult &&
  264. (!data.userLoginCheckResult.pwdCount ||
  265. (!data.userLoginCheckResult.mobileNumber &&
  266. this.schoolInfo.phoneLogin))
  267. ) {
  268. this.userInfo = {
  269. ...this.loginModel,
  270. ...data.userLoginCheckResult,
  271. phoneLogin: this.schoolInfo.phoneLogin,
  272. };
  273. this.$refs.ResetPwd.open();
  274. return;
  275. }
  276. if (data.roleList && data.roleList.includes("ADMIN")) {
  277. this.$router.push({
  278. name: "SelectSchool",
  279. });
  280. } else {
  281. this.$router.push({
  282. name: "Home",
  283. });
  284. }
  285. },
  286. resetPwdSuccess(data) {
  287. if (data.orgInfo)
  288. this.$ls.set("orgId", data.orgInfo.id, this.GLOBAL.authTimeout);
  289. if (data.schoolInfo && data.schoolInfo.length) {
  290. const curSchool = data.schoolInfo.find(
  291. (item) => item.code === this.loginModel.schoolCode
  292. );
  293. if (curSchool) {
  294. this.$ls.set("schoolId", curSchool.id, this.GLOBAL.authTimeout);
  295. this.$ls.set("schoolName", curSchool.name, this.GLOBAL.authTimeout);
  296. }
  297. }
  298. this.$ls.set(
  299. "user",
  300. { ...data, pwd: MD5(this.loginModel.password) },
  301. this.GLOBAL.authTimeout
  302. );
  303. this.$ls.set("token", data.accessToken, this.GLOBAL.authTimeout);
  304. if (data.roleList && data.roleList.includes("ADMIN")) {
  305. this.$router.push({
  306. name: "SelectSchool",
  307. });
  308. } else {
  309. this.$router.push({
  310. name: "Home",
  311. });
  312. }
  313. },
  314. resetCancel() {
  315. this.$ls.remove("orgId");
  316. this.$ls.remove("schoolId");
  317. this.$ls.remove("schoolName");
  318. this.$ls.remove("user");
  319. this.$ls.remove("token");
  320. },
  321. // code valid
  322. checkField(field) {
  323. return new Promise((resolve, reject) => {
  324. this.$refs.loginForm.validateField(field, (unvalid) => {
  325. if (unvalid) {
  326. reject();
  327. } else {
  328. resolve();
  329. }
  330. });
  331. });
  332. },
  333. async fetchSmsCode() {
  334. let result = true;
  335. await this.checkField("mobileNumber").catch(() => {
  336. result = false;
  337. });
  338. if (!result) return;
  339. this.isFetchingCode = true;
  340. const data = await getSmsCode({
  341. schoolCode: this.loginModel.schoolCode,
  342. mobileNumber: this.loginModel.mobileNumber,
  343. }).catch(() => {
  344. this.isFetchingCode = false;
  345. });
  346. if (!data) return;
  347. if (data.mobileNumber) {
  348. this.$message.success(
  349. `已向手机尾号【${data.mobileNumber.slice(
  350. -4
  351. )}】成功发送短信,请在2分钟内进行验证!`
  352. );
  353. this.changeContent();
  354. } else {
  355. this.isFetchingCode = false;
  356. this.$message.error("未绑定手机号,请先绑定!");
  357. // this.userInfo = {
  358. // ...data,
  359. // loginName: this.loginModel.loginName,
  360. // schoolCode: this.loginModel.schoolCode,
  361. // password: this.loginModel.password
  362. // };
  363. // this.$refs.ResetPwd.open();
  364. }
  365. },
  366. async fetchAccountSmsCode() {
  367. const validAll = [
  368. this.checkField("loginName"),
  369. this.checkField("password"),
  370. ];
  371. let result = true;
  372. await Promise.all(validAll).catch(() => {
  373. result = false;
  374. });
  375. if (!result) return;
  376. this.isFetchingCode = true;
  377. const data = await getAccountSmsCode({
  378. schoolCode: this.loginModel.schoolCode,
  379. loginName: this.loginModel.loginName,
  380. password: Base64(this.loginModel.password),
  381. }).catch(() => {
  382. this.isFetchingCode = false;
  383. });
  384. if (!data) return;
  385. if (data) {
  386. this.$message.success(
  387. `已向手机尾号【${data.slice(-4)}】成功发送短信,请在2分钟内进行验证!`
  388. );
  389. this.changeContent();
  390. } else {
  391. this.isFetchingCode = false;
  392. this.$message.error("未绑定手机号!");
  393. }
  394. },
  395. },
  396. };
  397. </script>