Files
car-system-app/utils/util.js
2024-07-13 13:50:04 +08:00

703 lines
18 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { TOKENNAME, baseUrl, imgPath } from '../config.js';
import { ossUpload } from '@/js_sdk/AliOSS/oss.js'
import { getToken } from '@/utils/auth';
export default {
/**
* opt object | string
* to_url object | string
* 例:
* this.Tips('/pages/test/test'); 跳转不提示
* this.Tips({title:'提示'},'/pages/test/test'); 提示并跳转
* this.Tips({title:'提示'},{tab:1,url:'/pages/index/index'}); 提示并跳转值table上
* tab=1 一定时间后跳转至 table上
* tab=2 一定时间后跳转至非 table上
* tab=3 一定时间后返回上页面
* tab=4 关闭所有页面跳转至非table上
* tab=5 关闭当前页面跳转至table上
*/
Tips: function(opt, to_url) {
if (typeof opt == 'string') {
to_url = opt;
opt = {};
}
let title = opt.title || '',
icon = opt.icon || 'none',
endtime = opt.endtime || 2000,
success = opt.success;
if (title) uni.showToast({
title: title,
icon: icon,
duration: endtime,
success
})
if (to_url != undefined) {
if (typeof to_url == 'object') {
let tab = to_url.tab || 1,
url = to_url.url || '';
switch (tab) {
case 1:
//一定时间后跳转至 table
setTimeout(function() {
uni.switchTab({
url: url
})
}, endtime);
break;
case 2:
//跳转至非table页面
setTimeout(function() {
uni.navigateTo({
url: url,
})
}, endtime);
break;
case 3:
//返回上页面
setTimeout(function() {
uni.navigateBack();
}, endtime);
break;
case 4:
//关闭当前所有页面跳转至非table页面
setTimeout(function() {
uni.reLaunch({
url: url,
})
}, endtime);
break;
case 5:
//关闭当前页面跳转至非table页面
setTimeout(function() {
uni.redirectTo({
url: url,
})
}, endtime);
break;
}
} else if (typeof to_url == 'function') {
setTimeout(function() {
to_url && to_url();
}, endtime);
} else {
//没有提示时跳转不延迟
setTimeout(function() {
uni.navigateTo({
url: to_url,
})
}, title ? endtime : 0);
}
}
},
getOnlinePath(path) {
return imgPath + path
},
/**
* 移除数组中的某个数组并组成新的数组返回
* @param array array 需要移除的数组
* @param int index 需要移除的数组的键值
* @param string | int 值
* @return array
*
*/
ArrayRemove: function(array, index, value) {
const valueArray = [];
if (array instanceof Array) {
for (let i = 0; i < array.length; i++) {
if (typeof index == 'number' && array[index] != i) {
valueArray.push(array[i]);
} else if (typeof index == 'string' && array[i][index] != value) {
valueArray.push(array[i]);
}
}
}
return valueArray;
},
/*
* 视频上传
* @param Object opt
* @param Boolean isShowLoading 是否开启加载框
* @param callable progress 进度监听
* @param callable successCallback 成功执行方法 data
* @param callable errorCallback 失败执行方法
*/
uploadVideo: function(opt,progress,isShowLoading=false, successCallback, errorCallback) {
uni.chooseVideo({
sourceType: ['album', 'camera'],
compressed:false,
success: (res) => {
console.log('视频信息',res);
uni.setStorageSync('video_info', res);
if(isShowLoading)uni.showLoading({mask:true,title:'上传中...'});
ossUpload(res.tempFilePath,res.tempFilePath,opt.dir||'lxkimage/public/product/',(p)=>{
if (progress != null && typeof(progress) === 'function') {
progress(p)
}
}, successCallback, errorCallback);
}
})
},
/*
* 单图上传
* @param object opt
* @param callable progress 进度监听
* @param callable successCallback 成功执行方法 data
* @param callable errorCallback 失败执行方法
*/
uploadImageOne: function(opt,progress, successCallback, errorCallback) {
uni.chooseImage({
count:opt.count || 1,
sizeType:opt.sizeType||['original'],
sourceType:opt.sourceType||['album', 'camera'],
success: (res) => {
// uni.showLoading({mask:true,title:'上传中...'});
res.tempFiles.forEach(item=>{
ossUpload(item.path,item.path,opt.dir||'lxkimage/public/maintain/',(p)=>{
if (progress != null && typeof(progress) === 'function') {
progress(p)
}
}, successCallback, errorCallback);
});
}
});
},
uploadFile(urlPath, localPath, opt, message) {
console.log(urlPath, localPath, opt, message);
let that = this;
return new Promise(async (resolve) => {
//启动上传等待中...
if (message) uni.showLoading({
title: message,
});
uni.uploadFile({
url: urlPath,
filePath: localPath,
name: opt.name || 'pics',
header: {
// #ifdef MP
"Content-Type": "multipart/form-data",
// #endif
// [TOKENNAME]: getToken()
},
success: function(res) {
uni.hideLoading();
if (res.statusCode == 403) {
that.Tips({
title: res.data
});
} else {
let data = res.data ? JSON.parse(res.data) : {};
if (data.code == 200) {
resolve(data.data);
} else {
that.Tips({
title: data.message
});
}
}
},
fail: function(res) {
uni.hideLoading();
that.Tips({
title: res
});
}
})
})
},
/**
* 处理服务器扫码带进来的参数
* @param string param 扫码携带参数
* @param string k 整体分割符 默认为:&
* @param string p 单个分隔符 默认为:=
* @return object
*
*/
// #ifdef MP
getUrlParams: function(param, k, p) {
if (typeof param != 'string') return {};
k = k ? k : '&'; //整体参数分隔符
p = p ? p : '='; //单个参数分隔符
var value = {};
if (param.indexOf(k) !== -1) {
param = param.split(k);
for (var val in param) {
if (param[val].indexOf(p) !== -1) {
var item = param[val].split(p);
value[item[0]] = item[1];
}
}
} else if (param.indexOf(p) !== -1) {
var item = param.split(p);
value[item[0]] = item[1];
} else {
return param;
}
return value;
},
/**根据格式组装公共参数
* @param {Object} value
*/
formatMpQrCodeData(value) {
let values = value.split(',');
let result = {};
if (values.length === 2) {
let v1 = values[0].split(":");
if (v1[0] === 'pid') {
result.spread = v1[1];
} else {
result.id = v1[1];
}
let v2 = values[1].split(":");
if (v2[0] === 'pid') {
result.spread = v2[1];
} else {
result.id = v2[1];
}
} else {
result.spread = values[0].split(":")[1];
}
return result;
},
// #endif
/*
* 合并数组
*/
SplitArray(list, sp) {
if (typeof list != 'object') return [];
if (sp === undefined) sp = [];
for (var i = 0; i < list.length; i++) {
sp.push(list[i]);
}
return sp;
},
trim(str) {
return String.prototype.trim.call(str);
},
/**
* 微信地址导入
* @param uploadUrl 上传接口地址
* @param filePath 上传文件路径
* @param successCallback success回调
* @param errorCallback err回调
*/
addressWxImport() {
let that = this;
uni.showLoading({
title: '加载中...'
});
return new Promise((resolve, reject) => {
uni.authorize({
scope: 'scope.address',
success: function(res) {
uni.hideLoading();
uni.chooseAddress({
success(resd) {
resolve(resd);
},
fail: function(err) {
if (err.errMsg == 'chooseAddress:cancel') return that.Tips({
title: '取消选择'
});
},
})
},
fail: function(err) {
uni.hideLoading();
uni.showModal({
title: '您已拒绝导入微信地址权限',
content: '是否进入权限管理,调整授权?',
success(err) {
if (err.confirm) {
uni.openSetting({
success: function(err) {
}
});
} else if (err.cancel) {
return that.Tips({
title: '已取消!'
});
}
}
})
}
})
});
},
/**
* 将时间转化为几小时
* @param {Object} data data必须是日期格式不能是时间戳
*/
getDateDiff(data) {
let datas = data;
// 传进来的data必须是日期格式不能是时间戳
//var str = data;
//将字符串转换成时间格式
var timePublish = new Date(data);
var timeNow = new Date();
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var month = day * 30;
var result = "2";
var diffValue = timeNow - timePublish;
var diffMonth = diffValue / month;
var diffWeek = diffValue / (7 * day);
var diffDay = diffValue / day;
var diffHour = diffValue / hour;
var diffMinute = diffValue / minute;
if (diffValue < 0) {} else if (diffMonth > 3) {
result = timePublish.getFullYear() + "-";
result += timePublish.getMonth() + "-";
result += timePublish.getDate();
} else if (diffMonth > 1) {
result = parseInt(diffMonth) + "月前";
} else if (diffWeek > 1) {
result = parseInt(diffWeek) + "周前";
} else if (diffDay > 1) {
result = parseInt(diffDay) + "天前";
} else if (diffHour > 1) {
result = parseInt(diffHour) + "小时前";
} else if (diffMinute > 1) {
result = parseInt(diffMinute) + "分钟前";
} else {
result = "刚刚";
}
return result;
},
$h: {
//除法函数,用来得到精确的除法结果
//说明javascript的除法结果会有误差在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
//调用:$h.Div(arg1,arg2)
//返回值arg1除以arg2的精确结果
Div: function(arg1, arg2) {
arg1 = parseFloat(arg1);
arg2 = parseFloat(arg2);
var t1 = 0,
t2 = 0,
r1, r2;
try {
t1 = arg1.toString().split(".")[1].length;
} catch (e) {}
try {
t2 = arg2.toString().split(".")[1].length;
} catch (e) {}
r1 = Number(arg1.toString().replace(".", ""));
r2 = Number(arg2.toString().replace(".", ""));
return this.Mul(r1 / r2, Math.pow(10, t2 - t1));
},
//加法函数,用来得到精确的加法结果
//说明javascript的加法结果会有误差在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
//调用:$h.Add(arg1,arg2)
//返回值arg1加上arg2的精确结果
Add: function(arg1, arg2) {
arg2 = parseFloat(arg2);
var r1, r2, m;
try {
r1 = arg1.toString().split(".")[1].length
} catch (e) {
r1 = 0
}
try {
r2 = arg2.toString().split(".")[1].length
} catch (e) {
r2 = 0
}
m = Math.pow(100, Math.max(r1, r2));
return (this.Mul(arg1, m) + this.Mul(arg2, m)) / m;
},
//减法函数,用来得到精确的减法结果
//说明javascript的加法结果会有误差在两个浮点数相加的时候会比较明显。这个函数返回较为精确的减法结果。
//调用:$h.Sub(arg1,arg2)
//返回值arg1减去arg2的精确结果
Sub: function(arg1, arg2) {
arg1 = parseFloat(arg1);
arg2 = parseFloat(arg2);
var r1, r2, m, n;
try {
r1 = arg1.toString().split(".")[1].length
} catch (e) {
r1 = 0
}
try {
r2 = arg2.toString().split(".")[1].length
} catch (e) {
r2 = 0
}
m = Math.pow(10, Math.max(r1, r2));
//动态控制精度长度
n = (r1 >= r2) ? r1 : r2;
return ((this.Mul(arg1, m) - this.Mul(arg2, m)) / m).toFixed(n);
},
//乘法函数,用来得到精确的乘法结果
//说明javascript的乘法结果会有误差在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
//调用:$h.Mul(arg1,arg2)
//返回值arg1乘以arg2的精确结果
Mul: function(arg1, arg2) {
arg1 = parseFloat(arg1);
arg2 = parseFloat(arg2);
var m = 0,
s1 = arg1.toString(),
s2 = arg2.toString();
try {
m += s1.split(".")[1].length
} catch (e) {}
try {
m += s2.split(".")[1].length
} catch (e) {}
return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
},
},
/**
* 设置APP中间遮掩层
*/
TabBarMidButtonTap(){
// #ifdef APP-PLUS
let screenWidth = plus.screen.resolutionWidth;
let margin = 45;
let left1 = margin / 360 * screenWidth;
//先创建遮罩层
let nvMask = new plus.nativeObj.View("nvMask", {
top: '0px',
left: '0px',
height: '100%',
width: '100%',
backgroundColor: 'rgba(0,0,0,0.2)'
});
//创建底部菜单
let nvImageMenu = new plus.nativeObj.View("nvImageMenu", {
bottom: '0px',
left: '0px',
height: '264px',
width: '100%',
backgroundColor: 'rgba(255,255,255,0)'
});
//绘制底部圆形
nvImageMenu.drawRect({ color: 'rgb(255,255,255)', radius: '20px' },{top:'15px'});
nvMask.addEventListener("click",()=>{ //处理遮罩层点击
nvMask.hide();
nvImageMenu.hide();
});
//绘制底部图标菜单的内容
nvImageMenu.draw([
{
tag: 'rect',
position: {
top: '50px',
left:'auto',
width:screenWidth-60,
height: '60px'
},
rectStyles:{
radius:'10px',
color:'#e7e7e7'
}
},
{
tag: 'img',
id: 'bf',
src: 'https://qicheoss.oss-cn-shanghai.aliyuncs.com/lxkimage/presets/bf.png',
position: {
top: '68px',
left: left1,
width: '26px',
height: '26px'
}
},
{
tag: 'font',
id: 'bftext',
text: '内容发布',
textStyles: {
size: '15px'
},
position: {
top: '70px',
left: left1+37,
width: '75px',
height: '20px'
}
},
{
tag: 'img',
id: 'rig',
src: 'https://qicheoss.oss-cn-shanghai.aliyuncs.com/lxkimage/presets/rig.png',
position: {
top: '72px',
right: left1,
width: '8px',
height: '14px'
}
},
{
tag: 'rect',
position: {
top: '130px',
left:'auto',
width:screenWidth-60,
height: '60px'
},
rectStyles:{
radius:'10px',
color:'#e7e7e7'
}
},
{
tag: 'img',
id: 'zb',
src: 'https://qicheoss.oss-cn-shanghai.aliyuncs.com/lxkimage/presets/zb.png',
position: {
top: '148px',
left: left1+2,
width: '23px',
height: '26px'
}
},
{
tag: 'font',
id: 'zbtext',
text: '开直播',
textStyles: {
size: '15px'
},
position: {
top: '150px',
left: left1+32,
width: '75px',
height: '20px'
}
},
{
tag: 'img',
id: 'rig2',
src: 'https://qicheoss.oss-cn-shanghai.aliyuncs.com/lxkimage/presets/rig.png',
position: {
top: '152px',
right: left1,
width: '8px',
height: '14px'
}
},
{
tag: 'img',
id: 'close1',
src: 'https://qicheoss.oss-cn-shanghai.aliyuncs.com/lxkimage/presets/close1.png',
position: {
bottom: '28px',
left:'auto',
height:'27px',
width:'45px'
}
},
]);
nvImageMenu.addEventListener('click',(e)=>{
if (e.screenY > plus.screen.resolutionHeight - 54) { //点击了底部取消按钮
nvMask.hide();
nvImageMenu.hide();
}else if (e.clientX < 30 || e.clientX > screenWidth - 30 || e.clientY < 30){
return false;
}else{
if(e.clientY >52 &&e.clientY <110&&e.clientX>left1){
nvMask.hide();
nvImageMenu.hide();
uni.$u.route('/pages/ContentDistribution/ContentDistribution');
}else if(e.clientY >130 &&e.clientY <190&&e.clientX>left1){
nvMask.hide();
nvImageMenu.hide();
uni.$u.route('/pages/discover/discover_liveBroadcast/discover_liveBroadcast');
}
return false;
}
});
//锁定
plus.screen.lockOrientation('portrait-primary');
//点击中间按钮
uni.onTabBarMidButtonTap(()=>{
nvMask.show()
nvImageMenu.show() //5+应支持从底部向上弹出的动画
});
// #endif
},
/**
* 获取系统剪贴板内容
*/
ClipboardData(){
uni.getClipboardData({
success: (res) => {
if(res.data){
let data = res.data.split(':');
let type = data[0].split(' ');
if(!data[1])return false;
let content = data[1].split('。。。')[0].split(',')[1];
if(type.length>1){
switch(type[1]){
case 'goodS':
uni.showModal({
title: '识别到商品,是否跳转当前页面',
content: content,
cancelColor: '#eeeeee',
confirmColor: '#FF0000',
success(res2) {
uni.setClipboardData({data:'',showToast:false});
if (res2.confirm) {
uni.$u.route('/pages/goods/goods_details/index',{id:type[0]});
}
}
});
break;
case 'discoverDetails':
uni.showModal({
title: '识别到笔记,是否跳转当前页面',
content: content,
cancelColor: '#eeeeee',
confirmColor: '#FF0000',
success(res2) {
uni.setClipboardData({data:'',showToast:false});
if (res2.confirm) {
uni.$u.route('/pages/discover/discover_details/index',{noteId:type[0]});
}
}
});
break;
case 'discoverVideo':
uni.showModal({
title: '识别到视频,是否跳转当前页面',
content: content,
cancelColor: '#eeeeee',
confirmColor: '#FF0000',
success(res2) {
uni.setClipboardData({data:'',showToast:false});
if (res2.confirm) {
uni.$u.route('/pages/discover/discover_video/index',{noteId:type[0]});
}
}
});
break;
case 'Showcase':
uni.showModal({
title: '识别到橱窗商品,是否跳转当前页面',
content: content,
cancelColor: '#eeeeee',
confirmColor: '#FF0000',
success(res2) {
uni.setClipboardData({data:'',showToast:false});
if (res2.confirm) {
let arr = type[0].split('_');
uni.$u.route('/pages/goods/goods_details/index',{id:arr[0],authUid:arr[1],isShareShort:1});
}
}
});
break;
default :
break;
}
}
}
}
})
}
}