From c5f6e635adacb8cdba0db5212d950ec8d6064893 Mon Sep 17 00:00:00 2001 From: ni ziyi <310925901@qq.com> Date: Tue, 13 Jan 2026 10:47:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=BE=AE=E4=BF=A1=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=92=8C=E6=8E=88=E6=9D=83=E7=99=BB=E5=BD=95=E5=8A=9F=E8=83=BD?= =?UTF-8?q?)=EF=BC=9A1.=E6=96=B0=E5=A2=9E=E7=99=BB=E5=BD=95=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C2.=E6=96=B0=E5=A2=9E=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/auth.ts | 19 +++++ src/pages/auth/login.vue | 137 ++++++++++++++++++++++++++++++--- src/pages/auth/role-switch.vue | 23 ++++-- src/store/modules/role.ts | 59 ++++++++++---- src/store/modules/user.ts | 115 +++++++++++++++------------ 5 files changed, 275 insertions(+), 78 deletions(-) diff --git a/src/api/auth.ts b/src/api/auth.ts index 8232998..e1f1718 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -41,6 +41,17 @@ export interface LoginResult { isNewUser: boolean } +export interface SwitchRoleParams { + userType: string + tenantId?: number +} + +export interface SwitchRoleResult { + token: string + userType: string + tenantId?: number +} + /** * 微信静默登录 * @param params 登录参数 @@ -72,6 +83,14 @@ export function updateUserInfo(data: Partial) { return put('/api/auth/user-info', data) } +/** + * 切换角色 + * @param params 角色切换参数 + */ +export function switchRole(params: SwitchRoleParams) { + return post('/api/auth/switch-role', params) +} + /** * 退出登录 */ diff --git a/src/pages/auth/login.vue b/src/pages/auth/login.vue index 4b0b6f8..3b282d4 100644 --- a/src/pages/auth/login.vue +++ b/src/pages/auth/login.vue @@ -13,9 +13,19 @@ - - + + + @@ -70,6 +80,8 @@ const roleStore = useRoleStore() const agreed = ref(false) const redirectUrl = ref('') +const needBindPhone = ref(false) +const tempOpenid = ref('') onLoad((options: any) => { redirectUrl.value = options?.redirect || roleStore.homePagePath @@ -80,8 +92,8 @@ const onAgreeChange = (e: any) => { agreed.value = e.detail.value.length > 0 } -// 手机号授权登录(模拟) -const handlePhoneLogin = async () => { +// 微信一键登录 +const handleWxLogin = async () => { if (!agreed.value) { uni.showToast({ title: '请先阅读并同意协议', @@ -93,16 +105,103 @@ const handlePhoneLogin = async () => { try { uni.showLoading({ title: '登录中...' }) - // 模拟微信登录 - const code = 'mock_code_' + Date.now() + // 调用微信登录获取code + const loginRes = await new Promise((resolve, reject) => { + uni.login({ + provider: 'weixin', + success: resolve, + fail: reject + }) + }) - // 调用登录接口 - await userStore.wxLogin(code) + if (!loginRes.code) { + throw new Error('获取登录凭证失败') + } + + // 获取用户信息(头像、昵称) + let nickname = '' + let avatar = '' + try { + const userProfile = await new Promise((resolve, reject) => { + // #ifdef MP-WEIXIN + uni.getUserProfile({ + desc: '用于完善用户资料', + success: resolve, + fail: reject + }) + // #endif + // #ifndef MP-WEIXIN + resolve({ userInfo: {} }) + // #endif + }) + nickname = userProfile.userInfo?.nickName || '' + avatar = userProfile.userInfo?.avatarUrl || '' + } catch (e) { + // 用户拒绝授权,继续登录 + } + + // 调用后端登录接口 + const result = await userStore.wxLogin(loginRes.code, nickname, avatar) + + uni.hideLoading() + + if (result.needBindPhone) { + // 需要绑定手机号 + needBindPhone.value = true + tempOpenid.value = result.openid + uni.showToast({ + title: '请授权手机号完成注册', + icon: 'none' + }) + } else { + // 登录成功 + uni.showToast({ + title: '登录成功', + icon: 'success' + }) + + // 跳转到角色选择页 + setTimeout(() => { + uni.redirectTo({ + url: '/pages/auth/role-switch?redirect=' + encodeURIComponent(redirectUrl.value) + }) + }, 1500) + } + + } catch (error: any) { + uni.hideLoading() + console.error('登录失败:', error) + uni.showToast({ + title: error.message || '登录失败', + icon: 'none' + }) + } +} + +// 获取手机号授权 +const handleGetPhoneNumber = async (e: any) => { + if (e.detail.errMsg !== 'getPhoneNumber:ok') { + uni.showToast({ + title: '需要授权手机号才能完成注册', + icon: 'none' + }) + return + } + + try { + uni.showLoading({ title: '注册中...' }) + + // 调用手机号登录接口 + const result = await userStore.phoneLogin({ + openid: tempOpenid.value, + encryptedData: e.detail.encryptedData, + iv: e.detail.iv + }) uni.hideLoading() uni.showToast({ - title: '登录成功', + title: '注册成功', icon: 'success' }) @@ -115,8 +214,9 @@ const handlePhoneLogin = async () => { } catch (error: any) { uni.hideLoading() + console.error('注册失败:', error) uni.showToast({ - title: error.message || '登录失败', + title: error.message || '注册失败', icon: 'none' }) } @@ -195,6 +295,21 @@ const viewAgreement = (type: string) => { display: flex; align-items: center; justify-content: center; + margin-bottom: 24rpx; + box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.15); + } + + .phone-btn { + width: 100%; + height: 96rpx; + background: #07c160; + color: #fff; + border-radius: 48rpx; + font-size: 32rpx; + font-weight: bold; + display: flex; + align-items: center; + justify-content: center; margin-bottom: 40rpx; box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.15); } diff --git a/src/pages/auth/role-switch.vue b/src/pages/auth/role-switch.vue index ed473a7..79dba3a 100644 --- a/src/pages/auth/role-switch.vue +++ b/src/pages/auth/role-switch.vue @@ -34,8 +34,8 @@ -