import store from '@/common/store/index.js' import { UPLOAD_URL } from '../../env.js'; // 获取时间 export const getTime = (t) => { t = Math.ceil(t) let minute = parseInt(t / 60) === 0 ? '00' : parseInt(t / 60) < 10 ? '0' + parseInt(t / 60) : parseInt(t / 60) let second = t % 60 < 10 ? '0' + t % 60 : t % 60 let this_time = minute + ':' + second return this_time } // 指定位置插入字符串 export const insertStr = (source, start, newStr) => { return source.slice(0, start) + newStr + source.slice(start) } // 去客服/律师聊天 export const goChat = (type, id, orderid) => { uni.$u.route({ url: '/pages/app/AI/chat', params: { type: type, id: id ? id : '', orderid: orderid ? orderid : '' } }); } // 查看文件 export const openFile = (url) => { if (url) { console.log(url) // #ifdef MP-WEIXIN const fileManage = uni.getFileSystemManager() // #endif const type = url.slice(url.lastIndexOf('.') + 1).toLowerCase() const document = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx'] const images = ['png', 'jpg', 'jpeg', 'gif'] const videos = ['mp4', '3gp', 'm3u8'] const audios = ['MP3', 'WAV', 'FLAV', 'AAC'] if (images.indexOf(type) > -1) { uni.previewImage({ urls: [url] }) } else if (videos.indexOf(type) > -1) { // #ifdef MP-WEIXIN wx.previewMedia({ sources: [{ url: url, type: 'video' }] }) // #endif } else { uni.showLoading({ mask: true, title: '加载中' }) uni.downloadFile({ url: url, success(rel) { if (document.indexOf(type) > -1) { console.log(rel) uni.openDocument({ filePath: rel.tempFilePath, showMenu: true, complete(re) { console.log(re) } }) } else { // #ifdef MP-WEIXIN fileManage.saveFile({ tempFilePath: rel.tempFilePath, success(res) { uni.hideLoading() uni.showToast({ icon: 'success', mask: true, title: '保存成功' }) }, fail() { uni.hideLoading() uni.$u.toast('保存失败,请重试') } }) // #endif // #ifdef APP uni.saveFile({ tempFilePath: rel.tempFilePath, success(res) { uni.hideLoading() uni.showToast({ icon: 'success', mask: true, title: '保存成功' }) }, fail() { uni.hideLoading() uni.$u.toast('保存失败,请重试') } }) // #endif } }, fail() { uni.hideLoading() uni.$u.toast('保存失败,请重试') }, complete() { uni.hideLoading() } }); } } } // 保存文件 export const downloadFile = (url) => { if (url) { // #ifdef MP-WEIXIN const fileManage = uni.getFileSystemManager() // #endif uni.showLoading({ mask: true, title: '保存中' }) uni.downloadFile({ url: url, success(rel) { const type = url.slice(url.lastIndexOf('.') + 1).toLowerCase() const document = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx'] const images = ['png', 'jpg', 'jpeg', 'gif'] const videos = ['mp4', '3gp', 'm3u8'] const audios = ['MP3', 'WAV', 'FLAV', 'AAC'] if (images.indexOf(type) > -1) { uni.saveImageToPhotosAlbum({ filePath: rel.tempFilePath, success() { uni.hideLoading() uni.showToast({ icon: 'success', mask: true, title: '保存成功' }) }, fail() { uni.hideLoading() uni.$u.toast('保存失败,请重试') } }); } else if (videos.indexOf(type) > -1) { uni.saveVideoToPhotosAlbum({ filePath: rel.tempFilePath, success() { uni.hideLoading() uni.showToast({ icon: 'success', mask: true, title: '保存成功' }) }, fail() { uni.hideLoading() uni.$u.toast('保存失败,请重试') } }); } else { if (document.indexOf(type) > -1) { uni.hideLoading() uni.openDocument({ filePath: rel.tempFilePath, showMenu: true }) } else { // #ifdef MP-WEIXIN fileManage.saveFile({ tempFilePath: rel.tempFilePath, success(res) { uni.hideLoading() uni.showToast({ icon: 'success', mask: true, title: '保存成功' }) }, fail() { uni.hideLoading() uni.$u.toast('保存失败,请重试') } }) // #endif // #ifdef APP uni.saveFile({ tempFilePath: rel.tempFilePath, success(res) { uni.hideLoading() uni.showToast({ icon: 'success', mask: true, title: '保存成功' }) }, fail() { uni.hideLoading() uni.$u.toast('保存失败,请重试') } }) // #endif } } }, fail() { uni.hideLoading() uni.$u.toast('保存失败,请重试') } }); } } // 格式化文字展示文本域格式文字 export const textFormat = (val) => { if (val) { let newString = val.replace(/\n/g, '_@').replace(/\r/g, '_#'); newString = newString.replace(/_#_@/g, '
'); // IE7-8 newString = newString.replace(/_@/g, '
'); // IE9、FF、chrome newString = newString.replace(/\s/g, ' '); // 空格处理 return newString; } } /** * 乘法 * @param arg1 * @param arg2 * @returns {Number} */ export const accMul = (arg1, 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) } /** * 除法 * @param arg1 * @param arg2 * @returns {Number} */ export const accDiv = (arg1, 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 (r1 / r2) * Math.pow(10, t2 - t1); } /** * 加法 * @param arg1 * @param arg2 * @returns {Number} */ export const accAdd = (arg1, arg2) => { var r1, r2, m, c; try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 } try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 } c = Math.abs(r1 - r2); m = Math.pow(10, Math.max(r1, r2)) if (c > 0) { var cm = Math.pow(10, c); if (r1 > r2) { arg1 = Number(arg1.toString().replace(".", "")); arg2 = Number(arg2.toString().replace(".", "")) * cm; } else { arg1 = Number(arg1.toString().replace(".", "")) * cm; arg2 = Number(arg2.toString().replace(".", "")); } } else { arg1 = Number(arg1.toString().replace(".", "")); arg2 = Number(arg2.toString().replace(".", "")); } return (arg1 + arg2) / m } /** * 减法 * @param arg1 * @param arg2 * @returns */ export const accSub = (arg1, 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)); //last modify by deeka //动态控制精度长度 n = (r1 >= r2) ? r1 : r2; return ((arg1 * m - arg2 * m) / m).toFixed(n); } export const add0 = (m) => { return m < 10 ? '0' + m : m } export const format = (e) => { var time = new Date(e); var y = time.getFullYear(); var m = time.getMonth() + 1; var d = time.getDate(); return y + '-' + add0(m) + '-' + add0(d); } /** * 格式化时间 月-日 */ export const formatMD = (e) => { var time = new Date(e); var m = time.getMonth() + 1; var d = time.getDate(); return add0(m) + '-' + add0(d); } /** * 保存聊天记录 */ export const setChat = (data, id) => { uni.getStorage({ key: 'yzychat:message_' + uni.getStorageSync('userInfo').id + '_' + id, success: function(res) { // 储存所有消息 // let arr = res.data; // 优化,只储存 100条数据,超过100条,通过接口获取历史记录 let arr = res.data.slice(-96); arr.push(data); uni.setStorageSync('yzychat:message_' + uni.getStorageSync('userInfo').id + '_' + id, arr); }, fail: function(res) { uni.setStorageSync('yzychat:message_' + uni.getStorageSync('userInfo').id + '_' + id, [data]); } }); // 返回原始数据 return data; } /** * 文件大小 字节转换单位 * @param size * @returns {string|*} */ export const filterSize = (size) => { if (!size) return ''; if (size < pow1024(1)) return size + ' B'; if (size < pow1024(2)) return (size / pow1024(1)).toFixed(2) + ' KB'; if (size < pow1024(3)) return (size / pow1024(2)).toFixed(2) + ' MB'; if (size < pow1024(4)) return (size / pow1024(3)).toFixed(2) + ' GB'; return (size / pow1024(4)).toFixed(2) + ' TB' } export const pow1024 = (num) => { return Math.pow(1024, num) } // 计算字符串包含某个字符的数量 export const countChar = (str, char) => { var parts = str.split(char); // 根据指定字符分隔字符串为多个部分 return parts.length - 1; // 返回分隔后的部分数组长度-1(因为最后一个部分不包含指定字符) } export const setCache = (page, type, value) => { const datas = { ...store.getters.cacheDatas } datas[page][type] = value store.commit('setCacheDatas', datas) } export const exportWord = (html) => { // #ifdef H5 html = html.replace(/

/g, '').replace(/<\/h2>/g, '') html = html .replace(/
/g, '

') .replace( '', '' ) return new Promise((resolve, reject) => { const blob = new Blob(['\ufeff', html], { type: 'application/docx' }); uni.uploadFile({ url: UPLOAD_URL, file: blob, header: { token: `${store.getters.token}` }, success: (res) => { if (res.data) { let data = JSON.parse(res.data) if (data.code === 1) { resolve(data.data) } else { uni.showToast({ title: '网络不稳定,请重试', icon: 'none' }) reject() } } else { uni.showToast({ title: '网络不稳定,请重试', icon: 'none' }) reject() } }, fail(err) { console.log(err) uni.showToast({ title: '网络不稳定,请重试', icon: 'none' }) reject() } }) }); // #endif } /** * Parse the time to string * @param {(Object|string|number)} time * @param {string} cFormat * @returns {string | null} */ export function parseDate(time, cFormat) { if (arguments.length === 0 || !time) { return null } const format = cFormat || '{y}-{m}-{d} {h}:{i}' const date = new Date(time) const formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() } const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => { const value = formatObj[key] // Note: getDay() returns 0 on Sunday if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] } return value.toString().padStart(2, '0') }) return time_str }