func.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.navigateBack({
  12. delta: 1
  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. uni.navigateBack({
  28. delta: 1
  29. })
  30. }
  31. // 检查登录状态
  32. const checkLogin = (e = {}) => {
  33. if (!vm.isLogin) {
  34. // uni.navigateTo({
  35. // url: '/pages/login/login-account'
  36. // })
  37. // uni.reLaunch({
  38. // url: '/pages/login/login-account'
  39. // })
  40. return false
  41. }
  42. return true
  43. }
  44. // 跳转路由前检查登录状态
  45. const route = (url) => {
  46. if (!vm.isLogin) {
  47. uni.showToast({
  48. title: '请先登录',
  49. icon: 'none'
  50. })
  51. setTimeout(() => {
  52. uni.navigateTo({
  53. url: '/pages/login/login-account'
  54. })
  55. }, 500)
  56. return false
  57. }
  58. uni.navigateTo({
  59. url: url
  60. })
  61. }
  62. // URL参数转对象
  63. const paramsToObj = (url) => {
  64. if (url.indexOf('?') != -1) {
  65. let arr = url.split('?')[1]
  66. }
  67. let arr = url.split('&')
  68. let obj = {}
  69. for (let i of arr) {
  70. obj[i.split('=')[0]] = i.split('=')[1]
  71. }
  72. return obj
  73. }
  74. // 刷新当前页面
  75. const refreshPage = () => {
  76. const pages = getCurrentPages()
  77. const currentPage = pages[pages.length - 1]
  78. const path = '/' + currentPage.route + vm.$u.queryParams(currentPage.options)
  79. if (vm.$u.test.contains(currentPage.route, 'tabbar')) {
  80. uni.reLaunch({
  81. url: path,
  82. fail: (err) => {
  83. console.log(err)
  84. }
  85. })
  86. } else {
  87. uni.redirectTo({
  88. url: path,
  89. fail: (err) => {
  90. console.log(err)
  91. }
  92. })
  93. }
  94. }
  95. // 提示
  96. const showToast = (data = {}) => {
  97. if (typeof data == 'string') {
  98. uni.showToast({
  99. title: data,
  100. icon: 'none'
  101. })
  102. } else {
  103. uni.showToast({
  104. title: data.title,
  105. icon: data.icon || 'none',
  106. image: data.image || '',
  107. mask: data.mask || false,
  108. position: data.position || 'center',
  109. duration: data.duration || 1500,
  110. success: () => {
  111. setTimeout(() => {
  112. if (data.back) return uni.navigateBack()
  113. data.success && data.success()
  114. }, data.duration || 1500)
  115. }
  116. })
  117. }
  118. }
  119. // 将定义的方法挂载,使用this.$u.func.xx调用
  120. Vue.prototype.$u.func = {
  121. login,
  122. logout,
  123. route,
  124. checkLogin,
  125. paramsToObj,
  126. refreshPage,
  127. showToast
  128. }
  129. }
  130. export default {
  131. install
  132. }