forget-password.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <template>
  2. <view class="forget-container">
  3. <view class="page-header">
  4. <text class="back-icon" @click="goBack">←</text>
  5. <text class="page-title">找回密码</text>
  6. <text class="placeholder"></text>
  7. </view>
  8. <view class="forget-form">
  9. <view class="form-group">
  10. <view class="input-group">
  11. <text class="input-icon">手</text>
  12. <input
  13. class="input"
  14. type="number"
  15. placeholder="请输入手机号"
  16. v-model="formData.phone"
  17. maxlength="11"
  18. />
  19. </view>
  20. </view>
  21. <view class="form-group">
  22. <view class="input-group">
  23. <text class="input-icon">验</text>
  24. <input
  25. class="input"
  26. type="number"
  27. placeholder="请输入验证码"
  28. v-model="formData.code"
  29. maxlength="6"
  30. />
  31. <text
  32. class="code-btn"
  33. :class="{ disabled: codeBtnDisabled }"
  34. @click="getVerificationCode"
  35. >
  36. {{ codeBtnText }}
  37. </text>
  38. </view>
  39. </view>
  40. <view class="form-group">
  41. <view class="input-group">
  42. <text class="input-icon">新</text>
  43. <input
  44. class="input"
  45. :type="showPassword ? 'text' : 'password'"
  46. placeholder="请设置新密码"
  47. v-model="formData.password"
  48. />
  49. <text class="eye-icon" @click="togglePasswordVisibility">
  50. {{ showPassword ? '👁️' : '👁️‍🗨️' }}
  51. </text>
  52. </view>
  53. </view>
  54. <view class="form-group">
  55. <view class="input-group">
  56. <text class="input-icon">确</text>
  57. <input
  58. class="input"
  59. :type="showConfirmPassword ? 'text' : 'password'"
  60. placeholder="请确认新密码"
  61. v-model="formData.confirmPassword"
  62. />
  63. <text class="eye-icon" @click="toggleConfirmPasswordVisibility">
  64. {{ showConfirmPassword ? '👁️' : '👁️‍🗨️' }}
  65. </text>
  66. </view>
  67. </view>
  68. <button class="submit-btn" @click="handleResetPassword" :loading="isSubmitting">重置密码</button>
  69. <view class="login-link">
  70. <text>记得密码?</text>
  71. <text class="link" @click="navigateToLogin">立即登录</text>
  72. </view>
  73. </view>
  74. <!-- #ifdef MP-WEIXIN -->
  75. <view class="wechat-tip">
  76. <text>微信用户可直接使用微信登录</text>
  77. <button class="wechat-btn" @click="navigateToLogin">
  78. <text class="wechat-icon">微</text>
  79. <text>返回登录</text>
  80. </button>
  81. </view>
  82. <!-- #endif -->
  83. <view class="notice">
  84. <text class="notice-title">温馨提示</text>
  85. <text class="notice-text">密码重置后,将使用新密码登录系统,请妥善保管您的密码。如有问题请联系客服。</text>
  86. </view>
  87. </view>
  88. </template>
  89. <script>
  90. import storage from "@/utils/storage.js";
  91. // import { resetPassword, sendVerificationCode } from '@/api/services/auth.js';
  92. export default {
  93. data() {
  94. return {
  95. formData: {
  96. phone: '',
  97. code: '',
  98. password: '',
  99. confirmPassword: ''
  100. },
  101. showPassword: false,
  102. showConfirmPassword: false,
  103. codeBtnText: '获取验证码',
  104. codeBtnDisabled: false,
  105. countdown: 60,
  106. isSubmitting: false
  107. }
  108. },
  109. methods: {
  110. goBack() {
  111. uni.navigateBack()
  112. },
  113. togglePasswordVisibility() {
  114. this.showPassword = !this.showPassword
  115. },
  116. toggleConfirmPasswordVisibility() {
  117. this.showConfirmPassword = !this.showConfirmPassword
  118. },
  119. navigateToLogin() {
  120. uni.navigateBack()
  121. },
  122. getVerificationCode() {
  123. if (this.codeBtnDisabled) return
  124. if (!this.formData.phone) {
  125. uni.showToast({
  126. title: '请输入手机号',
  127. icon: 'none'
  128. })
  129. return
  130. }
  131. if (!/^1\d{10}$/.test(this.formData.phone)) {
  132. uni.showToast({
  133. title: '请输入正确的手机号',
  134. icon: 'none'
  135. })
  136. return
  137. }
  138. // 发送验证码请求
  139. uni.showLoading({ title: '发送中...' })
  140. // 调用发送验证码接口
  141. uni.request({
  142. url: 'API_URL/user/sendCode', // 替换为实际接口地址
  143. method: 'POST',
  144. data: {
  145. phone: this.formData.phone,
  146. type: 'reset'
  147. },
  148. success: (res) => {
  149. if (res.data.code === 200) {
  150. uni.showToast({
  151. title: '验证码已发送',
  152. icon: 'success'
  153. });
  154. this.startCountdown();
  155. } else {
  156. uni.showToast({
  157. title: res.data.msg || '发送失败,请稍后重试',
  158. icon: 'none'
  159. });
  160. }
  161. },
  162. fail: (err) => {
  163. uni.showToast({
  164. title: '网络异常,请稍后重试',
  165. icon: 'none'
  166. });
  167. console.error(err);
  168. },
  169. complete: () => {
  170. uni.hideLoading();
  171. }
  172. });
  173. },
  174. startCountdown() {
  175. this.codeBtnDisabled = true
  176. this.codeBtnText = `${this.countdown}秒`
  177. const timer = setInterval(() => {
  178. this.countdown--
  179. this.codeBtnText = `${this.countdown}秒`
  180. if (this.countdown <= 0) {
  181. clearInterval(timer)
  182. this.codeBtnDisabled = false
  183. this.codeBtnText = '获取验证码'
  184. this.countdown = 60
  185. }
  186. }, 1000)
  187. },
  188. validateForm() {
  189. if (!this.formData.phone) {
  190. uni.showToast({
  191. title: '请输入手机号',
  192. icon: 'none'
  193. })
  194. return false
  195. }
  196. if (!/^1\d{10}$/.test(this.formData.phone)) {
  197. uni.showToast({
  198. title: '请输入正确的手机号',
  199. icon: 'none'
  200. })
  201. return false
  202. }
  203. if (!this.formData.code) {
  204. uni.showToast({
  205. title: '请输入验证码',
  206. icon: 'none'
  207. })
  208. return false
  209. }
  210. if (!this.formData.password) {
  211. uni.showToast({
  212. title: '请设置新密码',
  213. icon: 'none'
  214. })
  215. return false
  216. }
  217. if (this.formData.password.length < 6) {
  218. uni.showToast({
  219. title: '密码长度不能少于6位',
  220. icon: 'none'
  221. })
  222. return false
  223. }
  224. if (this.formData.password !== this.formData.confirmPassword) {
  225. uni.showToast({
  226. title: '两次输入的密码不一致',
  227. icon: 'none'
  228. })
  229. return false
  230. }
  231. return true
  232. },
  233. handleResetPassword() {
  234. if (!this.validateForm()) return
  235. this.isSubmitting = true;
  236. // 调用重置密码接口
  237. uni.showLoading({ title: '提交中...' })
  238. // 调用重置密码接口
  239. uni.request({
  240. url: 'API_URL/user/resetPassword', // 替换为实际接口地址
  241. method: 'POST',
  242. data: {
  243. phone: this.formData.phone,
  244. code: this.formData.code,
  245. password: this.formData.password
  246. },
  247. success: (res) => {
  248. if (res.data.code === 200) {
  249. uni.showToast({
  250. title: '密码重置成功',
  251. icon: 'success'
  252. });
  253. // 延迟跳转到登录页
  254. setTimeout(() => {
  255. uni.navigateBack();
  256. }, 1500);
  257. } else {
  258. uni.showToast({
  259. title: res.data.msg || '重置失败,请稍后重试',
  260. icon: 'none'
  261. });
  262. }
  263. },
  264. fail: (err) => {
  265. uni.showToast({
  266. title: '网络异常,请稍后重试',
  267. icon: 'none'
  268. });
  269. console.error(err);
  270. },
  271. complete: () => {
  272. uni.hideLoading();
  273. this.isSubmitting = false;
  274. }
  275. });
  276. }
  277. }
  278. }
  279. </script>
  280. <style>
  281. .forget-container {
  282. min-height: 100vh;
  283. padding: 0 40rpx 40rpx;
  284. background-color: #fff;
  285. display: flex;
  286. flex-direction: column;
  287. }
  288. .page-header {
  289. display: flex;
  290. justify-content: space-between;
  291. align-items: center;
  292. padding: 40rpx 0;
  293. }
  294. .back-icon {
  295. font-size: 50rpx;
  296. color: #333;
  297. width: 60rpx;
  298. }
  299. .page-title {
  300. font-size: 36rpx;
  301. font-weight: bold;
  302. color: #333;
  303. }
  304. .placeholder {
  305. width: 60rpx;
  306. }
  307. .forget-form {
  308. width: 100%;
  309. margin-bottom: 30rpx;
  310. }
  311. .form-group {
  312. margin-bottom: 30rpx;
  313. }
  314. .input-group {
  315. display: flex;
  316. align-items: center;
  317. border-bottom: 1rpx solid #E0E0E0;
  318. padding: 20rpx 0;
  319. }
  320. .input-icon {
  321. width: 44rpx;
  322. height: 44rpx;
  323. background-color: #E8F5E9;
  324. color: #4CAF50;
  325. font-size: 24rpx;
  326. border-radius: 8rpx;
  327. display: flex;
  328. align-items: center;
  329. justify-content: center;
  330. margin-right: 20rpx;
  331. }
  332. .input {
  333. flex: 1;
  334. height: 44rpx;
  335. font-size: 28rpx;
  336. }
  337. .eye-icon {
  338. padding: 0 10rpx;
  339. color: #999;
  340. }
  341. .code-btn {
  342. font-size: 28rpx;
  343. color: #4CAF50;
  344. padding: 0 10rpx;
  345. }
  346. .code-btn.disabled {
  347. color: #999;
  348. }
  349. .submit-btn {
  350. width: 100%;
  351. height: 90rpx;
  352. background-color: #4CAF50;
  353. color: #fff;
  354. border-radius: 45rpx;
  355. font-size: 32rpx;
  356. font-weight: bold;
  357. display: flex;
  358. align-items: center;
  359. justify-content: center;
  360. border: none;
  361. margin: 60rpx 0 30rpx;
  362. }
  363. .login-link {
  364. display: flex;
  365. justify-content: center;
  366. font-size: 28rpx;
  367. color: #666;
  368. }
  369. .link {
  370. color: #4CAF50;
  371. margin-left: 10rpx;
  372. }
  373. /* 微信提示区域 */
  374. .wechat-tip {
  375. margin-top: 60rpx;
  376. display: flex;
  377. flex-direction: column;
  378. align-items: center;
  379. }
  380. .wechat-tip text {
  381. font-size: 28rpx;
  382. color: #666;
  383. margin-bottom: 20rpx;
  384. }
  385. .wechat-btn {
  386. width: 80%;
  387. height: 90rpx;
  388. background-color: #07C160;
  389. color: #fff;
  390. border-radius: 45rpx;
  391. font-size: 32rpx;
  392. font-weight: bold;
  393. display: flex;
  394. align-items: center;
  395. justify-content: center;
  396. border: none;
  397. padding: 0;
  398. }
  399. .wechat-icon {
  400. width: 40rpx;
  401. height: 40rpx;
  402. background-color: rgba(255, 255, 255, 0.2);
  403. color: #fff;
  404. border-radius: 20rpx;
  405. font-size: 20rpx;
  406. display: flex;
  407. align-items: center;
  408. justify-content: center;
  409. margin-right: 10rpx;
  410. }
  411. /* 提示区域 */
  412. .notice {
  413. margin-top: 60rpx;
  414. padding: 30rpx;
  415. background-color: #F5F5F5;
  416. border-radius: 16rpx;
  417. }
  418. .notice-title {
  419. font-size: 30rpx;
  420. color: #333;
  421. font-weight: bold;
  422. margin-bottom: 10rpx;
  423. display: block;
  424. }
  425. .notice-text {
  426. font-size: 24rpx;
  427. color: #666;
  428. line-height: 1.6;
  429. }
  430. /* #ifdef H5 */
  431. /* H5特殊样式调整 */
  432. @media screen and (min-width: 768px) {
  433. .forget-form {
  434. max-width: 600rpx;
  435. margin: 0 auto;
  436. }
  437. .notice {
  438. max-width: 600rpx;
  439. margin: 60rpx auto 0;
  440. }
  441. }
  442. /* #endif */
  443. </style>