forget-password.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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">重置密码</button>
  69. <view class="login-link">
  70. <text>记得密码?</text>
  71. <text class="link" @click="navigateToLogin">立即登录</text>
  72. </view>
  73. </view>
  74. </view>
  75. </template>
  76. <script>
  77. import { resetPassword, sendVerificationCode } from '@/api/services/auth.js';
  78. export default {
  79. data() {
  80. return {
  81. formData: {
  82. phone: '',
  83. code: '',
  84. password: '',
  85. confirmPassword: ''
  86. },
  87. showPassword: false,
  88. showConfirmPassword: false,
  89. codeBtnText: '获取验证码',
  90. codeBtnDisabled: false,
  91. countdown: 60
  92. }
  93. },
  94. methods: {
  95. goBack() {
  96. uni.navigateBack()
  97. },
  98. togglePasswordVisibility() {
  99. this.showPassword = !this.showPassword
  100. },
  101. toggleConfirmPasswordVisibility() {
  102. this.showConfirmPassword = !this.showConfirmPassword
  103. },
  104. navigateToLogin() {
  105. uni.navigateBack()
  106. },
  107. getVerificationCode() {
  108. if (this.codeBtnDisabled) return
  109. if (!this.formData.phone) {
  110. uni.showToast({
  111. title: '请输入手机号',
  112. icon: 'none'
  113. })
  114. return
  115. }
  116. if (!/^1\d{10}$/.test(this.formData.phone)) {
  117. uni.showToast({
  118. title: '请输入正确的手机号',
  119. icon: 'none'
  120. })
  121. return
  122. }
  123. // 发送验证码请求
  124. uni.showLoading({ title: '发送中...' })
  125. // 使用授权服务发送验证码
  126. sendVerificationCode({
  127. phone: this.formData.phone,
  128. type: 'reset'
  129. })
  130. .then(() => {
  131. uni.showToast({
  132. title: '验证码已发送',
  133. icon: 'success'
  134. });
  135. this.startCountdown();
  136. })
  137. .catch(error => {
  138. uni.showToast({
  139. title: error,
  140. icon: 'none'
  141. });
  142. })
  143. .finally(() => {
  144. uni.hideLoading();
  145. });
  146. },
  147. startCountdown() {
  148. this.codeBtnDisabled = true
  149. this.codeBtnText = `${this.countdown}秒`
  150. const timer = setInterval(() => {
  151. this.countdown--
  152. this.codeBtnText = `${this.countdown}秒`
  153. if (this.countdown <= 0) {
  154. clearInterval(timer)
  155. this.codeBtnDisabled = false
  156. this.codeBtnText = '获取验证码'
  157. this.countdown = 60
  158. }
  159. }, 1000)
  160. },
  161. validateForm() {
  162. if (!this.formData.phone) {
  163. uni.showToast({
  164. title: '请输入手机号',
  165. icon: 'none'
  166. })
  167. return false
  168. }
  169. if (!/^1\d{10}$/.test(this.formData.phone)) {
  170. uni.showToast({
  171. title: '请输入正确的手机号',
  172. icon: 'none'
  173. })
  174. return false
  175. }
  176. if (!this.formData.code) {
  177. uni.showToast({
  178. title: '请输入验证码',
  179. icon: 'none'
  180. })
  181. return false
  182. }
  183. if (!this.formData.password) {
  184. uni.showToast({
  185. title: '请设置新密码',
  186. icon: 'none'
  187. })
  188. return false
  189. }
  190. if (this.formData.password.length < 6) {
  191. uni.showToast({
  192. title: '密码长度不能少于6位',
  193. icon: 'none'
  194. })
  195. return false
  196. }
  197. if (this.formData.password !== this.formData.confirmPassword) {
  198. uni.showToast({
  199. title: '两次输入的密码不一致',
  200. icon: 'none'
  201. })
  202. return false
  203. }
  204. return true
  205. },
  206. handleResetPassword() {
  207. if (!this.validateForm()) return
  208. // 调用重置密码接口
  209. uni.showLoading({ title: '提交中...' })
  210. // 使用授权服务重置密码
  211. resetPassword({
  212. phone: this.formData.phone,
  213. code: this.formData.code,
  214. password: this.formData.password
  215. })
  216. .then(() => {
  217. uni.showToast({
  218. title: '密码重置成功',
  219. icon: 'success'
  220. });
  221. // 延迟跳转到登录页
  222. setTimeout(() => {
  223. uni.navigateBack();
  224. }, 1500);
  225. })
  226. .catch(error => {
  227. uni.showToast({
  228. title: error,
  229. icon: 'none'
  230. });
  231. })
  232. .finally(() => {
  233. uni.hideLoading();
  234. });
  235. }
  236. }
  237. }
  238. </script>
  239. <style>
  240. .forget-container {
  241. min-height: 100vh;
  242. padding: 0 40rpx 40rpx;
  243. background-color: #fff;
  244. display: flex;
  245. flex-direction: column;
  246. }
  247. .page-header {
  248. display: flex;
  249. justify-content: space-between;
  250. align-items: center;
  251. padding: 40rpx 0;
  252. }
  253. .back-icon {
  254. font-size: 50rpx;
  255. color: #333;
  256. width: 60rpx;
  257. }
  258. .page-title {
  259. font-size: 36rpx;
  260. font-weight: bold;
  261. color: #333;
  262. }
  263. .placeholder {
  264. width: 60rpx;
  265. }
  266. .forget-form {
  267. width: 100%;
  268. margin-bottom: 30rpx;
  269. }
  270. .form-group {
  271. margin-bottom: 30rpx;
  272. }
  273. .input-group {
  274. display: flex;
  275. align-items: center;
  276. border-bottom: 1rpx solid #E0E0E0;
  277. padding: 20rpx 0;
  278. }
  279. .input-icon {
  280. width: 44rpx;
  281. height: 44rpx;
  282. background-color: #E8F5E9;
  283. color: #4CAF50;
  284. font-size: 24rpx;
  285. border-radius: 8rpx;
  286. display: flex;
  287. align-items: center;
  288. justify-content: center;
  289. margin-right: 20rpx;
  290. }
  291. .input {
  292. flex: 1;
  293. height: 44rpx;
  294. font-size: 28rpx;
  295. }
  296. .eye-icon {
  297. padding: 0 10rpx;
  298. color: #999;
  299. }
  300. .code-btn {
  301. font-size: 28rpx;
  302. color: #4CAF50;
  303. padding: 0 10rpx;
  304. }
  305. .code-btn.disabled {
  306. color: #999;
  307. }
  308. .submit-btn {
  309. width: 100%;
  310. height: 90rpx;
  311. background-color: #4CAF50;
  312. color: #fff;
  313. border-radius: 45rpx;
  314. font-size: 32rpx;
  315. font-weight: bold;
  316. display: flex;
  317. align-items: center;
  318. justify-content: center;
  319. border: none;
  320. margin: 60rpx 0 30rpx;
  321. }
  322. .login-link {
  323. display: flex;
  324. justify-content: center;
  325. font-size: 28rpx;
  326. color: #666;
  327. }
  328. .link {
  329. color: #4CAF50;
  330. margin-left: 10rpx;
  331. }
  332. </style>