chore: 🔨 合并微信登录

This commit is contained in:
Ray.Hao
2024-12-01 19:12:17 +08:00
5 changed files with 81 additions and 0 deletions

2
.gitignore vendored
View File

@@ -22,3 +22,5 @@ dist
*.sw?
launch.json
.hbuilderx
pnpm-lock.yaml

View File

@@ -20,6 +20,23 @@ const AuthAPI = {
});
},
/**
* 微信登录接口
*
* @param code 微信登录code
* @returns 返回 token
*/
wechatLogin(code: string): Promise<LoginResult> {
return request<LoginResult>({
url: "/api/v1/auth/wechat-login",
method: "POST",
data: { code },
header: {
"Content-Type": "application/x-www-form-urlencoded",
},
});
},
/**
* 登出接口
*/

View File

@@ -28,6 +28,12 @@
<wd-button size="large" type="primary" block @click="handleLogin">提交</wd-button>
</view>
</wd-form>
<div class="flex justify-center mt-[100rpx]">
<div class="flex flex-col items-center" @click="handleWechatLogin">
<img src="/static/icons/icon_wx.png" class="w-[100rpx] h-[100rpx]" />
<span class="mt-2">微信登录</span>
</div>
</div>
</view>
<view class="agreement-text">
<view class="agreement-links">
@@ -71,6 +77,7 @@ const handleLogin = () => {
}
});
};
const navigateToUserAgreement = () => {
uni.navigateTo({
url: "/pages/mine/user-agreement/index",
@@ -81,6 +88,45 @@ const navigateToPrivacy = () => {
url: "/pages/mine/privacy/index",
});
};
// 微信登录处理
const handleWechatLogin = async () => {
try {
// 获取微信登录的临时 code
const { code } = await uni.login({
provider: "weixin",
});
// 调用后端接口进行登录认证
const result = await userStore.loginByWechat(code);
if (result) {
// 获取用户信息
await userStore.getInfo();
uni.showToast({
title: "登录成功",
icon: "success",
});
const pages = getCurrentPages();
if (pages.length > 1) {
uni.navigateBack();
} else {
uni.reLaunch({
url: "/pages/index/index",
});
}
}
} catch (error: any) {
console.error("微信登录失败", error);
uni.showToast({
title: error.message || "微信登录失败",
icon: "none",
});
}
};
</script>
<style lang="scss" scoped>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

View File

@@ -21,6 +21,21 @@ export const useUserStore = defineStore("user", () => {
});
};
// 微信登录
const loginByWechat = (code: string) => {
return new Promise((resolve, reject) => {
AuthAPI.wechatLogin(code)
.then((data) => {
setToken(data.accessToken);
resolve(data);
})
.catch((error) => {
console.error("微信登录失败", error);
reject(error);
});
});
};
// 获取用户信息
const getInfo = () => {
return new Promise((resolve, reject) => {
@@ -52,6 +67,7 @@ export const useUserStore = defineStore("user", () => {
return {
userInfo,
login,
loginByWechat,
logout,
getInfo,
};