main.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 = 'http://192.168.0.128:8222'
  12. // uni.$wsLocation = 'ws://192.168.0.128:8222'
  13. $http.baseUrl = 'https://hometeacher2.natappvip.cc'
  14. uni.$wsLocation = 'wss://hometeacher2.natappvip.cc'
  15. // 封装弹窗的方法
  16. uni.$showMsg = function (title = '数据加载失败!', duration = 1500) {
  17. uni.showToast({
  18. title,
  19. duration,
  20. icon: 'none',
  21. })
  22. }
  23. // 请求拦截器
  24. $http.beforeRequest = function(options) {
  25. uni.showLoading({
  26. title: '数据加载中'
  27. })
  28. this.header = {
  29. token: uni.getStorageSync('token')
  30. }
  31. }
  32. // 响应拦截器
  33. $http.afterRequest = res => {
  34. uni.hideLoading()
  35. if (!res.data.success) {
  36. uni.$showMsg(res.data.message)
  37. if (res.data.message === '登录过期,请重新登录') {
  38. uni.setStorageSync('token', '')
  39. setTimeout(() => {
  40. uni.redirectTo({
  41. url:'/subpkg/my/user/my_user_login'
  42. })
  43. }, 500);
  44. }
  45. }
  46. }
  47. Vue.config.productionTip = false
  48. App.mpType = 'app'
  49. const app = new Vue({
  50. ...App,
  51. // 2. 将 store 挂载到 Vue 实例上
  52. store
  53. })
  54. app.$mount()
  55. // #endif
  56. // #ifdef VUE3
  57. import { createSSRApp } from 'vue'
  58. import App from './App.vue'
  59. export function createApp() {
  60. const app = createSSRApp(App)
  61. return {
  62. app
  63. }
  64. }
  65. // #endif