main.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. $http.baseUrl = 'https://hometeacher2.natappvip.cc'
  12. uni.$wsLocation = 'wss://hometeacher2.natappvip.cc'
  13. // 封装弹窗的方法
  14. uni.$showMsg = function (title = '数据加载失败!', duration = 1500) {
  15. uni.showToast({
  16. title,
  17. duration,
  18. icon: 'none',
  19. })
  20. }
  21. // 请求拦截器
  22. $http.beforeRequest = function(options) {
  23. uni.showLoading({
  24. title: '数据加载中'
  25. })
  26. this.header = {
  27. token: uni.getStorageSync('token')
  28. }
  29. }
  30. // 响应拦截器
  31. $http.afterRequest = 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