func.js 2.8 KB

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