changePassword.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. export default {
  42. data() {
  43. return {
  44. oldPassword: '',
  45. newPassword: '',
  46. confirmPassword: '',
  47. // 为每个密码框单独设置显示状态
  48. showOldPassword: false,
  49. showNewPassword: false,
  50. showConfirmPassword: false
  51. }
  52. },
  53. onLoad() {},
  54. methods: {
  55. // 分别控制每个密码框的显示/隐藏
  56. toggleOldPassword() {
  57. this.showOldPassword = !this.showOldPassword;
  58. },
  59. toggleNewPassword() {
  60. this.showNewPassword = !this.showNewPassword;
  61. },
  62. toggleConfirmPassword() {
  63. this.showConfirmPassword = !this.showConfirmPassword;
  64. },
  65. confirm() {
  66. // 验证逻辑
  67. if (!this.oldPassword) {
  68. uni.showToast({
  69. title: '请输入原密码',
  70. icon: 'none'
  71. });
  72. return;
  73. }
  74. if (!this.newPassword) {
  75. uni.showToast({
  76. title: '请输入新密码',
  77. icon: 'none'
  78. });
  79. return;
  80. }
  81. // 密码格式验证
  82. const passwordRegex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{8,20}$/;
  83. if (!passwordRegex.test(this.newPassword)) {
  84. uni.showToast({
  85. title: '密码需8-20位字母和数字组合',
  86. icon: 'none'
  87. });
  88. return;
  89. }
  90. if (this.newPassword !== this.confirmPassword) {
  91. uni.showToast({
  92. title: '两次输入的新密码不一致',
  93. icon: 'none'
  94. });
  95. return;
  96. }
  97. // 调用修改密码的API
  98. this.changePassword();
  99. },
  100. // 修改密码的API调用
  101. async changePassword() {
  102. try {
  103. uni.showLoading({
  104. title: '修改中...'
  105. });
  106. // 这里添加实际的API调用
  107. // const res = await uni.request({
  108. // url: '/api/change-password',
  109. // method: 'POST',
  110. // data: {
  111. // oldPassword: this.oldPassword,
  112. // newPassword: this.newPassword
  113. // }
  114. // });
  115. // 模拟成功
  116. setTimeout(() => {
  117. uni.hideLoading();
  118. uni.showToast({
  119. title: '密码修改成功',
  120. icon: 'success'
  121. });
  122. // 清空表单
  123. this.oldPassword = '';
  124. this.newPassword = '';
  125. this.confirmPassword = '';
  126. // 跳转到其他页面或返回
  127. setTimeout(() => {
  128. uni.navigateBack();
  129. }, 1500);
  130. }, 1000);
  131. } catch (error) {
  132. uni.hideLoading();
  133. uni.showToast({
  134. title: error.message || '修改失败',
  135. icon: 'none'
  136. });
  137. }
  138. }
  139. }
  140. }
  141. </script>
  142. <style lang="scss" scoped>
  143. .setting-page {
  144. background: #ffffff;
  145. min-height: 100vh;
  146. .content {
  147. background: #ffffff;
  148. }
  149. .info-item {
  150. height: 104upx;
  151. display: flex;
  152. align-items: center;
  153. justify-content: space-between;
  154. border-bottom: 1px solid #F5F6FA;
  155. .title {
  156. width: 160rpx;
  157. font-size: 28rpx;
  158. color: #666666;
  159. margin-left: 32rpx;
  160. }
  161. .input-field {
  162. width: 100%;
  163. font-size: 30rpx;
  164. font-family: PingFang SC;
  165. color: #333333;
  166. }
  167. .code-input {
  168. flex: 1;
  169. }
  170. .img-box {
  171. padding: 32rpx;
  172. .icon {
  173. width: 32rpx;
  174. height: 32rpx;
  175. }
  176. }
  177. }
  178. .btn-box {
  179. padding: 64upx 32upx;
  180. .confirm {
  181. width: 100%;
  182. height: 88upx;
  183. line-height: 88upx;
  184. text-align: center;
  185. font-family: PingFang SC;
  186. font-size: 32upx;
  187. color: #FFFFFF;
  188. background: #388BFF;
  189. border-radius: 44upx;
  190. }
  191. }
  192. }
  193. </style>