ResetPwd.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <el-dialog
  3. class="reset-pwd"
  4. :visible.sync="modalIsShow"
  5. :title="title"
  6. top="10vh"
  7. width="520px"
  8. :close-on-click-modal="false"
  9. :close-on-press-escape="false"
  10. append-to-body
  11. :show-close="false"
  12. @open="visibleChange"
  13. >
  14. <el-form
  15. ref="modalFormComp"
  16. :model="modalForm"
  17. :rules="resetRules"
  18. label-position="top"
  19. >
  20. <template v-if="needResetPwd">
  21. <el-form-item prop="oldPassword" label="旧密码:">
  22. <el-input
  23. type="password"
  24. v-model="modalForm.oldPassword"
  25. placeholder="请输入旧密码"
  26. clearable
  27. ></el-input>
  28. </el-form-item>
  29. <el-form-item prop="password" label="新密码:">
  30. <el-input
  31. type="password"
  32. v-model="modalForm.password"
  33. placeholder="请输入新密码"
  34. clearable
  35. ></el-input>
  36. </el-form-item>
  37. <el-form-item prop="rePassword" label="再次密码:">
  38. <el-input
  39. type="password"
  40. v-model="modalForm.rePassword"
  41. placeholder="请再次输入新密码"
  42. clearable
  43. ></el-input>
  44. </el-form-item>
  45. </template>
  46. <template v-if="needBindMobile && schoolInfo.phoneLogin">
  47. <el-form-item prop="mobileNumber" label="手机号:">
  48. <el-input
  49. v-model.trim="modalForm.mobileNumber"
  50. placeholder="请输入账号"
  51. name="mobileNumber"
  52. clearable
  53. >
  54. </el-input>
  55. </el-form-item>
  56. <el-form-item prop="code" label="验证码:">
  57. <div class="vlcode">
  58. <div class="vlcode-right">
  59. <el-button
  60. style="width: 100%"
  61. type="text"
  62. @click="fetchSmsCode"
  63. :disabled="isFetchingCode"
  64. >+{{ codeContent }}</el-button
  65. >
  66. </div>
  67. <div class="vlcode-left">
  68. <el-input
  69. v-model.trim="modalForm.code"
  70. placeholder="请输入手机验证码"
  71. name="code"
  72. clearable
  73. >
  74. </el-input>
  75. </div>
  76. </div>
  77. </el-form-item>
  78. </template>
  79. </el-form>
  80. <div slot="footer">
  81. <el-button type="primary" :disabled="isSubmit" @click="submit"
  82. >确认</el-button
  83. >
  84. <el-button @click="cancel">取消</el-button>
  85. </div>
  86. </el-dialog>
  87. </template>
  88. <script>
  89. import { updatePwd } from "../api";
  90. import { getSmsCodeForBind } from "@/modules/login/api";
  91. import { password, strictPassword, phone, smscode } from "@/plugins/formRules";
  92. import { Base64 } from "@/plugins/crypto";
  93. import fetchSmsMixins from "@/modules/login/fetchSmsMixins";
  94. const initModalForm = {
  95. id: "",
  96. oldPassword: "",
  97. password: "",
  98. rePassword: "",
  99. mobileNumber: "",
  100. code: "",
  101. };
  102. export default {
  103. name: "reset-pwd",
  104. props: {
  105. userInfo: {
  106. type: Object,
  107. default() {
  108. return {
  109. userId: "",
  110. loginName: "",
  111. schoolCode: "",
  112. password: "",
  113. mobileNumber: "",
  114. pwdCount: "",
  115. };
  116. },
  117. },
  118. },
  119. mixins: [fetchSmsMixins],
  120. data() {
  121. const passwordRule = [
  122. ...strictPassword,
  123. {
  124. validator: (rule, value, callback) => {
  125. if (value === this.userInfo.loginName) {
  126. return callback(new Error("禁止使用用户账户号作为密码"));
  127. }
  128. return callback();
  129. },
  130. },
  131. ];
  132. const equalToOldPswd = (rule, value, callback) => {
  133. if (value === this.modalForm.oldPassword) {
  134. callback(new Error("新旧密码不可以相同"));
  135. } else {
  136. callback();
  137. }
  138. };
  139. const equalToPswd = (rule, value, callback) => {
  140. if (value !== this.modalForm.password) {
  141. callback(new Error("两次输入的密码不一致"));
  142. } else {
  143. callback();
  144. }
  145. };
  146. return {
  147. modalIsShow: false,
  148. isSubmit: false,
  149. nameWaitTime: "resetpwd",
  150. isFetchingCode: false,
  151. modalForm: { ...initModalForm },
  152. resetRules: {
  153. code: smscode,
  154. mobileNumber: phone,
  155. oldPassword: password,
  156. password: [
  157. ...passwordRule,
  158. {
  159. validator: equalToOldPswd,
  160. trigger: "change",
  161. },
  162. ],
  163. rePassword: [
  164. ...passwordRule,
  165. {
  166. validator: equalToPswd,
  167. trigger: "change",
  168. },
  169. ],
  170. },
  171. schoolInfo: {},
  172. };
  173. },
  174. computed: {
  175. needBindMobile() {
  176. return !this.userInfo.mobileNumber;
  177. },
  178. needResetPwd() {
  179. return !this.userInfo.pwdCount;
  180. },
  181. title() {
  182. if (this.needBindMobile && this.needResetPwd)
  183. return "修改密码与绑定手机号";
  184. if (this.needBindMobile) return "绑定手机号";
  185. if (this.needResetPwd) return "修改密码";
  186. return "修改密码";
  187. },
  188. },
  189. methods: {
  190. initData(val) {
  191. this.modalForm = { ...initModalForm };
  192. this.modalForm.oldPassword = this.userInfo.password || "";
  193. this.modalForm.id = this.userInfo.userId;
  194. },
  195. visibleChange() {
  196. this.initData(this.instance);
  197. this.schoolInfo = this.$ls.get("schoolInfo", {});
  198. },
  199. cancel() {
  200. this.modalIsShow = false;
  201. this.$emit("cancel");
  202. },
  203. open() {
  204. this.modalIsShow = true;
  205. },
  206. checkField(field) {
  207. return new Promise((resolve, reject) => {
  208. this.$refs.modalFormComp.validateField(field, (unvalid) => {
  209. if (unvalid) {
  210. reject();
  211. } else {
  212. resolve();
  213. }
  214. });
  215. });
  216. },
  217. async fetchSmsCode() {
  218. // console.log("111");
  219. let result = true;
  220. await this.checkField("mobileNumber").catch(() => {
  221. result = false;
  222. });
  223. if (!result) return;
  224. this.isFetchingCode = true;
  225. const data = await getSmsCodeForBind({
  226. loginName: this.userInfo.loginName,
  227. schoolCode: this.userInfo.schoolCode,
  228. password: Base64(this.modalForm.oldPassword),
  229. mobileNumber: this.modalForm.mobileNumber,
  230. }).catch(() => {
  231. this.isFetchingCode = false;
  232. });
  233. if (!data) return;
  234. this.$message.success(
  235. `已向手机尾号【${this.modalForm.mobileNumber.slice(
  236. -4
  237. )}】成功发送短信,请在2分钟内进行验证!`
  238. );
  239. this.changeContent();
  240. },
  241. async submit() {
  242. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  243. if (!valid) return;
  244. if (this.isSubmit) return;
  245. this.isSubmit = true;
  246. let datas = {
  247. id: this.modalForm.id,
  248. oldPassword: Base64(this.modalForm.oldPassword),
  249. };
  250. if (this.needBindMobile) {
  251. datas = {
  252. ...datas,
  253. mobileNumber: this.modalForm.mobileNumber,
  254. verifyCode: this.modalForm.code,
  255. };
  256. }
  257. if (this.needResetPwd) {
  258. datas = {
  259. ...datas,
  260. password: Base64(this.modalForm.password),
  261. };
  262. }
  263. const data = await updatePwd(datas).catch(() => {
  264. this.isSubmit = false;
  265. });
  266. if (!data) return;
  267. this.isSubmit = false;
  268. this.$message.success("修改成功!");
  269. this.$emit("modified", data);
  270. this.modalIsShow = false;
  271. },
  272. },
  273. };
  274. </script>