import Vue from 'vue'
import store from './store'
import App from './App'
import util from './js/util.js'
import until from './js/until.js'
import bindid from './js/bindid.js'
import uView from '@/uni_modules/uview-ui'
import homeApi from '@/api/home/index.js'
import paycenter from '@/api/paycenter/paycenter.js'
// 引入 tob-less
import setupTobLess from '@/uni_modules/tob-less/index.js'
import siteinfo from "@/siteinfo.js"
import author from "@/author.js"
import sharebox from "@/components/sharebox/sharebox.vue"
// #ifdef MP-WEIXIN
let mpShare = require('@/uni_modules/uview-ui/libs/mixin/mpShare.js');
// #endif
App.mpType = 'app'

Vue.component('sharebox', sharebox)
const showToast = (title, duration = 2000, mask = false, icon = 'none') => {
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}
/**

 *  因工具函数属于公司资产, 所以直接在Vue实例挂载几个常用的函数
 *  所有测试用数据均存放于根目录json.js
 *  
 *  css部分使用了App.vue下的全局样式和iconfont图标，有需要图标库的可以留言。
 *  示例使用了uni.scss下的变量, 除变量外已尽量移除特有语法,可直接替换为其他预处理器使用
 */
const msg = (title, duration = 2000, mask = false, icon = 'none') => {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}
const json = type => {}

const prePage = () => {
	let pages = getCurrentPages();
	let prePage = pages[pages.length - 2];
	// #ifdef H5
	return prePage;
	// #endif
	if (prePage && prePage.$vm) {
		return prePage.$vm;
	} else {
		return null;
	}
}
Vue.config.productionTip = false
Vue.prototype.$store = store;
//获取登录用户ID
Vue.prototype.$getuid = () => {
	let userInfo = uni.getStorageSync('userInfo');
	if (userInfo) {
		return userInfo.id;
	} else {
		return 0;
	}
}

// #ifdef H5
const getUrlParms = name => {
	const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`)
	const r = window.location.search.substr(1).match(reg)
	console.log(r,'r');
	
	if (r !== null)
	
		return unescape(r[2])
	return null
}
Vue.prototype.$getUrlParms = getUrlParms //全局配置
// #endif
//判断是否是微信浏览器的函数
Vue.prototype.isWeiXin = function() {
	// #ifdef H5
	var ua = window.navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == 'micromessenger') {
		return true;
	} else {
		// 普通浏览器
		return false;
	}
	// #endif
	return false;
}
//广告时间
Vue.prototype.$adtime = {
	mvideo_time: 0,
	pscreen_time: 0,
	banner_time: 0
};
Vue.prototype.$paycenter = paycenter;


Vue.prototype.$primarys = {
	"A": "#ff8903",
	"B": "#e52340",
	"C": "#ffd100",
	"D": "#4B086D",
	"E": "#014872",
	"F": "#067d68",
	"G": "#56AB2F",

}
Vue.prototype.$api = {
	msg,
	json,
	prePage
};
Vue.prototype.$util = util;
Vue.prototype.$until = until;
Vue.prototype.$bindid = bindid;
Vue.prototype.$Toast = showToast;
Vue.prototype.$siteinfo = siteinfo
Vue.prototype.$author = author
Vue.prototype.$siteinfo.root_img = siteinfo.siteroot;
Vue.prototype.$siteinfo.root_rich_img = siteinfo.siteroot;
if (process.env.NODE_ENV === 'development') {
	Vue.prototype.$siteinfo.h5_siteroot = siteinfo.siteroot;
} else {
	//生产环境
	// #ifdef H5
	let httpurl = ''
	if (location.href.indexOf('https') != -1) {
		httpurl = `https://${window.location.host}`
	} else {
		httpurl = `http://${window.location.host}` //全局配置
	}
	Vue.prototype.$siteinfo.h5_siteroot = httpurl
	Vue.prototype.$siteinfo.root_rich_img = httpurl;
	// #endif
}
if (Vue.prototype.$author.we7_v2 == 1) {
	// #ifdef H5
	Vue.prototype.$siteinfo.root_img = Vue.prototype.$siteinfo.h5_siteroot + "/addons/yq_breedingmall/public"
	// #endif
	// #ifndef H5
	Vue.prototype.$siteinfo.root_img = Vue.prototype.$siteinfo.root_img + "/addons/yq_breedingmall/public"
	// #endif
}
Vue.prototype.$config = {
	app: {
		position: 0
	}
} //全局配置
Vue.prototype.platform = 0
Vue.prototype.$uuid = 0
// #ifdef H5
Vue.config.ignoredElements = [...Vue.config.ignoredElements, 'wx-open-launch-weapp']
// #endif
const init_data = (data, fun) => {
	Vue.prototype.$config = data;
	if (fun) {
		fun(data);
	}
}
Vue.prototype.$init_data = init_data
Vue.prototype.$cache_config_info_key = 'config_info_key';

const getprimary = () => {
	let config = until.myCache(Vue.prototype.$cache_config_info_key);
	if (config) {
		return config.primary
	}
	return '#56AB2F'
}
Vue.prototype.$getprimary = getprimary
Vue.prototype.$theme = {
	primary: getprimary()
}

const init_config = (rtype = 2, fun = null) => {
	let config = until.myCache(Vue.prototype.$cache_config_info_key);
	if (rtype == 1) {
		until.myCache(Vue.prototype.$cache_config_info_key, null)
		config = null;
	}
	if (config) {
		Vue.prototype.$init_data(config, fun);
	} else {
		homeApi.home().then(res => {
			if (res.status == 200) {
				// #ifdef MP-WEIXIN
				uni.$u.mpShare = {
					title: res.data.sharedata.title, // 默认为小程序名称，可自定义
					path: '', // 默认为当前页面路径，一般无需修改，QQ小程序不支持
					// 分享图标，路径可以是本地文件路径、代码包文件路径或者网络图片路径。
					// 支持PNG及JPG，默认为当前页面的截图
					imageUrl: res.data.sharedata.imageUrl
				}
				// #endif
				res.data['primary'] = Vue.prototype.$primarys[res.data.initAppTheme]
				Vue.prototype.$theme.primary = res.data['primary']
				until.myCache(Vue.prototype.$cache_config_info_key, res.data, 60 * 10);
				setupTobLess(Vue, {
					initAppTheme: res.data.initAppTheme
				})
				Vue.prototype.$init_data(res.data, fun);
			} else {
				uni.showToast({
					icon: 'none',
					title: res.data
				})
			}
		})
	}
}
Vue.prototype.$init_config = init_config

const start = () => {
	setupTobLess(Vue, {
		initAppTheme: "G"
	})
	// #ifdef H5
	Vue.prototype.$uuid = Vue.prototype.$getUrlParms("uuid")
	console.log(Vue.prototype.$getUrlParms("pid"),'Vue.prototype.$getUrlParms("pid")');
	Vue.prototype.$bindid.getbindid({
		pid: Vue.prototype.$getUrlParms("pid")
	}, Vue.prototype.$store);
	// #endif
	Vue.prototype.$init_config(1);
	uni.setStorageSync('tabbar', null)
	const res = uni.getSystemInfoSync();
	if (res.platform == 'android') {
		Vue.prototype.platform = 1
	} else if (res.platform == 'ios') {
		Vue.prototype.platform = 2
	}
	//全局配置
	// #ifdef MP-WEIXIN
	Vue.mixin(mpShare)
	// #endif
	Vue.use(uView);
	const app = new Vue({
		...App
	})
	app.$mount()
}
start();
