chore: 🔨 合并微信登录
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -22,3 +22,5 @@ dist
|
|||||||
*.sw?
|
*.sw?
|
||||||
|
|
||||||
launch.json
|
launch.json
|
||||||
|
.hbuilderx
|
||||||
|
pnpm-lock.yaml
|
||||||
|
|||||||
@@ -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",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登出接口
|
* 登出接口
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -28,6 +28,12 @@
|
|||||||
<wd-button size="large" type="primary" block @click="handleLogin">提交</wd-button>
|
<wd-button size="large" type="primary" block @click="handleLogin">提交</wd-button>
|
||||||
</view>
|
</view>
|
||||||
</wd-form>
|
</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>
|
||||||
<view class="agreement-text">
|
<view class="agreement-text">
|
||||||
<view class="agreement-links">
|
<view class="agreement-links">
|
||||||
@@ -71,6 +77,7 @@ const handleLogin = () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const navigateToUserAgreement = () => {
|
const navigateToUserAgreement = () => {
|
||||||
uni.navigateTo({
|
uni.navigateTo({
|
||||||
url: "/pages/mine/user-agreement/index",
|
url: "/pages/mine/user-agreement/index",
|
||||||
@@ -81,6 +88,45 @@ const navigateToPrivacy = () => {
|
|||||||
url: "/pages/mine/privacy/index",
|
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>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
|||||||
BIN
src/static/icons/icon_wx.png
Normal file
BIN
src/static/icons/icon_wx.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.1 KiB |
@@ -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 = () => {
|
const getInfo = () => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@@ -52,6 +67,7 @@ export const useUserStore = defineStore("user", () => {
|
|||||||
return {
|
return {
|
||||||
userInfo,
|
userInfo,
|
||||||
login,
|
login,
|
||||||
|
loginByWechat,
|
||||||
logout,
|
logout,
|
||||||
getInfo,
|
getInfo,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user