forked from dmego/together
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
158 lines (155 loc) · 5.56 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
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
import Touches from './utils/Touches.js'
var Bmob = require("utils/bmob.js");
var common = require("utils/common.js");
const __utils = require('utils/util')
Bmob.initialize("9f1068e931203bfa908f26353f9d8080", "396e699e6cda7e452ef36e9effe5ef35");
App({
version: 'v2.2.4', //版本号
onLaunch: function () {
var that = this;
//调用系统API获取设备的信息
wx.getSystemInfo({
success: function (res) {
var kScreenW = res.windowWidth / 375
var kScreenH = res.windowHeight / 603
wx.setStorageSync('kScreenW', kScreenW)
wx.setStorageSync('kScreenH', kScreenH)
}
})
//调用API从本地缓存中获取数据
try {
var value = wx.getStorageSync('user_openid')
if (value) {
} else {
console.log('执行login1')
wx.login({
success: function (res) {
if (res.code) {
console.log('执行login2', res);
}
}
});
wx.login({
success: function (res) {
if (res.code) {
Bmob.User.requestOpenId(res.code, {
success: function (userData) {
wx.getUserInfo({
success: function (result) {
var userInfo = result.userInfo
var nickName = userInfo.nickName
var avatarUrl = userInfo.avatarUrl
var sex = userInfo.gender
Bmob.User.logIn(nickName, userData.openid, {
success: function (user) {
try {
wx.setStorageSync('user_openid', user.get('userData').openid)
wx.setStorageSync('user_id', user.id)
wx.setStorageSync('my_nick', user.get("nickname"))
wx.setStorageSync('my_username', user.get("username"))
wx.setStorageSync('my_sex', user.get("sex"))
wx.setStorageSync('my_avatar', user.get("userPic"))
} catch (e) {
}
console.log("登录成功");
},
error: function (user, error) {
if (error.code == '101') {
var user = new Bmob.User();//开始注册用户
user.set('username', nickName);
user.set('password', userData.openid);
user.set("nickname", nickName);
user.set("userPic", avatarUrl);
user.set("userData", userData);
user.set('sex', sex);
user.set('feednum',0);
user.signUp(null, {
success: function (result) {
console.log('注册成功');
try {//将返回的3rd_session存储到缓存中
wx.setStorageSync('user_openid', user.get('userData').openid)
wx.setStorageSync('user_id', user.id)
wx.setStorageSync('my_nick', user.get("nickname"))
wx.setStorageSync('my_username', user.get("username"))
wx.setStorageSync('my_sex', user.get("sex"))
wx.setStorageSync('my_avatar', user.get("userPic"))
} catch (e) {
}
},
error: function (userData, error) {
console.log("openid=" + userData);
console.log(error)
}
});
}
}
});
}
})
},
error: function (error) {
console.log("Error: " + error.code + " " + error.message);
}
});
} else {
console.log('获取用户登录态失败1!' + res.errMsg)
}
},
complete: function (e) {
console.log('获取用户登录态失败2!' + e)
}
});
}
} catch (e) {
console.log("登陆失败")
}
wx.checkSession({
success: function () {
},
fail: function () {
//登录态过期,重新登录
wx.login()
}
})
},
onShow: function () {
},
formate_data: function (date) {
let month_add = date.getMonth() + 1;
var formate_result = date.getFullYear() + '年'
+ month_add + '月'
+ date.getDate() + '日'
+ ' '
+ date.getHours() + '点'
+ date.getMinutes() + '分';
return formate_result;
},
getUserInfo: function (cb) {
var that = this;
if (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;
typeof cb == "function" && cb(that.globalData.userInfo)
}
})
}
});
}
},
globalData: {
userInfo: null,
},
onPullDownRefresh: function () {
//wx.stopPullDownRefresh()
},
onError: function (msg) {
},
Touches: new Touches(),
util: __utils,
})