- 登录:微信一键登录(code2Session)+ 账号登录双 tab;个人中心为整页登录,其它页走抽屉 - 协议:登录前必须阅读并同意,版本号随登录上报落库 - 不强制登录:启动不弹窗,点功能或遇到 401 才提示(符合小程序审核) - 页面:首页统计 / 扫码记录 / 个人中心;H5 预览端口 7304 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
467 lines
12 KiB
Vue
467 lines
12 KiB
Vue
<script setup lang="ts">
|
||
/**
|
||
* 登录表单面板:抽屉(`LoginDrawer`)与页面版登录(`pages/login`)共用
|
||
*
|
||
* - 两个 tab:**微信登录(默认,优先)** / 账号登录;
|
||
* - 协议正文单独弹窗(`AgreementDialog`),面板里只留「我已阅读并同意《xx》」一行;
|
||
* - 两种登录都必须先同意当前协议版本;
|
||
* - 每次 `active` 变成 true 都清空勾选并重拉协议(同意记录按「登录」这一动作落库);
|
||
* - 登录成功只抛 `success`,关抽屉 / 返回上一页交给使用方。
|
||
*/
|
||
import { computed, reactive, ref, watch } from 'vue';
|
||
import { ApiError } from '@/api/client';
|
||
import { fetchAgreements, login, wechatLogin } from '@/api/web';
|
||
import type { AgreementItem, WechatLoginResult } from '@/api/types';
|
||
import AgreementDialog from '@/components/AgreementDialog.vue';
|
||
import { setSession } from '@/composables/useAuth';
|
||
import { applySubjects } from '@/composables/useSubject';
|
||
import { getWechatLoginCode } from '@/utils/wechat';
|
||
|
||
const props = withDefaults(defineProps<{
|
||
/** 面板是否处于打开状态(true 时重置勾选并加载协议) */
|
||
active?: boolean;
|
||
/** 是否显示右上角关闭按钮 */
|
||
showCancel?: boolean;
|
||
}>(), {
|
||
active: true,
|
||
showCancel: false,
|
||
});
|
||
|
||
const emit = defineEmits<{
|
||
(e: 'success'): void;
|
||
(e: 'cancel'): void;
|
||
}>();
|
||
|
||
type Tab = 'wechat' | 'account';
|
||
|
||
/** 默认落在微信登录(优先) */
|
||
const tab = ref<Tab>('wechat');
|
||
const form = reactive({ account: '', password: '' });
|
||
const busy = ref(false);
|
||
const error = ref('');
|
||
const tip = ref('');
|
||
|
||
/** 协议:当前版本 + 是否已阅读同意(按 code 记录) */
|
||
const agreements = ref<AgreementItem[]>([]);
|
||
const agreementsLoading = ref(false);
|
||
const agreementsError = ref('');
|
||
const accepted = ref<Record<string, boolean>>({});
|
||
|
||
/** 正在阅读的协议(null = 弹窗关闭) */
|
||
const reading = ref<AgreementItem | null>(null);
|
||
const readerVisible = ref(false);
|
||
|
||
const requiredAgreements = computed(() => agreements.value.filter(item => item.required));
|
||
const allAgreed = computed(() => requiredAgreements.value.every(item => accepted.value[item.code]));
|
||
const agreementsReady = computed(
|
||
() => !agreementsLoading.value && !agreementsError.value && allAgreed.value,
|
||
);
|
||
|
||
const canSubmitWechat = computed(() => agreementsReady.value);
|
||
const canSubmitAccount = computed(
|
||
() => Boolean(form.account.trim() && form.password) && agreementsReady.value,
|
||
);
|
||
|
||
async function loadAgreements() {
|
||
agreementsLoading.value = true;
|
||
agreementsError.value = '';
|
||
try {
|
||
const data = await fetchAgreements();
|
||
agreements.value = (data.agreements || []).filter(item => item && item.version);
|
||
} catch (err) {
|
||
agreements.value = [];
|
||
agreementsError.value = err instanceof ApiError ? err.message : '协议加载失败';
|
||
} finally {
|
||
agreementsLoading.value = false;
|
||
}
|
||
}
|
||
|
||
/** 打开协议弹窗(已同意时再点一次=重新阅读,不会取消勾选) */
|
||
function openAgreement(item: AgreementItem) {
|
||
error.value = '';
|
||
reading.value = item;
|
||
readerVisible.value = true;
|
||
}
|
||
|
||
/** 弹窗里点过「我已阅读并同意」→ 打勾 */
|
||
function onAgreed() {
|
||
const item = reading.value;
|
||
if (!item) return;
|
||
accepted.value = { ...accepted.value, [item.code]: true };
|
||
error.value = '';
|
||
uni.showToast({ title: `已同意《${item.name}》`, icon: 'none' });
|
||
}
|
||
|
||
/** 点圆圈:取消勾选(重新阅读后再勾) */
|
||
function revoke(item: AgreementItem) {
|
||
if (!accepted.value[item.code]) {
|
||
openAgreement(item);
|
||
return;
|
||
}
|
||
accepted.value = { ...accepted.value, [item.code]: false };
|
||
error.value = '';
|
||
}
|
||
|
||
watch(
|
||
() => props.active,
|
||
(active) => {
|
||
if (!active) return;
|
||
error.value = '';
|
||
tip.value = '';
|
||
tab.value = 'wechat';
|
||
accepted.value = {};
|
||
readerVisible.value = false;
|
||
reading.value = null;
|
||
loadAgreements();
|
||
},
|
||
{ immediate: true },
|
||
);
|
||
|
||
/** 登录成功后统一收尾:落地令牌 + 主体列表,抛事件给使用方 */
|
||
function finish(data: WechatLoginResult, message: string) {
|
||
setSession(data.token, data.user);
|
||
// 登录接口直接下发主体列表,落地后页面就不用再请求一次
|
||
if (Array.isArray(data.subjects)) applySubjects(data.subjects);
|
||
form.password = '';
|
||
accepted.value = {};
|
||
error.value = '';
|
||
tip.value = '';
|
||
uni.showToast({ title: message, icon: 'none' });
|
||
emit('success');
|
||
}
|
||
|
||
/** 428:协议版本变了 → 清勾选、拉最新正文 */
|
||
async function handleAgreementRequired(err: ApiError, fallback: string) {
|
||
error.value = err.message || fallback;
|
||
accepted.value = {};
|
||
await loadAgreements();
|
||
}
|
||
|
||
async function doWechatLogin() {
|
||
if (busy.value) return;
|
||
error.value = '';
|
||
tip.value = '';
|
||
if (!allAgreed.value) {
|
||
error.value = '请先阅读并同意协议';
|
||
return;
|
||
}
|
||
|
||
busy.value = true;
|
||
try {
|
||
const code = await getWechatLoginCode();
|
||
const payload = requiredAgreements.value.map(item => ({
|
||
code: item.code,
|
||
versionId: item.version.id,
|
||
}));
|
||
const data = await wechatLogin(code, payload);
|
||
finish(data, data.firstTime ? '微信登录成功,已为你创建账号' : '微信登录成功');
|
||
} catch (err) {
|
||
if (err instanceof ApiError && err.code === 'agreement_required') {
|
||
await handleAgreementRequired(err, '协议已更新,请重新阅读并同意');
|
||
} else if (err instanceof ApiError) {
|
||
error.value = err.message;
|
||
} else {
|
||
error.value = err instanceof Error ? err.message : '微信登录失败,请稍后再试';
|
||
}
|
||
} finally {
|
||
busy.value = false;
|
||
}
|
||
}
|
||
|
||
async function doLogin() {
|
||
if (busy.value) return;
|
||
error.value = '';
|
||
tip.value = '';
|
||
if (!form.account.trim() || !form.password) {
|
||
error.value = '请填写账号和密码';
|
||
return;
|
||
}
|
||
if (!allAgreed.value) {
|
||
error.value = '请先阅读并同意协议';
|
||
return;
|
||
}
|
||
|
||
busy.value = true;
|
||
try {
|
||
const payload = requiredAgreements.value.map(item => ({
|
||
code: item.code,
|
||
versionId: item.version.id,
|
||
}));
|
||
const data = await login(form.account.trim(), form.password, payload, 'mini_program');
|
||
finish(data, '登录成功');
|
||
} catch (err) {
|
||
if (err instanceof ApiError && err.code === 'agreement_required') {
|
||
await handleAgreementRequired(err, '协议已更新,请重新阅读并同意');
|
||
} else {
|
||
error.value = err instanceof ApiError ? err.message : '登录失败,请稍后再试';
|
||
}
|
||
} finally {
|
||
busy.value = false;
|
||
}
|
||
}
|
||
|
||
function switchTab(next: Tab) {
|
||
if (tab.value === next) return;
|
||
tab.value = next;
|
||
error.value = '';
|
||
tip.value = '';
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<view class="login-panel">
|
||
<view v-if="showCancel" class="login-panel__close" @click="emit('cancel')">×</view>
|
||
|
||
<view class="login-panel__tabs">
|
||
<view
|
||
class="login-panel__tab"
|
||
:class="{ 'is-active': tab === 'wechat' }"
|
||
@click="switchTab('wechat')"
|
||
>
|
||
微信登录
|
||
</view>
|
||
<view
|
||
class="login-panel__tab"
|
||
:class="{ 'is-active': tab === 'account' }"
|
||
@click="switchTab('account')"
|
||
>
|
||
账号登录
|
||
</view>
|
||
</view>
|
||
|
||
<view v-if="error" class="alert alert-error">{{ error }}</view>
|
||
<view v-if="tip" class="alert alert-ok">{{ tip }}</view>
|
||
|
||
<template v-if="tab === 'account'">
|
||
<view class="field">
|
||
<input
|
||
v-model="form.account"
|
||
class="input"
|
||
placeholder="用户名 / 手机号 / 邮箱"
|
||
placeholder-class="login-panel__placeholder"
|
||
:disabled="busy"
|
||
/>
|
||
</view>
|
||
|
||
<view class="field">
|
||
<input
|
||
v-model="form.password"
|
||
class="input"
|
||
password
|
||
placeholder="请输入密码"
|
||
placeholder-class="login-panel__placeholder"
|
||
confirm-type="done"
|
||
:disabled="busy"
|
||
@confirm="doLogin"
|
||
/>
|
||
</view>
|
||
</template>
|
||
|
||
<view v-else class="wx-desc">
|
||
<view class="wx-desc__title">使用微信一键登录,无需记住密码</view>
|
||
<view class="wx-desc__sub hint">首次登录会自动为你创建账号</view>
|
||
</view>
|
||
|
||
<view class="agree-block">
|
||
<view class="agree-head">
|
||
<text class="agree-title">阅读并同意协议</text>
|
||
<text v-if="agreementsLoading" class="hint">加载中…</text>
|
||
</view>
|
||
|
||
<view v-if="agreementsError" class="agree-error">
|
||
<text class="hint">{{ agreementsError }}</text>
|
||
<text class="agree-link agree-retry" @click="loadAgreements">重试</text>
|
||
</view>
|
||
|
||
<view v-for="item in agreements" :key="item.code" class="agree-row">
|
||
<view
|
||
class="agree-check"
|
||
:class="{ 'is-checked': accepted[item.code] }"
|
||
@click="revoke(item)"
|
||
>
|
||
<text v-if="accepted[item.code]" class="agree-tick">✓</text>
|
||
</view>
|
||
<text class="agree-text">我已阅读并同意</text>
|
||
<text class="agree-link" @click="openAgreement(item)">《{{ item.name }}》</text>
|
||
<text class="agree-version">v{{ item.version.version }}</text>
|
||
</view>
|
||
</view>
|
||
|
||
<button
|
||
v-if="tab === 'wechat'"
|
||
class="btn login-panel__submit wx-btn"
|
||
:disabled="busy || !canSubmitWechat"
|
||
@click="doWechatLogin"
|
||
>
|
||
{{ busy ? '登录中…' : '微信一键登录' }}
|
||
</button>
|
||
<button
|
||
v-else
|
||
class="btn login-panel__submit"
|
||
:disabled="busy || !canSubmitAccount"
|
||
@click="doLogin"
|
||
>
|
||
{{ busy ? '登录中…' : '登录' }}
|
||
</button>
|
||
|
||
<view class="login-panel__tip hint">账号注册、找回密码请在电脑浏览器打开 Web 端操作。</view>
|
||
|
||
<agreement-dialog
|
||
v-model:show="readerVisible"
|
||
:agreement="reading"
|
||
@agree="onAgreed"
|
||
/>
|
||
</view>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.login-panel {
|
||
position: relative;
|
||
}
|
||
|
||
.login-panel__close {
|
||
position: absolute;
|
||
top: -8rpx;
|
||
right: -12rpx;
|
||
width: 56rpx;
|
||
height: 56rpx;
|
||
border-radius: 50%;
|
||
color: #9ca3af;
|
||
font-size: 40rpx;
|
||
line-height: 52rpx;
|
||
text-align: center;
|
||
}
|
||
|
||
.login-panel__submit {
|
||
margin-top: 8rpx;
|
||
}
|
||
|
||
.login-panel__tabs {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 40rpx;
|
||
margin-bottom: 24rpx;
|
||
}
|
||
|
||
.login-panel__tab {
|
||
position: relative;
|
||
padding: 6rpx 0 12rpx;
|
||
color: var(--text-muted);
|
||
font-size: 30rpx;
|
||
}
|
||
|
||
.login-panel__tab.is-active {
|
||
color: var(--text);
|
||
font-weight: 600;
|
||
}
|
||
|
||
.login-panel__tab.is-active::after {
|
||
content: '';
|
||
position: absolute;
|
||
left: 50%;
|
||
bottom: 0;
|
||
width: 40rpx;
|
||
height: 6rpx;
|
||
border-radius: 999rpx;
|
||
background: var(--brand);
|
||
transform: translateX(-50%);
|
||
}
|
||
|
||
.wx-desc {
|
||
padding: 12rpx 0 8rpx;
|
||
}
|
||
|
||
.wx-desc__title {
|
||
font-size: 28rpx;
|
||
}
|
||
|
||
.wx-desc__sub {
|
||
margin-top: 6rpx;
|
||
}
|
||
|
||
.wx-btn {
|
||
background: #07c160;
|
||
}
|
||
|
||
.login-panel__tip {
|
||
margin-top: 20rpx;
|
||
text-align: center;
|
||
}
|
||
|
||
.login-panel__placeholder {
|
||
color: #b6bcc8;
|
||
}
|
||
|
||
.agree-block {
|
||
margin: 2rpx 0 20rpx;
|
||
}
|
||
|
||
.agree-head {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
margin-bottom: 4rpx;
|
||
}
|
||
|
||
.agree-title {
|
||
color: var(--text-muted);
|
||
font-size: 24rpx;
|
||
}
|
||
|
||
.agree-error {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 12rpx;
|
||
margin-top: 6rpx;
|
||
}
|
||
|
||
.agree-retry {
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.agree-row {
|
||
display: flex;
|
||
align-items: center;
|
||
margin-top: 10rpx;
|
||
font-size: 24rpx;
|
||
line-height: 1.5;
|
||
}
|
||
|
||
.agree-check {
|
||
width: 34rpx;
|
||
height: 34rpx;
|
||
margin-right: 10rpx;
|
||
border: 2rpx solid #c7ccd6;
|
||
border-radius: 50%;
|
||
flex-shrink: 0;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
}
|
||
|
||
.agree-check.is-checked {
|
||
border-color: var(--brand);
|
||
background: var(--brand);
|
||
}
|
||
|
||
.agree-tick {
|
||
color: #fff;
|
||
font-size: 22rpx;
|
||
line-height: 1;
|
||
}
|
||
|
||
.agree-text {
|
||
color: #4b5563;
|
||
}
|
||
|
||
.agree-link {
|
||
color: var(--brand);
|
||
}
|
||
|
||
.agree-version {
|
||
margin-left: 8rpx;
|
||
color: #9ca3af;
|
||
font-size: 20rpx;
|
||
}
|
||
</style>
|