func.js 2.9 KB

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