main.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // #ifndef VUE3
  2. import Vue from 'vue'
  3. import App from './App'
  4. // 导入 store 的实例对象
  5. import store from './store/store.js'
  6. // 按需导入 $http 对象
  7. import { $http } from '@escook/request-miniprogram'
  8. // 在 uni-app 项目中,可以把 $http 挂载到 uni 顶级对象之上,方便全局调用
  9. uni.$http = $http
  10. // 封装弹窗的方法
  11. uni.$showMsg = function (title = '数据加载失败!', duration = 1500) {
  12. uni.showToast({
  13. title,
  14. duration,
  15. icon: 'none',
  16. })
  17. }
  18. // 请求拦截器
  19. $http.beforeRequest = function(options) {
  20. uni.showLoading({
  21. title: '数据加载中'
  22. })
  23. this.header = {
  24. token: uni.getStorageSync('token')
  25. }
  26. }
  27. // 请求的根路径
  28. //$http.baseUrl = 'http://192.168.0.207:8222'
  29. $http.baseUrl = 'http://localhost:8222'
  30. // 响应拦截器
  31. $http.afterRequest = function(res) {
  32. uni.hideLoading()
  33. if (!res.data.success) {
  34. uni.$showMsg(res.data.message)
  35. if (res.data.message === '登录过期,请重新登录') {
  36. uni.setStorageSync('token', '')
  37. uni.switchTab({
  38. url: '/pages/my/my'
  39. })
  40. }
  41. }
  42. }
  43. Vue.config.productionTip = false
  44. App.mpType = 'app'
  45. const app = new Vue({
  46. ...App,
  47. // 2. 将 store 挂载到 Vue 实例上
  48. store
  49. })
  50. app.$mount()
  51. // #endif
  52. // #ifdef VUE3
  53. import { createSSRApp } from 'vue'
  54. import App from './App.vue'
  55. export function createApp() {
  56. const app = createSSRApp(App)
  57. return {
  58. app
  59. }
  60. }
  61. // #endif