func.js 2.7 KB

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