import Taro from '@tarojs/taro' import { isLoggedIn, goToRegister } from './auth' /** * 需要登录才能执行的动作类型。 * - 这些点都是"成交前置行为":加购、结算、收藏、领券、查看订单、下单等 * - 业务页(首页/分类/搜索/详情)始终对游客开放,价格字段做脱敏即可 */ export type LoginRequiredAction = | 'addToCart' | 'buyNow' | 'checkout' | 'favorite' | 'receiveCoupon' | 'viewOrder' | 'payOrder' | 'submitComment' const ACTION_LABEL: Record = { addToCart: '加入购物车', buyNow: '立即购买', checkout: '结算', favorite: '收藏', receiveCoupon: '领取优惠券', viewOrder: '查看订单', payOrder: '支付订单', submitComment: '提交评价', } const ACTION_REDIRECT_FALLBACK: Record = { addToCart: '/pages/shop/cart', buyNow: '/pages/shop/cart', checkout: '/pages/shop/cart', favorite: '/pages/user/favorite-list', receiveCoupon: '/pages/index/coupon-center', viewOrder: '/pages/order/order', payOrder: '/pages/order/order', submitComment: '/pages/order/order', } export interface RequireLoginOptions { /** 动作类型,用于生成提示文案 */ action: LoginRequiredAction /** 登录后跳回的目标 URL(相对于本小程序),不传则用动作默认值 */ redirect?: string /** 自定义弹窗标题 */ title?: string /** 自定义弹窗内容(不传则按 action 自动生成) */ content?: string /** * true: 不弹窗,直接跳登录页(仅用于"非常确定用户就要登录"的场景,比如优惠券领取) * 默认 false: 弹一个非阻塞的二次确认 */ silent?: boolean } /** * 行为级登录守卫:游客态触发时弹出一个非阻塞的二次确认。 * * 返回: * - true : 已登录,可继续执行原动作 * - false : 游客态,已拦截(弹窗点了取消 / 弹窗点了去登录但还在当前页停留) * * 微信审核要点: * 1. 永远不要在 useLaunch / onLoad 里主动调; * 2. 永远不要在 tabBar 切换时调(tabBar 三个页面要 100% 游客可浏览); * 3. 不要在 onShow 里无脑调,否则审核员每次切回都要点确认,体验差。 */ export function requireLogin(options: RequireLoginOptions): boolean { if (isLoggedIn()) return true const actionLabel = ACTION_LABEL[options.action] const redirect = options.redirect || ACTION_REDIRECT_FALLBACK[options.action] const title = options.title || '需要登录' const content = options.content || `登录后才能${actionLabel},是否前往登录?` if (options.silent) { goToRegister({ redirect }) return false } Taro.showModal({ title, content, confirmText: '去登录', cancelText: '再逛逛', confirmColor: '#0e932e', success: ({ confirm }) => { if (confirm) { goToRegister({ redirect }) } }, }) return false }