Ver código fonte

实现忘记密码功能

jiuling 2 meses atrás
pai
commit
e3f30e5abf
2 arquivos alterados com 93 adições e 70 exclusões
  1. 22 0
      api/services/connect.js
  2. 71 70
      pages/login/forget-password.vue

+ 22 - 0
api/services/connect.js

@@ -9,7 +9,29 @@ import {
   // 使用storage模块的方法设置登录状态为false
 import storage from "@/utils/storage.js";
 const request = http.request;
+/**
+ * 程序用户校验信息,用于修改用户密码
+ * @returns {Promise} 校验结果
+ */
+export function forgetPassword(params) {
+  return http.request({
+    url: "uniapp/wechat/forget",
+    method: Method.POST,
+    data:params
+  });
+}
 
+/**
+ * 重置密码
+ * @returns {Promise} 重置密码结果
+ */
+export function resetPassword(params){
+	return http.request({
+	  url: "uniapp/wechat/resetPassword",
+	  method: Method.POST,
+	  data:params
+	});
+}
 
 /**
  * 发送手机验证码 (必须是绑定注册过的手机)

+ 71 - 70
pages/login/forget-password.vue

@@ -98,7 +98,7 @@
 <script setup>
 import { ref, reactive } from 'vue'
 import storage from "@/utils/storage.js"
-// import { resetPassword, sendVerificationCode } from '@/api/services/auth.js'
+import { resetPassword, forgetPassword, sendPhoneVcodeNoUnique } from '@/api/services/connect.js'
 
 // 响应式数据
 const formData = reactive({
@@ -149,7 +149,7 @@ const startCountdown = () => {
   }, 1000)
 }
 
-const getVerificationCode = () => {
+const getVerificationCode = async () => {
   if (codeBtnDisabled.value) return
   
   if (!formData.phone) {
@@ -168,42 +168,51 @@ const getVerificationCode = () => {
     return
   }
   
-  // 发送验证码请求
-  uni.showLoading({ title: '发送中...' })
+  uni.showLoading({ title: '验证中...' })
   
-  // 调用发送验证码接口
-  uni.request({
-    url: 'API_URL/user/sendCode', // 替换为实际接口地址
-    method: 'POST',
-    data: {
-      phone: formData.phone,
-      type: 'reset'
-    },
-    success: (res) => {
-      if (res.data.code === 200) {
-        uni.showToast({
-          title: '验证码已发送',
-          icon: 'success'
-        })
-        startCountdown()
-      } else {
-        uni.showToast({
-          title: res.data.msg || '发送失败,请稍后重试',
-          icon: 'none'
-        })
-      }
-    },
-    fail: (err) => {
+  try {
+    // 第一步:校验用户信息
+    // const verifyRes = await forgetPassword({
+    //   phonenumber: formData.phone
+    // })
+    // console.log("verifyRes",verifyRes);
+    
+    // if (verifyRes.data.code !== 200) {
+    //   uni.showToast({
+    //     title: verifyRes.data.msg || '用户信息校验失败',
+    //     icon: 'none'
+    //   })
+    //   return
+    // }
+    
+    // 第二步:发送验证码
+    uni.showLoading({ title: '发送中...' })
+    const codeRes = await sendPhoneVcodeNoUnique({
+      terminal: formData.phone,
+      scene: 'FORGET_PWD'
+    })
+    console.log("codeRes",codeRes);
+    if (codeRes.data.code === 200) {
+      uni.showToast({
+        title: '验证码已发送',
+        icon: 'success'
+      })
+      startCountdown()
+    } else {
       uni.showToast({
-        title: '网络异常,请稍后重试',
+        title: codeRes.data.msg || '发送失败,请稍后重试',
         icon: 'none'
       })
-      console.error(err)
-    },
-    complete: () => {
-      uni.hideLoading()
     }
-  })
+  } catch (err) {
+    uni.showToast({
+      title: '网络异常,请稍后重试',
+      icon: 'none'
+    })
+    console.error(err)
+  } finally {
+    uni.hideLoading()
+  }
 }
 
 const validateForm = () => {
@@ -258,53 +267,45 @@ const validateForm = () => {
   return true
 }
 
-const handleResetPassword = () => {
+const handleResetPassword = async () => {
   if (!validateForm()) return
   
   isSubmitting.value = true
-  
-  // 调用重置密码接口
   uni.showLoading({ title: '提交中...' })
   
-  // 调用重置密码接口
-  uni.request({
-    url: 'API_URL/user/resetPassword', // 替换为实际接口地址
-    method: 'POST',
-    data: {
-      phone: formData.phone,
-      code: formData.code,
+  try {
+    const res = await resetPassword({
+      phonenumber: formData.phone,
+      vcode: formData.code,
       password: formData.password
-    },
-    success: (res) => {
-      if (res.data.code === 200) {
-        uni.showToast({
-          title: '密码重置成功',
-          icon: 'success'
-        })
-        
-        // 延迟跳转到登录页
-        setTimeout(() => {
-          uni.navigateBack()
-        }, 1500)
-      } else {
-        uni.showToast({
-          title: res.data.msg || '重置失败,请稍后重试',
-          icon: 'none'
-        })
-      }
-    },
-    fail: (err) => {
+    })
+    
+    if (res.data.code === 200) {
+      uni.showToast({
+        title: '密码重置成功',
+        icon: 'success'
+      })
+      
+      // 延迟跳转到登录页
+      setTimeout(() => {
+        uni.navigateBack()
+      }, 1500)
+    } else {
       uni.showToast({
-        title: '网络异常,请稍后重试',
+        title: res.data.msg || '重置失败,请稍后重试',
         icon: 'none'
       })
-      console.error(err)
-    },
-    complete: () => {
-      uni.hideLoading()
-      isSubmitting.value = false
     }
-  })
+  } catch (err) {
+    uni.showToast({
+      title: '网络异常,请稍后重试',
+      icon: 'none'
+    })
+    console.error(err)
+  } finally {
+    uni.hideLoading()
+    isSubmitting.value = false
+  }
 }
 </script>