feat: ✨ 小程序端实现微信登录
This commit is contained in:
@@ -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>
|
||||
</template>
|
||||
@@ -71,6 +77,35 @@ const handleLogin = () => {
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 微信登录处理
|
||||
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",
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("微信登录失败", error);
|
||||
uni.showToast({
|
||||
title: error.message || "微信登录失败",
|
||||
icon: "none",
|
||||
});
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
|
||||
Reference in New Issue
Block a user