-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
129 lines (124 loc) · 3.48 KB
/
Copy pathapp.js
File metadata and controls
129 lines (124 loc) · 3.48 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
//app.js
App({
onLaunch: function () {
this.getLogin();
// 展示本地存储能力
// var logs = wx.getStorageSync('logs') || []
// logs.unshift(Date.now())
// wx.setStorageSync('logs', logs)
// console.log(111)
var isDebug = false;//调试状态使用本地服务器,非调试状态使用远程服务器
if (!isDebug) {
//远程域名
wx.setStorageSync('domainName', "https://wxapp.llwell.net/api/O2O/")
}
else {
//本地测试域名
wx.setStorageSync('domainName', "http://172.16.10.11:5000/api/O2O/")
}
},
getLogin: function (resolve){
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
this.Ajax(
'Users',
'POST',
'Login',
{ code: res.code },
function (json) {
console.log(json);
if (json.success) {
wx.setStorageSync('token', json.data.sessionId);
console.log(json.data.sessionId);
if (resolve){
resolve();
}
} else {
console.log(json.msg.code);
console.log(json.msg.msg);
}
}
);
}
})
},
globalData: {
userInfo: null
},
Ajax: function (url, type, method, data, callback) {
wx.showLoading({
title: '加载中'
});
var send = {
token: wx.getStorageSync('token'),
method: method,
param: data,
};
wx.request({
url: wx.getStorageSync('domainName') + url,
data: send,
method: type, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: {
'content-type': 'application/json' // 默认值
}, // 设置请求的 header
success: function (res) {
// 发送请求成功执行的函数
if (typeof callback === 'function') {
callback(res.data);
}
},
fail: function (res) {
},
complete: function () {
wx.hideLoading();
}
})
},
// Json: function (one, two) { //合并两个json为一个新的json
// var newJson = {};
// for (var attr in one) {
// newJson[attr] = one[attr];
// }
// for (var attr in two) {
// newJson[attr] = two[attr];
// }
// return newJson;
// },
//当前时间格式
getNowFormatDate: function () {
var date = new Date();
var seperator1 = ".";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
return currentdate;
},
//时间戳转化时间格式
formatDateTime: function (timeStamp) {
var date = new Date();
date.setTime(timeStamp * 1000);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '.' + m + '.' + d + ' ' + h + ':' + minute + ':' + second;
}
})