login-sheet.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * 全局登录浮层 mixin(注册于 main.js:Vue.mixin(loginSheetMixin))
  3. * 页面内任意位置调用 this.openLoginSheet(options) 即从底部浮出登录浮层
  4. * - options:{ inviteCode, inviteType, refCode, redirect, onSuccess }
  5. * onSuccess 为登录成功回调(JS 直接传函数,不经序列化),在 storage 写入、浮层关闭、
  6. * pendingFamilyInviteCode 跳转判断之前执行,适合「登录后续接购买/报名」类流程
  7. * - 组件内(LoginGuideCard)调用时,本 mixin 会沿 $parent 链向上找到宿主页面浮层;
  8. * 宿主页面未挂浮层时回退到整页登录(历史行为,向后兼容)
  9. *
  10. * 已登录守卫:
  11. * - 若 uni.getStorageSync('token') 存在,不弹浮层,优先调用页面 onLoginSuccess(),
  12. * 否则 loadData(),都无则静默返回(防止已登录用户误触发登录浮层)
  13. */
  14. function findLoginSheet(vm) {
  15. var cur = vm
  16. var depth = 0
  17. while (cur && depth < 20) {
  18. if (cur.$refs && cur.$refs.loginSheet) {
  19. return cur.$refs.loginSheet
  20. }
  21. cur = cur.$parent
  22. depth++
  23. }
  24. return null
  25. }
  26. export default {
  27. methods: {
  28. openLoginSheet: function(options) {
  29. options = options || {}
  30. // 已登录守卫
  31. var token = uni.getStorageSync('token')
  32. if (token) {
  33. if (typeof this.onLoginSuccess === 'function') {
  34. try {
  35. this.onLoginSuccess()
  36. } catch (e) {
  37. console.warn('onLoginSuccess 调用失败', e)
  38. }
  39. return
  40. }
  41. if (typeof this.loadData === 'function') {
  42. try {
  43. this.loadData()
  44. } catch (e) {
  45. console.warn('loadData 调用失败', e)
  46. }
  47. return
  48. }
  49. return
  50. }
  51. var sheet = findLoginSheet(this)
  52. if (sheet && typeof sheet.open === 'function') {
  53. sheet.open(options)
  54. } else {
  55. // 宿主页面未挂浮层时回退到整页登录(历史行为,向后兼容)
  56. var url = '/pages/login/login'
  57. var qs = []
  58. if (options.inviteCode) {
  59. qs.push('inviteCode=' + encodeURIComponent(options.inviteCode))
  60. qs.push('inviteType=' + encodeURIComponent(options.inviteType || 'family'))
  61. }
  62. if (options.refCode) {
  63. qs.push('refCode=' + encodeURIComponent(options.refCode))
  64. }
  65. if (options.redirect) {
  66. qs.push('redirect=' + encodeURIComponent(options.redirect))
  67. }
  68. if (qs.length) url += '?' + qs.join('&')
  69. uni.navigateTo({ url: url })
  70. }
  71. },
  72. closeLoginSheet: function() {
  73. var sheet = findLoginSheet(this)
  74. if (sheet && typeof sheet.close === 'function') {
  75. sheet.close()
  76. }
  77. },
  78. onSheetLoginSuccess: function(data) {
  79. if (typeof this.onLoginSuccess === 'function') {
  80. try {
  81. this.onLoginSuccess(data)
  82. } catch (e) {
  83. console.warn('onSheetLoginSuccess 调用失败', e)
  84. }
  85. } else if (typeof this.loadData === 'function') {
  86. try {
  87. this.loadData()
  88. } catch (e) {
  89. console.warn('loadData 调用失败', e)
  90. }
  91. }
  92. // 登录成功后,尝试刷新当前页面,重置登录状态
  93. try {
  94. var pages = getCurrentPages()
  95. if (pages.length) {
  96. var curPage = pages[pages.length - 1]
  97. if (curPage && typeof curPage.onLoad === 'function' && typeof curPage.onShow === 'function') {
  98. var options = curPage.options || {}
  99. curPage.onLoad(options)
  100. curPage.onShow()
  101. } else if (curPage && typeof curPage.onShow === 'function') {
  102. curPage.onShow()
  103. }
  104. }
  105. } catch (e) {
  106. console.warn('刷新当前页失败', e)
  107. }
  108. }
  109. }
  110. }