forgetPassword.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. uni.removeStorageSync('AppToken',null);
  196. uni.removeStorageSync('userInfo',null);
  197. uni.$emit('refreshLogin');
  198. uni.navigateTo({
  199. url: '/pages/auth/login'
  200. })
  201. }, 1500);
  202. } else {
  203. uni.showToast({
  204. title: res.msg || '修改失败',
  205. icon: 'none'
  206. });
  207. }
  208. } catch (error) {
  209. uni.hideLoading();
  210. console.error('修改密码失败', error);
  211. uni.showToast({
  212. title: error.message || '修改失败',
  213. icon: 'none'
  214. });
  215. }
  216. }
  217. }
  218. }
  219. </script>
  220. <style lang="scss" scoped>
  221. .setting-page {
  222. background: #ffffff;
  223. min-height: 100vh;
  224. .content {
  225. background: #ffffff;
  226. }
  227. .info-item {
  228. height: 104upx;
  229. display: flex;
  230. align-items: center;
  231. justify-content: space-between;
  232. border-bottom: 1px solid #F5F6FA;
  233. .title {
  234. width: 160rpx;
  235. font-size: 28rpx;
  236. color: #666666;
  237. margin-left: 32rpx;
  238. }
  239. .input-field {
  240. width: 100%;
  241. font-size: 30rpx;
  242. font-family: PingFang SC;
  243. color: #333333;
  244. }
  245. .code-input {
  246. flex: 1;
  247. }
  248. .img-box {
  249. padding: 32rpx;
  250. .icon {
  251. width: 32rpx;
  252. height: 32rpx;
  253. }
  254. }
  255. .get-code-btn {
  256. margin-right: 32rpx;
  257. font-size: 28rpx;
  258. font-weight: 500;
  259. color: #157CF8;
  260. padding-left: 20rpx;
  261. white-space: nowrap;
  262. }
  263. }
  264. .btn-box {
  265. padding: 64upx 32upx;
  266. .confirm {
  267. width: 100%;
  268. height: 88upx;
  269. line-height: 88upx;
  270. text-align: center;
  271. font-family: PingFang SC;
  272. font-size: 32upx;
  273. color: #FFFFFF;
  274. background: #388BFF;
  275. border-radius: 44upx;
  276. }
  277. }
  278. }
  279. </style>