import $http from '@/config/requestConfig'
import store from '@/store';
import base from '@/config/baseUrl';
// #ifdef H5
import {
	getLatLonH5,
	publicShareFun,
	wxPublicPay,
	getBrowser,
	appMutual
} from '@/config/html5Utils';
// 公众号分享
export const publicShare = publicShareFun;
// #endif
// #ifdef APP-PLUS
import appShareFun, {
	closeShare
} from '@/uni_modules/zhouWei-APPshare/js_sdk/appShare';
// APP分享
export const appShare = function(data, callbcak) {
	let userInfo = store.state.userInfo;
	if (!(userInfo && userInfo.uid)) {
		userInfo = uni.getStorageSync("userInfo");
	}
	let shareData = {
		shareTitle: data.shareTitle || base.share.title,
		shareUrl: data.shareUrl || base.share.link,
		shareContent: data.shareContent || base.share.desc,
		shareImg: data.shareImg || base.share.imgUrl,
	}
	if (userInfo && userInfo.uid) {
		if (data.shareUrl) {
			if (data.shareUrl.indexOf("?") >= 0) {
				shareData.shareUrl = data.shareUrl + "&recommend=" + userInfo.uid
			} else {
				shareData.shareUrl = data.shareUrl + "?recommend=" + userInfo.uid
			}
		} else if (base.share && base.share.link) {
			if (base.share.link.indexOf("?") >= 0) {
				shareData.shareUrl = base.share.link + "&recommend=" + userInfo.uid
			} else {
				shareData.shareUrl = base.share.link + "?recommend=" + userInfo.uid
			}
		}
	}
	return appShareFun({
		shareTitle: data.shareTitle || base.share.title,
		shareUrl: data.shareUrl || base.share.link,
		shareContent: data.shareContent || base.share.desc,
		shareImg: data.shareImg || base.share.imgUrl,
	}, callbcak);
};
export const closeAppShare = closeShare;
// #endif

// #ifdef MP-WEIXIN
// 微信小程序分享
export const wxShare = function(data = {}) {
	let shareInfo = {
		title: data.title || base.share.title,
	};
	if (data.path && typeof(data.path) == "string") {
		shareInfo.path = data.path;
	} else if (data.path != 1) {
		shareInfo.path = "pages/home/index";
	}
	if (data.imageUrl) {
		shareInfo.imageUrl = data.imageUrl;
	}
	let userInfo = store.state.userInfo;
	if (!(userInfo && userInfo.uid)) {
		userInfo = uni.getStorageSync("userInfo");
	}
	if (userInfo && userInfo.uid) {
		if (data.query && typeof(data.query) == "object") {
			data.query.recommendCode = userInfo.uid;
		} else {
			data.query = {
				recommendCode: userInfo.uid
			};
		}
	}
	if (data.query && typeof(data.query) == "object") {
		Object.keys(data.query).forEach((key, index) => {
			if (index > 0 && shareInfo.query) {
				shareInfo.query += "&" + key + "=" + data.query[key];
				shareInfo.path += "&" + key + "=" + data.query[key];
			} else {
				shareInfo.query = key + "=" + data.query[key];
				shareInfo.path += "?" + key + "=" + data.query[key];
			}
		});
	}
	return shareInfo;
}
// #endif

//支付（APP微信支付、APP支付宝支付、微信小程序支付）
export const setPay = function(payInfo, callback) {
	console.log(payInfo)
	payInfo.customerId = store.state.userInfo.id
	let httpUrl = "";
	if (payInfo.typePay == 'wxpay') {
		// APP微信支付
		httpUrl = '/v3/pay/placeAnOrder/app'
	} else if (payInfo.typePay == 'alipay') {
		// APP支付宝支付
		httpUrl = 'pay/aliPay/pay'
	} else if (payInfo.typePay == 'smallPay') {
		// 微信小程序支付
		httpUrl = 'api/pay/v1/small_pay_sign_wx'
	} else if (payInfo.typePay == 'appleiap') {
		// 苹果支付
	}
	$http.post(httpUrl, payInfo).then(data => {
		console.log(data, 'data')
		let payData = {
			success: function(res) {
				callback && callback({
					success: true,
					data: res
				});
				console.log('success:' + JSON.stringify(res));
			},
			fail: function(err) {

				callback && callback({
					success: false,
					data: err
				});

				console.log('fail:' + JSON.stringify(err));
			}
		};
		if (payInfo.typePay == 'smallPay') {
			// 小程序
			payData.provider = 'wxpay';
			payData.timeStamp = data.timeStamp;
			payData.nonceStr = data.nonceStr;
			payData.package = data.package;
			// payData.package = "prepay_id=" + data.prepayid;
			payData.signType = "MD5";
			payData.paySign = data.sign;
		} else if (payInfo.typePay == 'wxpay') {
			// app微信
			payData.provider = 'wxpay';
			payData.orderInfo = data;
		} else if (payInfo.typePay == 'alipay') {
			// app 支付宝
			payData.provider = 'alipay';
			payData.orderInfo = data.orderInfo;
		} else if (payInfo.typePay == 'baidu') {
			payData.provider = 'baidu';
			payData.orderInfo = data;
		}

		if (payData.orderInfo != '' && payData.orderInfo != undefined) {
			console.log("支付参数", payData);
			// let EnvUtils = plus.android.importClass('com.alipay.sdk.app.EnvUtils');
			// EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);			
			uni.requestPayment(payData);
		}

	}, err => {
		callback && callback({
			success: false,
			data: err
		});
	});
}

// 支付统一分配
export const setPayAssign = function(orderInfo, callback) {
	orderInfo.price = orderInfo.price || orderInfo.pricePay;
	orderInfo.title = orderInfo.title || orderInfo.orderTitle;
	//支付
	// #ifdef APP-PLUS
	uni.navigateTo({
		url: '/pages/home/weChatPay?orderNo=' + orderInfo.orderNo + '&price=' + orderInfo.price +
			'&title=' + orderInfo.title
	});
	// #endif 
	// #ifdef MP-WEIXIN
	setPay({
		...orderInfo,
		type: "smallPay"
	}, res => {
		if (res.success) {
			uni.redirectTo({
				url: "/pages/shopCar/paySuccess?orderNo=" + orderInfo.orderNo
			});
		}
	});
	// #endif
	// #ifdef H5
	if (getBrowser() === '微信') {
		wxPublicPay({
			orderNo: orderInfo.orderNo
		}, function() {
			uni.redirectTo({
				url: "/pages/shopCar/paySuccess?orderNo=" + orderInfo.orderNo
			});
		});
	} else {
		appMutual('setJumpPay', orderInfo);
	}
	// #endif
}
// 获取地址信息 （微信小程序、APP、公众号）
export const getLatLon = function(tip) {
	return new Promise((resolve, reject) => {
		const successProcess = function(res) {
			store.commit("setCurrentAddress", {
				latitude: res.latitude,
				longitude: res.longitude
			});
			resolve(res);
		};
		const errProcess = function(err) {
			reject(err);
			if (tip) {
				uni.showToast({
					title: err,
					icon: "none"
				});
			}
		};
		// #ifdef H5
		getLatLonH5(successProcess, errProcess);
		// #endif
	});
}

// 查看是否有某本书的权限
export const checkBookRight = function(data, callback) {
	console.log('接受的值', data)
	$http.request({
		url: "/book/user/checkUserBook",
		method: "POST", // POST、GET、PUT、DELETE，具体说明查看官方文档
		data: data,
		header: { //默认 无 说明：请求头1
			'Content-Type': 'application/json'
		},
	}).then(res => {
		// console.log(res,'checkBookRight')			
		if (res.code === 0) {
			callback && callback({
				success: true,
				data: '有权限'
			});
		}
	}).catch(e => {
		callback && callback({
			success: false,
			data: '无权限'
		});
	})
}

// 单独微信支付
export const setWXPay = function(payInfo, callback) {
	$http.request({
		url: "pay/placeAnOrder/shoppingPay",
		method: "POST", // POST、GET、PUT、DELETE，具体说明查看官方文档
		data: {
			...payInfo,
			appName: 'thyy'
		},
		header: { //默认 无 说明：请求头1
			'Content-Type': 'application/json'
		},
	}).then(res => {
		console.log(res, 'resshoppingPay')
		if (res.code === 0) {
			console.log('接下来走微信接口啦')
			let payData = {
				provider: 'wxpay',
				orderInfo: {
					"appid": res.paramMap.appid, // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
					"noncestr": res.Map.noncestr, // 随机字符串
					"package": res.Map.package, // 固定值
					"partnerid": res.paramMap.mchid, // 微信支付商户号
					"prepayid": res.Map.prepayid, // 统一下单订单号
					"timestamp": res.Map.timestamp, // 时间戳（单位：秒）
					"signType": "SHA256-RSA",
					"sign": res.Map.sign
				},
				success(res) {
					callback && callback({
						success: true,
						data: res
					});
					console.log('success:' + JSON.stringify(res));
				},
				fail(err) {
					callback && callback({
						success: false,
						data: err
					});
					console.log('微信错误fail:' + JSON.stringify(err));

				}
			}
			// console.log(payData)
			if (payData.orderInfo.prepayid != '' && payData.orderInfo.prepayid != undefined) {
				uni.requestPayment(payData);
			}
		}
	})
}