forgetPassword.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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="number" v-model="phone"
  7. placeholder="请输入手机号码" maxlength="11" />
  8. </view>
  9. <view class="info-item">
  10. <text class="title">验证码</text>
  11. <input class="input-field code-input" type="number" v-model="code"
  12. placeholder="请输入验证码" maxlength="6" />
  13. <view class="get-code-btn" @click="getVerifyCode">
  14. {{ codeText }}
  15. </view>
  16. </view>
  17. <view class="info-item">
  18. <text class="title">密码</text>
  19. <input class="input-field code-input" type="text" :password="!showNewPassword" v-model="newPassword"
  20. placeholder="请输入8-16位字符,必须包含数字和字母" />
  21. <view class="img-box" @click="toggleNewPassword">
  22. <image class="icon"
  23. :src="showNewPassword?'/static/image/icon_visible.png':'/static/image/icon_invisible.png'"
  24. mode="aspectFill"></image>
  25. </view>
  26. </view>
  27. </view>
  28. <view class="btn-box">
  29. <view class="confirm" @click="confirm">确认</view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. import { resetPassword, sendPasswordVerifyCode } from '@/api/password'
  35. export default {
  36. data() {
  37. return {
  38. phone: '',
  39. code: '',
  40. codeText: '获取验证码', // 验证码按钮文字
  41. countdown: 0, // 倒计时
  42. countdownTimer: null, // 倒计时定时器
  43. newPassword: '',
  44. confirmPassword: '',
  45. // 为每个密码框单独设置显示状态
  46. showOldPassword: false,
  47. showNewPassword: false,
  48. showConfirmPassword: false
  49. }
  50. },
  51. onUnload() {
  52. // 清除倒计时
  53. if(this.countdownTimer) {
  54. clearInterval(this.countdownTimer);
  55. this.countdownTimer = null;
  56. }
  57. },
  58. onLoad() {},
  59. methods: {
  60. // 获取验证码
  61. async getVerifyCode() {
  62. if (this.countdown > 0) {
  63. return;
  64. }
  65. if (!this.phone) {
  66. uni.showToast({
  67. icon: 'none',
  68. title: "请输入手机号",
  69. });
  70. return;
  71. }
  72. if (!/^1[3-9]\d{9}$/.test(this.phone)) {
  73. uni.showToast({
  74. icon: 'none',
  75. title: "请输入正确的手机号",
  76. });
  77. return;
  78. }
  79. try {
  80. uni.showLoading({
  81. title: "发送中..."
  82. });
  83. const res = await sendPasswordVerifyCode({ phone: this.phone });
  84. uni.hideLoading();
  85. if (res.code === 200) {
  86. uni.showToast({
  87. icon: 'success',
  88. title: "验证码已发送",
  89. });
  90. // 开始倒计时
  91. this.countdown = 60;
  92. this.codeText = this.countdown + '秒重新获取';
  93. this.countdownTimer = setInterval(() => {
  94. this.countdown--;
  95. this.codeText = this.countdown + '秒重新获取';
  96. if (this.countdown <= 0) {
  97. clearInterval(this.countdownTimer);
  98. this.countdownTimer = null;
  99. this.codeText = '获取验证码';
  100. }
  101. }, 1000);
  102. } else {
  103. uni.showToast({
  104. icon: 'none',
  105. title: res.msg || "发送验证码失败",
  106. });
  107. }
  108. } catch (e) {
  109. uni.hideLoading();
  110. console.error('发送验证码失败', e);
  111. uni.showToast({
  112. icon: 'none',
  113. title: "发送验证码失败",
  114. });
  115. }
  116. },
  117. // 分别控制每个密码框的显示/隐藏
  118. toggleOldPassword() {
  119. this.showOldPassword = !this.showOldPassword;
  120. },
  121. toggleNewPassword() {
  122. this.showNewPassword = !this.showNewPassword;
  123. },
  124. toggleConfirmPassword() {
  125. this.showConfirmPassword = !this.showConfirmPassword;
  126. },
  127. confirm() {
  128. // 验证逻辑
  129. if (!this.phone) {
  130. uni.showToast({
  131. title: '请输入手机号',
  132. icon: 'none'
  133. });
  134. return;
  135. }
  136. if (!/^1[3-9]\d{9}$/.test(this.phone)) {
  137. uni.showToast({
  138. title: '请输入正确的手机号',
  139. icon: 'none'
  140. });
  141. return;
  142. }
  143. if (!this.code) {
  144. uni.showToast({
  145. title: '请输入验证码',
  146. icon: 'none'
  147. });
  148. return;
  149. }
  150. if (!this.newPassword) {
  151. uni.showToast({
  152. title: '请输入新密码',
  153. icon: 'none'
  154. });
  155. return;
  156. }
  157. // 密码格式验证
  158. const passwordRegex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{8,20}$/;
  159. if (!passwordRegex.test(this.newPassword)) {
  160. uni.showToast({
  161. title: '密码需8-20位字母和数字组合',
  162. icon: 'none'
  163. });
  164. return;
  165. }
  166. // 调用重置密码的API
  167. this.resetPassword();
  168. },
  169. // 重置密码的API调用
  170. async resetPassword() {
  171. try {
  172. uni.showLoading({
  173. title: '修改中...'
  174. });
  175. const res = await resetPassword({
  176. phone: this.phone,
  177. code: this.code,
  178. newPassword: this.newPassword,
  179. confirmPassword: this.newPassword,
  180. });
  181. uni.hideLoading();
  182. if (res.code === 200) {
  183. uni.showToast({
  184. title: '密码修改成功',
  185. icon: 'success'
  186. });
  187. // 清空表单
  188. this.phone = '';
  189. this.code = '';
  190. this.newPassword = '';
  191. this.confirmPassword = '';
  192. // 跳转到其他页面或返回
  193. setTimeout(() => {
  194. uni.navigateBack();
  195. }, 1500);
  196. } else {
  197. uni.showToast({
  198. title: res.msg || '修改失败',
  199. icon: 'none'
  200. });
  201. }
  202. } catch (error) {
  203. uni.hideLoading();
  204. console.error('修改密码失败', error);
  205. uni.showToast({
  206. title: error.message || '修改失败',
  207. icon: 'none'
  208. });
  209. }
  210. }
  211. }
  212. }
  213. </script>
  214. <style lang="scss" scoped>
  215. .setting-page {
  216. background: #ffffff;
  217. min-height: 100vh;
  218. .content {
  219. background: #ffffff;
  220. }
  221. .info-item {
  222. height: 104upx;
  223. display: flex;
  224. align-items: center;
  225. justify-content: space-between;
  226. border-bottom: 1px solid #F5F6FA;
  227. .title {
  228. width: 160rpx;
  229. font-size: 28rpx;
  230. color: #666666;
  231. margin-left: 32rpx;
  232. }
  233. .input-field {
  234. width: 100%;
  235. font-size: 30rpx;
  236. font-family: PingFang SC;
  237. color: #333333;
  238. }
  239. .code-input {
  240. flex: 1;
  241. }
  242. .img-box {
  243. padding: 32rpx;
  244. .icon {
  245. width: 32rpx;
  246. height: 32rpx;
  247. }
  248. }
  249. .get-code-btn {
  250. margin-right: 32rpx;
  251. font-size: 28rpx;
  252. font-weight: 500;
  253. color: #157CF8;
  254. padding-left: 20rpx;
  255. white-space: nowrap;
  256. }
  257. }
  258. .btn-box {
  259. padding: 64upx 32upx;
  260. .confirm {
  261. width: 100%;
  262. height: 88upx;
  263. line-height: 88upx;
  264. text-align: center;
  265. font-family: PingFang SC;
  266. font-size: 32upx;
  267. color: #FFFFFF;
  268. background: #388BFF;
  269. border-radius: 44upx;
  270. }
  271. }
  272. }
  273. </style>