main.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // 响应拦截器
  30. $http.afterRequest = function(res) {
  31. uni.hideLoading()
  32. if (!res.data.success) {
  33. uni.$showMsg(res.data.message)
  34. if (res.data.message === '登录过期,请重新登录') {
  35. uni.setStorageSync('token', '')
  36. uni.reLaunch({
  37. url: '/pages/my/my.vue'
  38. })
  39. }
  40. }
  41. }
  42. Vue.config.productionTip = false
  43. App.mpType = 'app'
  44. const app = new Vue({
  45. ...App,
  46. // 2. 将 store 挂载到 Vue 实例上
  47. store
  48. })
  49. app.$mount()
  50. // #endif
  51. // #ifdef VUE3
  52. import { createSSRApp } from 'vue'
  53. import App from './App.vue'
  54. export function createApp() {
  55. const app = createSSRApp(App)
  56. return {
  57. app
  58. }
  59. }
  60. // #endif