app.js
8.9 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//app.js
App({
onShow() {
console.log('app-onshow');
const self = this;
// 查看是否授权
wx.getSetting({
success(res) {
if (res.authSetting['scope.userInfo']) {
console.log('授权过到首页');
self.globalData.isLogin = true;
// wx.redirectTo({
// url: '/pages/index/index', //跳转到授权页面
// });
} else {
console.log('授权失效到授权页');
self.globalData.isLogin = false;
wx.navigateTo({
url: '/pages/my/my', //跳转到授权页面
})
}
}
})
},
onLaunch: function () {
console.log('app-onLaunch');
// 展示本地存储能力
// var logs = wx.getStorageSync('logs') || []
// logs.unshift(Date.now())
// wx.setStorageSync('logs', logs)
// 获取用户信息
},
getLogin() {
const self = this;
return new Promise(function(resolve, reject) {
wx.getUserInfo({
success: function (user) {
// console.log(user);
// 登录
let url = '/portal/Login/getToken';
wx.login({
success: function (res) {
if(self.loginCallback) {
console.log('onlaunch-callback');
self.loginCallback(user)
}
console.log('获取code', res);
self.post(url, {
code: res.code,
user_nickname: user.userInfo.nickName,
avatar: user.userInfo.avatarUrl
}, {}).then((ret) => {
console.log('获取token', ret);
wx.setStorageSync('token', ret.data.token);
self.globalData.userInfo = user.userInfo;
resolve(ret.data);
setTimeout(function () {
wx.navigateBack({delta:1})
},200)
})
},
fail: function (res) {
},
complete: function (res) {
},
});
}
});
})
},
post: function (url, data, headerParams) {
/**
* 自定义post函数,返回Promise
* +-------------------
* @param {String} url 接口网址
* @param {arrayObject} data 要传的数组对象 like: {name: 'name', age: 32}
* +-------------------
* @return {Promise} promise 返回promise供后续操作
*/
// wx.showLoading({
// title: '加载中',
// })
wx.showNavigationBarLoading()
// wx.showToast({
// title: '加载中',
// icon:'none',
// })
var promise = new Promise((resolve, reject) => {
//init
let that = this;
let postData = data;
let baseUrl = 'http://wmatchrd.com/api';
//网络请求
let header = {
'content-type': 'application/x-www-form-urlencoded'
}
header = Object.assign(header, headerParams)
wx.request({
url: baseUrl + url,
data: postData,
method: 'POST',
header: header,
success: function (res) {//返回取得的数据
// console.log('返回取得的数据res', res);
if(res.data.code == 1){
resolve(res.data);
}else if (res.data.code == '20000') {
console.log('20000');
resolve(res.data.data);
} else if (res.data.data.code == '10001') { //未获取授权
console.log('10001');
wx.showModal({
title: '提示',
content: res.data.data.msg,
showCancel: false,
success: function (res) {
if (res.confirm) {
// wx.removeStorageSync('token');
wx.navigateTo({
url: '/pages/index/index',
})
}
}
});
resolve(res.data.data);
} else if (res.data.data.code == '30000') {
resolve(res.data);
} else if (res.data.data.code == '40001') {//返回错误提示信息
console.log(555);
wx.showModal({
title: '提示',
content: res.data.data.msg,
showCancel: false,
success: function (res) {
if (res.confirm) {
// wx.removeStorageSync('token');
// wx.navigateTo({
// url: '/pages/start/start',
// })
}
}
});
resolve(res.data.msg);
} else if (res.data.code == '40000') {
console.log(333);
// wx.showModal({
// title: '提示',
// content: res.data.data.msg,
// showCancel: false,
// success: function (res) { }
// });
resolve(res.data.data);
} else if (res.data.data.code == '40106') {
console.log('重复绑定');
resolve(res.data.data);
} else if (res.data.data.code == '40107') {
console.log('手机号已注册');
resolve(res.data.data);
} else if (res.data.data.code == '40111') {
console.log('新旧密码不能相同');
resolve(res.data.data);
} else {
console.log('code=0');
console.log(res.data);
wx.showModal({
title: '提示',
content: res.data.message,
showCancel: false,
})
resolve(res.data.data);
reject(res.data)
}
setTimeout(function () {
// wx.hideLoading()
// wx.hideToast()
wx.hideNavigationBarLoading()
}, 500)
},
error: function (e) {
reject('网络出错');
// wx.hideLoading()
wx.hideNavigationBarLoading()
}
})
});
return promise;
},
//获取当前日期
nowDate() {
let date = new Date();
let month = date.getMonth() + 1;
let day = date.getDate();
// let hours = date.getHours();
// let minutes = date.getMinutes();
let Y = date.getFullYear() + '-' + (month > 9 ? month : ('0' + month)) + '-' + (day > 9 ? day : ('0' + day));
// let H = (hours>9?hours:('0'+hours)) + ':' +(minutes>9?minutes:('0'+minutes));
let time = [];
time.push(Y);
// let new_time = time.join(' ');
return time;
},
//获取截止时间的前几个小时
nextTime(over_time,hour_num) {
let over = new Date(over_time); //转成标准时间
let month = over.getMonth() + 1;
let day = over.getDate();
let hours = over.getHours();//获取截止时间的小时
let minutes = over.getMinutes();
let Y = over.getFullYear() + '.' + (month > 9 ? month : ('0' + month)) + '.' + (day > 9 ? day : ('0' + day));
let H = (hours > 9 ? hours - hour_num : ('0' + hours - hour_num)) + ':' + (minutes > 9 ? minutes : ('0' + minutes));
let time = [];
// console.log('Y,H,month', Y, H,month);
time.push(Y,H);
let new_time = time.join(' ');
return new_time;
},
globalData: {
userInfo: null,
isLogin:null
}
});