main.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import Vue from 'vue'
  2. import App from './App'
  3. import { config } from './config/config'
  4. import store from './store'
  5. import Request from '@/js_sdk/luch-request/luch-request/index.js';
  6. const http = new Request();
  7. http.interceptors.response.use((response) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
  8. if (response.data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
  9. uni.showToast({
  10. icon:'none',
  11. mask:false,
  12. title:`${response.data.message}`,
  13. duration: 2000
  14. });
  15. };
  16. if(response.data.code == 401){
  17. // console.log('response',response);
  18. // 是否产品 是的话登录后跳转产品页
  19. // console.log('location.href',location.href.split('#')[1]);
  20. // console.log('是否产品',location.href.split('#')[1].indexOf("/pages/product/product") != -1);
  21. if(location.href.split('#')[1].indexOf("/pages/product/product") != -1){
  22. let productID = location.href.split('#')[1].split('=')[1];
  23. // console.log('productID',productID)
  24. uni.setStorage({
  25. key: 'productID',
  26. data:productID
  27. });
  28. };
  29. // 是否产品 是的话登录后跳转产品页
  30. uni.showToast({
  31. title:response.data.message + " 即将跳转到登录页",
  32. icon:"none",
  33. duration:2000
  34. });
  35. setTimeout(i=>{
  36. uni.navigateTo({
  37. url: '/pages/login/login',
  38. fail:function(err){
  39. console.log(err)
  40. }
  41. });
  42. },2000);
  43. return Promise.reject(response)
  44. };
  45. // return Promise.reject(response) // return Promise.reject 可使promise状态进入catch
  46. if (response.config.custom.verification) { // 演示自定义参数的作用
  47. return response.data
  48. }
  49. // console.log(response)
  50. return response
  51. }, (response) => { /* 对响应错误做点什么 (statusCode !== 200)*/
  52. console.log(response);
  53. return Promise.reject(response)
  54. });
  55. import './utils/filter'
  56. Vue.config.productionTip = false
  57. //栏目标题设置
  58. const setNavbarTitle = (title,defaultTitle) => {
  59. uni.setNavigationBarTitle({
  60. title:title || defaultTitle
  61. })
  62. }
  63. //统一提示方便全局修改
  64. const msg = (title, duration=1500, mask=false, icon='none')=>{
  65. if(Boolean(title) === false){
  66. return;
  67. }
  68. uni.showToast({
  69. title,
  70. duration,
  71. mask,
  72. icon
  73. });
  74. }
  75. //封装全局登录检查函数:backpage为登录后返回的页面;backtype为打开页面的类型[1 : redirectTo 2 : switchTab]
  76. //3种页面跳转方式:NavigationTo(直接打开新页面),RedirectTo(覆盖原页面后打开新页面),SwitchTo(切换顶部导航的方式来切换页面)
  77. Vue.prototype.checkLogin = function(backpage, backtype){
  78. var TOKEN = uni.getStorageSync('token');//本地持久化存储
  79. var TOKENHEAD = uni.getStorageSync('tokenhead');
  80. var USERINFO = uni.getStorageSync('userInfo');
  81. if(TOKEN == '' || TOKENHEAD == '' || USERINFO == ''){
  82. uni.redirectTo({url:'/pages/login/login?backpage='+backpage+'&backtype='+backtype});
  83. return false;
  84. }
  85. return [TOKEN,TOKENHEAD,USERINFO];//已经登录返回数组TOKEN等用户信息
  86. }
  87. import $wxApi from "./wxapi.js";
  88. Vue.prototype.$wxApi = $wxApi;
  89. //微信支付封装
  90. import $pay from "./pay.js";
  91. Vue.prototype.$pay = $pay
  92. Vue.prototype.$api = {msg,http,setNavbarTitle}
  93. Vue.prototype.$getimg = config.imgUrl
  94. Vue.prototype.$placeImg = config.placeImg
  95. Vue.prototype.$store = store
  96. Vue.prototype.config = config
  97. App.mpType = 'app'
  98. const app = new Vue({
  99. ...App
  100. })
  101. app.$mount()