index.js
4.62 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
export const getImgUrl = function(src, width = 300, height = 300, mode = 2) {
return src ? src.replace(/(\{width}|\{height}|\{mode})/g, function($0) {
const dict = {
'{width}': width,
'{height}': height,
'{mode}': mode || 2
};
return dict[$0];
}) : '';
};
export const replaceHttp = function(src) {
return src.replace(/https?:/, '');
};
export const debounce = (idle, action) => { // 函数去抖动,超过一定时间才会执行,如果周期内触发,充值计时器
let last;
return function() {
let args = arguments;
if (last) {
clearTimeout(last);
}
last = setTimeout(() => {
action.apply(this, args);
}, idle);
};
};
export const throttle = (delay, action) => { // 函数节流器,定义函数执行间隔,按频率触发函数
let last = 0;
return function() {
let args = arguments;
let curr = +new Date();
if (curr - last > delay) {
action.apply(this, args);
last = curr;
}
};
};
/**
* 时间戳转化为年 月 日 时 分 秒
* time: 传入时间戳
* format:返回格式,支持自定义,但参数必须与formateArr里保持一致
* formatTimeByDefined(1488481383,'Y/M/D h:m:s') => 2017/03/03 03:03:03
*/
export const formatTimeByDefined = function (time, format) {
var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
var returnArr = [];
var date = new Date(time * 1000);
returnArr.push(date.getFullYear());
returnArr.push(formatNumber(date.getMonth() + 1));
returnArr.push(formatNumber(date.getDate()));
returnArr.push(formatNumber(date.getHours()));
returnArr.push(formatNumber(date.getMinutes()));
returnArr.push(formatNumber(date.getSeconds()));
for (var i in returnArr) {
format = format.replace(formateArr[i], returnArr[i]);
}
return format;
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}
/**
* 时间戳转化为 分 秒
* time: 传入时间戳
* format:返回格式,支持自定义,但参数必须与formateArr里保持一致
* formatTimeByDefined(1488481383,'Y/M/D h:m:s') => 2017/03/03 03:03:03
*/
function formatTimeByMin(time, format) {
var formateArr = ['m', 's'];
var returnArr = [];
var int_day, int_hour, int_minute, int_second;
var date = new Date(time * 1000);
returnArr.push(formatNumber(date.getMinutes()));
returnArr.push(formatNumber(date.getSeconds()));
// if (time >= 0) {
// // int_day = Math.floor(time / 86400000)
// // time -= int_day * 86400000;
// // int_hour = Math.floor(time / 3600000)
// // time -= int_hour * 3600000;
// int_minute = Math.floor(time / 60000)
// if (time < 60000) {
// int_minute = Math.floor(0);
// } else {
// time -= int_minute * 60000;
// }
// int_second = Math.floor(time / 1000)
// }
// // returnArr.push(formatNumber(int_day));
// // returnArr.push(formatNumber(int_hour));
// returnArr.push(formatNumber(int_minute));
// returnArr.push(formatNumber(int_second));
for (var i in returnArr) {
format = format.replace(formateArr[i], returnArr[i]);
}
return format;
}
const getGlobalData = () => {
const app = getApp();
let globalData;
if (app && app.globalData) {
// 原生小程序的 globalData 获取方式
globalData = app && app.globalData;
} else {
// Taro 下 globalData 获取方式
globalData = app && app.props && app.props.globalData;
}
return globalData;
}
const getYHStorageSync = (key, fromPage) => {
try {
let res = wx.getStorageSync(key);
return res;
} catch (e) {
wx.reportAnalytics('ufo_get_storage_error', {
key: key,
error: e.message,
error_code: e.code,
from_page: fromPage,
});
return null;
}
}
const getStorageUserInfo = () => {
const globalData = getGlobalData();
if (globalData.userInfo.length > 0) {
return globalData.userInfo;
} else {
return getYHStorageSync('userInfo');
}
}
const getUid = () => {
const userInfo = getStorageUserInfo();
// 判断 globalData 是否包含 uid
let uid = userInfo && userInfo.uid ? userInfo.uid : 0;
return uid
}
const setUserInfo = (cb) => {
let globalData = getGlobalData()
globalData.userInfo = Object.assign(globalData.userInfo, cb);
wx.setStorage({
key: "userInfo",
data: globalData.userInfo
})
}
const isLogin = ()=> {
const userInfo = getStorageUserInfo();
// 判断 globalData 是否包含 uid
let uid = userInfo && userInfo.uid ? userInfo.uid : 0;
return uid > 0
}
module.exports = {
formatTimeByDefined,
formatTimeByMin,
getImgUrl,
getGlobalData,
getYHStorageSync,
getStorageUserInfo,
getUid,
isLogin,
setUserInfo,
}