-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
94 lines (85 loc) · 2.28 KB
/
app.js
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
//app.js
App({
onLaunch: function () {
var that = this
// 登录
var code = null
wx.login({
success: function(res){
// success
code = res.code
console.log("login success")
},
fail: function() {
// fail
},
complete: function() {
// complete
}
})
/*
* 官文文档:
* code String 用户登录凭证(有效期五分钟)。开发者需要在开发者服务器后台调用 api,使用 code 换 取 openid 和 session_key 等信息
*/
// 发送 res.code 到后台换取 openId, sessionKey, unionId
wx.getUserInfo({
success: function(res){
// success
console.log('getUserInfo success', res)
var userInfo = res.userInfo
var nickName = userInfo.nickName
var avatarUrl = userInfo.avatarUrl
var gender = userInfo.gender //性别 0:未知、1:男、2:女
var province = userInfo.province
var city = userInfo.city
var country = userInfo.country
//把用户信息存储到本地
wx.setStorageSync('name', nickName)
wx.setStorageSync('avatarUrl', avatarUrl)
/**
* 1,把code发送给后台,后台获取session_key 和 openid
* 2,然后查询数据库user表有没有这个用户(要把openid)
* 3, 若无,则新增加一条记录(增加一个用户)
*/
wx.request({
url: getApp().host + '/api/login',
data: {
code: code,
name: nickName,
avatarUrl: avatarUrl,
gender: gender,
province: province,
city: city,
country: country
},
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
// header: {}, // 设置请求的 header
success: function (res) {
wx.setStorageSync('openid', res.data.data.openid)
//把用户信息set到userInfo
let userInfoObject = { name: nickName, avatarUrl: avatarUrl }
that.globalData.userInfo = userInfoObject
console.log("/api/login success")
},
fail: function () {
// fail
},
complete: function () {
// complete
}
})
},
fail: function() {
// fail
},
complete: function() {
// complete
}
})
},
globalData: {
userInfo: null
},
host: "http://localhost:8080/wx"
// host: "http://wiwikiky.s1.natapp.cc/wx"
})