diff --git a/.gitignore b/.gitignore index 1dc2512..a7907ba 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ dist *.sw? launch.json +.hbuilderx +pnpm-lock.yaml diff --git a/src/api/auth/index.ts b/src/api/auth/index.ts index c36e587..7f8ae3a 100644 --- a/src/api/auth/index.ts +++ b/src/api/auth/index.ts @@ -20,6 +20,23 @@ const AuthAPI = { }); }, + /** + * 微信登录接口 + * + * @param code 微信登录code + * @returns 返回 token + */ + wechatLogin(code: string): Promise { + return request({ + url: "/api/v1/auth/wechat-login", + method: "POST", + data: { code }, + header: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + }, + /** * 登出接口 */ diff --git a/src/pages/login/index.vue b/src/pages/login/index.vue index cb45597..f9325a8 100644 --- a/src/pages/login/index.vue +++ b/src/pages/login/index.vue @@ -28,6 +28,12 @@ 提交 +
+
+ + 微信登录 +
+
@@ -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", + }); + } +};