Login.vue 11 KB

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