func.js 2.7 KB

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