平安校园
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

179 行
3.4 KiB

  1. // 全局公共方法
  2. const install = (Vue, vm) => {
  3. // 登录操作
  4. const login = (userInfo, redirect) => {
  5. vm.$u.vuex('userInfo', userInfo)
  6. vm.$u.vuex('accessToken', userInfo.access_token)
  7. vm.$u.vuex('refreshToken', userInfo.refresh_token)
  8. vm.$u.vuex('isLogin', true)
  9. if(userInfo.user_type == 1) {
  10. // 教师首页
  11. if (redirect) {
  12. uni.redirectTo({
  13. url: redirect,
  14. fail() {
  15. uni.switchTab({
  16. url: redirect
  17. })
  18. }
  19. })
  20. } else uni.redirectTo({
  21. url: '/pages/home/index'
  22. })
  23. } else if(userInfo.user_type == 2) {
  24. // 学生首页
  25. if (redirect) {
  26. uni.redirectTo({
  27. url: redirect,
  28. fail() {
  29. uni.switchTab({
  30. url: redirect
  31. })
  32. }
  33. })
  34. } else uni.navigateTo({
  35. url: '/pages-student/home/index'
  36. })
  37. }
  38. // if(userInfo)
  39. // debugger
  40. }
  41. // 退出登录
  42. const logout = (callback) => {
  43. // vm.$u.vuex('userInfo', {
  44. // avatar: '',
  45. // nick_name: '游客',
  46. // tenant_id: '暂无'
  47. // })
  48. // vm.$u.vuex('accessToken', '')
  49. // vm.$u.vuex('isLogin', false)
  50. // uni.redirectTo({
  51. // url: '/pages/login/login'
  52. // })
  53. callback()
  54. }
  55. // 检查登录状态
  56. const checkLogin = (e = {}) => {
  57. if (!vm.isLogin) {
  58. uni.navigateTo({
  59. url: '/pages/login/login'
  60. })
  61. return false
  62. }
  63. return true
  64. }
  65. // 跳转路由前检查登录状态
  66. const route = (url) => {
  67. if (!vm.isLogin) {
  68. uni.showToast({
  69. title: '请先登录',
  70. icon: 'none'
  71. })
  72. const pages = getCurrentPages()
  73. const currentPage = pages[pages.length - 1]
  74. setTimeout(() => {
  75. uni.navigateTo({
  76. url: `/pages/login/login?redirect=/${currentPage.route}`
  77. })
  78. }, 500)
  79. return false
  80. }
  81. uni.navigateTo({
  82. url: url
  83. })
  84. }
  85. // URL参数转对象
  86. const paramsToObj = (url) => {
  87. if (url.indexOf('?') != -1) {
  88. let arr = url.split('?')[1]
  89. }
  90. let arr = url.split('&')
  91. let obj = {}
  92. for (let i of arr) {
  93. obj[i.split('=')[0]] = i.split('=')[1]
  94. }
  95. return obj
  96. }
  97. // 刷新当前页面
  98. const refreshPage = () => {
  99. const pages = getCurrentPages()
  100. const currentPage = pages[pages.length - 1]
  101. const path = '/' + currentPage.route + vm.$u.queryParams(currentPage.options)
  102. if (vm.$u.test.contains(currentPage.route, 'tabbar')) {
  103. uni.reLaunch({
  104. url: path,
  105. fail: (err) => {
  106. console.log(err)
  107. }
  108. })
  109. } else {
  110. uni.redirectTo({
  111. url: path,
  112. fail: (err) => {
  113. console.log(err)
  114. }
  115. })
  116. }
  117. }
  118. // 提示
  119. const showToast = (data = {}) => {
  120. if (typeof data == 'string') {
  121. uni.showToast({
  122. title: data,
  123. icon: 'none'
  124. })
  125. } else {
  126. uni.showToast({
  127. title: data.title,
  128. icon: data.icon || 'none',
  129. image: data.image || '',
  130. mask: data.mask || false,
  131. position: data.position || 'center',
  132. duration: data.duration || 1500,
  133. success: () => {
  134. setTimeout(() => {
  135. if (data.back) return uni.navigateBack()
  136. data.success && data.success()
  137. }, data.duration || 1500)
  138. }
  139. })
  140. }
  141. }
  142. // 刷新token
  143. const refreshToken = (data) => {
  144. return new Promise((resolve) => {
  145. console.log('refreshTokenrefreshToken',data)
  146. vm.$u.vuex('accessToken', data.access_token)
  147. vm.$u.vuex('refreshToken', data.refresh_token)
  148. resolve()
  149. })
  150. }
  151. // 将定义的方法挂载,使用this.$u.func.xx调用
  152. Vue.prototype.$u.func = {
  153. login,
  154. logout,
  155. route,
  156. checkLogin,
  157. paramsToObj,
  158. refreshPage,
  159. showToast,
  160. refreshToken
  161. }
  162. }
  163. export default {
  164. install
  165. }