//请求教育接口
let host = ''
if (process.env.NODE_ENV === "development") {
	//host = 'https://jjswapi.oytour.com/api'
	// host = 'http://192.168.10.206:5005/api'
	host = 'http://192.168.10.128:8900/api'
} else {
	host = 'https://jjswapi.oytour.com/api'
}
import Toast from '../wxcomponents/vant/toast/toast';
let request = (param) => {
	return new Promise((resolve, reject) => {
		let token = uni.getStorageSync('erpUserInfo').Token;
		uni.request({
			url: host + param.url,
			method: param.method || "Post",
			header: param.header || {
				'content-type': "application/json",
				'token': token || ""
			},
			data: {
				Msg: param.data,
			},
			success: res => {
				if (res.data.Code === 1) {
					resolve(res.data)
				} else {
					if (res.data.Code == 10001 || res.data.Code == 10000) {
						// uni.showToast({
						// 	title: "用户凭证失效,请重新授权",
						// 	duration: 2000,
						// 	icon: "none",
						// });
						// uni.removeStorageSync('erpUserInfo');
						// uni.reLaunch({
						// 	url:"/pages/login/login"
						// })
						// reject()
						//重新获取TOKEN
						// updateToken()
						// request(param)
					} else {
						uni.showToast({
							title: res.data.Message,
							duration: 2000,
							icon: "none",
							success() {
								if (res.data.Code === 0) {
									resolve(res.data)
								} else {
									reject()
								}
							}
						});

					}

				}
			},
			fail: (err) => {
				console.warn("fail:" + JSON.stringify(err));
				uni.showToast({
					title: '网络请求失败',
					duration: 2000,
					icon: "error",
				});
				reject()
			}
		});
	})
}
let updateToken= ()=> {
	let u = uni.getStorageSync('erpUserInfo')
	let userWxInfo = uni.getStorageSync('userWxInfo')
	let wxUserAuthInfo = uni.getStorageSync('wxUserAuthInfo')
	if (u) {
		request({
			url: '/AppletLogin/GetStudentsByClassInAccount',
			method: 'post',
			data: {
				Account: u.Account,
				WName: userWxInfo.userInfo.nickName,
				WImage: userWxInfo.userInfo.avatarUrl,
				UnionId: wxUserAuthInfo.unionid
			}
		}).then(r => {
			if (r.Data && r.Data.length > 0) {
				r.Data.forEach(x => {
					if (x.ClassInAccount == u.Account) {
						uni.setStorageSync('erpUserInfo', x)
					}
				})
				if (!flag) {
					r.Data.forEach(x => {
						if (x.RealPhone == u.Account) {
							flag = true
							uni.setStorageSync('erpUserInfo', x)
						}
					})
				}
				uni.setStorageSync("ErpStus", r.Data)

			}
		})

	}
}
export default request
