import App from './App'
import store from './store'

import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'

// 引入全局TuniaoUI
import TuniaoUI from 'tuniao-ui'
Vue.use(TuniaoUI)

// 引入TuniaoUI提供的vuex简写方法
let vuexStore = require('@/store/$tn.mixin.js')
Vue.mixin(vuexStore)

// 引入TuniaoUI对小程序分享的mixin封装
let mpShare = require('tuniao-ui/libs/mixin/mpShare.js')
Vue.mixin(mpShare)

const app = new Vue({
  store,
  ...App
})

const api = {
	getToken() {
		return uni.getStorageSync("access_token");
	},
	setToken: function(token) {
		uni.setStorageSync("access_token", token);
	},
	getUser() {
		return JSON.parse(uni.getStorageSync("user"));
	},
	setUser: function(user) {
		uni.setStorageSync("user", JSON.stringify(user));
	},
	isLogin: function() {
		return uni.getStorageSync("access_token") ? true : false
	},
	getUserPermission:function(){
		let user =api.getUser();
		if(user){
			return user.applet_permission;
		}
		uni.removeStorageSync("access_token")
		uni.removeStorageSync("user")
		// // #ifdef APP-PLUS || MP
		// 	this.deleteJYJPushAlias();
		// // #endif
		let url = '/pages/start/start?msg=' + (res.data.msg == undefined ? '' : res.data.msg)
		uni.navigateTo({
		  url: url
		})
		return 0;
	},
	apiHost: function() {
		//接口地址
		// return 'http://192.168.31.75/'
		return 'https://ai.my-123.cn/api/'
	},
	
	request: function(url, params, method,type,hideLoading) {
		//接口请求
		if (!hideLoading) {
			uni.showLoading({
				mask: true,
				title: '请稍候...'
			})
		}
		if(method==null||method==""){
			method="POST"
		}
		if(type==null||type==""){
			type='application/json'
		}
		return new Promise((resolve, reject) => {
			uni.request({
				url: this.apiHost() + url,
				data: params,
				header: {
					'Authorization':'Basic d3hBcHBsZXQ6d3hBcHBsZXRfc2VjcmV0',
					'tenant_id':'000000',
					'Blade-Auth': "bearer "+this.getToken(),
					'content-type': type,
				},
				method: method, 
				// dataType: 'json',
				success: (res) => {
					!hideLoading && uni.hideLoading()
					if (res.data.code == 200) { //访问成功
						resolve(res.data.data)
					}else if (res.data.code == 401) {
						uni.removeStorageSync("access_token")
						uni.removeStorageSync("user")
						// // #ifdef APP-PLUS || MP
						// 	this.deleteJYJPushAlias();
						// // #endif
						let url = '/pages/start/start?msg=' + (res.data.msg == undefined ? '' : res.data.msg)
						uni.navigateTo({
						  url: url
						})
					}else {
						this.toast( res.data.msg == null ? "请求失败 , 请重试" : res.data.msg);
						reject(res)
					}
				},
				fail: (res) => {
					if (!hideLoading) {
						this.toast("网络不给力，请稍后再试~")
					}
					reject(res)
				}
			})
		})
	},
	toast: function(text, duration, success) {
		uni.showToast({
			title: text,
			icon: success ? 'success' : 'none',
			duration: duration || 2000
		})
	},
	formatTime:function(date, state) {
	  if (!date) {
	    date = new Date()  
	  }else{
		  date = new Date(date)  
	  }
	  let year = date.getFullYear()
	  let month = date.getMonth() + 1
	  let day = date.getDate()
	  // const week = weekFormat(date.getDay())
	  let hour = date.getHours()
	  let minute = date.getMinutes()
	  let second = date.getSeconds()
	  let milliSecond = date.getMilliseconds()
	  if(month<10){
		  month="0"+month;
	  }
	  if(day<10){
		  day="0"+day;
	  }
	  if(hour<10){
		  hour="0"+hour;
	  }
	  if(minute<10){
		  minute="0"+minute;
	  }
	  if(second<10){
		  second="0"+second;
	  }
	  if (state == 'Y') {
	    return year
	  }
	  if (state == 'YM') {
	    return [year, month].join('-')
	  }
	  if (state == 'YMD') {
	    return [year, month, day].join('-')
	  }
	  if (state == 'YMDHM') {
	    return [year, month, day].join('-') + ' ' + [hour, minute].join(':')
	  }
	  if (state == 'HM') {
	    return [hour, minute].join(':')
	  }
	  // if (state == 'MWHM') {
	  //   return [month, day].map(formatNumber).join('-') + ' ' + week + ' ' + [hour, minute].map(formatNumber).join(':')
	  // }
	  return [year, month, day].join('-')
	},
}
// // 引入请求封装
// require('./util/request/index')(app)

Vue.prototype.api = api

app.$mount()