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
function strlen(value) {
//中文、中文标点、全角字符按1长度,英文、英文符号、数字按0.5长度计算
let cnReg = /([\u4e00-\u9fa5]|[\u3000-\u303F]|[\uFF00-\uFF60])/g;
let mat = value.match(cnReg);
let length = 0;
if (mat) {
return (length = mat.length + (value.length - mat.length) * 0.5);
} else {
return (length = value.length * 0.5);
}
}
/**
*
* 判断是否在微信浏览器 true是
*/
function isWeiXinBrowser() {
// #ifdef H5
let ua = window.navigator.userAgent.toLowerCase()
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
return true
} else {
return false
}
// #endif
return false
}
/**
* 获取url参数
* @param {*} name
* @param {*}
* @returns
*/
function getQueryString(name, url) {
var url = url || window.location.href
var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
var r = url.substr(1).match(reg)
if (r != null) {
return r[2]
}
return null
}
//路径转化
function getPath(path) {
if (path.indexOf('?') != -1) {
let arr = path.split('?');
return arr[0];
}
return path;
}
function uniCopy({
content,
success,
error
}) {
content = typeof content === 'string' ? content : content.toString() // 复制内容,必须字符串,数字需要转换为字符串
/**
* 小程序端 和 app端的复制逻辑
*/
//#ifndef H5
uni.setClipboardData({
data: content,
success: function() {
success("复制成功~")
},
fail: function() {
error("复制失败~")
}
});
//#endif
/**
* H5端的复制逻辑
*/
// #ifdef H5
if (!document.queryCommandSupported('copy')) { //为了兼容有些浏览器 queryCommandSupported 的判断
// 不支持
error('浏览器不支持')
}
let textarea = document.createElement("textarea")
textarea.value = content
textarea.readOnly = "readOnly"
document.body.appendChild(textarea)
textarea.select() // 选择对象
textarea.setSelectionRange(0, content.length) //核心
let result = document.execCommand("copy") // 执行浏览器复制命令
if (result) {
success("复制成功~")
} else {
error("复制失败,请检查h5中调用该方法的方式,是不是用户点击的方式调用的,如果不是请改为用户点击的方式触发该方法,因为h5中安全性,不能js直接调用!")
}
textarea.remove()
// #endif
}
function getNavHeight(height=0,platform=''){
// 转换字符数值为真正的数值
// #ifdef H5
return 0;
// #endif
// #ifdef APP-PLUS
return height ? height : 44;
// #endif
// #ifdef MP
// 小程序特别处理,让导航栏高度 = 胶囊高度 + 两倍胶囊顶部与状态栏底部的距离之差(相当于同时获得了导航栏底部与胶囊底部的距离)
// 此方法有缺陷,暂不用(会导致少了几个px),采用直接固定值的方式
// return menuButtonInfo.height + (menuButtonInfo.top - this.statusBarHeight) * 2;//导航高度
let h = platform == 'ios' ? 44 : 48;
return height ? height : h;
// #endif
}
//设置缓存
function setDb(name,value,db_time=7200){
let time = (new Date()).getTime();
let data = {
value:value,
time:time,
db_time:db_time
}
uni.setStorageSync(name, data);
}
//获取缓存
function getDb(name){
try{
let res = uni.getStorageSync(name);
if(!res){
return '';
}
let time = (new Date()).getTime();
if((time-res.time)/1000>=res.db_time){
uni.removeStorageSync(name);
return '';
}
return res.value;
}catch(e){
//TODO handle the exception
return '';
}
}
//时间戳转换成日期时间
function getDateTime(unixtime) {
var date = new Date(unixtime);
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;//年月日时分秒
return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
}
function randStr(len) {
const randomChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let result = '';
for (let i=0; i<len; i++) {
result += randomChars.charAt(Math.ceil(Math.random()*100000000) % randomChars.length);
}
return result;
}
export {
strlen,
isWeiXinBrowser,
getQueryString,
getPath,
uniCopy,
getNavHeight,
setDb,
getDb,
getDateTime,
randStr
}