setNewPwd.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <!--设置新密码-->
  3. <view class="retrieve-pwd">
  4. <u--form
  5. labelPosition="left"
  6. :model="formData"
  7. :rules="rules"
  8. ref="uForm"
  9. labelWidth="80"
  10. >
  11. <u-form-item borderBottom label="新密码" prop="password">
  12. <c-input
  13. v-model="formData.password"
  14. placeholder="8-16位数字、字母、符号组合"
  15. maxlength="16"
  16. border="none"
  17. show-password
  18. type="password"
  19. >
  20. </c-input>
  21. </u-form-item>
  22. <u-form-item label="确认密码" prop="passwordTo">
  23. <c-input
  24. v-model="formData.passwordTo"
  25. placeholder="请再次输入密码"
  26. border="none"
  27. type="password"
  28. show-password
  29. >
  30. </c-input>
  31. </u-form-item>
  32. </u--form>
  33. <view class="footer" style="margin-top: 130rpx">
  34. <c-button type="primary" @click="handleComplete">
  35. 完成
  36. </c-button>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import { common } from '@/api/user/login/retrievePwd.js';
  42. import { rsaEncrypt } from '@/utils/jsencrypt';
  43. export default {
  44. name: 'retrievePwd',
  45. data() {
  46. const pwdValidator = (rule, value, callback) => {
  47. const val = value || '';
  48. if (val.length < 8 || val.length > 16) {
  49. callback('长度在 8 到 16 个字符');
  50. return;
  51. }
  52. const letterSmall = /(?=.*?[a-z])/.test(val); // 包含小写字母
  53. const number = /\d/.test(val); // 包含数字
  54. const letterBig = /(?=.*?[A-Z])/.test(val); // 包含大写字母。
  55. const specialWord =
  56. /(?=.*[!·()【】、;’,。、#……¥~!@#$%^&*().,\\`|/[\]+-])/.test(
  57. val
  58. ); // 包含特殊字符
  59. /*小写+数字+大写*/
  60. const arr1 = [
  61. letterSmall, // 包含小写字母
  62. number, // 包含数字
  63. letterBig, // 包含大写字母
  64. ];
  65. if (arr1.every((item) => item)) {
  66. callback();
  67. return;
  68. }
  69. /*小写+数字+特殊字符*/
  70. const arr2 = [
  71. letterSmall, // 包含小写字母
  72. number, // 包含数字
  73. specialWord, // 包含特殊字符
  74. ];
  75. if (arr2.every((item) => item)) {
  76. callback();
  77. return;
  78. }
  79. /*大写+数字+特殊字符*/
  80. const arr3 = [
  81. letterBig, // 包含大写字母
  82. number, // 包含数字
  83. specialWord, // 包含特殊字符
  84. ];
  85. if (arr3.every((item) => item)) {
  86. callback();
  87. return;
  88. }
  89. /*大写+特殊字符+小写*/
  90. const arr4 = [
  91. letterBig, // 包含大写字母
  92. specialWord, // 包含特殊字符
  93. letterSmall, // 包含小写字母
  94. ];
  95. if (arr4.every((item) => item)) {
  96. callback();
  97. return;
  98. }
  99. // 不通过
  100. callback('至少要有数字、大小写字母、符号中的三种');
  101. };
  102. return {
  103. tips: '',
  104. formData: {
  105. phone: '',
  106. uuid: '', // 上个页面校验验证码接口返回的
  107. password: '',
  108. passwordTo: '',
  109. },
  110. rules: {
  111. password: [
  112. {
  113. type: 'string',
  114. required: true,
  115. message: '请输入新密码',
  116. trigger: ['change'],
  117. },
  118. {
  119. type: 'string',
  120. validator: pwdValidator,
  121. trigger: ['change'],
  122. },
  123. ],
  124. passwordTo: {
  125. type: 'string',
  126. required: true,
  127. validator: (rule, value) => {
  128. return value === this.formData.password;
  129. },
  130. message: '两次密码不一致',
  131. trigger: ['change'],
  132. },
  133. },
  134. };
  135. },
  136. onLoad(params) {
  137. this.formData.phone = params.phone;
  138. this.formData.uuid = params.uuid;
  139. },
  140. methods: {
  141. async handleComplete() {
  142. if (this.formData.password.includes(' ')) {
  143. uni.$c.toast({
  144. title: '密码中不能出现空格',
  145. duration: 2000,
  146. });
  147. this.formData.password = '';
  148. return;
  149. }
  150. await this.$refs.uForm.validate();
  151. try {
  152. this.$c.loading();
  153. const { password, passwordTo, phone, uuid } = this.formData;
  154. const params = {
  155. uuid,
  156. phone: rsaEncrypt(phone), // 加密
  157. password: rsaEncrypt(password), // 加密
  158. passwordTo: rsaEncrypt(passwordTo), // 加密
  159. };
  160. await common(params);
  161. uni.hideLoading();
  162. await this.$c.toast({
  163. icon: 'success',
  164. title: '修改成功',
  165. duration: 1000,
  166. });
  167. uni.$u.route({
  168. type: 'reLaunch',
  169. url: '/pages/user/login/index',
  170. });
  171. } catch (e) {
  172. uni.hideLoading();
  173. console.error(e);
  174. }
  175. },
  176. getCode() {
  177. if (this.$refs.uCode.canGetCode) {
  178. // 模拟向后端请求验证码
  179. uni.showLoading({
  180. title: '正在获取验证码',
  181. });
  182. setTimeout(() => {
  183. uni.hideLoading();
  184. // 这里此提示会被this.start()方法中的提示覆盖
  185. uni.$u.toast('验证码已发送');
  186. // 通知验证码组件内部开始倒计时
  187. this.$refs.uCode.start();
  188. }, 2000);
  189. }
  190. },
  191. codeChange(text) {
  192. this.tips = text;
  193. },
  194. },
  195. };
  196. </script>
  197. <style scoped lang="scss">
  198. .retrieve-pwd {
  199. padding: 28rpx;
  200. background-color: #f8f8f8;
  201. height: 100%;
  202. .u-form {
  203. padding: 8rpx 20rpx;
  204. background-color: #fff;
  205. border-radius: 6px;
  206. }
  207. }
  208. </style>