| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- /**
- * 全局登录浮层 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)
- }
- }
- // 登录成功后,尝试刷新当前页面,重置登录状态
- try {
- var pages = getCurrentPages()
- if (pages.length) {
- var curPage = pages[pages.length - 1]
- if (curPage && typeof curPage.onLoad === 'function' && typeof curPage.onShow === 'function') {
- var options = curPage.options || {}
- curPage.onLoad(options)
- curPage.onShow()
- } else if (curPage && typeof curPage.onShow === 'function') {
- curPage.onShow()
- }
- }
- } catch (e) {
- console.warn('刷新当前页失败', e)
- }
- }
- }
- }
|