From b21609d1d8278d2f6c33a3dcf038663e6573ed9c Mon Sep 17 00:00:00 2001 From: zhanchao Date: Thu, 29 Jan 2026 17:39:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=8F=B7=E6=8E=88=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 1 + .env.localhost | 2 + pages.json | 12 +++--- pages/login/login.vue | 82 +++++++++++++++++++++++++++----------- pages/webview/webview.vue | 24 ++++++++++- store/account.js | 19 ++++++++- wx-callback.html | 83 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 192 insertions(+), 31 deletions(-) create mode 100644 wx-callback.html diff --git a/.env.development b/.env.development index d8e7453..ccee692 100644 --- a/.env.development +++ b/.env.development @@ -4,3 +4,4 @@ MP_CACHE_PREFIX=development MP_WX_APP_ID=wx93af55767423938e MP_CORP_ID=wwe3fb2faa52cf9dfb MP_TIM_SDK_APP_ID=1600123876 +MP_WX_MP_APP_ID=wxce46d19ff09c1832 \ No newline at end of file diff --git a/.env.localhost b/.env.localhost index d8e7453..0115abe 100644 --- a/.env.localhost +++ b/.env.localhost @@ -4,3 +4,5 @@ MP_CACHE_PREFIX=development MP_WX_APP_ID=wx93af55767423938e MP_CORP_ID=wwe3fb2faa52cf9dfb MP_TIM_SDK_APP_ID=1600123876 +MP_WX_MP_APP_ID=wxce46d19ff09c1832 +MP_WX_MP_OAUTH_URL=www.youcan365.com \ No newline at end of file diff --git a/pages.json b/pages.json index a825f10..f4e1ac1 100644 --- a/pages.json +++ b/pages.json @@ -1,5 +1,11 @@ { "pages": [ + { + "path": "pages/login/login", + "style": { + "navigationBarTitleText": "授权登录" + } + }, { "path": "pages/login/redirect-page", "style": { @@ -206,12 +212,6 @@ "navigationBarTitleText": "上传证照" } }, - { - "path": "pages/login/login", - "style": { - "navigationBarTitleText": "授权登录" - } - }, { "path": "pages/work/team/invite/invite-patient", "style": { diff --git a/pages/login/login.vue b/pages/login/login.vue index d339408..9eb7fc3 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -1,25 +1,31 @@ @@ -28,7 +34,7 @@ import { ref } from "vue"; import { storeToRefs } from "pinia"; import { onLoad } from "@dcloudio/uni-app"; import useAccountStore from "@/store/account"; -import { get } from "@/utils/cache"; +import { get, set } from "@/utils/cache"; import { toast } from "@/utils/widget"; const team = ref(true); @@ -67,8 +73,30 @@ function toHome() { }); } +function toMpOauth() { + // 写死:公众号网页授权(snsapi_base) + // TODO: 把 YOUR_MP_APPID 替换为真实「公众号 appid」 + // 注意:redirect_uri 的域名必须在公众号后台配置为“网页授权域名”(你们是 www.youcan365.com) + const MP_APPID = __VITE_ENV__.MP_WX_MP_APP_ID; + const REDIRECT_URI = "https://www.youcan365.com/wx-callback"; + const state = `ykt_wxapp_${Date.now()}`; + const oauthUrl = + `https://open.weixin.qq.com/connect/oauth2/authorize` + + `?appid=${MP_APPID}` + + `&redirect_uri=${encodeURIComponent(REDIRECT_URI)}` + + `&response_type=code` + + `&scope=snsapi_base` + + `&state=${encodeURIComponent(state)}` + + `#wechat_redirect`; + uni.navigateTo({ + url: `/pages/webview/webview?purpose=mp_oauth&url=${encodeURIComponent(oauthUrl)}`, + }); +} + async function getPhoneNumber(e) { + console.log('e', e); const phoneCode = e && e.detail && e.detail.code; + console.log('phoneCode', phoneCode); // if (e && !phoneCode) return; const res = await login(phoneCode); @@ -92,6 +120,11 @@ async function attempToPage(url) { } onLoad((opts) => { + // 如果你从公众号H5 OAuth回跳到小程序,可以把公众号 oauth code 通过参数带过来(比如 ?mpCode=xxx) + // 这里先缓存下来,供 store/account.js 登录时透传给后端做用户映射 + if (opts && opts.mpCode) { + set("mp-oauth-code", opts.mpCode, 300); // 5分钟过期 + } if (opts.source === "teamInvite") { team.value = get("invite-team-info"); redirectUrl.value = `/pages/archive/edit-archive?teamId=${team.value.teamId}&corpId=${team.value.corpId}`; @@ -142,4 +175,9 @@ onLoad((opts) => { .login-btn:active { background: linear-gradient(270deg, #1b5cc8 2.26%, #0877f1 94.33%); } + +.mp-oauth-link { + color: #0877f1; + font-size: 26rpx; +} diff --git a/pages/webview/webview.vue b/pages/webview/webview.vue index d4d603f..bf0e5df 100644 --- a/pages/webview/webview.vue +++ b/pages/webview/webview.vue @@ -1,19 +1,41 @@ + + + + +

微信公众号授权回调页

+ +

当前页面地址:

+
+ +

状态:

+
正在处理...
+ + + + + + \ No newline at end of file