func.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // 全局公共方法
  2. const install = (Vue, vm) => {
  3. // 登录操作
  4. const login = (userInfo, redirect) => {
  5. vm.$u.vuex('userInfo', userInfo)
  6. vm.$u.vuex('accessToken', userInfo.access_token)
  7. vm.$u.vuex('refreshToken', userInfo.refresh_token)
  8. vm.$u.vuex('isLogin', true)
  9. if (redirect) {
  10. uni.redirectTo({
  11. url: redirect,
  12. fail() {
  13. uni.switchTab({
  14. url: redirect
  15. })
  16. }
  17. })
  18. } else uni.switchTab({
  19. url: '/pages/home/home'
  20. })
  21. }
  22. // 退出登录
  23. const logout = () => {
  24. vm.$u.vuex('userInfo', {
  25. avatar: '',
  26. nick_name: '游客',
  27. tenant_id: '暂无'
  28. })
  29. vm.$u.vuex('accessToken', '')
  30. vm.$u.vuex('isLogin', false)
  31. uni.redirectTo({
  32. url: '/pages/login/login-account'
  33. })
  34. }
  35. // 检查登录状态
  36. const checkLogin = (e = {}) => {
  37. if (!vm.isLogin) {
  38. uni.navigateTo({
  39. url: '/pages/login/login-account'
  40. })
  41. return false
  42. }
  43. return true
  44. }
  45. // 跳转路由前检查登录状态
  46. const route = (url) => {
  47. if (!vm.isLogin) {
  48. uni.showToast({
  49. title: '请先登录',
  50. icon: 'none'
  51. })
  52. const pages = getCurrentPages()
  53. const currentPage = pages[pages.length - 1]
  54. setTimeout(() => {
  55. uni.navigateTo({
  56. url: `/pages/login/login-account?redirect=/${currentPage.route}`
  57. })
  58. }, 500)
  59. return false
  60. }
  61. uni.navigateTo({
  62. url: url
  63. })
  64. }
  65. // URL参数转对象
  66. const paramsToObj = (url) => {
  67. if (url.indexOf('?') != -1) {
  68. let arr = url.split('?')[1]
  69. }
  70. let arr = url.split('&')
  71. let obj = {}
  72. for (let i of arr) {
  73. obj[i.split('=')[0]] = i.split('=')[1]
  74. }
  75. return obj
  76. }
  77. // 刷新当前页面
  78. const refreshPage = () => {
  79. const pages = getCurrentPages()
  80. const currentPage = pages[pages.length - 1]
  81. const path = '/' + currentPage.route + vm.$u.queryParams(currentPage.options)
  82. if (vm.$u.test.contains(currentPage.route, 'tabbar')) {
  83. uni.reLaunch({
  84. url: path,
  85. fail: (err) => {
  86. console.log(err)
  87. }
  88. })
  89. } else {
  90. uni.redirectTo({
  91. url: path,
  92. fail: (err) => {
  93. console.log(err)
  94. }
  95. })
  96. }
  97. }
  98. // 提示
  99. const showToast = (data = {}) => {
  100. if (typeof data == 'string') {
  101. uni.showToast({
  102. title: data,
  103. icon: 'none'
  104. })
  105. } else {
  106. uni.showToast({
  107. title: data.title,
  108. icon: data.icon || 'none',
  109. image: data.image || '',
  110. mask: data.mask || false,
  111. position: data.position || 'center',
  112. duration: data.duration || 1500,
  113. success: () => {
  114. setTimeout(() => {
  115. if (data.back) return uni.navigateBack()
  116. data.success && data.success()
  117. }, data.duration || 1500)
  118. }
  119. })
  120. }
  121. }
  122. // 刷新token
  123. const refreshToken = (data) => {
  124. return new Promise((resolve) => {
  125. vm.$u.vuex('accessToken', data.access_token)
  126. vm.$u.vuex('refreshToken', data.refresh_token)
  127. resolve()
  128. })
  129. }
  130. // 将定义的方法挂载,使用this.$u.func.xx调用
  131. Vue.prototype.$u.func = {
  132. login,
  133. logout,
  134. route,
  135. checkLogin,
  136. paramsToObj,
  137. refreshPage,
  138. showToast,
  139. refreshToken
  140. }
  141. }
  142. export default {
  143. install
  144. }