From 6ff685afc918791b8a2a0e376174ee7d8af12ab2 Mon Sep 17 00:00:00 2001 From: linda Date: Wed, 28 Oct 2026 16:13:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(login):=20=E6=89=93=E5=BC=80=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=9B=B4=E6=8E=A5=E8=BF=9B=E4=B8=BB=E9=A1=B5?= =?UTF-8?q?=EF=BC=88=E6=B8=B8=E5=AE=A2=E5=8F=AF=E9=80=9B=20+=20=E9=9D=99?= =?UTF-8?q?=E9=BB=98=E5=BE=AE=E4=BF=A1=E7=99=BB=E5=BD=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 首页 onShow 不再强制跳转登录页;改为先尝试静默微信登录 (uni.login 取 code → /auth/wxlogin 建号发 token),成功即无感登录可直接下单 - 静默登录失败(非微信环境/网络异常)保持游客态,正常浏览类目与服务 - 需要身份的动作(下单/我的/订单)才引导到登录页 - 登录页补充返回按钮(从「我的」进入时可见,作为启动页时不显示) --- YJYF/YJYF.vue | 29 +++++++++++++++++++++++++++-- pages/login/login.vue | 12 ++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/YJYF/YJYF.vue b/YJYF/YJYF.vue index d10a903..823dd3b 100644 --- a/YJYF/YJYF.vue +++ b/YJYF/YJYF.vue @@ -221,7 +221,7 @@ import { ref } from 'vue' import { onShow } from '@dcloudio/uni-app' import api from '@/utils/api.js' - import { isLogin, toLogin } from '@/utils/request.js' + import { isLogin, toLogin, setToken, setUser } from '@/utils/request.js' /* ---------- 系统适配:状态栏 / 导航栏高度(刘海屏 + 胶囊按钮) ---------- */ const statusBarHeight = ref(20) @@ -255,8 +255,33 @@ const cats = ref([]) const badge = ref({ coupon: 0, unread: 0 }) + let silentTried = false + + /** + * 静默微信登录:进主页时自动换取 openid 并建号, + * 成功 → 无感登录(可直接下单);失败 → 保持游客态,不打断浏览 + */ + function trySilentLogin() { + if (isLogin() || silentTried) return Promise.resolve() + silentTried = true + return new Promise((resolve) => { + uni.login({ + provider: 'weixin', + success: (r) => { + if (!r.code) { resolve(); return } + api.wxLogin({ code: r.code }).then((data) => { + setToken(data.token) + setUser(data.userinfo) + resolve() + }).catch(() => resolve()) + }, + fail: () => resolve() + }) + }) + } + async function loadHome() { - if (!isLogin()) { toLogin(); return } + await trySilentLogin() try { const res = await api.home() cats.value = res.cats || [] diff --git a/pages/login/login.vue b/pages/login/login.vue index a3833e9..6d29112 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -3,6 +3,7 @@ + ‹ 优匠优服 专业师傅 · 上门到家 @@ -32,15 +33,24 @@