import Vue from 'vue'
import App from './App'
import { config } from './config/config'
import store from './store' 
import Request from '@/js_sdk/luch-request/luch-request/index.js';
const http = new Request();
http.interceptors.response.use((response) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
   if (response.data.code !== 200) { // 服务端返回的状态码不等于200，则reject()
	   uni.showToast({
		icon:'none',
		mask:false,
		title:`${response.data.message}`,
		duration: 2000
	   });
	 };
	 if(response.data.code == 401){
		// console.log('response',response);
		// 是否产品 是的话登录后跳转产品页
		// console.log('location.href',location.href.split('#')[1]);
		// console.log('是否产品',location.href.split('#')[1].indexOf("/pages/product/product") != -1);
		if(location.href.split('#')[1].indexOf("/pages/product/product") != -1){
			let productID = location.href.split('#')[1].split('=')[1];
			// console.log('productID',productID)
			uni.setStorage({
				key: 'productID',
				data:productID
			});
		};
		// 是否产品 是的话登录后跳转产品页
		uni.showToast({
		 	title:response.data.message + "    即将跳转到登录页",
		 	icon:"none",
		 	duration:2000
		});
		setTimeout(i=>{
			uni.navigateTo({
				url: '/pages/login/login',
				fail:function(err){
					console.log(err)
				}
			});
		},2000);
		return Promise.reject(response) 
		
	 };
     // return Promise.reject(response) // return Promise.reject 可使promise状态进入catch
 if (response.config.custom.verification) { // 演示自定义参数的作用
    return response.data
  }
  // console.log(response)
  return response
}, (response) => { /*  对响应错误做点什么 （statusCode !== 200）*/
  console.log(response);
  return Promise.reject(response)
});

import './utils/filter' 

Vue.config.productionTip = false

//栏目标题设置
const setNavbarTitle = (title,defaultTitle) => {
	uni.setNavigationBarTitle({
		title:title || defaultTitle
	})
}

//统一提示方便全局修改
const msg = (title, duration=1500, mask=false, icon='none')=>{
	if(Boolean(title) === false){
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}

//封装全局登录检查函数:backpage为登录后返回的页面；backtype为打开页面的类型[1 : redirectTo 2 : switchTab]
//3种页面跳转方式：NavigationTo(直接打开新页面),RedirectTo(覆盖原页面后打开新页面),SwitchTo(切换顶部导航的方式来切换页面)
Vue.prototype.checkLogin = function(backpage, backtype){
    var TOKEN  = uni.getStorageSync('token');//本地持久化存储
	var TOKENHEAD = uni.getStorageSync('tokenhead');
	var USERINFO = uni.getStorageSync('userInfo');
    if(TOKEN == '' || TOKENHEAD == '' || USERINFO == ''){
        uni.redirectTo({url:'/pages/login/login?backpage='+backpage+'&backtype='+backtype});
        return false;
    }
    return [TOKEN,TOKENHEAD,USERINFO];//已经登录返回数组TOKEN等用户信息
}

import $wxApi from "./wxapi.js";
Vue.prototype.$wxApi = $wxApi;

//微信支付封装
import $pay from "./pay.js";
Vue.prototype.$pay = $pay

Vue.prototype.$api = {msg,http,setNavbarTitle}
Vue.prototype.$getimg = config.imgUrl
Vue.prototype.$placeImg = config.placeImg
Vue.prototype.$store = store
Vue.prototype.config = config
App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()
