feat: 新增微信小程序登录和手机号绑定功能

This commit is contained in:
Ray.Hao
2026-03-05 07:45:07 +08:00
parent a8b7177a0a
commit 67ada6263b
4 changed files with 801 additions and 393 deletions

View File

@@ -13,7 +13,8 @@ export interface LoginResult {
tokenType: string;
expiresIn: number;
isNewUser?: boolean;
isProfileComplete?: boolean;
needBindMobile?: boolean;
openid?: string;
}
export interface SmsLoginData {
@@ -21,20 +22,42 @@ export interface SmsLoginData {
code: string;
}
export interface WechatMiniappLoginResult {
accessToken?: string;
refreshToken?: string;
tokenType?: string;
expiresIn?: number;
isNewUser: boolean;
needBindMobile: boolean;
openid?: string;
}
export interface WechatMiniappPhoneLoginData {
loginCode: string;
phoneCode: string;
}
export interface WechatMiniappBindMobileData {
openid: string;
mobile: string;
code: string;
}
const AuthAPI = {
/**
* 账号密码登录
* @param data 登录表单数据
* @returns 登录结果
*/
login(data: LoginData): Promise<LoginResult> {
return publicRequest<LoginResult>({
url: `${AUTH_BASE_URL}/login`,
method: "POST",
data: data, // 发送JSON格式的请求体
data: data,
});
},
/**
* 发送短信验证码
*/
sendSmsLoginCode(mobile: string): Promise<void> {
const mobileSafe = encodeURIComponent(mobile);
return publicRequest<void>({
@@ -43,6 +66,9 @@ const AuthAPI = {
});
},
/**
* 短信验证码登录
*/
loginBySms(data: SmsLoginData): Promise<LoginResult> {
const mobileSafe = encodeURIComponent(data.mobile);
const codeSafe = encodeURIComponent(data.code);
@@ -52,9 +78,40 @@ const AuthAPI = {
});
},
/**
* 微信小程序登录(个人小程序)
*/
wechatMiniappLogin(code: string): Promise<WechatMiniappLoginResult> {
return publicRequest<WechatMiniappLoginResult>({
url: `${AUTH_BASE_URL}/wechat-miniapp/login?code=${encodeURIComponent(code)}`,
method: "POST",
});
},
/**
* 微信小程序一键登录(企业小程序)
*/
wechatMiniappPhoneLogin(data: WechatMiniappPhoneLoginData): Promise<LoginResult> {
return publicRequest<LoginResult>({
url: `${AUTH_BASE_URL}/wechat-miniapp/phone-login`,
method: "POST",
data,
});
},
/**
* 微信小程序绑定手机号
*/
wechatMiniappBindMobile(data: WechatMiniappBindMobileData): Promise<LoginResult> {
return publicRequest<LoginResult>({
url: `${AUTH_BASE_URL}/wechat-miniapp/bind-mobile`,
method: "POST",
data,
});
},
/**
* 检查会话有效性
* @returns 会话是否有效
*/
checkSession(): Promise<{ valid: boolean }> {
return request<{ valid: boolean }>({
@@ -65,7 +122,6 @@ const AuthAPI = {
/**
* 登出
* @returns 登出结果
*/
logout(): Promise<any> {
return request<any>({
@@ -76,8 +132,6 @@ const AuthAPI = {
/**
* 刷新令牌
* @param refreshToken 刷新令牌
* @returns 新的访问令牌
*/
refreshToken(refreshToken: string): Promise<{ accessToken: string; expiresIn: number }> {
return publicRequest<{ accessToken: string; expiresIn: number }>({

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,10 @@
import { defineStore } from "pinia";
import AuthAPI, { type LoginData, type SmsLoginData } from "@/api/auth";
import AuthAPI, {
type LoginData,
type SmsLoginData,
type WechatMiniappPhoneLoginData,
type WechatMiniappBindMobileData,
} from "@/api/auth";
import UserAPI, { type UserInfo } from "@/api/user";
import { setAccessToken, clearTokens } from "@/utils/auth";
import { getUserInfo, setUserInfo } from "@/utils/storage";
@@ -24,6 +29,7 @@ export const useUserStore = defineStore("user", () => {
});
};
// 短信验证码登录
const loginBySms = (data: SmsLoginData) => {
return new Promise((resolve, reject) => {
AuthAPI.loginBySms(data)
@@ -38,6 +44,53 @@ export const useUserStore = defineStore("user", () => {
});
};
// 微信小程序登录(个人小程序)
const loginByWechatMiniapp = (code: string) => {
return new Promise((resolve, reject) => {
AuthAPI.wechatMiniappLogin(code)
.then((data) => {
if (data.accessToken) {
setAccessToken(data.accessToken);
}
resolve(data);
})
.catch((error) => {
console.error("微信小程序登录失败", error);
reject(error);
});
});
};
// 微信小程序一键登录(企业小程序)
const loginByWechatMiniappPhone = (data: WechatMiniappPhoneLoginData) => {
return new Promise((resolve, reject) => {
AuthAPI.wechatMiniappPhoneLogin(data)
.then((data) => {
setAccessToken(data.accessToken);
resolve(data);
})
.catch((error) => {
console.error("微信小程序一键登录失败", error);
reject(error);
});
});
};
// 微信小程序绑定手机号
const bindMobileForWechatMiniapp = (data: WechatMiniappBindMobileData) => {
return new Promise((resolve, reject) => {
AuthAPI.wechatMiniappBindMobile(data)
.then((data) => {
setAccessToken(data.accessToken);
resolve(data);
})
.catch((error) => {
console.error("绑定手机号失败", error);
reject(error);
});
});
};
// 检查会话状态
const checkSession = (): Promise<boolean> => {
return new Promise((resolve) => {
@@ -70,25 +123,20 @@ export const useUserStore = defineStore("user", () => {
// 登出
const logout = async () => {
try {
await AuthAPI.logout(); // 调用后台注销接口
await AuthAPI.logout();
} catch (error) {
console.error("登出失败", error);
} finally {
clearTokens(); // 清除本地的 token
Storage.remove(USER_INFO_KEY); // 清除用户信息缓存
userInfo.value = undefined; // 清空用户信息
// 跳转到登录页面
uni.reLaunch({
url: "/pages/login/index",
});
clearTokens();
Storage.remove(USER_INFO_KEY);
userInfo.value = undefined;
uni.reLaunch({ url: "/pages/login/index" });
}
};
// 判断用户信息是否完整
const isUserInfoComplete = (): boolean => {
if (!userInfo.value) return false;
return !!(userInfo.value.nickname && userInfo.value.avatar);
};
@@ -96,6 +144,9 @@ export const useUserStore = defineStore("user", () => {
userInfo,
login,
loginBySms,
loginByWechatMiniapp,
loginByWechatMiniappPhone,
bindMobileForWechatMiniapp,
logout,
getInfo,
checkSession,

View File

@@ -24,7 +24,6 @@ export default {
"flex-column": "flex flex-col",
"flex-row": "flex flex-row",
// 垂直布局并居中对齐
"flex-col-center": "flex flex-col items-center",
},
],