App.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <script>
  2. import {
  3. BASE_URL
  4. } from './config.js'
  5. import {
  6. useUserStore
  7. } from '@/store/userStore.js'
  8. export default {
  9. onLaunch: function() {
  10. console.log('App Launch')
  11. const user = useUserStore()
  12. uni.addInterceptor('request', {
  13. invoke(options) {
  14. if (!options.url.startsWith('http')) {
  15. options.url = BASE_URL + options.url
  16. }
  17. options.timeout = 20000
  18. options.header = {
  19. ...options.header,
  20. Token: user.token
  21. }
  22. console.log(options)
  23. },
  24. success(res) {
  25. console.log(res)
  26. if (res.data.code === 2000) {
  27. user.updateToken('')
  28. uni.showToast({
  29. icon: 'none',
  30. title: '令牌过期,请重新登录'
  31. })
  32. setTimeout(() => {
  33. uni.reLaunch({
  34. url: "/pages/login/login",
  35. })
  36. }, 1000)
  37. }
  38. }
  39. })
  40. },
  41. onShow: function() {
  42. console.log('App Show')
  43. },
  44. onHide: function() {
  45. console.log('App Hide')
  46. }
  47. }
  48. </script>
  49. <style>
  50. /*每个页面公共css */
  51. /* 设置全局背景颜色 */
  52. page {
  53. background-color: #f4f4f4;
  54. }
  55. </style>