123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- 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, '<br/>'); // IE7-8
- newString = newString.replace(/_@/g, '<br/>'); // 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(/<h2>/g, '').replace(/<\/h2>/g, '')
- html = html
- .replace(/<br>/g, '</p><p>')
- .replace(
- '<body>',
- '<body><style>h1{text-align: center;margin-bottom: 2em;}p{text-align: justify;line-height: 1.5;text-indent: 2em;margin-bottom: 2em;}ol{text-align: justify;line-height: 1.5;padding-left: 2em;margin-bottom: 2em;}</style>'
- )
- 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
- }
|