app.js
3.7 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
//app.js
App({
onLaunch: function() {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
this.changeToken();
// 获取用户信息
console.log(111)
// 添加显示条数
// wx.setTabBarItem({
// index: 4,
// text: '1',
// // iconPath: 'images/index1.png',
// // selectedIconPath: 'images/index1.png'
// })
},
changeToken() {
let url = '/api/user/codeToToken',
t = this;
wx.login({
success: function(res) {
t.post(url, {
code: res.code
}).then((r) => {
wx.setStorageSync('token', r.userInfo.token)
})
}
})
},
post: function(url, data, showLoad) {
let app = this;
/**
* 自定义post函数,返回Promise
* +-------------------
* @param {String} url 接口网址
* @param {arrayObject} data 要传的数组对象 like: {name: 'name', age: 32}
* +-------------------
* @return {Promise} promise 返回promise供后续操作
*/
(showLoad == undefined || showLoad) ?
wx.showLoading({
title: '加载中',
}): ''
wx.showNavigationBarLoading()
var promise = new Promise((resolve, reject) => {
//init
let that = this,
token = wx.getStorageSync('token'),
header = {
'content-type': 'application/x-www-form-urlencoded',
'token': token || ''
},
postData;
data.header ? (token ? (header['token'] = token, delete data.header) : setTimeout(() => {
wx.navigateTo({
url: '/pages/register/register',
})
}, 1100)) : ""
//网络请求
wx.request({
url: this.globalData.baseUrl + url,
data: data,
method: 'POST',
header: header,
success: function(res) { //返回取得的数据
if (res.data.code == '1') {
resolve(res.data.data);
} else if (res.data.code == '201') {
resolve(res.data);
} else {
wx.showToast({
title: res.data.msg,
icon: 'none',
})
reject(res.data)
if (res.data.code == '401') {
console.log(app.globalData.register)
if (!app.globalData.register){
setTimeout(() => {
wx.navigateTo({
url: '/pages/register/register',
})
}, 1200)
app.globalData.register = 1
return false;
}
}
}
(showLoad || showLoad == undefined) ? wx.hideLoading(): "";
wx.hideNavigationBarLoading()
},
fail: function(e) {
reject('网络出错');
(showLoad || showLoad == undefined) ? wx.hideLoading(): "";
wx.hideNavigationBarLoading()
}
})
});
return promise;
},
timeFormate(timestamp, timeType) {
if (timestamp) {
var timeStamp = timestamp.length == 13 ? timestamp : timestamp * 1000
var date = new Date(timeStamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
var Y = date.getFullYear() + '-';
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var D = date.getDate() + ' ';
var h = date.getHours() + ':';
var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
var s = date.getSeconds();
if (timeType == 'YYMMDD') {
return Y + M + D;
} else if (timeType == 'YYMMDDHHMM') {
return Y + M + D + " " + h + m;
} else {
return h + m;
}
}
},
globalData: {
userInfo: null,
baseUrl: 'https://txgyy.jjjtech.cn',
}
})