func.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // 全局公共方法
  2. import { wxlogin} from '@/api/user.js'
  3. const install = (Vue, vm) => {
  4. //拿到全局变量
  5. // 登录操作
  6. const login = (userInfo, redirect) => {
  7. vm.$u.vuex('userInfo', userInfo)
  8. vm.$u.vuex('accessToken', userInfo.access_token)
  9. vm.$u.vuex('refreshToken', userInfo.refresh_token)
  10. vm.$u.vuex('userInfo.tokenGeneratedTime', Date.now())
  11. vm.$u.vuex('isLogin', true)
  12. if (redirect) {
  13. uni.redirectTo({
  14. url: redirect,
  15. fail() {
  16. uni.switchTab({
  17. url: redirect
  18. })
  19. }
  20. })
  21. } else uni.switchTab({
  22. url: '/pages/index/index'
  23. })
  24. }
  25. // 更新个人信息
  26. const updateInfos = (pInfo, redirect) => {
  27. vm.$u.vuex('userInfo.detail.phone', pInfo.phone)
  28. vm.$u.vuex('userInfo.detail.sfzh', pInfo.sfzh)
  29. vm.$u.vuex('userInfo.detail.name', pInfo.name)
  30. vm.$u.vuex('userInfo.detail.dz', pInfo.dz)
  31. vm.$u.vuex('userInfo.detail.sqName', pInfo.sqmc)
  32. if (redirect) {
  33. uni.redirectTo({
  34. url: redirect,
  35. fail() {
  36. uni.switchTab({
  37. url: redirect
  38. })
  39. }
  40. })
  41. } else{
  42. uni.switchTab({
  43. url: '/pages/my/index'
  44. })
  45. }
  46. }
  47. // 退出登录
  48. const logout = () => {
  49. vm.$u.vuex('userInfo', {
  50. avatar: '',
  51. nick_name: '游客',
  52. tenant_id: '暂无'
  53. })
  54. vm.$u.vuex('accessToken', '')
  55. vm.$u.vuex('isLogin', false)
  56. uni.redirectTo({
  57. url: '/pageA/my/login'
  58. })
  59. }
  60. const checkUpdate = ()=>{
  61. if (vm.userInfo.detail.status==0) {
  62. vm.$u.func.showToast({ title: '请完善基础信息'})
  63. uni.navigateTo({
  64. url: '/pageA/my/profile'
  65. })
  66. return false
  67. }
  68. return true
  69. }
  70. // 检查登录状态
  71. const checkLogin = (e = {}) => {
  72. const tokenValidPeriod = 86400000;
  73. if (!vm.isLogin) {
  74. uni.navigateTo({
  75. url: '/pageA/my/login'
  76. })
  77. return false
  78. }else if(vm.isLogin && Number(Date.now() - vm.userInfo.tokenGeneratedTime) > Number(tokenValidPeriod)){
  79. // 如果token为空或者已经超过有效期,需要生成新的token
  80. vm.$u.vuex('userInfo', {
  81. avatar: '',
  82. nick_name: '游客',
  83. tenant_id: '暂无'
  84. })
  85. vm.$u.vuex('accessToken', '')
  86. vm.$u.vuex('isLogin', false)
  87. console.log('token过期');
  88. uni.navigateTo({
  89. url: '/pageA/my/login'
  90. })
  91. return false
  92. }else{
  93. return true
  94. }
  95. }
  96. const userLogin =(code)=>{
  97. wxlogin({code:code}).then(res=>{
  98. console.log('接口返回------',res);
  99. if(res.data.code=='100'||res.data.code=='500'){
  100. vm.$u.func.showToast({ title: res.data.msg})
  101. uni.navigateTo({
  102. url: '/pageA/my/login='+res.data.data+'&code='+code
  103. });
  104. }
  105. if(res.data.code=='101'){
  106. vm.$u.func.showToast({ title: res.data.msg})
  107. }
  108. if(res.data.code=='102'){
  109. vm.$u.func.showToast({ title: res.data.msg})
  110. }
  111. if(res.data.code=='401'){
  112. vm.$u.func.showToast({ title: res.data.msg})
  113. }
  114. if(res.data.code=='200'){
  115. //console.log(res,'res')
  116. //vm.$u.func.showToast({ title: '登陆成功'})
  117. vm.$u.vuex('accessToken', res.data.data.access_token)
  118. vm.$u.vuex('refreshToken', res.data.data.refresh_token)
  119. vm.$u.vuex('isLogin', true)
  120. vm.$u.vuex('userInfo',res.data.data)
  121. }
  122. })
  123. }
  124. // 跳转路由前检查登录状态
  125. // 跳转路由前检查登录状态
  126. const route = (url) => {
  127. if (!vm.isLogin) {
  128. uni.showToast({
  129. title: '请先登录',
  130. icon: 'none'
  131. })
  132. const pages = getCurrentPages()
  133. const currentPage = pages[pages.length - 1]
  134. setTimeout(() => {
  135. uni.navigateTo({
  136. url: `/pageA/my/login?redirect=/${currentPage.route}`
  137. })
  138. }, 500)
  139. return false
  140. }
  141. uni.navigateTo({
  142. url: url
  143. })
  144. }
  145. // URL参数转对象
  146. const paramsToObj = (url) => {
  147. if (url.indexOf('?') != -1) {
  148. let arr = url.split('?')[1]
  149. }
  150. let arr = url.split('&')
  151. let obj = {}
  152. for (let i of arr) {
  153. obj[i.split('=')[0]] = i.split('=')[1]
  154. }
  155. return obj
  156. }
  157. // 刷新当前页面
  158. const refreshPage = () => {
  159. const pages = getCurrentPages()
  160. const currentPage = pages[pages.length - 1]
  161. const path = '/' + currentPage.route + vm.$u.queryParams(currentPage.options)
  162. if (vm.$u.test.contains(currentPage.route, 'tabbar')) {
  163. uni.reLaunch({
  164. url: path,
  165. fail: (err) => {
  166. console.log(err)
  167. }
  168. })
  169. } else {
  170. uni.redirectTo({
  171. url: path,
  172. fail: (err) => {
  173. console.log(err)
  174. }
  175. })
  176. }
  177. }
  178. // 提示
  179. const showToast = (data = {}) => {
  180. if (typeof data == 'string') {
  181. uni.showToast({
  182. title: data,
  183. icon: 'none'
  184. })
  185. } else {
  186. uni.showToast({
  187. title: data.title,
  188. icon: data.icon || 'none',
  189. image: data.image || '',
  190. mask: data.mask || false,
  191. position: data.position || 'center',
  192. duration: data.duration || 1500,
  193. success: () => {
  194. setTimeout(() => {
  195. if (data.back) return uni.navigateBack()
  196. data.success && data.success()
  197. }, data.duration || 1500)
  198. }
  199. })
  200. }
  201. }
  202. // 刷新token
  203. const refreshToken = (data) => {
  204. return new Promise((resolve) => {
  205. vm.$u.vuex('accessToken', data.access_token)
  206. vm.$u.vuex('refreshToken', data.refresh_token)
  207. resolve()
  208. })
  209. }
  210. // 将定义的方法挂载,使用this.$u.func.xx调用
  211. Vue.prototype.$u.func = {
  212. login,
  213. logout,
  214. route,
  215. updateInfos,
  216. checkLogin,
  217. checkUpdate,
  218. paramsToObj,
  219. refreshPage,
  220. showToast,
  221. refreshToken
  222. }
  223. }
  224. export default {
  225. install
  226. }