import { getWxUserInfo, getWxConfig } from '../api/login'
import { getUrlParam } from '../utils/utils.js'
export default {
	install(Vue) {
		Vue.prototype.getOpenid = async function () {
			if (localStorage.getItem("openid") != 'undefined' && localStorage.getItem("openid") != null) {
				return;
			}
			var code = this.getUrlParam("code");
			console.log(code, 'code====++++')
			if (code == 'undefined' || code == null || code == "") {
				// alert("参数异常，请返回首页重试");
				this.urlredirect();
				return;
			}

			const res = await getWxUserInfo({ code: code })
			console.log(res, '================================getWxUserInfo')

			// 未绑定账号 获取openId后去登录页
			if(res.code == '501'){
				localStorage.setItem("openid", res.openId);
				this.$router.push({ path: '/Login' })
				return
			}
			if (res.code == '0') {
				console.log('直接走+======', res)
				localStorage.setItem("openid", 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 = getUrlParam


		Vue.prototype.getUser = function () {
			this.$http.post(this.$store.state.host + "/personal/getPersonalInfoByUserId.do", { userId: this.userId }, { emulateJSON: true })
				.then(res => {
					//发送成功
					if (res.body.msg == 'success') {
						this.user = res.body.personalInfo;
					}
				}, res => {
					//发送失败
				})
		}
		//代收人列表
		Vue.prototype.findCollectAgentList = function () {
			this.$http.post(this.$store.state.host + "/sendMail/findCollectAgentList.do", {}, { emulateJSON: true })
				.then(res => {
					//发送成功
					if (res.body.msg == 'success') {
						this.dsList = res.body.list
						for (var i = 0; i < this.dsList.length; i++) {
							this.dsList[i].text = this.dsList[i].name + ' ' + this.dsList[i].phone + ' ' + this.dsList[i].department
						}
					}
				}, res => {
					//发送失败
				})
		}
		//获取对应人的地址
		Vue.prototype.getPersonalAddressById = function (id) {
			this.$http.post(this.$store.state.host + "/personal/getPersonalAddress.do", { userId: id }, { emulateJSON: true })
				.then(res => {
					//发送成功
					if (res.body.msg == 'success') {
						this.address = res.body.address
					}
				}, 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++) {
							if (this.columns[i].companyFlag == '' || this.columns[i].companyFlag == undefined) {
								this.columns[i].text = this.columns[i].expressCompany
							} else {
								this.columns[i].text = this.columns[i].expressCompany + '(' + this.columns[i].companyFlag + ')'
							}
						}
						//默认顺丰快递
						this.expressInfoShow = this.columns[0].text
						this.expressInfo = this.columns[0].id
						this.expressPhone = this.columns[0].phone
					}
				}, 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.costCenterList.length; i++) {
							this.costCenterList[i].text = this.costCenterList[i].costCenterName
						}
						this.costCenterInfoShow = this.costCenterList[0].text
						this.costCenterName = this.costCenterList[0].costCenterName
						this.costCenterId = this.costCenterList[0].id
					}
				}, res => {
					//发送失败
				})
		}

		//代收人列表
		Vue.prototype.getDefaultAddress = function () {
			this.$http.post(this.$store.state.host + "/solic/getDefaultAddress.do", {
				userId: localStorage.getItem("userId"),
				mailRoomId: localStorage.getItem("roomId")
			}, { emulateJSON: true })
				.then(res => {
					//发送成功
					if (res.body.msg == 'success') {
						this.jjname = res.body.personalInfo.name + ' ' + res.body.personalInfo.phone
						this.jjadd = res.body.mailRoomInfo.province + " " + res.body.mailRoomInfo.city + " " + res.body.mailRoomInfo.area + ' ' + res.body.mailRoomInfo.address
						this.jjInfo.jjname = this.jjname
						this.jjInfo.jjadd = this.jjadd
						this.jjInfo.senderMobile = res.body.personalInfo.telephone
						this.jjInfo.sender = res.body.personalInfo.name
						this.jjInfo.senderPhone = res.body.personalInfo.phone
						this.jjInfo.sendAddr = res.body.mailRoomInfo.province + " " + res.body.mailRoomInfo.city + " " + res.body.mailRoomInfo.area
						this.jjInfo.sendAddress = res.body.mailRoomInfo.address
						this.$store.commit("getJjInfo", this.jjInfo)
					}
				}, res => {
					//发送失败
				})
		}

		//登录判断
		Vue.prototype.isLogin = function () {
			// 	this.$http.post(this.$store.state.host+"/appSysUser/isLogin.do",{
			// 	},{emulateJSON:true})
			// 		.then(res=>{
			// 			//console.log(res.body)
			// 			//发送成功
			// 			if(res.body.msg=='success'){

			// 			}else{
			//   //本地(服务器下面这个要放开)
			// 				localStorage.clear();
			// 			}
			// 	},res=>{
			// 			 //发送失败
			// 	})
		}
	}


}
