cqcy.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. import cqcyCode from "@/utils/cqcyCode";
  2. /**
  3. * 通用js方法封装处理
  4. */
  5. // 日期格式化
  6. export function parseTime(time, pattern) {
  7. if (arguments.length === 0 || !time) {
  8. return null
  9. }
  10. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  11. let date
  12. if (typeof time === 'object') {
  13. date = time
  14. } else {
  15. if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
  16. time = parseInt(time)
  17. } else if (typeof time === 'string') {
  18. time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
  19. }
  20. if ((typeof time === 'number') && (time.toString().length === 10)) {
  21. time = time * 1000
  22. }
  23. date = new Date(time)
  24. }
  25. const formatObj = {
  26. y: date.getFullYear(),
  27. m: date.getMonth() + 1,
  28. d: date.getDate(),
  29. h: date.getHours(),
  30. i: date.getMinutes(),
  31. s: date.getSeconds(),
  32. a: date.getDay()
  33. }
  34. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  35. let value = formatObj[key]
  36. // Note: getDay() returns 0 on Sunday
  37. if (key === 'a') {
  38. return ['日', '一', '二', '三', '四', '五', '六'][value]
  39. }
  40. if (result.length > 0 && value < 10) {
  41. value = '0' + value
  42. }
  43. return value || 0
  44. })
  45. return time_str
  46. }
  47. // 表单重置
  48. export function resetForm(refName) {
  49. if (this.$refs[refName]) {
  50. this.$refs[refName].resetFields();
  51. }
  52. }
  53. // 添加日期范围
  54. export function addDateRange(params, dateRange, propName) {
  55. let search = params;
  56. search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
  57. dateRange = Array.isArray(dateRange) ? dateRange : [];
  58. if (typeof (propName) === 'undefined') {
  59. search.params['beginTime'] = dateRange[0];
  60. search.params['endTime'] = dateRange[1];
  61. } else {
  62. search.params['begin' + propName] = dateRange[0];
  63. search.params['end' + propName] = dateRange[1];
  64. }
  65. return search;
  66. }
  67. // 回显数据字典
  68. export function selectDictLabel(datas, value) {
  69. if (value === undefined) {
  70. return "";
  71. }
  72. var actions = [];
  73. Object.keys(datas).some((key) => {
  74. if (datas[key].value == ('' + value)) {
  75. actions.push(datas[key].label);
  76. return true;
  77. }
  78. })
  79. if (actions.length === 0) {
  80. actions.push(value);
  81. }
  82. return actions.join('');
  83. }
  84. // 回显数据字典(字符串数组)
  85. export function selectDictLabels(datas, value, separator) {
  86. if (value === undefined) {
  87. return "";
  88. }
  89. var actions = [];
  90. var currentSeparator = undefined === separator ? "," : separator;
  91. var temp = value.split(currentSeparator);
  92. Object.keys(value.split(currentSeparator)).some((val) => {
  93. var match = false;
  94. Object.keys(datas).some((key) => {
  95. if (datas[key].value == ('' + temp[val])) {
  96. actions.push(datas[key].label + currentSeparator);
  97. match = true;
  98. }
  99. })
  100. if (!match) {
  101. actions.push(temp[val] + currentSeparator);
  102. }
  103. })
  104. return actions.join('').substring(0, actions.join('').length - 1);
  105. }
  106. // 字符串格式化(%s )
  107. export function sprintf(str) {
  108. var args = arguments, flag = true, i = 1;
  109. str = str.replace(/%s/g, function () {
  110. var arg = args[i++];
  111. if (typeof arg === 'undefined') {
  112. flag = false;
  113. return '';
  114. }
  115. return arg;
  116. });
  117. return flag ? str : '';
  118. }
  119. // 转换字符串,undefined,null等转化为""
  120. export function parseStrEmpty(str) {
  121. if (!str || str == "undefined" || str == "null") {
  122. return "";
  123. }
  124. return str;
  125. }
  126. // 数据合并
  127. export function mergeRecursive(source, target) {
  128. for (var p in target) {
  129. try {
  130. if (target[p].constructor == Object) {
  131. source[p] = mergeRecursive(source[p], target[p]);
  132. } else {
  133. source[p] = target[p];
  134. }
  135. } catch (e) {
  136. source[p] = target[p];
  137. }
  138. }
  139. return source;
  140. };
  141. /**
  142. * 构造树型结构数据
  143. * @param {*} data 数据源
  144. * @param {*} id id字段 默认 'id'
  145. * @param {*} parentId 父节点字段 默认 'parentId'
  146. * @param {*} children 孩子节点字段 默认 'children'
  147. */
  148. export function handleTree(data, id, parentId, children) {
  149. let config = {
  150. id: id || 'id',
  151. parentId: parentId || 'parentId',
  152. childrenList: children || 'children'
  153. };
  154. var childrenListMap = {};
  155. var nodeIds = {};
  156. var tree = [];
  157. for (let d of data) {
  158. let parentId = d[config.parentId];
  159. if (childrenListMap[parentId] == null) {
  160. childrenListMap[parentId] = [];
  161. }
  162. nodeIds[d[config.id]] = d;
  163. childrenListMap[parentId].push(d);
  164. }
  165. for (let d of data) {
  166. let parentId = d[config.parentId];
  167. if (nodeIds[parentId] == null) {
  168. tree.push(d);
  169. }
  170. }
  171. for (let t of tree) {
  172. adaptToChildrenList(t);
  173. }
  174. function adaptToChildrenList(o) {
  175. if (childrenListMap[o[config.id]] !== null) {
  176. o[config.childrenList] = childrenListMap[o[config.id]];
  177. }
  178. if (o[config.childrenList]) {
  179. for (let c of o[config.childrenList]) {
  180. adaptToChildrenList(c);
  181. }
  182. }
  183. }
  184. return tree;
  185. }
  186. /**
  187. * 参数处理
  188. * @param {*} params 参数
  189. */
  190. export function tansParams(params) {
  191. let result = ''
  192. for (const propName of Object.keys(params)) {
  193. const value = params[propName];
  194. var part = encodeURIComponent(propName) + "=";
  195. if (value !== null && value !== "" && typeof (value) !== "undefined") {
  196. if (typeof value === 'object') {
  197. for (const key of Object.keys(value)) {
  198. if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
  199. let params = propName + '[' + key + ']';
  200. var subPart = encodeURIComponent(params) + "=";
  201. result += subPart + encodeURIComponent(value[key]) + "&";
  202. }
  203. }
  204. } else {
  205. result += part + encodeURIComponent(value) + "&";
  206. }
  207. }
  208. }
  209. return result
  210. }
  211. // 验证是否为blob格式
  212. export async function blobValidate(data) {
  213. try {
  214. const text = await data.text();
  215. JSON.parse(text);
  216. return false;
  217. } catch (error) {
  218. return true;
  219. }
  220. }
  221. /**
  222. * 创建加载框
  223. * @param tips
  224. * @returns {Promise<*>}
  225. */
  226. export function showLoading(_this, tips) {
  227. const loading = _this.$loading({
  228. lock: true,
  229. text: tips ? tips : '加载中···',
  230. spinner: 'el-icon-loading',
  231. background: 'rgba(0, 0, 0, 0.7)'
  232. });
  233. setTimeout(() => {
  234. if (loading) loading.close()
  235. }, 30 * 1000)
  236. return loading
  237. }
  238. /**
  239. * HOST 地址验证
  240. * @param url
  241. * @returns {boolean}
  242. */
  243. export function testHost(url) {
  244. const strRegex = '^(http(s)?://)' // (https或http或ftp):// 可有可无
  245. + '(([\\w_!~*\'()\\.&=+$%-]+: )?[\\w_!~*\'()\\.&=+$%-]+@)?' // ftp的user@ 可有可无
  246. + '(([0-9]{1,3}\\.){3}[0-9]{1,3}' // IP形式的URL- 3位数字.3位数字.3位数字.3位数字
  247. + '|' // 允许IP和DOMAIN(域名)
  248. + '(localhost)|' // 匹配localhost
  249. + '([\\w_!~*\'()-]+\\.)*' // 域名- 至少一个[英文或数字_!~*\'()-]加上.
  250. + '\\w+\\.' // 一级域名 -英文或数字 加上.
  251. + '[a-zA-Z]{1,6})' // 顶级域名- 1-6位英文
  252. + '(:[0-9]{1,5})?' // 端口- :80 ,1-5位数字
  253. + '((/?)|' // url无参数结尾 - 斜杆或这没有
  254. + '(/[\\w_!~*\'()\\.;?:@&=+$,%#-]+)+/?)$'; // 请求参数结尾- 英文或数字和[]内的各种字符
  255. const re = new RegExp(strRegex, 'i'); // 大小写不敏感
  256. if (!re.test(encodeURI(url))) {
  257. return false
  258. }
  259. return true
  260. }
  261. /**
  262. * 获取当前时间
  263. * @param type
  264. * @returns {string}
  265. */
  266. export function getNowFormatDate(type) {
  267. let date = new Date(),
  268. year = date.getFullYear(), //获取完整的年份(4位)
  269. month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
  270. strDate = date.getDate(), // 获取当前日(1-31)
  271. hours = date.getHours(), // 获取时(1-24)
  272. minutes = date.getMinutes(), // 获取分(1-60)
  273. seconds = date.getSeconds() // 获取秒(1-60)
  274. if (month < 10) month = `0${month}` // 如果月份是个位数,在前面补0
  275. if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数,在前面补0
  276. if (hours < 10) hours = `0${hours}` // 如果时是个位数,在前面补0
  277. if (minutes < 10) minutes = `0${minutes}` // 如果分是个位数,在前面补0
  278. if (seconds < 10) seconds = `0${seconds}` // 如果秒是个位数,在前面补0
  279. if (type === 'yyyy-MM-dd') {
  280. return `${year}-${month}-${strDate}`
  281. }
  282. if (type === 'yyyy-MM-dd HH:mm:ss') {
  283. return `${year}-${month}-${strDate} ${hours}:${minutes}:${seconds}`
  284. }
  285. return `${year}-${month}-${strDate} ${hours}:${minutes}:${seconds}`
  286. }
  287. /**
  288. * 获取显示的数组
  289. * @param node
  290. * @returns {*}
  291. */
  292. export function traverseNode(node) {
  293. if (Object.prototype.toString.call(node) === '[object Array]') {
  294. return node.map(t => traverseNode(t))
  295. }
  296. let data = node.data
  297. data.visible = node.visible
  298. return data
  299. }
  300. /**
  301. * 树节点过滤显示节点
  302. * @param arr
  303. * @returns {*}
  304. */
  305. export function traverseVisible(arr) {
  306. return arr.filter(t => {
  307. let visible = t.visible
  308. if (!visible) {
  309. return false
  310. }
  311. if (t.children) {
  312. t.children = traverseVisible(t.children)
  313. }
  314. delete t.visible
  315. return visible
  316. })
  317. }
  318. /**
  319. * 过滤树信息
  320. * @param list
  321. * @param value
  322. * @returns {*}
  323. */
  324. export function deepTreeFilter(list, value) {
  325. return list.filter(item => {
  326. if (!item.isLeaf) {
  327. item.children = deepTreeFilter(item.children, value)
  328. return true
  329. }
  330. return item.label.indexOf(value) !== -1
  331. })
  332. }
  333. /**
  334. * 树数据添加属性
  335. * @param list
  336. * @param key
  337. */
  338. export function deepTreeKey(list, key) {
  339. list.forEach(item => {
  340. if (item.children && item.children.length > 0) {
  341. deepTreeKey(item.children, key)
  342. } else {
  343. item[key] = true
  344. }
  345. })
  346. }
  347. /**
  348. * 排序比较
  349. * @param {string} propertyName 排序的属性名
  350. * @param {string} sort ascending(升序)/descending(降序)
  351. * @return {function}
  352. */
  353. export function customCompare(propertyName, sort) {
  354. // 判断是否为数字
  355. function isNumberV(val) {
  356. // 非负浮点数
  357. const regPos = /^\d+(\.\d+)?$/
  358. // 负浮点数
  359. const regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/
  360. if (regPos.test(val) || regNeg.test(val)) {
  361. return true
  362. } else {
  363. return false
  364. }
  365. }
  366. return function (obj1, obj2) {
  367. let value1 = obj1[propertyName]
  368. let value2 = obj2[propertyName]
  369. // 数字类型的比较
  370. if (isNumberV(value1) || isNumberV(value2)) {
  371. if (sort === 'ascending') {
  372. return value1 - value2
  373. } else {
  374. return value2 - value1
  375. }
  376. }
  377. // 布尔值的比较:利用减法-转化true 和 false
  378. // true => 1 false ⇒ 0
  379. // true-false => 1 false-true => -1
  380. // 下面方法是按照先false后true的顺序排序,如果想先true后false,调整value1-value2 和 value2 - value1的顺序即可
  381. else if (_.isBoolean(value1) && _.isBoolean(value2)) {
  382. if (sort === 'ascending') {
  383. return value1 - value2
  384. } else {
  385. return value2 - value1
  386. }
  387. }
  388. // 字符比较使用localeCompare()
  389. else {
  390. value1 = value1 ? value1 : '默认值'
  391. const res = value1.localeCompare(value2, 'zh')
  392. return sort === 'ascending' ? res : -res
  393. }
  394. }
  395. }
  396. /**
  397. * 获取在线表格数据信息
  398. * @returns {string|null}
  399. */
  400. export function getLuckysheetConfig() {
  401. if (!luckysheet) return null
  402. let ls = luckysheet.getLuckysheetfile()
  403. if (!ls) return null
  404. ls.forEach((item, index) => {
  405. if(item.chart) {
  406. item.chart.forEach((chart, i) => {
  407. ls[index].chart[i] = {
  408. ...ls[index].chart[i],
  409. chartOptions: {...chartmix.default.getChartJson(chart.chart_id)}
  410. }
  411. let div = document.getElementById(chart.chart_id + '_c');
  412. if(div.style) {
  413. ls[index].chart[i].left = parseInt(div.style.left)
  414. ls[index].chart[i].top = parseInt(div.style.top)
  415. ls[index].chart[i].width = parseInt(div.style.width)
  416. ls[index].chart[i].height = parseInt(div.style.height)
  417. }
  418. })
  419. }
  420. })
  421. return JSON.stringify(ls)
  422. }
  423. /**
  424. * 显示alert弹出层
  425. * @param _this
  426. * @param title
  427. * @param e
  428. * @param callback
  429. */
  430. export function showAlertWin(_this, title, e, callback) {
  431. if (e && e.name == 'TypeError') {
  432. console.error(e)
  433. return
  434. }
  435. _this.$alert(e, title ? title : cqcyCode[100], {
  436. confirmButtonText: '确定',
  437. showClose: false,
  438. callback: action => {
  439. resetConfirmWinPosition()
  440. if (callback) callback(action)
  441. }
  442. })
  443. }
  444. /**
  445. * 显示消息提示
  446. * @param _this
  447. * @param title
  448. * @param msg
  449. * @param callback
  450. */
  451. export function showAlertMsgWin(_this, title, msg, callback) {
  452. _this.$alert(msg, title ? title : cqcyCode[100], {
  453. confirmButtonText: '确定',
  454. showClose: false,
  455. callback: action => {
  456. resetConfirmWinPosition()
  457. if (callback) callback(action)
  458. }
  459. })
  460. }
  461. /**
  462. * 显示询问框
  463. * @param _this
  464. * @param title
  465. * @param msg
  466. * @param callback
  467. */
  468. export function showConfirmWin(_this, title, msg, callback) {
  469. _this.$confirm(msg, title ? title : cqcyCode[100], {
  470. confirmButtonText: '确定',
  471. cancelButtonText: '取消',
  472. customClass: 'close_confirm',
  473. closeOnClickModal: false,
  474. type: 'warning'
  475. }).then(() => {
  476. resetConfirmWinPosition()
  477. if (callback) callback()
  478. }).catch(() => {
  479. resetConfirmWinPosition()
  480. })
  481. }
  482. /**
  483. * 显示输入框
  484. * @param _this
  485. * @param title
  486. * @param msg
  487. * @param val
  488. * @param valid
  489. * @param callback
  490. * @param cancelCallback
  491. */
  492. export function showPromptWin(_this, title, msg, val, valid, callback, cancelCallback) {
  493. _this.$prompt(msg, title ? title : cqcyCode[100], {
  494. confirmButtonText: '确定',
  495. cancelButtonText: '取消',
  496. customClass: 'close_confirm',
  497. closeOnClickModal: false,
  498. inputValue: val ? val : '',
  499. inputValidator: valid
  500. }).then(({value}) => {
  501. resetConfirmWinPosition()
  502. if (callback) callback(value)
  503. }).catch(() => {
  504. resetConfirmWinPosition()
  505. if (cancelCallback) cancelCallback()
  506. })
  507. }
  508. /**
  509. * 重置询问框初始位置
  510. */
  511. export function resetConfirmWinPosition() {
  512. setTimeout(() => {
  513. const dragDom = document.querySelector('.el-message-box')
  514. if (dragDom) dragDom.style = ''
  515. }, 500)
  516. }