changePassword.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <view class="setting-page">
  3. <view class="content">
  4. <view class="info-item">
  5. <text class="title">原密码</text>
  6. <input class="input-field code-input" type="text" :password="!showOldPassword" v-model="oldPassword"
  7. placeholder="请输入现有密码" />
  8. <view class="img-box" @click="toggleOldPassword">
  9. <image class="icon"
  10. :src="showOldPassword?'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_visible.png':'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_invisible.png'"
  11. mode="aspectFill"></image>
  12. </view>
  13. </view>
  14. <view class="info-item">
  15. <text class="title">新密码</text>
  16. <input class="input-field code-input" type="text" :password="!showNewPassword" v-model="newPassword"
  17. placeholder="请输入8-20位字母、数字" />
  18. <view class="img-box" @click="toggleNewPassword">
  19. <image class="icon"
  20. :src="showNewPassword?'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_visible.png':'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_invisible.png'"
  21. mode="aspectFill"></image>
  22. </view>
  23. </view>
  24. <view class="info-item">
  25. <text class="title">确认密码</text>
  26. <input class="input-field code-input" type="text" :password="!showConfirmPassword" v-model="confirmPassword"
  27. placeholder="请再次输入新密码" />
  28. <view class="img-box" @click="toggleConfirmPassword">
  29. <image class="icon"
  30. :src="showConfirmPassword?'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_visible.png':'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_invisible.png'"
  31. mode="aspectFill"></image>
  32. </view>
  33. </view>
  34. </view>
  35. <view class="btn-box">
  36. <view class="confirm" @click="confirm">确认</view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import { changePWD } from '@/api/user.js'
  42. export default {
  43. data() {
  44. return {
  45. oldPassword: '',
  46. newPassword: '',
  47. confirmPassword: '',
  48. // 为每个密码框单独设置显示状态
  49. showOldPassword: false,
  50. showNewPassword: false,
  51. showConfirmPassword: false
  52. }
  53. },
  54. onLoad() {},
  55. methods: {
  56. // 分别控制每个密码框的显示/隐藏
  57. toggleOldPassword() {
  58. this.showOldPassword = !this.showOldPassword;
  59. },
  60. toggleNewPassword() {
  61. this.showNewPassword = !this.showNewPassword;
  62. },
  63. toggleConfirmPassword() {
  64. this.showConfirmPassword = !this.showConfirmPassword;
  65. },
  66. confirm() {
  67. // 验证逻辑
  68. if (!this.oldPassword) {
  69. uni.showToast({
  70. title: '请输入原密码',
  71. icon: 'none'
  72. });
  73. return;
  74. }
  75. if (!this.newPassword) {
  76. uni.showToast({
  77. title: '请输入新密码',
  78. icon: 'none'
  79. });
  80. return;
  81. }
  82. // 密码格式验证
  83. const passwordRegex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{8,20}$/;
  84. if (!passwordRegex.test(this.newPassword)) {
  85. uni.showToast({
  86. title: '密码需8-20位字母和数字组合',
  87. icon: 'none'
  88. });
  89. return;
  90. }
  91. if (this.newPassword !== this.confirmPassword) {
  92. uni.showToast({
  93. title: '两次输入的新密码不一致',
  94. icon: 'none'
  95. });
  96. return;
  97. }
  98. // 调用修改密码的API
  99. this.changePassword();
  100. },
  101. // 修改密码的API调用
  102. async changePassword() {
  103. try {
  104. uni.showLoading({
  105. title: '修改中...'
  106. });
  107. // 构建请求参数
  108. const params = {
  109. oldPassword: this.oldPassword,
  110. newPassword: this.newPassword,
  111. confirmPassword: this.confirmPassword
  112. };
  113. // 调用修改密码接口
  114. const res = await changePWD(params);
  115. uni.hideLoading();
  116. if (res.code === 200) {
  117. uni.showToast({
  118. title: '密码修改成功,将退出登录',
  119. icon: 'success'
  120. });
  121. // 清空表单
  122. this.oldPassword = '';
  123. this.newPassword = '';
  124. this.confirmPassword = '';
  125. // 清除登录状态
  126. uni.removeStorageSync('AppToken');
  127. uni.removeStorageSync('userInfo');
  128. // 跳转到登录页面
  129. setTimeout(() => {
  130. uni.navigateTo({
  131. url: '/pages/auth/login'
  132. });
  133. }, 1500);
  134. } else {
  135. uni.showToast({
  136. title: res.message || '修改失败',
  137. icon: 'none'
  138. });
  139. }
  140. } catch (error) {
  141. uni.hideLoading();
  142. uni.showToast({
  143. title: error.message || '修改失败',
  144. icon: 'none'
  145. });
  146. }
  147. }
  148. }
  149. }
  150. </script>
  151. <style lang="scss" scoped>
  152. .setting-page {
  153. background: #ffffff;
  154. min-height: 100vh;
  155. .content {
  156. background: #ffffff;
  157. }
  158. .info-item {
  159. height: 104upx;
  160. display: flex;
  161. align-items: center;
  162. justify-content: space-between;
  163. border-bottom: 1px solid #F5F6FA;
  164. .title {
  165. width: 160rpx;
  166. font-size: 28rpx;
  167. color: #666666;
  168. margin-left: 32rpx;
  169. }
  170. .input-field {
  171. width: 100%;
  172. font-size: 30rpx;
  173. font-family: PingFang SC;
  174. color: #333333;
  175. }
  176. .code-input {
  177. flex: 1;
  178. }
  179. .img-box {
  180. padding: 32rpx;
  181. .icon {
  182. width: 32rpx;
  183. height: 32rpx;
  184. }
  185. }
  186. }
  187. .btn-box {
  188. padding: 64upx 32upx;
  189. .confirm {
  190. width: 100%;
  191. height: 88upx;
  192. line-height: 88upx;
  193. text-align: center;
  194. font-family: PingFang SC;
  195. font-size: 32upx;
  196. color: #FFFFFF;
  197. background: #388BFF;
  198. border-radius: 44upx;
  199. }
  200. }
  201. }
  202. </style>