public.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import store from '@/common/store/index.js'
  2. import {
  3. UPLOAD_URL
  4. } from '../../env.js';
  5. // 获取时间
  6. export const getTime = (t) => {
  7. t = Math.ceil(t)
  8. let minute = parseInt(t / 60) === 0 ? '00' : parseInt(t / 60) < 10 ? '0' + parseInt(t / 60) : parseInt(t / 60)
  9. let second = t % 60 < 10 ? '0' + t % 60 : t % 60
  10. let this_time = minute + ':' + second
  11. return this_time
  12. }
  13. // 指定位置插入字符串
  14. export const insertStr = (source, start, newStr) => {
  15. return source.slice(0, start) + newStr + source.slice(start)
  16. }
  17. // 去客服/律师聊天
  18. export const goChat = (type, id, orderid) => {
  19. uni.$u.route({
  20. url: '/pages/app/AI/chat',
  21. params: {
  22. type: type,
  23. id: id ? id : '',
  24. orderid: orderid ? orderid : ''
  25. }
  26. });
  27. }
  28. // 查看文件
  29. export const openFile = (url) => {
  30. if (url) {
  31. console.log(url)
  32. // #ifdef MP-WEIXIN
  33. const fileManage = uni.getFileSystemManager()
  34. // #endif
  35. const type = url.slice(url.lastIndexOf('.') + 1).toLowerCase()
  36. const document = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx']
  37. const images = ['png', 'jpg', 'jpeg', 'gif']
  38. const videos = ['mp4', '3gp', 'm3u8']
  39. const audios = ['MP3', 'WAV', 'FLAV', 'AAC']
  40. if (images.indexOf(type) > -1) {
  41. uni.previewImage({
  42. urls: [url]
  43. })
  44. } else if (videos.indexOf(type) > -1) {
  45. // #ifdef MP-WEIXIN
  46. wx.previewMedia({
  47. sources: [{
  48. url: url,
  49. type: 'video'
  50. }]
  51. })
  52. // #endif
  53. } else {
  54. uni.showLoading({
  55. mask: true,
  56. title: '加载中'
  57. })
  58. uni.downloadFile({
  59. url: url,
  60. success(rel) {
  61. if (document.indexOf(type) > -1) {
  62. console.log(rel)
  63. uni.openDocument({
  64. filePath: rel.tempFilePath,
  65. showMenu: true,
  66. complete(re) {
  67. console.log(re)
  68. }
  69. })
  70. } else {
  71. // #ifdef MP-WEIXIN
  72. fileManage.saveFile({
  73. tempFilePath: rel.tempFilePath,
  74. success(res) {
  75. uni.hideLoading()
  76. uni.showToast({
  77. icon: 'success',
  78. mask: true,
  79. title: '保存成功'
  80. })
  81. },
  82. fail() {
  83. uni.hideLoading()
  84. uni.$u.toast('保存失败,请重试')
  85. }
  86. })
  87. // #endif
  88. // #ifdef APP
  89. uni.saveFile({
  90. tempFilePath: rel.tempFilePath,
  91. success(res) {
  92. uni.hideLoading()
  93. uni.showToast({
  94. icon: 'success',
  95. mask: true,
  96. title: '保存成功'
  97. })
  98. },
  99. fail() {
  100. uni.hideLoading()
  101. uni.$u.toast('保存失败,请重试')
  102. }
  103. })
  104. // #endif
  105. }
  106. },
  107. fail() {
  108. uni.hideLoading()
  109. uni.$u.toast('保存失败,请重试')
  110. },
  111. complete() {
  112. uni.hideLoading()
  113. }
  114. });
  115. }
  116. }
  117. }
  118. // 保存文件
  119. export const downloadFile = (url) => {
  120. if (url) {
  121. // #ifdef MP-WEIXIN
  122. const fileManage = uni.getFileSystemManager()
  123. // #endif
  124. uni.showLoading({
  125. mask: true,
  126. title: '保存中'
  127. })
  128. uni.downloadFile({
  129. url: url,
  130. success(rel) {
  131. const type = url.slice(url.lastIndexOf('.') + 1).toLowerCase()
  132. const document = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx']
  133. const images = ['png', 'jpg', 'jpeg', 'gif']
  134. const videos = ['mp4', '3gp', 'm3u8']
  135. const audios = ['MP3', 'WAV', 'FLAV', 'AAC']
  136. if (images.indexOf(type) > -1) {
  137. uni.saveImageToPhotosAlbum({
  138. filePath: rel.tempFilePath,
  139. success() {
  140. uni.hideLoading()
  141. uni.showToast({
  142. icon: 'success',
  143. mask: true,
  144. title: '保存成功'
  145. })
  146. },
  147. fail() {
  148. uni.hideLoading()
  149. uni.$u.toast('保存失败,请重试')
  150. }
  151. });
  152. } else if (videos.indexOf(type) > -1) {
  153. uni.saveVideoToPhotosAlbum({
  154. filePath: rel.tempFilePath,
  155. success() {
  156. uni.hideLoading()
  157. uni.showToast({
  158. icon: 'success',
  159. mask: true,
  160. title: '保存成功'
  161. })
  162. },
  163. fail() {
  164. uni.hideLoading()
  165. uni.$u.toast('保存失败,请重试')
  166. }
  167. });
  168. } else {
  169. if (document.indexOf(type) > -1) {
  170. uni.hideLoading()
  171. uni.openDocument({
  172. filePath: rel.tempFilePath,
  173. showMenu: true
  174. })
  175. } else {
  176. // #ifdef MP-WEIXIN
  177. fileManage.saveFile({
  178. tempFilePath: rel.tempFilePath,
  179. success(res) {
  180. uni.hideLoading()
  181. uni.showToast({
  182. icon: 'success',
  183. mask: true,
  184. title: '保存成功'
  185. })
  186. },
  187. fail() {
  188. uni.hideLoading()
  189. uni.$u.toast('保存失败,请重试')
  190. }
  191. })
  192. // #endif
  193. // #ifdef APP
  194. uni.saveFile({
  195. tempFilePath: rel.tempFilePath,
  196. success(res) {
  197. uni.hideLoading()
  198. uni.showToast({
  199. icon: 'success',
  200. mask: true,
  201. title: '保存成功'
  202. })
  203. },
  204. fail() {
  205. uni.hideLoading()
  206. uni.$u.toast('保存失败,请重试')
  207. }
  208. })
  209. // #endif
  210. }
  211. }
  212. },
  213. fail() {
  214. uni.hideLoading()
  215. uni.$u.toast('保存失败,请重试')
  216. }
  217. });
  218. }
  219. }
  220. // 格式化文字展示文本域格式文字
  221. export const textFormat = (val) => {
  222. if (val) {
  223. let newString = val.replace(/\n/g, '_@').replace(/\r/g, '_#');
  224. newString = newString.replace(/_#_@/g, '<br/>'); // IE7-8
  225. newString = newString.replace(/_@/g, '<br/>'); // IE9、FF、chrome
  226. newString = newString.replace(/\s/g, '&nbsp;'); // 空格处理
  227. return newString;
  228. }
  229. }
  230. /**
  231. * 乘法
  232. * @param arg1
  233. * @param arg2
  234. * @returns {Number}
  235. */
  236. export const accMul = (arg1, arg2) => {
  237. var m = 0,
  238. s1 = arg1.toString(),
  239. s2 = arg2.toString();
  240. try {
  241. m += s1.split(".")[1].length
  242. } catch (e) {}
  243. try {
  244. m += s2.split(".")[1].length
  245. } catch (e) {}
  246. return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
  247. }
  248. /**
  249. * 除法
  250. * @param arg1
  251. * @param arg2
  252. * @returns {Number}
  253. */
  254. export const accDiv = (arg1, arg2) => {
  255. var t1 = 0,
  256. t2 = 0,
  257. r1, r2;
  258. try {
  259. t1 = arg1.toString().split(".")[1].length
  260. } catch (e) {}
  261. try {
  262. t2 = arg2.toString().split(".")[1].length
  263. } catch (e) {}
  264. r1 = Number(arg1.toString().replace(".", ""))
  265. r2 = Number(arg2.toString().replace(".", ""))
  266. return (r1 / r2) * Math.pow(10, t2 - t1);
  267. }
  268. /**
  269. * 加法
  270. * @param arg1
  271. * @param arg2
  272. * @returns {Number}
  273. */
  274. export const accAdd = (arg1, arg2) => {
  275. var r1, r2, m, c;
  276. try {
  277. r1 = arg1.toString().split(".")[1].length
  278. } catch (e) {
  279. r1 = 0
  280. }
  281. try {
  282. r2 = arg2.toString().split(".")[1].length
  283. } catch (e) {
  284. r2 = 0
  285. }
  286. c = Math.abs(r1 - r2);
  287. m = Math.pow(10, Math.max(r1, r2))
  288. if (c > 0) {
  289. var cm = Math.pow(10, c);
  290. if (r1 > r2) {
  291. arg1 = Number(arg1.toString().replace(".", ""));
  292. arg2 = Number(arg2.toString().replace(".", "")) * cm;
  293. } else {
  294. arg1 = Number(arg1.toString().replace(".", "")) * cm;
  295. arg2 = Number(arg2.toString().replace(".", ""));
  296. }
  297. } else {
  298. arg1 = Number(arg1.toString().replace(".", ""));
  299. arg2 = Number(arg2.toString().replace(".", ""));
  300. }
  301. return (arg1 + arg2) / m
  302. }
  303. /**
  304. * 减法
  305. * @param arg1
  306. * @param arg2
  307. * @returns
  308. */
  309. export const accSub = (arg1, arg2) => {
  310. var r1, r2, m, n;
  311. try {
  312. r1 = arg1.toString().split(".")[1].length
  313. } catch (e) {
  314. r1 = 0
  315. }
  316. try {
  317. r2 = arg2.toString().split(".")[1].length
  318. } catch (e) {
  319. r2 = 0
  320. }
  321. m = Math.pow(10, Math.max(r1, r2));
  322. //last modify by deeka
  323. //动态控制精度长度
  324. n = (r1 >= r2) ? r1 : r2;
  325. return ((arg1 * m - arg2 * m) / m).toFixed(n);
  326. }
  327. export const add0 = (m) => {
  328. return m < 10 ? '0' + m : m
  329. }
  330. export const format = (e) => {
  331. var time = new Date(e);
  332. var y = time.getFullYear();
  333. var m = time.getMonth() + 1;
  334. var d = time.getDate();
  335. return y + '-' + add0(m) + '-' + add0(d);
  336. }
  337. /**
  338. * 格式化时间 月-日
  339. */
  340. export const formatMD = (e) => {
  341. var time = new Date(e);
  342. var m = time.getMonth() + 1;
  343. var d = time.getDate();
  344. return add0(m) + '-' + add0(d);
  345. }
  346. /**
  347. * 保存聊天记录
  348. */
  349. export const setChat = (data, id) => {
  350. uni.getStorage({
  351. key: 'yzychat:message_' + uni.getStorageSync('userInfo').id + '_' + id,
  352. success: function(res) {
  353. // 储存所有消息
  354. // let arr = res.data;
  355. // 优化,只储存 100条数据,超过100条,通过接口获取历史记录
  356. let arr = res.data.slice(-96);
  357. arr.push(data);
  358. uni.setStorageSync('yzychat:message_' + uni.getStorageSync('userInfo').id + '_' + id,
  359. arr);
  360. },
  361. fail: function(res) {
  362. uni.setStorageSync('yzychat:message_' + uni.getStorageSync('userInfo').id + '_' + id,
  363. [data]);
  364. }
  365. });
  366. // 返回原始数据
  367. return data;
  368. }
  369. /** * 文件大小 字节转换单位 * @param size * @returns {string|*} */
  370. export const filterSize = (size) => {
  371. if (!size) return '';
  372. if (size < pow1024(1)) return size + ' B';
  373. if (size < pow1024(2)) return (size / pow1024(1)).toFixed(2) + ' KB';
  374. if (size < pow1024(3)) return (size / pow1024(2)).toFixed(2) + ' MB';
  375. if (size < pow1024(4)) return (size / pow1024(3)).toFixed(2) + ' GB';
  376. return (size / pow1024(4)).toFixed(2) + ' TB'
  377. }
  378. export const pow1024 = (num) => {
  379. return Math.pow(1024, num)
  380. }
  381. // 计算字符串包含某个字符的数量
  382. export const countChar = (str, char) => {
  383. var parts = str.split(char); // 根据指定字符分隔字符串为多个部分
  384. return parts.length - 1; // 返回分隔后的部分数组长度-1(因为最后一个部分不包含指定字符)
  385. }
  386. export const setCache = (page, type, value) => {
  387. const datas = {
  388. ...store.getters.cacheDatas
  389. }
  390. datas[page][type] = value
  391. store.commit('setCacheDatas', datas)
  392. }
  393. export const exportWord = (html) => {
  394. // #ifdef H5
  395. html = html.replace(/<h2>/g, '').replace(/<\/h2>/g, '')
  396. html = html
  397. .replace(/<br>/g, '</p><p>')
  398. .replace(
  399. '<body>',
  400. '<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>'
  401. )
  402. return new Promise((resolve, reject) => {
  403. const blob = new Blob(['\ufeff', html], {
  404. type: 'application/docx'
  405. });
  406. uni.uploadFile({
  407. url: UPLOAD_URL,
  408. file: blob,
  409. header: {
  410. token: `${store.getters.token}`
  411. },
  412. success: (res) => {
  413. if (res.data) {
  414. let data = JSON.parse(res.data)
  415. if (data.code === 1) {
  416. resolve(data.data)
  417. } else {
  418. uni.showToast({
  419. title: '网络不稳定,请重试',
  420. icon: 'none'
  421. })
  422. reject()
  423. }
  424. } else {
  425. uni.showToast({
  426. title: '网络不稳定,请重试',
  427. icon: 'none'
  428. })
  429. reject()
  430. }
  431. },
  432. fail(err) {
  433. console.log(err)
  434. uni.showToast({
  435. title: '网络不稳定,请重试',
  436. icon: 'none'
  437. })
  438. reject()
  439. }
  440. })
  441. });
  442. // #endif
  443. }
  444. /**
  445. * Parse the time to string
  446. * @param {(Object|string|number)} time
  447. * @param {string} cFormat
  448. * @returns {string | null}
  449. */
  450. export function parseDate(time, cFormat) {
  451. if (arguments.length === 0 || !time) {
  452. return null
  453. }
  454. const format = cFormat || '{y}-{m}-{d} {h}:{i}'
  455. const date = new Date(time)
  456. const formatObj = {
  457. y: date.getFullYear(),
  458. m: date.getMonth() + 1,
  459. d: date.getDate(),
  460. h: date.getHours(),
  461. i: date.getMinutes(),
  462. s: date.getSeconds(),
  463. a: date.getDay()
  464. }
  465. const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
  466. const value = formatObj[key]
  467. // Note: getDay() returns 0 on Sunday
  468. if (key === 'a') {
  469. return ['日', '一', '二', '三', '四', '五', '六'][value]
  470. }
  471. return value.toString().padStart(2, '0')
  472. })
  473. return time_str
  474. }