Files
memind/miniapp/pages/login/index.js
T
john 048f25f580 feat(miniapp): add privacy consent and legal pages
Show privacy popup on launch, gate login on legal consent, and link
user agreement and privacy policy pages in the mini program.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-12 12:18:54 +08:00

174 lines
4.3 KiB
JavaScript

const {
checkAuth,
loginWithPassword,
loginWithWechatCode,
isTouristMode,
getApiBaseUrl,
getStoredSession
} = require('../../utils/api');
const { MINIAPP_LOGIN_PATH } = require('../../utils/config');
const {
saveLegalConsent,
shouldSkipLoginConsent
} = require('../../utils/legal-consent');
function hasStoredSessionCookie() {
const session = getStoredSession();
return Boolean(session?.cookie);
}
Page({
data: {
username: '',
password: '',
error: '',
wechatLoading: false,
passwordLoading: false,
touristWarning: '',
envHint: '',
agreedToLegal: false,
showLegalConsent: true,
showConsentModal: false
},
onLoad() {
const skipConsent = hasStoredSessionCookie() && shouldSkipLoginConsent();
this.setData({
agreedToLegal: false,
showLegalConsent: !skipConsent
});
this.refreshEnvHints();
this.tryAutoLogin();
},
refreshEnvHints() {
const apiBaseUrl = getApiBaseUrl();
let touristWarning = '';
let envHint = '';
if (isTouristMode()) {
touristWarning =
'游客模式:微信一键登录不可用。请用 AppID wx3e79ecd530c88da4 重新导入项目,并确认 DevTools 登录微信号已是小程序管理员。';
}
if (/127\.0\.0\.1|localhost/i.test(apiBaseUrl)) {
envHint = '当前连本地 Portal;请确保已运行 pnpm dev 并已重启。';
}
this.setData({ touristWarning, envHint });
},
async tryAutoLogin() {
if (!hasStoredSessionCookie()) {
return;
}
try {
const status = await checkAuth();
if (status?.authenticated) {
const app = getApp();
if (status.user) {
app.globalData.user = status.user;
}
wx.switchTab({ url: '/pages/chat/index' });
}
} catch {
// Stay on the login page when the existing session cannot be verified.
}
},
handleUsername(event) {
this.setData({ username: event.detail.value, error: '' });
},
handlePassword(event) {
this.setData({ password: event.detail.value, error: '' });
},
handleConsentChange(event) {
const agreedToLegal = (event.detail.value || []).includes('agreed');
this.setData({ agreedToLegal, error: '' });
},
openTerms() {
wx.navigateTo({ url: '/pages/legal/terms/index' });
},
openPrivacy() {
wx.navigateTo({ url: '/pages/legal/privacy/index' });
},
openLegalCenter() {
wx.navigateTo({ url: '/pages/legal/index/index' });
},
ensureLegalConsent() {
if (!this.data.showLegalConsent || this.data.agreedToLegal) {
return true;
}
this.setData({
error: '请先阅读并同意《用户服务协议》和《隐私政策》后再登录',
showConsentModal: true
});
return false;
},
acceptConsentModal() {
this.setData({
agreedToLegal: true,
showConsentModal: false,
error: ''
});
},
declineConsentModal() {
this.setData({ showConsentModal: false });
wx.showToast({
title: '需同意协议后方可使用',
icon: 'none'
});
},
persistConsentAfterLogin() {
saveLegalConsent();
},
async handleWechatLogin() {
if (!this.ensureLegalConsent()) return;
this.setData({ wechatLoading: true, error: '' });
try {
await loginWithWechatCode(MINIAPP_LOGIN_PATH);
this.persistConsentAfterLogin();
wx.switchTab({ url: '/pages/chat/index' });
} catch (error) {
this.setData({
error:
error?.message ||
'微信登录暂不可用。若后端尚未开放小程序登录接口,请先使用 H5 账号登录。'
});
} finally {
this.setData({ wechatLoading: false });
}
},
async handlePasswordLogin() {
if (!this.ensureLegalConsent()) return;
const username = this.data.username.trim();
const password = this.data.password;
if (!username || !password) {
this.setData({ error: '请输入用户名和密码' });
return;
}
this.setData({ passwordLoading: true, error: '' });
try {
await loginWithPassword(username, password);
this.persistConsentAfterLogin();
wx.switchTab({ url: '/pages/chat/index' });
} catch (error) {
this.setData({ error: error?.message || '登录失败,请重试' });
} finally {
this.setData({ passwordLoading: false });
}
}
});