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) => {
return http({
// method: 'POST',
// url: '/UserLogin/login',
method: 'GET',
url: '/loginFailureWrongCode',
url: '/loginSuccessByEmail',
data: {
data
},
@ -26,14 +28,31 @@ export const LoginApi = (data) => {
/**
* 发送验证码
* @param {*} phoneNumber
* 发送邮箱验证码
* @param {*} email
* @returns
*/
export const SendCodeApi = (data) => {
export const SendEmailCodeApi = (data) => {
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
}

47
pages/start/login/login.vue

@ -191,7 +191,7 @@
</view>
<!-- 注册按钮 -->
<button class="register-btn" @click="register">登录</button>
<button class="register-btn" @click="Login">登录</button>
<!-- 或者 -->
<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 uniPopup from "../../../uni_modules/uni-popup/components/uni-popup/uni-popup.vue";
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 type = ref("member");
@ -328,7 +334,7 @@ function switchPhone() {
}
//
async function register() {
async function Login() {
if (!basicVerification()) {
return;
}
@ -351,6 +357,12 @@ async function register() {
title: "登录成功",
icon: "success",
});
const userStore = useUserStore();
userStore.setUserInfo(res.data)
console.log("userInfo为",userStore.userInfo);
//
uni.switchTab({
url: "/pages/start/index/index",
@ -529,6 +541,7 @@ function handleCancel() {
agreementPopup.value.close();
}
//
function loginWithApple() {
// Apple
console.log("通过Apple登录");
@ -551,6 +564,7 @@ function loginWithApple() {
});
}
//
function loginWithGoogle() {
// Google
console.log("通过Google登录");
@ -591,17 +605,22 @@ function onPhoneInput(e) {
}
}
//
async function sendCode() {
if (!VerCodeVerfifcation()) {
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;
if (res.code === 200) {
@ -610,6 +629,22 @@ async function sendCode() {
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;

23
server/login.json

@ -1,15 +1,22 @@
{
"loginSuccessByEmail": {
"code": 200,
"message": "登录成功",
"interface": "login",
"message": "c3e9ed50ad72073b94dfb04860562b58",
"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": {

2
stores/modules/userInfo.js

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

14
utils/http.js

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

Loading…
Cancel
Save