export default {
	install(Vue, options) {
		Vue.prototype.host = "https://wx.weibaoge.cn/web/index.php?_mall_id=1285"
		// Vue.prototype.host2 = "http://192.168.0.110:8200"
		 // Vue.prototype.host2 = "https://mallApi.oytour.com"	
		// Vue.prototype.host2 = "http://192.168.1.5:8088"
		Vue.prototype.host2 = "http://192.168.20.17:8014"
		//Vue.prototype.host2 = "http://192.168.20.13:8088"
		// Vue.prototype.host2 = "http://testmall.oytour.com/"

		Vue.prototype.request = function(param, success, failed) {
			//网络请求
			uni.request({
				url: this.host + "&r=" + param.data.r,
				method: param.method || "GET",
				header: param.header || {
					'content-type': "application/json"
				},
				data: param.data,
				success: res => {
					if (res.data.code != 0 && res.data.msg != "商品未上架") {}
					typeof success == "function" && success(res.data);
				},
				fail: (e) => {
					console.log("网络请求fail:" + JSON.stringify(e));
					uni.showModal({
						content: "" + res.errMsg
					});
					typeof failed == "function" && failed(res.data);
				}
			});
		}
		Vue.prototype.request2 = function(param, success, failed) {
			//网络请求
			uni.request({
				url: this.host2 + param.url,
				method: param.method || "Post",
				header: param.header || {
					'content-type': "application/json"
				},
				data: {
					MallBaseId: this.GetMiniAppId().MallBaseId,
					TenantId: this.GetMiniAppId().TenantId,
					OpenId: this.GetOpenId().OpenId,
					UserId: this.GetOpenId().UserId,
					SmallShopsId: this.GetOpenId().SmallShopsId,
					MiniAppId: this.GetMiniAppId().AppId,
					msg: param.data,
					token: this.GetOpenId().token
				},
				success: res => {
					if (res.data.resultCode == 10000) {
						let u = uni.getStorageSync("mall_UserInfo");
						if (u) {
							this.getLogin_t(u)
							typeof failed == "function" && failed(res.data);
						}
					} else if (res.data.resultCode != 1) {
						if (res.data.message != '') {
							uni.showToast({
								title: res.data.message,
								icon: "none",
							});
						}
						typeof failed == "function" && failed(res.data);
					} else {
						typeof success == "function" && success(res.data);
					}
				},
				fail: (e) => {
					console.log("网络请求fail:" + JSON.stringify(e));
					uni.showModal({
						content: "" + res.errMsg
					});
					typeof failed == "function" && failed(res.data);
				}
			});
		}
		// token失效再次登录接口
		Vue.prototype.getLogin_t = function(obj) {
			let pid = uni.getStorageSync("pid") ? uni.getStorageSync("pid").pid : 0;
			let Up = uni.getStorageSync("Up") ? uni.getStorageSync("Up").Up : 0;
			let SmallShopId = uni.getStorageSync("SmallShopId") ? uni.getStorageSync("SmallShopId").SmallShopId : 0;
			let CounponPassword = uni.getStorageSync("CounponPassword") ? uni.getStorageSync("CounponPassword").CounponPassword :
				0;
			let KeyWord = uni.getStorageSync("KeyWord") ? uni.getStorageSync("KeyWord").KeyWord : '';
			this.request2({
					url: '/api/AppletLogin/Login',
					data: {
						Source: 1,
						OpenId: obj.OpenId,
						Name: obj.Name,
						Photo: obj.Photo,
						Moblie: '',
						SuperiorId: pid,
						SmallShopId: SmallShopId,
						UserPageType: Up,
						CounponPassword: CounponPassword,
						KeyWord: KeyWord,
					}
				},
				res => {
					if (res.resultCode == 1) {
						uni.setStorageSync('mall_UserInfo', res.data);
						uni.removeStorageSync('pid');
						uni.removeStorageSync('SmallShopId');
						uni.removeStorageSync("Up");
						uni.removeStorageSync("CounponPassword");
						uni.removeStorageSync("KeyWord");
					}
				}
			);
		}
		// 获取小程序APPID
		Vue.prototype.GetMiniAppId = function() {
			let appObj = {};
			let appType = 2;
			switch (appType) {
				case 1: //赞羊
					appObj = {
						AppId: 'wxacd9f8cc3480d29e',
						TenantId: 1,
						MallBaseId: 1,
					};
					break;
				case 2: //旅小友
					appObj = {
						AppId: 'wxcf0727a7c78b501e',
						TenantId: 12,
						MallBaseId: 2,
					};
					break;
				case 3:
					appObj = {
						AppId: 'wx01350b305d45a63e',
						TenantId: 14,
						MallBaseId: 4,
					};
					break;
				case 4:
					appObj = { //郍田
						AppId: 'wx1bf3a7c76b10bb6d',
						TenantId: 16,
						MallBaseId: 6,
					};
					break;
				case 5:
					appObj = { //惠爱
						AppId: 'wxcab2515c608644b9',
						TenantId: 17,
						MallBaseId: 7,
					};
					break;
				case 6:
					appObj = { //甲鹤
						AppId: 'wx38e054ee42b054f4',
						TenantId: 15,
						MallBaseId: 5,
					};
					break;
				case 7:
					appObj = {//韩国馆
						AppId: 'wxbb033190e0ffa5db',
						TenantId: 18,
						MallBaseId: 8,
					};
					break;
				case 8:
					appObj = {//恋爱星球
						AppId: 'wx681f8ad58e6c48e4',
						TenantId: 21,
						MallBaseId: 11,
					};
					break;
				case 9:
					appObj = {//罗演的杂货铺
						AppId: 'wx67a8fb290ec58af4',
						TenantId: 20,
						MallBaseId: 10,
					};
					break;
			}
			return appObj;
		}
		//获取OpenId
		Vue.prototype.GetOpenId = function() {
			let mall_userinfo = wx.getStorageSync('mall_UserInfo')
			var obj = {
				OpenId: '',
				UserId: 0,
				token: '',
				SmallShopsId: 0,
			}
			if (mall_userinfo) {
				if (mall_userinfo.OpenId) {
					obj.OpenId = mall_userinfo.OpenId;
					obj.UserId = mall_userinfo.UserId;
					obj.token = mall_userinfo.Token;
					obj.SmallShopsId = mall_userinfo.UserSmallShopId;
				}
			}
			return obj
		}
		//公用判断图片地址 判断是否包含http
		Vue.prototype.getIconLink = function(url) {
			let str = ''
			if (url && url != '') {
				if (url.indexOf('http') != -1) {
					str = url
				} else {
					str = 'https://viitto-1301420277.cos.ap-chengdu.myqcloud.com' + url;
				}
				return str;
			}
		}
		//价格返回.00
		Vue.prototype.getPrice = function(val) {
			val = Number(val).toFixed(2);
			return val
		}

		Vue.prototype.apiheader = function() {
			return {
				'X-Access-Token': '_4Y_WpUZ4a6SI5uJgsZ4Lb7t9mvqJTyr',
				'X-App-Platform': 'wxapp',
				'X-App-Version': '4.2.47',
				'X-Form-Id-List': '[]',
				'X-Requested-With': 'XMLHttpRequest',
				'X-User-Id': '19992',
				'content-type': "application/json"
			}
		}
	}
}
