first commit
This commit is contained in:
108
utils/fun.js
Normal file
108
utils/fun.js
Normal file
@@ -0,0 +1,108 @@
|
||||
import {
|
||||
getOssUploadParams
|
||||
} from "@/api/api.js"
|
||||
|
||||
//预览图片
|
||||
export const lookImg = (e) => {
|
||||
uni.previewImage({
|
||||
urls: e
|
||||
});
|
||||
}
|
||||
|
||||
//拨打电话
|
||||
export const call = (e) => uni.makePhoneCall({
|
||||
phoneNumber: e
|
||||
});
|
||||
|
||||
// 剪贴板
|
||||
export const copy = (e) => uni.setClipboardData({
|
||||
data: e,
|
||||
success: function() {
|
||||
console.log('success');
|
||||
}
|
||||
});;
|
||||
|
||||
//获取超链接参数
|
||||
export const urlQuery = (e) => {
|
||||
let search = e
|
||||
search = search.split('?')[1];
|
||||
const pairs = search ? search.split('&') : [];
|
||||
const query = {};
|
||||
for (let i = 0; i < pairs.length; ++i) {
|
||||
const [key, value] = pairs[i].split('=');
|
||||
query[key] = query[key] || decodeURIComponent(value);
|
||||
}
|
||||
return query
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
export const uploadFiles = (urls) => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let res = await getOssUploadParams()
|
||||
const fd = {
|
||||
policy: res.policy,
|
||||
OSSAccessKeyId: res.accessId,
|
||||
success_action_status: 200,
|
||||
signature: res.signature,
|
||||
key: res.dir + Date.now() + Math.floor(100000 + Math.random() *
|
||||
900000) + '.' +
|
||||
urls.substring(urls.lastIndexOf(".") + 1),
|
||||
};
|
||||
uni.uploadFile({
|
||||
url: res.host, // Example, not a real endpoint
|
||||
filePath: urls,
|
||||
header: {
|
||||
"content-type": 'multipart/form-data'
|
||||
},
|
||||
name: 'file',
|
||||
formData: fd,
|
||||
success: (_res) => {
|
||||
resolve(res.host + '/' + fd.key);
|
||||
},
|
||||
fail: (err) => {
|
||||
console.log(err);
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
console.log();
|
||||
});
|
||||
}
|
||||
|
||||
export function getUrlParam(url, paramName) {
|
||||
// 正则表达式模式,用于匹配URL中的参数部分。正则表达式的含义是匹配不包含 ?、&、= 的字符作为参数名,之后是等号和不包含 & 的字符作为参数值
|
||||
let pattern = /([^?&=]+)=([^&]+)/g;
|
||||
let params = {};
|
||||
|
||||
// match用于存储正则匹配的结果
|
||||
let match;
|
||||
// while 循环和正则表达式 exec 方法来迭代匹配URL中的参数
|
||||
while ((match = pattern.exec(url)) !== null) {
|
||||
// 在字符串url中循环匹配pattern,并对每个匹配项进行解码操作,将解码后的键和值分别存储在key和value变量中
|
||||
let key = decodeURIComponent(match[1]);
|
||||
let value = decodeURIComponent(match[2]);
|
||||
|
||||
if (params[key]) {
|
||||
if (Array.isArray(params[key])) {
|
||||
params[key].push(value);
|
||||
} else {
|
||||
params[key] = [params[key], value];
|
||||
}
|
||||
} else {
|
||||
// 参数名在 params 对象中不存在,将该参数名和对应的值添加到 params 对象中
|
||||
params[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
if (!paramName) {
|
||||
// 没有传入参数名称, 返回含有所有参数的对象params
|
||||
return params;
|
||||
} else {
|
||||
if (params[paramName]) {
|
||||
return params[paramName];
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user