文件
yjyf/pages/login/login.vue
linda 6ff685afc9 feat(login): 打开小程序直接进主页(游客可逛 + 静默微信登录)
- 首页 onShow 不再强制跳转登录页;改为先尝试静默微信登录
  (uni.login 取 code → /auth/wxlogin 建号发 token),成功即无感登录可直接下单
- 静默登录失败(非微信环境/网络异常)保持游客态,正常浏览类目与服务
- 需要身份的动作(下单/我的/订单)才引导到登录页
- 登录页补充返回按钮(从「我的」进入时可见,作为启动页时不显示)
2026-10-28 16:13:55 +08:00

129 行
5.5 KiB
Vue

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<template>
<view class="page">
<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>
<view class="card">
<view class="role">
<view class="role-item" :class="{ on: role === 'user' }" @tap="role = 'user'">
<text class="role-name">我是业主</text>
<text class="role-desc">下单找师傅</text>
</view>
<view class="role-item" :class="{ on: role === 'master' }" @tap="role = 'master'">
<text class="role-name">我是师傅</text>
<text class="role-desc">接单赚收入</text>
</view>
</view>
<view class="wx-btn" :class="{ disabled: loading }" @tap="wxLogin">
<view class="wx-icon"><text class="wx-icon-t">微</text></view>
<text class="wx-txt">{{ loading ? '微信授权中...' : '微信一键登录' }}</text>
</view>
<view class="tip">授权仅用于账号识别,不会获取你的其他信息</view>
<view class="agree">登录即代表同意《用户服务协议》与《隐私政策》</view>
</view>
</view>
</template>
<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;
loading.value = true;
uni.login({
provider: 'weixin',
success: async (res) => {
if (!res.code) {
loading.value = false;
toast('微信授权失败,请重试');
return;
}
try {
const data = await api.wxLogin({ code: res.code });
setToken(data.token);
setUser(data.userinfo);
setRole(role.value);
toast(data.is_new ? '登录成功,欢迎加入' : '登录成功');
setTimeout(() => routeAfterLogin(), 600);
} catch (e) {
toast(e.message || '登录失败');
} finally {
loading.value = false;
}
},
fail: () => {
loading.value = false;
toast('未检测到微信环境,请在小程序内打开');
}
});
}
function routeAfterLogin() {
if (role.value === 'user') {
uni.reLaunch({ url: '/YJYF/YJYF' });
return;
}
api.masterInfo().then((res) => {
if (!res.applied || res.master.verify_status !== 'pass') {
uni.redirectTo({ url: '/pages/master/apply' });
} else {
uni.redirectTo({ url: '/pages/master/workbench' });
}
}).catch(() => {
uni.redirectTo({ url: '/pages/master/apply' });
});
}
</script>
<style scoped>
.page { min-height: 100vh; background: #F5F6F8; }
.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); }
.slogan { position: relative; display: block; margin-top: 14rpx; font-size: 26rpx; color: rgba(255,255,255,.88); letter-spacing: 2rpx; }
.card { position: relative; margin: -140rpx 40rpx 0; padding: 40rpx 34rpx 34rpx; background: #fff; border-radius: 24rpx; box-shadow: 0 16rpx 40rpx rgba(226,120,26,.16); }
.role { display: flex; margin-bottom: 36rpx; }
.role-item { flex: 1; padding: 22rpx 0; border-radius: 16rpx; background: #F7F7F7; display: flex; flex-direction: column; align-items: center; border: 2rpx solid transparent; }
.role-item.on { background: linear-gradient(135deg, #FFF8EE 0%, #FFE9D2 100%); border-color: #F9A65B; }
.role-name { font-size: 30rpx; font-weight: 600; color: #333; }
.role-item.on .role-name { color: #C4580A; }
.role-desc { margin-top: 6rpx; font-size: 22rpx; color: #9A9A9A; }
.wx-btn { height: 96rpx; display: flex; align-items: center; justify-content: center; border-radius: 48rpx; background: linear-gradient(135deg, #4BC86A 0%, #2FA84F 100%); box-shadow: 0 10rpx 24rpx rgba(47,168,79,.28); }
.wx-btn.disabled { opacity: .7; }
.wx-icon { width: 44rpx; height: 44rpx; border-radius: 50%; background: #fff; display: flex; align-items: center; justify-content: center; margin-right: 14rpx; }
.wx-icon-t { font-size: 24rpx; font-weight: 700; color: #2FA84F; }
.wx-txt { font-size: 32rpx; font-weight: 600; color: #fff; letter-spacing: 1rpx; }
.tip { display: block; margin-top: 24rpx; text-align: center; font-size: 20rpx; color: #B5B5B5; }
.agree { display: block; margin-top: 12rpx; text-align: center; font-size: 20rpx; color: #C4C4C4; }
</style>