ResetPwd.vue 6.6 KB

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