Browse Source

优化登录接口

lihuilin/feature-20251024095243-我的
Ethereal 4 weeks ago
parent
commit
249f4c911a
  1. 31
      api/start/login.js
  2. 47
      pages/start/login/login.vue
  3. 23
      server/login.json
  4. 2
      stores/modules/userInfo.js
  5. 14
      utils/http.js

31
api/start/login.js

@ -15,8 +15,10 @@ import { http } from '../../utils/http'
*/ */
export const LoginApi = (data) => { export const LoginApi = (data) => {
return http({ return http({
// method: 'POST',
// url: '/UserLogin/login',
method: 'GET', method: 'GET',
url: '/loginFailureWrongCode',
url: '/loginSuccessByEmail',
data: { data: {
data data
}, },
@ -26,14 +28,31 @@ export const LoginApi = (data) => {
/** /**
* 发送验证码
* @param {*} phoneNumber
* 发送邮箱验证码
* @param {*} email
* @returns * @returns
*/ */
export const SendCodeApi = (data) => {
export const SendEmailCodeApi = (data) => {
return http({ return http({
method: 'GET',
url: '/sendCodeFailureTooFrequent',
method: 'POST',
url: '/UserLogin/sendEmail',
data:{
data
}
})
}
/**
* 发送手机验证码
* @param {*} email
* @returns
*/
export const SendPhoneCodeApi = (data) => {
return http({
method: 'POST',
url: '/UserLogin/sendPhone',
data:{ data:{
data data
} }

47
pages/start/login/login.vue

@ -191,7 +191,7 @@
</view> </view>
<!-- 注册按钮 --> <!-- 注册按钮 -->
<button class="register-btn" @click="register">登录</button>
<button class="register-btn" @click="Login">登录</button>
<!-- 或者 --> <!-- 或者 -->
<text class="or-text" @click="goToRegistration" <text class="or-text" @click="goToRegistration"
@ -252,7 +252,13 @@ import footerBar from "../../../components/footerBar-cn";
import uniPopupDialogVue from "../../../uni_modules/uni-popup/components/uni-popup-dialog/uni-popup-dialog.vue"; import uniPopupDialogVue from "../../../uni_modules/uni-popup/components/uni-popup-dialog/uni-popup-dialog.vue";
import uniPopup from "../../../uni_modules/uni-popup/components/uni-popup/uni-popup.vue"; import uniPopup from "../../../uni_modules/uni-popup/components/uni-popup/uni-popup.vue";
import { verificationPhone, verificationEmail } from "../login/verification"; import { verificationPhone, verificationEmail } from "../login/verification";
import { LoginApi, SendCodeApi } from "../../../api/start/login";
import {
LoginApi,
SendEmailCodeApi,
SendPhoneCodeApi,
} from "../../../api/start/login";
import { useUserStore } from "../../../stores/modules/userInfo";
const deepChartID = ref(""); const deepChartID = ref("");
const type = ref("member"); const type = ref("member");
@ -328,7 +334,7 @@ function switchPhone() {
} }
// //
async function register() {
async function Login() {
if (!basicVerification()) { if (!basicVerification()) {
return; return;
} }
@ -351,6 +357,12 @@ async function register() {
title: "登录成功", title: "登录成功",
icon: "success", icon: "success",
}); });
const userStore = useUserStore();
userStore.setUserInfo(res.data)
console.log("userInfo为",userStore.userInfo);
// //
uni.switchTab({ uni.switchTab({
url: "/pages/start/index/index", url: "/pages/start/index/index",
@ -529,6 +541,7 @@ function handleCancel() {
agreementPopup.value.close(); agreementPopup.value.close();
} }
//
function loginWithApple() { function loginWithApple() {
// Apple // Apple
console.log("通过Apple登录"); console.log("通过Apple登录");
@ -551,6 +564,7 @@ function loginWithApple() {
}); });
} }
//
function loginWithGoogle() { function loginWithGoogle() {
// Google // Google
console.log("通过Google登录"); console.log("通过Google登录");
@ -591,17 +605,22 @@ function onPhoneInput(e) {
} }
} }
//
async function sendCode() { async function sendCode() {
if (!VerCodeVerfifcation()) { if (!VerCodeVerfifcation()) {
return; return;
} }
// //
if (isCodeBtnDisabled.value) return; if (isCodeBtnDisabled.value) return;
if (switchType.value === "Phone") {
// //
const res = await SendCodeApi({});
console.log("验证码:", res.message);
const phoneAll = `${country.value}${phone.value}`;
const res = await SendPhoneCodeApi({
phone: phoneAll,
});
console.log("手机验证码:", res.message);
const message = res.message; const message = res.message;
if (res.code === 200) { if (res.code === 200) {
@ -610,6 +629,22 @@ async function sendCode() {
icon: "none", icon: "none",
}); });
} }
}
if (switchType.value === "Email") {
//
const res = await SendEmailCodeApi({
email: email.value,
});
console.log("邮箱验证码:", res.message);
const message = res.message;
if (res.code === 200) {
uni.showToast({
title: message,
icon: "none",
});
}
}
// //
isCodeBtnDisabled.value = true; isCodeBtnDisabled.value = true;

23
server/login.json

@ -1,15 +1,22 @@
{ {
"loginSuccessByEmail": { "loginSuccessByEmail": {
"code": 200, "code": 200,
"message": "登录成功",
"interface": "login",
"message": "c3e9ed50ad72073b94dfb04860562b58",
"data": { "data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"userInfo": {
"id": "123456789",
"username": "q614588746",
"email": "q614588746@163.com"
}
"loginType": null,
"device": "unknown",
"dccode": "90047686",
"account": null,
"password": null,
"verifyCode": null,
"useCode": false,
"idToken": null,
"token": "c3e9ed50ad72073b94dfb04860562b58",
"market": "新加坡",
"phone": "17861484516",
"email": "q614588746@163.com",
"language": "中文",
"avatar": "123"
} }
}, },
"loginFailureEmailNotFound": { "loginFailureEmailNotFound": {

2
stores/modules/userInfo.js

@ -2,7 +2,7 @@ import { defineStore } from 'pinia'
import { ref } from 'vue' import { ref } from 'vue'
// 定义 Store // 定义 Store
export const useMemberStore = defineStore(
export const useUserStore = defineStore(
'member', 'member',
() => { () => {
// 会员信息 // 会员信息

14
utils/http.js

@ -1,4 +1,4 @@
import { useMemberStore } from '@/stores'
import { useUserStore } from "../stores/modules/userInfo";
const baseURL = "http://localhost:8888" const baseURL = "http://localhost:8888"
@ -19,10 +19,12 @@ const httpInterceptor = {
'source-client': 'miniapp' 'source-client': 'miniapp'
} }
//4 添加token //4 添加token
const memberStore = useMemberStore()
const token = memberStore.profile?.token
const memberStore = useUserStore()
const token = memberStore.userInfo?.token
if (token) { if (token) {
options.header.Authorization = token
options.header.Authorization = {
'token': token
}
} }
return options return options
} }
@ -41,8 +43,8 @@ export const http = (options) => {
resolve(result.data) resolve(result.data)
} else if (result.statusCode === 401) { } else if (result.statusCode === 401) {
// 清除登录信息 // 清除登录信息
const memberStore = useMemberStore()
memberStore.clearProfile()
const memberStore = useUserStore()
memberStore.clearUserInfo()
// 提示用户重新登录 // 提示用户重新登录
uni.navigateTo({ uni.navigateTo({
url: '/pages/login/login' url: '/pages/login/login'

Loading…
Cancel
Save