-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
159 lines (148 loc) · 5.54 KB
/
Copy pathapp.js
File metadata and controls
159 lines (148 loc) · 5.54 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//app.js
var app = getApp()
App({
onLaunch: function () {
//调用API从本地缓存中获取数据
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
this.getUserInfo('')
},
getUserInfo:function(cb){
var that = this;
console.log(this.globalData);
//this.globalData.userInfo && this.globalData.use!==null
if ( this.globalData.userInfo ){
this.globalData.userInfo.openid = 'ab77611314db87d981e2';
console.log(this.globalData.userInfo.openid);
this.globalData.userInfo.expires_in = 1;
wx.setStorageSync('userInfo', this.globalData.userInfo);
typeof cb == "function" && cb(this.globalData.userInfo)
}else{
//调用登录接口
wx.login({
success: function () {
wx.getUserInfo({
success: function (res) {
that.globalData.userInfo = res.userInfo;
try {
wx.setStorageSync('userInfo', res.userInfo);
} catch (e) {
console.log('error:' + e);
}
typeof cb == "function" && cb(that.globalData.userInfo)
}
})
}
});
that.globalData.userInfo.openid = 'ab77611314db87d981e2';
console.log(that.globalData.userInfo.openid);
that.globalData.userInfo.expires_in = res.data.expires_in;
wx.setStorageSync('userInfo', that.globalData.userInfo);
var staticData = that.globalData;//这里存储了appid、secret、token串
var link = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + staticData.appid + '&secret=' + staticData.secret + '&js_code=' + res.code + '&grant_type=authorization_code';
wx.request({
url: link,
data: {},
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
success: function (res) {
var user = {};
user.openid = res.data.openid;
user.expires_in = Date.now() + res.data.expires_in;
that.globalData.userInfo.openid = res.data.openid;
that.globalData.userInfo.expires_in = res.data.expires_in;
wx.setStorageSync('userInfo', that.globalData.userInfo);//存储openid
console.log(that.globalData.userInfo);
}
});
//获取用户信息
var openid = staticData.user.openid==null ? 'ab77611314db87d981e2' : staticData.user.openid;
//注册
wx.request({
url: 'http://www.backend.cn/api.php/weixin/user/add',
method: 'post',
data: {
openid: openid,
avatarUrl: staticData.userInfo.avatarUrl,
nickName: staticData.userInfo.nickName,
gender: staticData.userInfo.gender,
province: staticData.userInfo.province,
city: staticData.userInfo.city,
existResume: 0
},
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: function (res) {
console.log(res);
},
error: function (res) {
console.log(res);
}
});
}
},
globalData:{
appid: 'wx1665d7c6e8e50e1e',//appid需自己提供,此处的appid我随机编写
secret: 'f348d07a7469fc7a49513c50ec04166a',//secret需自己提供,此处的secret我随机编写
userInfo:null,
},
onShow: function () {
console.log('App Show')
},
onHide: function () {
console.log('App Hide')
},
})
// /***微信开发者
// AppID(应用ID)
// wx1665d7c6e8e50e1e
// AppSecret(应用密钥)
// f348d07a7469fc7a49513c50ec04166a***/
// App({
// globalData: {
// appid: 'wx1665d7c6e8e50e1e',//appid需自己提供,此处的appid我随机编写
// secret: 'f348d07a7469fc7a49513c50ec04166a',//secret需自己提供,此处的secret我随机编写
// userInfo: null
// },
// onLaunch: function () {
// var that = this
// var user = wx.getStorageSync('user') || {};
// var userInfo = wx.getStorageSync('userInfo') || {};
// if ((!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600)) && (!userInfo.nickName)) {
// wx.login({
// success: function (res) {
// if (res.code) {
// wx.getUserInfo({
// success: function (res) {
// var objz = {};
// objz.avatarUrl = res.userInfo.avatarUrl;
// objz.nickName = res.userInfo.nickName;
// //console.log(objz);
// wx.setStorageSync('userInfo', objz);//存储userInfo
// that.globalData.userInfo = res.userInfo;
// }
// });
// var d = that.globalData;//这里存储了appid、secret、token串
// var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appid + '&secret=' + d.secret + '&js_code=' + res.code + '&grant_type=authorization_code';
// wx.request({
// url: l,
// data: {},
// method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
// // header: {}, // 设置请求的 header
// success: function (res) {
// var obj = {};
// obj.openid = res.data.openid;
// obj.expires_in = Date.now() + res.data.expires_in;
// console.log(obj);
// wx.setStorageSync('user', obj);//存储openid
// }
// });
// } else {
// console.log('获取用户登录态失败!' + res.errMsg)
// }
// }
// });
// }
// },
// })