forget-password.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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, forgetPassword, sendPhoneVcodeNoUnique } from '@/api/services/connect.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 = async () => {
  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. uni.showLoading({ title: '验证中...' })
  150. try {
  151. // 第一步:校验用户信息
  152. // const verifyRes = await forgetPassword({
  153. // phonenumber: formData.phone
  154. // })
  155. // console.log("verifyRes",verifyRes);
  156. // if (verifyRes.data.code !== 200) {
  157. // uni.showToast({
  158. // title: verifyRes.data.msg || '用户信息校验失败',
  159. // icon: 'none'
  160. // })
  161. // return
  162. // }
  163. // 第二步:发送验证码
  164. uni.showLoading({ title: '发送中...' })
  165. const codeRes = await sendPhoneVcodeNoUnique({
  166. terminal: formData.phone,
  167. scene: 'FORGET_PWD'
  168. })
  169. console.log("codeRes",codeRes);
  170. if (codeRes.data.code === 200) {
  171. uni.showToast({
  172. title: '验证码已发送',
  173. icon: 'success'
  174. })
  175. startCountdown()
  176. } else {
  177. uni.showToast({
  178. title: codeRes.data.msg || '发送失败,请稍后重试',
  179. icon: 'none'
  180. })
  181. }
  182. } catch (err) {
  183. uni.showToast({
  184. title: '网络异常,请稍后重试',
  185. icon: 'none'
  186. })
  187. console.error(err)
  188. } finally {
  189. uni.hideLoading()
  190. }
  191. }
  192. const validateForm = () => {
  193. if (!formData.phone) {
  194. uni.showToast({
  195. title: '请输入手机号',
  196. icon: 'none'
  197. })
  198. return false
  199. }
  200. if (!/^1\d{10}$/.test(formData.phone)) {
  201. uni.showToast({
  202. title: '请输入正确的手机号',
  203. icon: 'none'
  204. })
  205. return false
  206. }
  207. if (!formData.code) {
  208. uni.showToast({
  209. title: '请输入验证码',
  210. icon: 'none'
  211. })
  212. return false
  213. }
  214. if (!formData.password) {
  215. uni.showToast({
  216. title: '请设置新密码',
  217. icon: 'none'
  218. })
  219. return false
  220. }
  221. if (formData.password.length < 6) {
  222. uni.showToast({
  223. title: '密码长度不能少于6位',
  224. icon: 'none'
  225. })
  226. return false
  227. }
  228. if (formData.password !== formData.confirmPassword) {
  229. uni.showToast({
  230. title: '两次输入的密码不一致',
  231. icon: 'none'
  232. })
  233. return false
  234. }
  235. return true
  236. }
  237. const handleResetPassword = async () => {
  238. if (!validateForm()) return
  239. isSubmitting.value = true
  240. uni.showLoading({ title: '提交中...' })
  241. try {
  242. const res = await resetPassword({
  243. phonenumber: formData.phone,
  244. vcode: formData.code,
  245. password: formData.password
  246. })
  247. if (res.data.code === 200) {
  248. uni.showToast({
  249. title: '密码重置成功',
  250. icon: 'success'
  251. })
  252. // 延迟跳转到登录页
  253. setTimeout(() => {
  254. uni.navigateBack()
  255. }, 1500)
  256. } else {
  257. uni.showToast({
  258. title: res.data.msg || '重置失败,请稍后重试',
  259. icon: 'none'
  260. })
  261. }
  262. } catch (err) {
  263. uni.showToast({
  264. title: '网络异常,请稍后重试',
  265. icon: 'none'
  266. })
  267. console.error(err)
  268. } finally {
  269. uni.hideLoading()
  270. isSubmitting.value = false
  271. }
  272. }
  273. </script>
  274. <style>
  275. .forget-container {
  276. min-height: 100vh;
  277. padding: 0 40rpx 40rpx;
  278. background-color: #fff;
  279. display: flex;
  280. flex-direction: column;
  281. }
  282. .page-header {
  283. display: flex;
  284. justify-content: space-between;
  285. align-items: center;
  286. padding: 40rpx 0;
  287. }
  288. .back-icon {
  289. font-size: 50rpx;
  290. color: #333;
  291. width: 60rpx;
  292. }
  293. .page-title {
  294. font-size: 36rpx;
  295. font-weight: bold;
  296. color: #333;
  297. }
  298. .placeholder {
  299. width: 60rpx;
  300. }
  301. .forget-form {
  302. width: 100%;
  303. margin-bottom: 30rpx;
  304. }
  305. .form-group {
  306. margin-bottom: 30rpx;
  307. }
  308. .input-group {
  309. display: flex;
  310. align-items: center;
  311. border-bottom: 1rpx solid #E0E0E0;
  312. padding: 20rpx 0;
  313. }
  314. .input-icon {
  315. width: 44rpx;
  316. height: 44rpx;
  317. background-color: #E8F5E9;
  318. color: #4CAF50;
  319. font-size: 24rpx;
  320. border-radius: 8rpx;
  321. display: flex;
  322. align-items: center;
  323. justify-content: center;
  324. margin-right: 20rpx;
  325. }
  326. .input {
  327. flex: 1;
  328. height: 44rpx;
  329. font-size: 28rpx;
  330. }
  331. .eye-icon {
  332. padding: 0 10rpx;
  333. color: #999;
  334. }
  335. .code-btn {
  336. font-size: 28rpx;
  337. color: #4CAF50;
  338. padding: 0 10rpx;
  339. }
  340. .code-btn.disabled {
  341. color: #999;
  342. }
  343. .submit-btn {
  344. width: 100%;
  345. height: 90rpx;
  346. background-color: #4CAF50;
  347. color: #fff;
  348. border-radius: 45rpx;
  349. font-size: 32rpx;
  350. font-weight: bold;
  351. display: flex;
  352. align-items: center;
  353. justify-content: center;
  354. border: none;
  355. margin: 60rpx 0 30rpx;
  356. }
  357. .login-link {
  358. display: flex;
  359. justify-content: center;
  360. font-size: 28rpx;
  361. color: #666;
  362. }
  363. .link {
  364. color: #4CAF50;
  365. margin-left: 10rpx;
  366. }
  367. /* 微信提示区域 */
  368. .wechat-tip {
  369. margin-top: 60rpx;
  370. display: flex;
  371. flex-direction: column;
  372. align-items: center;
  373. }
  374. .wechat-tip text {
  375. font-size: 28rpx;
  376. color: #666;
  377. margin-bottom: 20rpx;
  378. }
  379. .wechat-btn {
  380. width: 80%;
  381. height: 90rpx;
  382. background-color: #07C160;
  383. color: #fff;
  384. border-radius: 45rpx;
  385. font-size: 32rpx;
  386. font-weight: bold;
  387. display: flex;
  388. align-items: center;
  389. justify-content: center;
  390. border: none;
  391. padding: 0;
  392. }
  393. .wechat-icon {
  394. width: 40rpx;
  395. height: 40rpx;
  396. background-color: rgba(255, 255, 255, 0.2);
  397. color: #fff;
  398. border-radius: 20rpx;
  399. font-size: 20rpx;
  400. display: flex;
  401. align-items: center;
  402. justify-content: center;
  403. margin-right: 10rpx;
  404. }
  405. /* 提示区域 */
  406. .notice {
  407. margin-top: 60rpx;
  408. padding: 30rpx;
  409. background-color: #F5F5F5;
  410. border-radius: 16rpx;
  411. }
  412. .notice-title {
  413. font-size: 30rpx;
  414. color: #333;
  415. font-weight: bold;
  416. margin-bottom: 10rpx;
  417. display: block;
  418. }
  419. .notice-text {
  420. font-size: 24rpx;
  421. color: #666;
  422. line-height: 1.6;
  423. }
  424. /* #ifdef H5 */
  425. /* H5特殊样式调整 */
  426. @media screen and (min-width: 768px) {
  427. .forget-form {
  428. max-width: 600rpx;
  429. margin: 0 auto;
  430. }
  431. .notice {
  432. max-width: 600rpx;
  433. margin: 60rpx auto 0;
  434. }
  435. }
  436. /* #endif */
  437. /* #ifdef APP-PLUS || MP-HARMONY */
  438. /* APP和鸿蒙特殊样式调整 */
  439. .forget-container {
  440. padding-top: 20rpx;
  441. }
  442. .input-group {
  443. background-color: #f5f5f5;
  444. border-radius: 8rpx;
  445. padding: 20rpx 15rpx;
  446. border-bottom: none;
  447. margin-bottom: 20rpx;
  448. }
  449. .input {
  450. background-color: transparent;
  451. }
  452. .submit-btn {
  453. margin-top: 40rpx;
  454. }
  455. .notice {
  456. margin-top: 40rpx;
  457. border: 1rpx solid #E0E0E0;
  458. }
  459. .wechat-tip {
  460. margin-top: 40rpx;
  461. }
  462. /* #endif */
  463. </style>