diff --git a/src/api/auth.ts b/src/api/auth.ts index cfaeff6..144e40c 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -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 { return publicRequest({ url: `${AUTH_BASE_URL}/login`, method: "POST", - data: data, // 发送JSON格式的请求体 + data: data, }); }, + /** + * 发送短信验证码 + */ sendSmsLoginCode(mobile: string): Promise { const mobileSafe = encodeURIComponent(mobile); return publicRequest({ @@ -43,6 +66,9 @@ const AuthAPI = { }); }, + /** + * 短信验证码登录 + */ loginBySms(data: SmsLoginData): Promise { const mobileSafe = encodeURIComponent(data.mobile); const codeSafe = encodeURIComponent(data.code); @@ -52,9 +78,40 @@ const AuthAPI = { }); }, + /** + * 微信小程序登录(个人小程序) + */ + wechatMiniappLogin(code: string): Promise { + return publicRequest({ + url: `${AUTH_BASE_URL}/wechat-miniapp/login?code=${encodeURIComponent(code)}`, + method: "POST", + }); + }, + + /** + * 微信小程序一键登录(企业小程序) + */ + wechatMiniappPhoneLogin(data: WechatMiniappPhoneLoginData): Promise { + return publicRequest({ + url: `${AUTH_BASE_URL}/wechat-miniapp/phone-login`, + method: "POST", + data, + }); + }, + + /** + * 微信小程序绑定手机号 + */ + wechatMiniappBindMobile(data: WechatMiniappBindMobileData): Promise { + return publicRequest({ + 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 { return request({ @@ -76,8 +132,6 @@ const AuthAPI = { /** * 刷新令牌 - * @param refreshToken 刷新令牌 - * @returns 新的访问令牌 */ refreshToken(refreshToken: string): Promise<{ accessToken: string; expiresIn: number }> { return publicRequest<{ accessToken: string; expiresIn: number }>({ diff --git a/src/pages/login/index.vue b/src/pages/login/index.vue index ccf1015..683062d 100644 --- a/src/pages/login/index.vue +++ b/src/pages/login/index.vue @@ -1,105 +1,195 @@ @@ -113,24 +203,21 @@ diff --git a/src/store/modules/user-store.ts b/src/store/modules/user-store.ts index cc4bf34..ad5701c 100644 --- a/src/store/modules/user-store.ts +++ b/src/store/modules/user-store.ts @@ -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 => { 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, diff --git a/unocss.config.ts b/unocss.config.ts index cefd8ac..ed59fc1 100644 --- a/unocss.config.ts +++ b/unocss.config.ts @@ -24,7 +24,6 @@ export default { "flex-column": "flex flex-col", "flex-row": "flex flex-row", - // 垂直布局并居中对齐 "flex-col-center": "flex flex-col items-center", }, ],