
import { baseUrl } from './domain.js'


const install = (Vue, vm) => {
	Vue.prototype.$u.http.setConfig({
		baseUrl,
		loadingText: '加载中~',
		loadingTime: 800,
	});
  // 请求拦截,如果有token,携带token
  Vue.prototype.$u.http.interceptor.request = (config) => {
    const token = uni.getStorageSync('weapp_session_login_data');
    if(token){
      config.header['Authorization'] ='Bearer '+token.token;
    }
    return config
  }
  // 响应拦截，公共错误处理
  Vue.prototype.$u.http.interceptor.response = (res) => {
    if(res.code == 10000) {
      return res.data;
    }else if(res.code == 10003 || res.code == 20006){
      uni.hideToast();
      uni.showToast({
        icon:"none",
        title:"您的登录已失效，请重新登录",
        duration: 2000
      })
      uni.clearStorageSync();
      setTimeout(function () {
        uni.reLaunch({
          url: '/pages/login/index'
        });
      },2000);
      return false;
    }else{
      uni.hideLoading();
      uni.showToast({
        icon:"none",
        title:res.message,
        duration: 3000
      })
      return false;
    }
  }
}

export default {
	install
}