feat(login): 打开小程序直接进主页(游客可逛 + 静默微信登录)

- 首页 onShow 不再强制跳转登录页;改为先尝试静默微信登录
  (uni.login 取 code → /auth/wxlogin 建号发 token),成功即无感登录可直接下单
- 静默登录失败(非微信环境/网络异常)保持游客态,正常浏览类目与服务
- 需要身份的动作(下单/我的/订单)才引导到登录页
- 登录页补充返回按钮(从「我的」进入时可见,作为启动页时不显示)
这个提交包含在:
linda
2026-10-28 16:13:55 +08:00
父节点 1d92202b1a
当前提交 6ff685afc9
共有 2 个文件被更改,包括 39 次插入 和 2 次删除

查看文件

@@ -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 || []

查看文件

@@ -3,6 +3,7 @@
<view class="top">
<view class="deco d1"></view>
<view class="deco d2"></view>
<view class="back" v-if="showBack" @tap="goBack"><text class="back-t">‹</text></view>
<text class="brand">优匠优服</text>
<text class="slogan">专业师傅 · 上门到家</text>
</view>
@@ -32,15 +33,24 @@
<script setup>
import { ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import api from '@/utils/api.js';
import { setToken, setUser } from '@/utils/request.js';
import { setRole } from '@/utils/role.js';
const role = ref('user');
const loading = ref(false);
const showBack = ref(false);
function toast(t) { uni.showToast({ title: t, icon: 'none' }); }
/** 从「我的」进入时才显示返回键(作为启动页时不显示) */
onLoad(() => {
showBack.value = getCurrentPages().length > 1;
});
function goBack() { uni.navigateBack(); }
/** 微信授权登录:uni.login 取 code → 后端换 openid → 下发 token */
function wxLogin() {
if (loading.value) return;
@@ -95,6 +105,8 @@
.top { position: relative; height: 420rpx; padding: 120rpx 0 0 48rpx; overflow: hidden;
background: linear-gradient(160deg, #FFB85C 0%, #F98D25 46%, #ED6F0E 100%); border-radius: 0 0 48rpx 48rpx; }
.deco { position: absolute; border-radius: 50%; }
.back { position: absolute; left: 26rpx; top: 92rpx; width: 64rpx; height: 64rpx; border-radius: 50%; background: rgba(255,255,255,.22); display: flex; align-items: center; justify-content: center; }
.back-t { font-size: 44rpx; color: #fff; line-height: 44rpx; margin-top: -6rpx; }
.d1 { width: 360rpx; height: 360rpx; right: -120rpx; top: -160rpx; background: rgba(255,255,255,.14); }
.d2 { width: 220rpx; height: 220rpx; left: -90rpx; bottom: -80rpx; background: rgba(255,255,255,.10); }
.brand { position: relative; display: block; font-size: 52rpx; font-weight: 700; color: #fff; letter-spacing: 6rpx; text-shadow: 0 3rpx 12rpx rgba(180,80,10,.3); }