Blame view

src/main/webapp/wxaapp/app.js 2.49 KB
e80df919   atao   init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  var Promise = require('./utils/es6-promise.min.js').Promise;
  App({
  	onLaunch: function () {
  		//调用API从本地缓存中获取数据
  		var logs = wx.getStorageSync('logs') || []
  		logs.unshift(Date.now())
  		wx.setStorageSync('logs', logs)
  	},
  	getUserInfo:function(cb){
  		var that = this
  		console.log(this.globalData.userInfo);
  		if (this.globalData.userInfo) {
  			typeof cb == "function" && cb(this.globalData.userInfo)
  		} else {
  			login(that, cb);
  		}
  	},
  	globalData:{
  		userInfo:null
  	}
  })
  //调用登录接口
  function login(that, cb) {
  	new Promise(function(resolve){
  		// 显示加载中
  		wx.showToast({
  			title: "loading",
  			icon: 'loading'
  		});
  		wx.login({
  			success: function (res) {
  				resolve(res);
  			}
  		});
  	}).then(function (xdata){
  		return new Promise(function(resolve, reject){
  			wx.request({
  				url: 'http://localhost/wxa/user/login',
  				data: {
  					code: xdata.code
  				},
  				success: function(res) {
  					var data = res.data;
  					if (data.errcode !== 500) {
  						wx.setStorageSync("sessionId", data.sessionId);
  						resolve(res);
  					} else {
  						reject(data);
  					}
  				}
  			})
  		});
  	}).then(function (xdata){
  		return new Promise(function(resolve, reject){
  			wx.getUserInfo({
  				success: function (res) {
  					console.log(res);
  					that.globalData.userInfo = res.userInfo
  					resolve(res);
  				}, 
  				fail: function (res) {
  					// 取消授权
  					reject(res);
  				}
  			})
  		});
  	}, function(xdata){
  		wx.showModal({
  			title: '提示',
  			content: xdata.errmsg,
  			success: function(res) {
  				if (res.confirm) {
  					console.log('用户点击确定')
  				}
  			}
  		});
  	}).then(function (xdata){
  		return new Promise(function(resolve, reject){
  			wx.request({
  				url: 'http://localhost/wxa/user/info',
  				data: {
  					rawData: xdata.rawData,
  					signature: xdata.signature,
  					encryptedData: xdata.encryptedData,
  					iv: xdata.iv
  				},
  				header: {
  					'wxa-sessionid': wx.getStorageSync("sessionId")
  				},
  				success: function(res) {
  					resolve(res);
  				}
  			})
  		});
  	}, function(xdata){
  		wx.hideToast();
  		wx.showModal({
  			title: '提示',
  			content: "您取消了授权",
  			success: function(res) {
  				if (res.confirm) {
  					console.log('用户点击确定')
  				}
  			}
  		});
  	}).then(function(xdata){
  		wx.hideToast();
  		cb(that.globalData.userInfo);
  	}, function(xdata){
  		wx.hideToast();
  		wx.showModal({
  			title: '提示',
  			content: xdata.errmsg,
  			success: function(res) {
  				if (res.confirm) {
  					console.log('用户点击确定')
  				}
  			}
  		});
  	});
  }