changePassword.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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?'/static/image/icon_visible.png':'/static/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?'/static/image/icon_visible.png':'/static/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?'/static/image/icon_visible.png':'/static/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. setTimeout(() => {
  127. uni.navigateBack();
  128. }, 1500);
  129. } else {
  130. uni.showToast({
  131. title: res.message || '修改失败',
  132. icon: 'none'
  133. });
  134. }
  135. } catch (error) {
  136. uni.hideLoading();
  137. uni.showToast({
  138. title: error.message || '修改失败',
  139. icon: 'none'
  140. });
  141. }
  142. }
  143. }
  144. }
  145. </script>
  146. <style lang="scss" scoped>
  147. .setting-page {
  148. background: #ffffff;
  149. min-height: 100vh;
  150. .content {
  151. background: #ffffff;
  152. }
  153. .info-item {
  154. height: 104upx;
  155. display: flex;
  156. align-items: center;
  157. justify-content: space-between;
  158. border-bottom: 1px solid #F5F6FA;
  159. .title {
  160. width: 160rpx;
  161. font-size: 28rpx;
  162. color: #666666;
  163. margin-left: 32rpx;
  164. }
  165. .input-field {
  166. width: 100%;
  167. font-size: 30rpx;
  168. font-family: PingFang SC;
  169. color: #333333;
  170. }
  171. .code-input {
  172. flex: 1;
  173. }
  174. .img-box {
  175. padding: 32rpx;
  176. .icon {
  177. width: 32rpx;
  178. height: 32rpx;
  179. }
  180. }
  181. }
  182. .btn-box {
  183. padding: 64upx 32upx;
  184. .confirm {
  185. width: 100%;
  186. height: 88upx;
  187. line-height: 88upx;
  188. text-align: center;
  189. font-family: PingFang SC;
  190. font-size: 32upx;
  191. color: #FFFFFF;
  192. background: #388BFF;
  193. border-radius: 44upx;
  194. }
  195. }
  196. }
  197. </style>