first commit
This commit is contained in:
190
utils/fun.js
Normal file
190
utils/fun.js
Normal file
@@ -0,0 +1,190 @@
|
||||
import {
|
||||
proxyObj
|
||||
} from '@/api/http.ts'
|
||||
|
||||
//预览图片
|
||||
export const toast = (title, icon = 'none') => {
|
||||
uni.showToast({
|
||||
title: title,
|
||||
icon: icon
|
||||
});
|
||||
}
|
||||
|
||||
//预览图片
|
||||
export const lookImg = (e) => {
|
||||
uni.previewImage({
|
||||
urls: Array.isArray(e) ? e : [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 timeFormat = (time, fmt = 'yyyy-MM-dd hh:mm:ss', targetTimezone = 8) => {
|
||||
try {
|
||||
if (!time) {
|
||||
return '';
|
||||
}
|
||||
if (typeof time === 'string' && !isNaN(time)) time = Number(time);
|
||||
// 其他更多是格式化有如下:
|
||||
// yyyy-MM-dd hh:mm:ss|yyyy年MM月dd日 hh时MM分等,可自定义组合
|
||||
let date;
|
||||
if (typeof time === 'number') {
|
||||
if (time.toString().length == 10) time *= 1000;
|
||||
date = new Date(time);
|
||||
} else {
|
||||
date = time;
|
||||
}
|
||||
|
||||
const dif = date.getTimezoneOffset();
|
||||
const timeDif = dif * 60 * 1000 + targetTimezone * 60 * 60 * 1000;
|
||||
const east8time = date.getTime() + timeDif;
|
||||
|
||||
date = new Date(east8time);
|
||||
let opt = {
|
||||
'M+': date.getMonth() + 1, //月份
|
||||
'd+': date.getDate(), //日
|
||||
'h+': date.getHours(), //小时
|
||||
'm+': date.getMinutes(), //分
|
||||
's+': date.getSeconds(), //秒
|
||||
'q+': Math.floor((date.getMonth() + 3) / 3), //季度
|
||||
S: date.getMilliseconds() //毫秒
|
||||
};
|
||||
if (/(y+)/.test(fmt)) {
|
||||
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
|
||||
}
|
||||
for (let k in opt) {
|
||||
if (new RegExp('(' + k + ')').test(fmt)) {
|
||||
fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? opt[k] : ('00' + opt[k]).substr(('' + opt[k])
|
||||
.length));
|
||||
}
|
||||
}
|
||||
return fmt;
|
||||
} catch (err) {
|
||||
// 若格式错误,则原值显示
|
||||
return time;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const uploadFiles = (urls) => {
|
||||
console.log(urls);
|
||||
return new Promise(async (resolve, reject) => {
|
||||
uni.uploadFile({
|
||||
url: `${proxyObj[process.env.NODE_ENV || 'development']}/app-api/infra/file/upload`, // 上传接口地址
|
||||
filePath: urls, //必须通过 uni.chooseImage、uni.chooseMessageFile 等接口获取临时路径,不可直接使用静态路径
|
||||
name: 'file', // 文件对应的 key
|
||||
formData: {},
|
||||
header: {
|
||||
"Tenant-id": "134",
|
||||
},
|
||||
success: (uploadRes) => {
|
||||
console.log(uploadRes);
|
||||
const data = JSON.parse(uploadRes.data);
|
||||
if (data.code == 0) {
|
||||
resolve(data.data);
|
||||
} else {
|
||||
reject(data.msg);
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 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 '';
|
||||
}
|
||||
}
|
||||
}
|
||||
17
utils/mpShare.js
Normal file
17
utils/mpShare.js
Normal file
@@ -0,0 +1,17 @@
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
mpShare: {
|
||||
title: '', // 默认为小程序名称
|
||||
path: '', // 默认为当前页面路径
|
||||
imageUrl: '' // 默认为当前页面的截图
|
||||
}
|
||||
}
|
||||
},
|
||||
onShareAppMessage() {
|
||||
return this.mpShare;
|
||||
},
|
||||
onShareTimeline() {
|
||||
return this.mpShare;
|
||||
}
|
||||
}
|
||||
149
utils/sok.js
Normal file
149
utils/sok.js
Normal file
@@ -0,0 +1,149 @@
|
||||
import store from '@/store/index.js';
|
||||
import config from '@/config'
|
||||
|
||||
|
||||
export const init = () => {
|
||||
if(!uni.getStorageSync('token')){
|
||||
console.log('sok-未登录');
|
||||
return
|
||||
}else{
|
||||
console.log('sok-登录');
|
||||
}
|
||||
|
||||
|
||||
var timeout = 10000;
|
||||
var timeoutObj = null;
|
||||
var timeAnt = 0
|
||||
|
||||
setInterval(() => {
|
||||
timeAnt++
|
||||
}, 1000)
|
||||
|
||||
openConnection()
|
||||
|
||||
function checkOpenSocket() {
|
||||
uni.sendSocketMessage({
|
||||
data: 'ping',
|
||||
success: res => {
|
||||
timeAnt = 0
|
||||
return;
|
||||
},
|
||||
fail: err => {
|
||||
// 未连接打开websocket连接
|
||||
openConnection();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function openConnection() {
|
||||
// 打开连接
|
||||
// uni.closeSocket(); // 确保已经关闭后再重新打开
|
||||
uni.connectSocket({
|
||||
url: `wss://${config.sok}/ws/deviceUpMessage?token=${uni.getStorageSync('token')}`,
|
||||
success(res) {
|
||||
// console.log('连接成功 connectSocket=', res);
|
||||
},
|
||||
fail(err) {
|
||||
// console.log('连接失败 connectSocket=', err);
|
||||
}
|
||||
});
|
||||
uni.onSocketOpen(res => {
|
||||
checkOpenSocket()
|
||||
// console.log('连接成功');
|
||||
});
|
||||
uni.onSocketError(err => {
|
||||
// console.log('连接失败');
|
||||
});
|
||||
|
||||
onSocketMessage(); // 打开成功监听服务器返回的消息
|
||||
}
|
||||
// 打开成功监听服务器返回的消息
|
||||
function onSocketMessage() {
|
||||
// 消息
|
||||
timeout = 10000;
|
||||
timeoutObj = null;
|
||||
uni.onSocketMessage(res => {
|
||||
timeAnt = 0
|
||||
getSocketMsg(res.data); // 监听到有新服务器消息
|
||||
});
|
||||
}
|
||||
// 监听到有新服器消息
|
||||
function getSocketMsg(data) {
|
||||
console.log(data);
|
||||
var obj;
|
||||
let pages = getCurrentPages() //获取加载的页面
|
||||
let currentPage = pages[pages.length - 1] //获取当前页面的对象
|
||||
let url = currentPage.route //当前页面url
|
||||
if (data !== 'pong') {
|
||||
store.commit('setSokStatus', 1);
|
||||
uni.hideLoading()
|
||||
obj = JSON.parse(data)
|
||||
store.commit('setCommand', obj.command);
|
||||
store.commit('setDataObj', obj);
|
||||
// 插枪状态
|
||||
console.log(obj);
|
||||
if (obj.insertStatus || obj.insertStatus === 0) {
|
||||
store.commit('setInsertStatus', obj.insertStatus);
|
||||
}
|
||||
if (obj.command == 'startResult' && obj.result == '-1') {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '启动失败,请重试',
|
||||
showCancel: false,
|
||||
confirmText: "确认",
|
||||
success: () => {}
|
||||
})
|
||||
}
|
||||
if (obj.command == 'startResult' && obj.result == '-2') {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '启动超时,请重试',
|
||||
showCancel: false,
|
||||
confirmText: "确认",
|
||||
success: () => {}
|
||||
})
|
||||
}
|
||||
if (obj.command == 'startResult' && obj.result == '01') {
|
||||
if (url != 'pageOrder/recharge/recharge') {
|
||||
uni.navigateTo({
|
||||
url: `/pageOrder/recharge/recharge?transactionNo=${obj.transactionNo}&type=order`
|
||||
});
|
||||
}
|
||||
}
|
||||
if (obj.command == 'transactionResult') {
|
||||
if (url == 'pageOrder/recharge/recharge') {
|
||||
uni.navigateBack()
|
||||
}
|
||||
}
|
||||
}else{
|
||||
// store.commit('setSokStatus', 0);
|
||||
}
|
||||
timeAnt = 0
|
||||
reset(); // 检测心跳reset,防止长时间连接导致连接关闭
|
||||
}
|
||||
// 检测心跳reset
|
||||
function reset() {
|
||||
clearInterval(timeoutObj);
|
||||
start(); // 启动心跳
|
||||
}
|
||||
// 启动心跳 start
|
||||
function start() {
|
||||
timeoutObj = setInterval(function() {
|
||||
if (timeAnt > 10) {
|
||||
// console.log('连接失败重新连接....');
|
||||
openConnection();
|
||||
return
|
||||
}
|
||||
uni.sendSocketMessage({
|
||||
data: 'ping',
|
||||
success: res => {
|
||||
// console.log(res);
|
||||
},
|
||||
fail: err => {
|
||||
// console.log('连接失败重新连接....');
|
||||
openConnection();
|
||||
}
|
||||
});
|
||||
}, timeout);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user