-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·132 lines (127 loc) · 3.58 KB
/
Copy pathapp.js
File metadata and controls
executable file
·132 lines (127 loc) · 3.58 KB
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
122
123
124
125
126
127
128
129
130
131
132
//app.js
import {
configApi
} from './utils/constant';
import Promise from './utils/bluebird.min';
const applyApi = require('./utils/applyApi.js');
let that;
// 首次登录 --> 授权 --> 获取用户信息 --> 服务器登录
// 再次登录 --> 检查本地是否有userinfo --> 有则不用登录
var thirdId = ""
App({
data: {
systemInfo: wx.getSystemInfoSync()
},
onLaunch: function() {
that = this;
this.getImgMid();
// if (!wx.getStorageSync('honey-user')) {
// // 授权 登录
// this.LoggedIn();
// }
},
getSystemModelIPhoneX() {
var model = this.data.systemInfo.model;
let models = model.split('(');
if (models[0] == 'iPhone X ') {
return true;
} else {
return false;
}
},
getImgMid: function() {
var width = this.data.systemInfo.windowWidth;
var url = `?imageView2/1/w/${width}/h/${width}`;
return url;
},
getImgBig: function() {
var width = this.data.systemInfo.windowWidth * 2;
var url = `?imageView2/1/w/${width}/h/${width}`;
return url;
},
getPages: function(pre) {
var pages = getCurrentPages();
return pages[pages.length - pre - 1]; //上一个页面
},
LoggedIn() {
return new Promise((resolve, reject) => {
wx.authorize({
scope: 'scope.userInfo',
success () {
// 授权成功
return that.wxappLogin();
},
fail () {
// 授权失败 -> 打开设置
wx.openSetting({
success: (res) => {
console.log('openSetting 设置: ', res);
if (res.authSetting['scope.userInfo']) {
console.log('开启用户授权');
// 授权成功
return that.wxappLogin();
} else {
// 拒绝授权
}
}
})
}
})
})
},
wxappLogin() {
return new Promise((resolve, reject) => {
wx.login({
success(loginResult) {
var code = loginResult.code;
console.log('code **', code);
applyApi.jsonGetRequest('openId/getOpenId', {
code: code
}).then(result => {
wx.setStorageSync('honey-openId', result.openId);
console.log('openId ** ',result);
thirdId = result.openId
that.getUserInfo(thirdId);
}).catch(error => {
console.log(error);
});
console.log(thirdId)
return that.getUserInfo(thirdId);
},
fail(error) {}
});
})
},
getUserInfo(code) {
return new Promise((resolve, reject) => {
wx.getUserInfo({
success (res) {
return that.userLogin(code,res);
},
fail () {
if (callback) {
callback();
}
}
});
})
},
userLogin(code, res) {
return new Promise((resolve, reject) => {
applyApi.formPostRequest('user/userLogin', {
platformId: 1,
nickName: res.userInfo.nickName,
avatar: res.userInfo.avatarUrl,
thirdPartId: code,
thirdPartType: 'WX'
}).then(result => {
wx.setStorageSync('honey-user', result);
console.log(result);
return resolve(result)
}).catch(error => {
console.log(error);
return reject(error)
});
})
}
})