| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /**
- * 全局登录浮层 mixin(注册于 main.js:Vue.mixin(loginSheetMixin))
- * 页面内任意位置调用 this.openLoginSheet(options) 即从底部浮出登录浮层
- * - options:{ inviteCode, inviteType, refCode, redirect, onSuccess }
- * onSuccess 为登录成功回调(JS 直接传函数,不经序列化),在 storage 写入、浮层关闭、
- * pendingFamilyInviteCode 跳转判断之前执行,适合「登录后续接购买/报名」类流程
- * - 组件内(LoginGuideCard)调用时,本 mixin 会沿 $parent 链向上找到宿主页面浮层;
- * 宿主页面未挂浮层时回退到整页登录(历史行为,向后兼容)
- *
- * 已登录守卫:
- * - 若 uni.getStorageSync('token') 存在,不弹浮层,优先调用页面 onLoginSuccess(),
- * 否则 loadData(),都无则静默返回(防止已登录用户误触发登录浮层)
- */
- function findLoginSheet(vm) {
- var cur = vm
- var depth = 0
- while (cur && depth < 20) {
- if (cur.$refs && cur.$refs.loginSheet) {
- return cur.$refs.loginSheet
- }
- cur = cur.$parent
- depth++
- }
- return null
- }
- export default {
- methods: {
- openLoginSheet: function(options) {
- options = options || {}
- // 已登录守卫
- var token = uni.getStorageSync('token')
- if (token) {
- if (typeof this.onLoginSuccess === 'function') {
- try {
- this.onLoginSuccess()
- } catch (e) {
- console.warn('onLoginSuccess 调用失败', e)
- }
- return
- }
- if (typeof this.loadData === 'function') {
- try {
- this.loadData()
- } catch (e) {
- console.warn('loadData 调用失败', e)
- }
- return
- }
- return
- }
- var sheet = findLoginSheet(this)
- if (sheet && typeof sheet.open === 'function') {
- sheet.open(options)
- } else {
- // 宿主页面未挂浮层时回退到整页登录(历史行为,向后兼容)
- var url = '/pages/login/login'
- var qs = []
- if (options.inviteCode) {
- qs.push('inviteCode=' + encodeURIComponent(options.inviteCode))
- qs.push('inviteType=' + encodeURIComponent(options.inviteType || 'family'))
- }
- if (options.refCode) {
- qs.push('refCode=' + encodeURIComponent(options.refCode))
- }
- if (options.redirect) {
- qs.push('redirect=' + encodeURIComponent(options.redirect))
- }
- if (qs.length) url += '?' + qs.join('&')
- uni.navigateTo({ url: url })
- }
- },
- closeLoginSheet: function() {
- var sheet = findLoginSheet(this)
- if (sheet && typeof sheet.close === 'function') {
- sheet.close()
- }
- },
- onSheetLoginSuccess: function(data) {
- if (typeof this.onLoginSuccess === 'function') {
- try {
- this.onLoginSuccess(data)
- } catch (e) {
- console.warn('onSheetLoginSuccess 调用失败', e)
- }
- } else if (typeof this.loadData === 'function') {
- try {
- this.loadData()
- } catch (e) {
- console.warn('loadData 调用失败', e)
- }
- }
- }
- }
- }
|