import { getWxUserInfo, getWxConfig } from '../api/login'
export default {
	install(Vue) {
		Vue.prototype.getOpenid = async function () {
			if (localStorage.getItem("courierOpenid") != 'undefined' && localStorage.getItem("openid") != null)
				return;
			var code = this.getUrlParam("code");
			if (code == 'undefined' || code == null || code == "") {
				this.urlredirect();
				return;
			}
			const res = await getWxUserInfo({ code: code, userId: localStorage.getItem("courierUserId") })
			console.log(res, '================================getWxUserInfo')
			if (res.code == '0') {
				console.log('直接走+======', res)
				localStorage.setItem("courierOpenid", res.openId);
				localStorage.setItem("headimgurl", res.headimgurl);
			} else {
				this.urlredirect();
			}
		}

		Vue.prototype.urlredirect = async function () {
			const res = await getWxConfig({})
			const href = window.location.href
			window.location.href =
				`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${res.wxConfig.appId}&redirect_uri=${encodeURIComponent(href)}` + `&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect`;
		}
		Vue.prototype.getUrlParam = function (name) {
			var vars = [],
				hash;
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
			for (var i = 0; i < hashes.length; i++) {
				hash = hashes[i].split('=');
				vars.push(hash[0]);
				vars[hash[0]] = hash[1];
			}
			return decodeURIComponent(vars[name]);
		}

		// Vue.prototype.getUser = function () {
		// 	this.$http.post('https://tianzong.info666.com/tianzong' + "/personal/getSysUserInfoByUserId.do", { userId: this.userId }, { emulateJSON: true })
		// 		.then(res => {
		// 			//发送成功
		// 			if (res.body.msg == 'success') {
		// 				this.user = res.body.sysUserInfo;

		// 				console.log(res.body.sysUserInfo.id, 'id')
		// 			}
		// 		}, res => {
		// 			//发送失败
		// 		})
		// }
		//快递公司列表
		Vue.prototype.getLogisticsList = function () {
			this.$http.post(this.$store.state.host + "/sendMail/getLogisticsList.do", {}, { emulateJSON: true })
				.then(res => {
					//发送成功
					if (res.body.msg == 'success') {
						this.columns = res.body.list
						for (var i = 0; i < this.columns.length; i++) {
							this.columns[i].text = this.columns[i].expressCompany
						}
					}
				}, res => {
					//发送失败
				})
		}
		//成本中心列表
		Vue.prototype.getCostCenterList = function () {
			this.$http.post(this.$store.state.host + "/sendMail/getCostCenterList.do", { mailRoomId: this.mailRoomId }, { emulateJSON: true })
				.then(res => {
					//发送成功
					if (res.body.msg == 'success') {
						this.costCenterList = res.body.list
						for (var i = 0; i < this.columns.length; i++) {
							this.columns[i].text = this.columns[i].expressCompany
						}
					}
				}, res => {
					//发送失败
				})
		}
	}


}
