authorize.js 1.58 KB
let app = getApp();
let config = require('../../config');

Page({
	data: {
		authorized: true,
		canIUse: wx.canIUse('button.open-type.getUserInfo')
	},
	onLoad(options) {},
	onReady() {
		wx.showLoading();
		this.autoAuth();
	},
	bindGetUserInfo(e) {
		wx.showLoading();
		this.getUserInfo(e.detail);
	},
	// 点openType==userInfo 手动登陆
	getUserInfo(e) {
		let _this = this;
		// 直接就能拿到 userInfo
		app.globalData.userInfo = e.userInfo;
		wx.login({
			success: function (res) {
				app.post({
					url: app.api.decrypt,
					data: {
						encryptedData: e.encryptedData,
						iv: e.iv,
						code: res.code
					}
				}).then((res2) => {
					_this.autoAuth();
				}).catch((err) => {})
			}
		});
	},
	// 刷新首页数据
	refreshIndexData() {
		return new Promise((resolve, reject) => {
			app.post({
				url: app.api.index,
				data: {}
			}).then((res) => {
				app.globalData.indexInfo = res;
				resolve(res)
			}).catch((err) => {
				console.log("index err:", err);
			});
		});
	},
	// 判断结果去首页
	autoAuth() {
		// 调用自动登陆方法
		let _this = this;
		this.refreshIndexData().then((result) => {
			let indexInfo = app.globalData.indexInfo;
			// 自动登陆成功
			let loginSuc = true;
			if (loginSuc) {
				// 登陆成功跳转到首页
				setTimeout(() => {
					app.router.push({
						path: 'index',
						query: {},
						duration: 0,
						openType: "redirect"
					})
					wx.hideLoading();
				}, 3000);
			} else {
				// 登陆失败,隐藏loading自动登陆
				wx.hideLoading();
				this.setData({
					authorized: false
				})
			}
		});
	}
})