Commit f2190fe5 authored by youjie's avatar youjie

微信登录

parent 521a134e
......@@ -37,15 +37,15 @@
</a-avatar>
<template #content>
<a-doption @click="goPage('/personalCenter/myOrder')">
<!-- <template #icon>
<template #icon>
<icon-user />
</template> -->
</template>
{{ t('personal.profile') }}
</a-doption>
<a-doption @click="logOut">
<!-- <template #icon>
<icon-logout />
</template> -->
<template #icon>
<icon-poweroff />
</template>
{{ t('menu.logout') }}
</a-doption>
</template>
......
......@@ -368,6 +368,53 @@ class UserService {
return response as unknown as HttpResponse
}
/**
* 微信登录
* @param tenantId 租户ID
* @returns
*/
static async wechatLoginAsync(tenantId: string,code: string,distributorId: number,parentId?: any,redirectUri?: string): Promise<HttpResponse> {
const data = {
tenantId,
code,
distributorId,
parentId,
redirectUri,
}
// OtaRequest 的响应拦截器会返回 response.data
const response = await OtaRequest.post(
'/member-auth/we-chat-login-by-code',
data,
{
headers: {
'__tenant': tenantId
}
}
)
return response as unknown as HttpResponse
}
/**
* 获取微信AppID
* @param tenantId 租户ID
* @returns
*/
static async wechatAppIdRedirectUriAsync(tenantId: string): Promise<HttpResponse> {
const data = {
tenantId
}
// OtaRequest 的响应拦截器会返回 response.data
const response = await OtaRequest.post(
'/member-auth/we-chat-login-by-code',
data,
{
headers: {
'__tenant': tenantId
}
}
)
return response as unknown as HttpResponse
}
/**
* 刷新访问令牌
* @param refreshToken 刷新令牌
......
......@@ -127,9 +127,55 @@ export const useUserStore = defineStore('user', {
}
}
},
setUserLoginOut() {
this.token = ''
this.userInfo = {}
/**
* 微信登录
* @param tenantId 租户ID
*/
async setUserWechatLoginAsync(tenantId: string, code: string,distributorId: number,parentId?: any,redirectUri?: string): Promise<UserLoginResult> {
try {
const response = await UserService.wechatLoginAsync(tenantId, code,distributorId,parentId,redirectUri)
console.log('Google login response:', response)
this.token = response.token || ''
this.userInfo = response.userInfo || {}
this.denied = false
this.memberData = response.memberData
return {
status: 'SUCCESS',
verify: false,
data: [response]
}
} catch (error: any) {
console.error('Google login error:', error)
ResultMessage.Error(error.message)
return {
status: 'ERROR',
verify: false
}
}
},
/**
* 获取微信appid
* @param tenantId 租户ID
*/
async getUserWechatAppIdRedirectUriAsync(tenantId: string): Promise<UserLoginResult> {
try {
const response = await UserService.wechatAppIdRedirectUriAsync(tenantId)
console.log('微信appid-----', response)
return {
status: 'SUCCESS',
verify: false,
data: [response]
}
} catch (error: any) {
console.error('微信appid error:', error)
ResultMessage.Error(error.message)
return {
status: 'ERROR',
verify: false
}
}
},
/**
......
......@@ -92,7 +92,7 @@
rounded-full bg-[#FFF]
flex items-center justify-center cursor-pointer loginForm-item relative"
:class="[loginMsg.reType==1?'loginForm-itemActive':'']">
<img :src="loginMethods[1].url" alt="" class="w-[14px] h-[14px]"/>
<img :src="loginMethods[0].url" alt="" class="w-[14px] h-[14px]"/>
<div
ref="googleButtonContainer"
class="g-signin2 rounded-full !rounded-[13px] overflow-hidden
......@@ -109,7 +109,8 @@
flex items-center justify-center cursor-pointer loginForm-item"
:class="[loginMsg.reType==method.key?'loginForm-itemActive':'']"
@click="toggleLoginType(method.key)">
<img :src="method.url" alt="" class="w-[14px] h-[14px]"/>
<img v-if="method.url" :src="method.url" alt="" class="w-[14px] h-[14px]"/>
<icon-wechat v-if="method.icon=='icon-wechat'" size="18" class="!text-[#03DA6B]"/>
</div>
</template>
</div>
......@@ -164,11 +165,11 @@ const loginMsg = reactive({
})
const loginMethods = ref([
{
label: 'f',
key: 7,
url: f,
},
// {
// label: 'f',
// key: 7,
// url: f,
// },
{
label: 'G',
key: 1,
......@@ -179,6 +180,12 @@ const loginMethods = ref([
key: 0,
url: tel,
},
{
label: 'wechat',
key: 2,
url: '',
icon: 'icon-wechat'
},
{
label: 'inline',
key: 3,
......@@ -202,12 +209,72 @@ const rules = computed(() => ({
],
}))
const openInfo = ref({
AppID: 'wx82752cffa22d20e1',
State: '1',
OpenRedirectUri: 'https://www.oytour.com/#/',
openId: '',
})
// 获取微信登录OpenID
const getOpenIdByCode = async (code:string) => {
console.log(code,'---------code')
// useWechatLogin(code)
}
// 获取微信登录AppID 域名 重定向页面
const getAppIdRedirectUri = async () => {
const { AppID, State, OpenRedirectUri } = openInfo.value;
let redirect_uri = OpenRedirectUri;
const url = `https://open.weixin.qq.com/connect/qrconnect?appid=${AppID}&redirect_uri=${encodeURIComponent('https://www.oytour.com/#/login')}&response_type=code&scope=snsapi_login&state=${State}&wechat_redirect=${redirect_uri}`;
window.location.href = url;
return
loading.value = true
try {
// 获取微信登录AppID 域名
const response = await userStore.getUserWechatAppIdRedirectUriAsync(loginMsg.tenantId?.toString() || '')
if (response.status == 'SUCCESS') {
openInfo.value = response.data[0]
const { AppID, State, OpenRedirectUri } = openInfo.value;
let redirect_uri = OpenRedirectUri;
const url = `https://open.weixin.qq.com/connect/qrconnect?appid=${AppID}&redirect_uri=${encodeURIComponent('http://localhost:8002/login')}&response_type=code&scope=snsapi_login&state=${State}&wechat_redirect=${redirect_uri}`;
window.location.href = url;
}
}catch (error: any) {
Message.error(error.message)
} finally {
loading.value = false
}
}
// 微信登录
const useWechatLogin = async(code:string) => {
loading.value = true
try {
// 获取授权码
const response = await userStore.setUserWechatLoginAsync(loginMsg.tenantId?.toString() || '', code, loginMsg.distributorId,loginMsg.parentId,loginMsg.redirectUri)
if (response.status == 'SUCCESS') {
Message.success(t('login.loginSuccess'))
const forward = localStorage.getItem('forward')
localStorage.removeItem('forward')
router.push({
path: forward ? forward : '/',
})
}
}catch (error: any) {
Message.error(error.message)
} finally {
loading.value = false
}
}
const handleClick = (path: string) => {
router.push(path)
}
const toggleLoginType = (type: number) => {
if(type>1) return Message.error(t('login.loginTypeNotSupport'))
if(type==3) return Message.error(t('login.loginTypeNotSupport'))
if(type==2) return getAppIdRedirectUri()
loginMsg.reType = type
}
......@@ -348,6 +415,10 @@ onMounted(async () => {
// 确保 SDK 加载完成后渲染按钮
await new Promise(resolve => setTimeout(resolve));
renderGoogleButton()
const code = router.query.code;
if (code) {
getOpenIdByCode(code)
}
} catch (error) {
console.error('SDK 初始化失败:', error);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment