diff --git a/src/api/auth.ts b/src/api/auth.ts index c4c769c..2ba5f2e 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -7,13 +7,6 @@ export interface LoginData { password: string; } -export interface WxLoginData { - code: string; - encryptedData?: string; - iv?: string; - phoneCode?: string; -} - export interface LoginResult { accessToken: string; refreshToken?: string; @@ -23,6 +16,11 @@ export interface LoginResult { isProfileComplete?: boolean; } +export interface SmsLoginData { + mobile: string; + code: string; +} + const AuthAPI = { /** * 账号密码登录 @@ -37,42 +35,20 @@ const AuthAPI = { }); }, - /** - * 微信Web授权登录 (使用code进行授权登录) - * @param code 微信授权码 - * @returns 登录结果 - */ - loginByWechat(code: string): Promise { - return publicRequest({ - url: `${AUTH_BASE_URL}/login/wechat`, + sendSmsLoginCode(mobile: string): Promise { + const mobileSafe = encodeURIComponent(mobile); + return publicRequest({ + url: `${AUTH_BASE_URL}/sms/code?mobile=${mobileSafe}`, method: "POST", - data: { code }, }); }, - /** - * 微信小程序授权登录 (仅使用code获取OpenID) - * @param code 微信登录凭证 - * @returns 登录结果 - */ - loginByWxMiniAppCode(code: string): Promise { + loginBySms(data: SmsLoginData): Promise { + const mobileSafe = encodeURIComponent(data.mobile); + const codeSafe = encodeURIComponent(data.code); return publicRequest({ - url: `${AUTH_BASE_URL}/wx/miniapp/code-login`, + url: `${AUTH_BASE_URL}/login/sms?mobile=${mobileSafe}&code=${codeSafe}`, method: "POST", - data: { code }, - }); - }, - - /** - * 微信小程序手机号授权登录 - * @param data 包含code、encryptedData、iv等手机号相关数据 - * @returns 登录结果 - */ - loginByWxMiniAppPhone(data: WxLoginData): Promise { - return publicRequest({ - url: `${AUTH_BASE_URL}/wx/miniapp/phone-login`, - method: "POST", - data, }); }, diff --git a/src/api/user.ts b/src/api/user.ts index 63ce38a..e348c04 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -25,10 +25,7 @@ const UserAPI = { url: `${USER_BASE_URL}`, method: "GET", data: queryParams, - }).then((res: { data: UserPageVO[]; page?: { total?: number } }) => ({ - list: res.data, - total: res.page?.total ?? 0, - })); + }); }, /** * 添加用户 @@ -138,15 +135,6 @@ const UserAPI = { method: "DELETE", }); }, - - /** 获取微信手机号 */ - getPhoneNumber(data: WechatPhoneData): Promise { - return request({ - url: `${USER_BASE_URL}/wechat-phone`, - method: "POST", - data: data, - }); - }, }; export default UserAPI; @@ -326,23 +314,3 @@ export interface UserForm { /** 用户名 */ username?: string; } - -/** 微信手机号授权数据 */ -export interface WechatPhoneData { - /** 微信授权码 */ - code: string; - /** 加密数据 */ - encryptedData?: string; - /** 初始向量 */ - iv?: string; -} - -/** 手机号获取结果 */ -export interface PhoneNumberResult { - /** 手机号 */ - phoneNumber: string; - /** 纯手机号(去除+86) */ - purePhoneNumber?: string; - /** 国家代码 */ - countryCode?: string; -} diff --git a/src/composables/useWechat.ts b/src/composables/useWechat.ts deleted file mode 100644 index 871d938..0000000 --- a/src/composables/useWechat.ts +++ /dev/null @@ -1,159 +0,0 @@ -/** - * 微信授权服务 - * 处理微信登录授权、获取用户信息等功能 - */ - -import { ref } from "vue"; -import { getAccessToken } from "@/utils/auth"; - -export function useWechat() { - // 定义微信授权状态 - const authState = ref({ - isLogining: false, - authDenied: false, - }); - - /** - * 获取微信登录凭证code - * @returns Promise 返回登录凭证code - */ - const getLoginCode = (): Promise => { - return new Promise((resolve, reject) => { - // #ifdef MP-WEIXIN - uni.login({ - provider: "weixin", - success: (res) => { - if (res.code) { - resolve(res.code); - } else { - reject(new Error("获取微信登录凭证失败")); - } - }, - fail: (err) => { - reject(err); - }, - }); - // #endif - - // #ifndef MP-WEIXIN - reject(new Error("当前环境不支持微信登录")); - // #endif - }); - }; - - /** - * 获取微信用户手机号 - * @param e 微信授权返回的事件对象 - * @returns Promise 返回包含手机号加密数据的对象 - */ - const getPhoneNumber = ( - e: any - ): Promise<{ code: string; encryptedData?: string; iv?: string }> => { - return new Promise((resolve, reject) => { - authState.value.isLogining = true; - - // 判断授权是否成功 - if (e.detail.errMsg !== "getPhoneNumber:ok") { - authState.value.isLogining = false; - authState.value.authDenied = true; - reject(new Error("用户拒绝授权")); - return; - } - - // 获取登录凭证code - getLoginCode() - .then((code) => { - // 在微信小程序环境下,可以获取encryptedData和iv - // #ifdef MP-WEIXIN - resolve({ - code, - encryptedData: e.detail.encryptedData, - iv: e.detail.iv, - }); - // #endif - - // 其他环境或新版本接口 - // #ifndef MP-WEIXIN - resolve({ - code, - // 新版本接口在e.detail.code中包含手机号获取凭证 - ...(e.detail.code ? { phoneCode: e.detail.code } : {}), - }); - // #endif - }) - .catch((err) => { - reject(err); - }) - .finally(() => { - authState.value.isLogining = false; - }); - }); - }; - - /** - * 检查会话有效性 - * @returns Promise 返回会话是否有效 - */ - const checkSession = (): Promise => { - return new Promise((resolve) => { - const token = getAccessToken(); - - if (!token) { - resolve(false); - return; - } - - // 调用后端接口验证token有效性 - uni.request({ - url: "/api/v1/auth/check-session", - method: "GET", - header: { - Authorization: `Bearer ${token}`, - }, - success: (res: any) => { - if (res.statusCode === 200 && res.data.valid) { - resolve(true); - } else { - resolve(false); - } - }, - fail: () => { - resolve(false); - }, - }); - }); - }; - - /** - * 获取用户头像昵称 - * 注意:此接口已于2021年弃用,仅作为兼容保留 - * 推荐使用button组件的open-type="chooseAvatar"让用户选择头像 - */ - const getUserProfile = (): Promise => { - return new Promise((resolve, reject) => { - // #ifdef MP-WEIXIN - uni.getUserProfile({ - desc: "用于完善用户资料", - success: (res) => { - resolve(res.userInfo); - }, - fail: (err) => { - reject(err); - }, - }); - // #endif - - // #ifndef MP-WEIXIN - reject(new Error("当前环境不支持获取用户信息")); - // #endif - }); - }; - - return { - authState, - getLoginCode, - getPhoneNumber, - checkSession, - getUserProfile, - }; -} diff --git a/src/pages/login/index.vue b/src/pages/login/index.vue index e2263ab..2546873 100644 --- a/src/pages/login/index.vue +++ b/src/pages/login/index.vue @@ -6,7 +6,7 @@