changePassword.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <view>
  3. <view :style="{width: '100%', height: windowHeight + 'px'}"></view>
  4. <view style="display: flex;height: 88rpx;line-height: 88rpx;padding: 10rpx 20rpx;">
  5. <view v-if="!passTitle" style="display: flex;">
  6. <u-icon name="arrow-left" @click="navigateBack"></u-icon>
  7. </view>
  8. <view style="flex: 1;text-align: center;font-size: 30rpx;font-weight: 700;margin-right: 40rpx;">
  9. 修改密码
  10. </view>
  11. </view>
  12. <view class="passWorld">
  13. <view class="mian">
  14. <u--form labelPosition="left" :model="userInfo" :rules="rules" ref="uForm" labelWidth='80'>
  15. <u-form-item label="旧密码" prop="password" borderBottom ref="item1">
  16. <view class="passwordbox">
  17. <u-input :password-icon="true" :password="type=='password'" class="input-box"
  18. v-model="userInfo.password" placeholder="请输入密码" border="none">
  19. <template slot="suffix">
  20. <u-icon size="16" @click="changetype('type')"
  21. :name="type!='password'?'eye-off':'eye-fill'">
  22. </u-icon>
  23. </template>
  24. </u-input>
  25. </view>
  26. </u-form-item>
  27. <u-form-item label="新密码" prop="newPassword" borderBottom ref="item1">
  28. <view class="passwordbox">
  29. <u-input :password-icon="true" :password="type2=='password'" class="input-box"
  30. v-model="userInfo.newPassword" placeholder="请输入新密码" border="none">
  31. <template slot="suffix">
  32. <u-icon size="16" @click="changetype('type2')"
  33. :name="type2!='password'?'eye-off':'eye-fill'">
  34. </u-icon>
  35. </template>
  36. </u-input>
  37. </view>
  38. </u-form-item>
  39. <u-form-item label="确认新密码" prop="confirmPassword" borderBottom ref="item1">
  40. <view class="passwordbox">
  41. <u-input :password-icon="true" :password="type3=='password'" class="input-box"
  42. v-model="userInfo.confirmPassword" placeholder="请确认新密码" border="none">
  43. <template slot="suffix">
  44. <u-icon size="16" @click="changetype('type3')"
  45. :name="type3!='password'?'eye-off':'eye-fill'">
  46. </u-icon>
  47. </template>
  48. </u-input>
  49. </view>
  50. </u-form-item>
  51. </u--form>
  52. </view>
  53. <u-toast ref="uToast"></u-toast>
  54. <view class="button" @click="surePass">
  55. 确定
  56. </view>
  57. </view>
  58. </view>
  59. </template>
  60. <script>
  61. export default {
  62. data() {
  63. return {
  64. userInfo: {
  65. password: "",
  66. username: uni.getStorageSync('userInfo').username,
  67. newPassword: "",
  68. confirmPassword: "",
  69. token: uni.getStorageSync('token')
  70. },
  71. type: 'password',
  72. type2: 'password',
  73. type3: 'password',
  74. passTitle: '',
  75. rules: {
  76. password: [{
  77. required: true,
  78. message: '请输入旧密码',
  79. trigger: 'change'
  80. }],
  81. newPassword: [{
  82. validator: (rule, value, callback) => {
  83. if (!value) {
  84. return callback(new Error('请确认新密码'));
  85. }
  86. // /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&.#-?!])[A-Za-z\d@$!%*?&.#-?!]{6,18}$/;
  87. const passwordRegex = RegExp(uni.getStorageSync('passwordReg'))
  88. if (!passwordRegex.test(value)) {
  89. return callback(new Error('规则:6-18位,含大小写字母、数字、特殊符号(@$!%*?&.#-)'));
  90. }
  91. callback();
  92. },
  93. trigger: 'change'
  94. }, ],
  95. confirmPassword: [{
  96. validator: (rule, value, callback) => {
  97. if (!value) {
  98. return callback(new Error('请确认新密码'));
  99. }
  100. if (value != this.userInfo.newPassword) {
  101. return callback(new Error('两次密码输入不一致'));
  102. }
  103. callback();
  104. },
  105. trigger: 'change'
  106. }],
  107. },
  108. windowHeight: uni.getWindowInfo().statusBarHeight,
  109. }
  110. },
  111. mounted() {
  112. this.$refs.uForm.setRules(this.rules)
  113. },
  114. onBackPress(options) {
  115. // 触发返回就会调用此方法,这里实现的是禁用物理返回,顶部导航栏的自定义返回 uni.navigateBack 仍可使用
  116. if (options.from == 'backbutton') {
  117. let changePassword = uni.getStorageSync('changePassword')
  118. return changePassword;
  119. } else if (options.from == 'navigateBack') {
  120. return false;
  121. }
  122. },
  123. created() {
  124. let title = true;
  125. if (uni.getStorageSync('changePassword') == true) {
  126. return title = false
  127. }
  128. if (!title) {
  129. this.passTitle = false
  130. }
  131. },
  132. methods: {
  133. navigateBack() {
  134. uni.navigateBack();
  135. },
  136. changetype(e) {
  137. if (this[e] == 'password') {
  138. this[e] = 'text'
  139. } else {
  140. this[e] = 'password'
  141. }
  142. },
  143. surePass() {
  144. this.$refs.uForm.validate().then(res => {
  145. // uni.$u.toast('校验通过')
  146. this.$api.to_http(`/sys/user/password`,
  147. this.userInfo, "POST").then((res) => {
  148. if (res.data.code != 0) {
  149. return uni.$u.toast(res.data.msg)
  150. }
  151. uni.setStorageSync('changePassword', true);
  152. uni.$u.toast('修改成功')
  153. setTimeout(() => {
  154. uni.removeStorageSync('changePassword');
  155. uni.removeStorageSync('token');
  156. uni.removeStorageSync('passwordReg');
  157. uni.removeStorageSync('userInfo');
  158. uni.redirectTo({
  159. url: '/pages/login/login'
  160. })
  161. }, 500)
  162. })
  163. }).catch(errors => {
  164. uni.$u.toast('校验失败')
  165. })
  166. }
  167. }
  168. }
  169. </script>
  170. <style>
  171. .login .uni-checkbox-input {
  172. width: 36rpx !important;
  173. height: 36rpx;
  174. }
  175. </style>
  176. <style lang="less" scoped>
  177. .passWorld {
  178. background: #fff;
  179. position: absolute;
  180. min-height: 100%;
  181. width: 100%;
  182. box-sizing: border-box;
  183. padding: 2rem;
  184. .passwordbox {
  185. position: relative;
  186. width: 100%;
  187. .icion {
  188. position: absolute;
  189. right: 0rpx;
  190. top: 25rpx;
  191. z-index: 1;
  192. }
  193. }
  194. .button {
  195. background: #2561ef;
  196. color: #fff;
  197. padding: 0.5rem;
  198. text-align: center;
  199. border-radius: 0.5rem;
  200. margin-top: 3rem;
  201. }
  202. }
  203. </style>