func.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. // if (toUrl) {
  12. // let url = decodeURIComponent(toUrl);
  13. // uni.navigateTo({
  14. // url: url
  15. // })
  16. // } else {
  17. // uni.navigateBack({
  18. // delta: 1
  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. // uni.navigateBack({
  35. // delta: 1
  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. const checkLoginExpires = (e = {}) => {
  52. if (vm.isLogin) {
  53. try {
  54. let loginTime = uni.getStorageSync("loginTime");
  55. if (loginTime) {
  56. let dt = new Date();
  57. let time = dt.getTime();
  58. let expires = vm.userInfo.expires_in;
  59. if (expires) {
  60. if ((time - loginTime) / 1000 > (expires - 60)) {
  61. logout();
  62. }
  63. } else {
  64. logout();
  65. }
  66. } else {
  67. logout();
  68. }
  69. } catch (e) {
  70. logout();
  71. }
  72. } else {
  73. logout();
  74. }
  75. }
  76. // 跳转路由前检查登录状态
  77. const route = (url) => {
  78. if (!vm.isLogin) {
  79. uni.showToast({
  80. title: '请先登录',
  81. icon: 'none'
  82. })
  83. setTimeout(() => {
  84. uni.navigateTo({
  85. url: '/pages/login/login-account'
  86. })
  87. }, 500)
  88. return false
  89. }
  90. uni.navigateTo({
  91. url: url
  92. })
  93. }
  94. // URL参数转对象
  95. const paramsToObj = (url) => {
  96. if (url.indexOf('?') != -1) {
  97. let arr = url.split('?')[1]
  98. }
  99. let arr = url.split('&')
  100. let obj = {}
  101. for (let i of arr) {
  102. obj[i.split('=')[0]] = i.split('=')[1]
  103. }
  104. return obj
  105. }
  106. // 刷新当前页面
  107. const refreshPage = () => {
  108. const pages = getCurrentPages()
  109. const currentPage = pages[pages.length - 1]
  110. const path = '/' + currentPage.route + vm.$u.queryParams(currentPage.options)
  111. if (vm.$u.test.contains(currentPage.route, 'tabbar')) {
  112. uni.reLaunch({
  113. url: path,
  114. fail: (err) => {
  115. console.log(err)
  116. }
  117. })
  118. } else {
  119. uni.redirectTo({
  120. url: path,
  121. fail: (err) => {
  122. console.log(err)
  123. }
  124. })
  125. }
  126. }
  127. // 提示
  128. const showToast = (data = {}) => {
  129. if (typeof data == 'string') {
  130. uni.showToast({
  131. title: data,
  132. icon: 'none'
  133. })
  134. } else {
  135. uni.showToast({
  136. title: data.title,
  137. icon: data.icon || 'none',
  138. image: data.image || '',
  139. mask: data.mask || false,
  140. position: data.position || 'center',
  141. duration: data.duration || 1500,
  142. success: () => {
  143. setTimeout(() => {
  144. if (data.back) return uni.navigateBack()
  145. data.success && data.success()
  146. }, data.duration || 1500)
  147. }
  148. })
  149. }
  150. }
  151. // 将定义的方法挂载,使用this.$u.func.xx调用
  152. Vue.prototype.$u.func = {
  153. login,
  154. logout,
  155. checkLoginExpires,
  156. route,
  157. checkLogin,
  158. paramsToObj,
  159. refreshPage,
  160. showToast
  161. }
  162. }
  163. export default {
  164. install
  165. }