// app.js
App({
	onLaunch() {
		this.isreload();
		this.getVision();
		// this.getToken();
		this.getSessionKey()
	},
	// 小程序是否有新版本
	isreload: function () {
		if (wx.canIUse('getUpdateManager')) {
			const updateManager = wx.getUpdateManager();
			updateManager.onCheckForUpdate(function (res) {
				if (res.hasUpdate) {
					updateManager.onUpdateReady(function () {
						wx.showModal({
							title: '更新提示',
							content: '新版本已经准备好，是否重启应用？',
							success: function (res) {
								if (res.confirm) {
									updateManager.applyUpdate()
								}
							}
						})
					})
					updateManager.onUpdateFailed(function () {
						wx.showModal({
							title: '已经有新版本了哟~',
							content: '新版本已经上线啦，请您删除当前小程序，重新搜索打开！'
						})
					})
				}
			})
		} else {
			wx.showModal({
				title: '提示',
				content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试！'
			})
		}
	},
	getVision() {
		const accountInfo = wx.getAccountInfoSync();
		console.log(accountInfo)
		if (accountInfo.miniProgram.envVersion == 'develop') { // 开发板
			this.globalData.globalUrl = 'https://repairtest.zeruiedu.com'
		} else if (accountInfo.miniProgram.envVersion == 'trial') { // 体验版
			this.globalData.globalUrl = 'https://repairtest.zeruiedu.com'
		} else if (accountInfo.miniProgram.envVersion == 'release') { // 正式版
			this.globalData.globalUrl = 'https://repair.zeruiedu.com'
		} else {
			this.globalData.globalUrl = 'https://repairtest.zeruiedu.com'
		}
		console.log(this.globalData.globalUrl)
	},
	// 将token从缓存中 赋值到全局变量
	getToken: function () {
		var that = this
		try {
			var token = wx.getStorageSync('token')
			var userInfo = wx.getStorageSync('userInfo')
			console.log("token", token)
			if (token) {
				that.globalData.token = token
				that.globalData.userInfo = userInfo
			} else { }
		} catch (e) { }
	},
	// 登录获取sessionkey 提交后台换取基本openid unionid
	getSessionKey: function (cb, cb1) {
		var that = this
		// 调用登录接口
		wx.login({
			success: function (res) {
				that.codeChangeToken(res.code, cb, cb1)
			}
		})
	},
	saveUserInfo: function (info, cb) {
		var that = this
		if (that.globalData.first_token !== '') {
			wx.checkSession({
				success() {
					wx.request({
						url: that.globalData.globalUrl + '/api/v1.0/saveInfo',
						method: 'GET',
						header: {
							Authorization: that.globalData.first_token,
							// token: 'sys',
						},
						data: {
							encryptedData: encodeURIComponent(info.encryptedData),
							iv: encodeURIComponent(info.iv),
							token: that.globalData.token
						},
						success: function (res) {
							if (res.data.meta.code === 200) {
								wx.setStorageSync('userInfo', JSON.parse(info.rawData))
								that.globalData.userInfo = JSON.parse(info.rawData)
								that.globalData.hasTel = res.data.data.telephone
								if (res.data.data.telephone) {
									wx.setStorageSync('userTel', res.data.data.telephone)
								}
								typeof cb == "function" && cb()
							} else {
								wx.showToast({
									title: res.data.meta.message,
									icon: 'none'
								})
							}
						}
					})
				},
				fail() {
					that.getSessionKey(() => that.saveUserInfo(info, cb))
				}
			})
		} else {
			that.getSessionKey(() => that.saveUserInfo(info, cb))
		}
	},
	// sessionkey 换取token
	codeChangeToken(code, cb) {
		// 发起网络请求
		var that = this
		wx.request({
			url: 'https://new.open.xueyoubangedu.com/openxcx/auth',
			data: {
				code: code,
				app_type: 'repair',
				authorizer_appid: "wxafaf02db6f71f4a3"
			},
			success: function (res) {
				if (res.data.code === 200) {
					wx.setStorageSync('token', res.data.data.token)
					that.globalData.token = res.data.data.token
					// login
					wx.request({
						url: that.globalData.globalUrl + '/api/v1.0/login',
						data: {
							token: res.data.data.token
						},
						method: 'GET',
						success: function (res) {
							if (res.data.meta.code === 200) {
								wx.setStorageSync('token', res.data.data.token)
								that.globalData.first_token = res.data.data.token
								typeof cb == "function" && cb()
							} else {
								wx.showToast({
									title: res.data.meta.message,
									icon: 'none'
								})
							}
						}
					})

				} else {
					console.log(res)
				}
			}
		})
	},

	globalData: {
		userInfo: null,
		first_token: '',//login接口返回来的token
		token: '',//用户token
		globalUrl: '', // 全局Url
		app_id: 'wxafaf02db6f71f4a3',
		backurl: '', // 全局跳转页面路径
		tjgs_fresh:false,
		hasTel: false, // 是否手机号登录过
	}
})
