forget-password.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 setup>
  90. import { ref, reactive } from 'vue'
  91. import storage from "@/utils/storage.js"
  92. // import { resetPassword, sendVerificationCode } from '@/api/services/auth.js'
  93. // 响应式数据
  94. const formData = reactive({
  95. phone: '',
  96. code: '',
  97. password: '',
  98. confirmPassword: ''
  99. })
  100. const showPassword = ref(false)
  101. const showConfirmPassword = ref(false)
  102. const codeBtnText = ref('获取验证码')
  103. const codeBtnDisabled = ref(false)
  104. const countdown = ref(60)
  105. const isSubmitting = ref(false)
  106. // 方法定义
  107. const goBack = () => {
  108. uni.navigateBack()
  109. }
  110. const togglePasswordVisibility = () => {
  111. showPassword.value = !showPassword.value
  112. }
  113. const toggleConfirmPasswordVisibility = () => {
  114. showConfirmPassword.value = !showConfirmPassword.value
  115. }
  116. const navigateToLogin = () => {
  117. uni.navigateBack()
  118. }
  119. const startCountdown = () => {
  120. codeBtnDisabled.value = true
  121. codeBtnText.value = `${countdown.value}秒`
  122. const timer = setInterval(() => {
  123. countdown.value--
  124. codeBtnText.value = `${countdown.value}秒`
  125. if (countdown.value <= 0) {
  126. clearInterval(timer)
  127. codeBtnDisabled.value = false
  128. codeBtnText.value = '获取验证码'
  129. countdown.value = 60
  130. }
  131. }, 1000)
  132. }
  133. const getVerificationCode = () => {
  134. if (codeBtnDisabled.value) return
  135. if (!formData.phone) {
  136. uni.showToast({
  137. title: '请输入手机号',
  138. icon: 'none'
  139. })
  140. return
  141. }
  142. if (!/^1\d{10}$/.test(formData.phone)) {
  143. uni.showToast({
  144. title: '请输入正确的手机号',
  145. icon: 'none'
  146. })
  147. return
  148. }
  149. // 发送验证码请求
  150. uni.showLoading({ title: '发送中...' })
  151. // 调用发送验证码接口
  152. uni.request({
  153. url: 'API_URL/user/sendCode', // 替换为实际接口地址
  154. method: 'POST',
  155. data: {
  156. phone: formData.phone,
  157. type: 'reset'
  158. },
  159. success: (res) => {
  160. if (res.data.code === 200) {
  161. uni.showToast({
  162. title: '验证码已发送',
  163. icon: 'success'
  164. })
  165. startCountdown()
  166. } else {
  167. uni.showToast({
  168. title: res.data.msg || '发送失败,请稍后重试',
  169. icon: 'none'
  170. })
  171. }
  172. },
  173. fail: (err) => {
  174. uni.showToast({
  175. title: '网络异常,请稍后重试',
  176. icon: 'none'
  177. })
  178. console.error(err)
  179. },
  180. complete: () => {
  181. uni.hideLoading()
  182. }
  183. })
  184. }
  185. const validateForm = () => {
  186. if (!formData.phone) {
  187. uni.showToast({
  188. title: '请输入手机号',
  189. icon: 'none'
  190. })
  191. return false
  192. }
  193. if (!/^1\d{10}$/.test(formData.phone)) {
  194. uni.showToast({
  195. title: '请输入正确的手机号',
  196. icon: 'none'
  197. })
  198. return false
  199. }
  200. if (!formData.code) {
  201. uni.showToast({
  202. title: '请输入验证码',
  203. icon: 'none'
  204. })
  205. return false
  206. }
  207. if (!formData.password) {
  208. uni.showToast({
  209. title: '请设置新密码',
  210. icon: 'none'
  211. })
  212. return false
  213. }
  214. if (formData.password.length < 6) {
  215. uni.showToast({
  216. title: '密码长度不能少于6位',
  217. icon: 'none'
  218. })
  219. return false
  220. }
  221. if (formData.password !== formData.confirmPassword) {
  222. uni.showToast({
  223. title: '两次输入的密码不一致',
  224. icon: 'none'
  225. })
  226. return false
  227. }
  228. return true
  229. }
  230. const handleResetPassword = () => {
  231. if (!validateForm()) return
  232. isSubmitting.value = true
  233. // 调用重置密码接口
  234. uni.showLoading({ title: '提交中...' })
  235. // 调用重置密码接口
  236. uni.request({
  237. url: 'API_URL/user/resetPassword', // 替换为实际接口地址
  238. method: 'POST',
  239. data: {
  240. phone: formData.phone,
  241. code: formData.code,
  242. password: formData.password
  243. },
  244. success: (res) => {
  245. if (res.data.code === 200) {
  246. uni.showToast({
  247. title: '密码重置成功',
  248. icon: 'success'
  249. })
  250. // 延迟跳转到登录页
  251. setTimeout(() => {
  252. uni.navigateBack()
  253. }, 1500)
  254. } else {
  255. uni.showToast({
  256. title: res.data.msg || '重置失败,请稍后重试',
  257. icon: 'none'
  258. })
  259. }
  260. },
  261. fail: (err) => {
  262. uni.showToast({
  263. title: '网络异常,请稍后重试',
  264. icon: 'none'
  265. })
  266. console.error(err)
  267. },
  268. complete: () => {
  269. uni.hideLoading()
  270. isSubmitting.value = false
  271. }
  272. })
  273. }
  274. </script>
  275. <style>
  276. .forget-container {
  277. min-height: 100vh;
  278. padding: 0 40rpx 40rpx;
  279. background-color: #fff;
  280. display: flex;
  281. flex-direction: column;
  282. }
  283. .page-header {
  284. display: flex;
  285. justify-content: space-between;
  286. align-items: center;
  287. padding: 40rpx 0;
  288. }
  289. .back-icon {
  290. font-size: 50rpx;
  291. color: #333;
  292. width: 60rpx;
  293. }
  294. .page-title {
  295. font-size: 36rpx;
  296. font-weight: bold;
  297. color: #333;
  298. }
  299. .placeholder {
  300. width: 60rpx;
  301. }
  302. .forget-form {
  303. width: 100%;
  304. margin-bottom: 30rpx;
  305. }
  306. .form-group {
  307. margin-bottom: 30rpx;
  308. }
  309. .input-group {
  310. display: flex;
  311. align-items: center;
  312. border-bottom: 1rpx solid #E0E0E0;
  313. padding: 20rpx 0;
  314. }
  315. .input-icon {
  316. width: 44rpx;
  317. height: 44rpx;
  318. background-color: #E8F5E9;
  319. color: #4CAF50;
  320. font-size: 24rpx;
  321. border-radius: 8rpx;
  322. display: flex;
  323. align-items: center;
  324. justify-content: center;
  325. margin-right: 20rpx;
  326. }
  327. .input {
  328. flex: 1;
  329. height: 44rpx;
  330. font-size: 28rpx;
  331. }
  332. .eye-icon {
  333. padding: 0 10rpx;
  334. color: #999;
  335. }
  336. .code-btn {
  337. font-size: 28rpx;
  338. color: #4CAF50;
  339. padding: 0 10rpx;
  340. }
  341. .code-btn.disabled {
  342. color: #999;
  343. }
  344. .submit-btn {
  345. width: 100%;
  346. height: 90rpx;
  347. background-color: #4CAF50;
  348. color: #fff;
  349. border-radius: 45rpx;
  350. font-size: 32rpx;
  351. font-weight: bold;
  352. display: flex;
  353. align-items: center;
  354. justify-content: center;
  355. border: none;
  356. margin: 60rpx 0 30rpx;
  357. }
  358. .login-link {
  359. display: flex;
  360. justify-content: center;
  361. font-size: 28rpx;
  362. color: #666;
  363. }
  364. .link {
  365. color: #4CAF50;
  366. margin-left: 10rpx;
  367. }
  368. /* 微信提示区域 */
  369. .wechat-tip {
  370. margin-top: 60rpx;
  371. display: flex;
  372. flex-direction: column;
  373. align-items: center;
  374. }
  375. .wechat-tip text {
  376. font-size: 28rpx;
  377. color: #666;
  378. margin-bottom: 20rpx;
  379. }
  380. .wechat-btn {
  381. width: 80%;
  382. height: 90rpx;
  383. background-color: #07C160;
  384. color: #fff;
  385. border-radius: 45rpx;
  386. font-size: 32rpx;
  387. font-weight: bold;
  388. display: flex;
  389. align-items: center;
  390. justify-content: center;
  391. border: none;
  392. padding: 0;
  393. }
  394. .wechat-icon {
  395. width: 40rpx;
  396. height: 40rpx;
  397. background-color: rgba(255, 255, 255, 0.2);
  398. color: #fff;
  399. border-radius: 20rpx;
  400. font-size: 20rpx;
  401. display: flex;
  402. align-items: center;
  403. justify-content: center;
  404. margin-right: 10rpx;
  405. }
  406. /* 提示区域 */
  407. .notice {
  408. margin-top: 60rpx;
  409. padding: 30rpx;
  410. background-color: #F5F5F5;
  411. border-radius: 16rpx;
  412. }
  413. .notice-title {
  414. font-size: 30rpx;
  415. color: #333;
  416. font-weight: bold;
  417. margin-bottom: 10rpx;
  418. display: block;
  419. }
  420. .notice-text {
  421. font-size: 24rpx;
  422. color: #666;
  423. line-height: 1.6;
  424. }
  425. /* #ifdef H5 */
  426. /* H5特殊样式调整 */
  427. @media screen and (min-width: 768px) {
  428. .forget-form {
  429. max-width: 600rpx;
  430. margin: 0 auto;
  431. }
  432. .notice {
  433. max-width: 600rpx;
  434. margin: 60rpx auto 0;
  435. }
  436. }
  437. /* #endif */
  438. /* #ifdef APP-PLUS || MP-HARMONY */
  439. /* APP和鸿蒙特殊样式调整 */
  440. .forget-container {
  441. padding-top: 20rpx;
  442. }
  443. .input-group {
  444. background-color: #f5f5f5;
  445. border-radius: 8rpx;
  446. padding: 20rpx 15rpx;
  447. border-bottom: none;
  448. margin-bottom: 20rpx;
  449. }
  450. .input {
  451. background-color: transparent;
  452. }
  453. .submit-btn {
  454. margin-top: 40rpx;
  455. }
  456. .notice {
  457. margin-top: 40rpx;
  458. border: 1rpx solid #E0E0E0;
  459. }
  460. .wechat-tip {
  461. margin-top: 40rpx;
  462. }
  463. /* #endif */
  464. </style>