You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

481 lines
14 KiB

  1. import * as api from '@/api/api'
  2. import { isURL } from '@/utils/validate'
  3. import onlineCommons from '@jeecg/antd-online-beta220'
  4. export function timeFix() {
  5. const time = new Date()
  6. const hour = time.getHours()
  7. return hour < 9 ? '早上好' : (hour <= 11 ? '上午好' : (hour <= 13 ? '中午好' : (hour < 20 ? '下午好' : '晚上好')))
  8. }
  9. export function welcome() {
  10. const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
  11. let index = Math.floor((Math.random()*arr.length))
  12. return arr[index]
  13. }
  14. /**
  15. * 触发 window.resize
  16. */
  17. export function triggerWindowResizeEvent() {
  18. let event = document.createEvent('HTMLEvents')
  19. event.initEvent('resize', true, true)
  20. event.eventType = 'message'
  21. window.dispatchEvent(event)
  22. }
  23. /**
  24. * 过滤对象中为空的属性
  25. * @param obj
  26. * @returns {*}
  27. */
  28. export function filterObj(obj) {
  29. if (!(typeof obj == 'object')) {
  30. return;
  31. }
  32. for ( let key in obj) {
  33. if (obj.hasOwnProperty(key)
  34. && (obj[key] == null || obj[key] == undefined || obj[key] === '')) {
  35. delete obj[key];
  36. }
  37. }
  38. return obj;
  39. }
  40. /**
  41. * 时间格式化
  42. * @param value
  43. * @param fmt
  44. * @returns {*}
  45. */
  46. export function formatDate(value, fmt) {
  47. let regPos = /^\d+(\.\d+)?$/;
  48. if(regPos.test(value)){
  49. //如果是数字
  50. let getDate = new Date(value);
  51. let o = {
  52. 'M+': getDate.getMonth() + 1,
  53. 'd+': getDate.getDate(),
  54. 'h+': getDate.getHours(),
  55. 'm+': getDate.getMinutes(),
  56. 's+': getDate.getSeconds(),
  57. 'q+': Math.floor((getDate.getMonth() + 3) / 3),
  58. 'S': getDate.getMilliseconds()
  59. };
  60. if (/(y+)/.test(fmt)) {
  61. fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 - RegExp.$1.length))
  62. }
  63. for (let k in o) {
  64. if (new RegExp('(' + k + ')').test(fmt)) {
  65. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  66. }
  67. }
  68. return fmt;
  69. }else{
  70. //TODO
  71. value = value.trim();
  72. return value.substr(0,fmt.length);
  73. }
  74. }
  75. // 生成首页路由
  76. export function generateIndexRouter(data) {
  77. let indexRouter = [{
  78. path: '/',
  79. name: 'dashboard',
  80. //component: () => import('@/components/layouts/BasicLayout'),
  81. component: resolve => require(['@/components/layouts/TabLayout'], resolve),
  82. meta: { title: '首页' },
  83. redirect: '/dashboard/analysis',
  84. children: [
  85. ...generateChildRouters(data)
  86. ]
  87. },{
  88. "path": "*", "redirect": "/404", "hidden": true
  89. }]
  90. return indexRouter;
  91. }
  92. // 生成嵌套路由(子路由)
  93. function generateChildRouters (data) {
  94. const routers = [];
  95. for (let item of data) {
  96. let component = "";
  97. if(item.component.indexOf("layouts")>=0){
  98. component = "components/"+item.component;
  99. }else{
  100. component = "views/"+item.component;
  101. }
  102. // eslint-disable-next-line
  103. let URL = (item.meta.url|| '').replace(/{{([^}}]+)?}}/g, (s1, s2) => eval(s2)) // URL支持{{ window.xxx }}占位符变量
  104. if (isURL(URL)) {
  105. item.meta.url = URL;
  106. }
  107. //online菜单路由加载逻辑
  108. let componentPath
  109. if(item.component=="modules/online/cgform/OnlCgformHeadList"){
  110. componentPath = onlineCommons.OnlCgformHeadList
  111. }else if(item.component=="modules/online/cgform/OnlCgformCopyList"){
  112. componentPath = onlineCommons.OnlCgformCopyList
  113. }else if(item.component=="modules/online/cgform/auto/OnlCgformAutoList"){
  114. componentPath = onlineCommons.OnlCgformAutoList
  115. }else if(item.component=="modules/online/cgform/auto/OnlCgformTreeList"){
  116. componentPath = onlineCommons.OnlCgformTreeList
  117. }else if(item.component=="modules/online/cgform/auto/erp/OnlCgformErpList"){
  118. componentPath = onlineCommons.OnlCgformErpList
  119. }else if(item.component=="modules/online/cgform/auto/innerTable/OnlCgformInnerTableList"){
  120. componentPath = onlineCommons.OnlCgformInnerTableList
  121. }else if(item.component=="modules/online/cgreport/OnlCgreportHeadList"){
  122. componentPath = onlineCommons.OnlCgreportHeadList
  123. }else if(item.component=="modules/online/cgreport/auto/OnlCgreportAutoList"){
  124. componentPath = onlineCommons.OnlCgreportAutoList
  125. }else{
  126. componentPath = resolve => require(['@/' + component+'.vue'], resolve)
  127. }
  128. let menu = {
  129. path: item.path,
  130. name: item.name,
  131. redirect:item.redirect,
  132. component: componentPath,
  133. hidden:item.hidden,
  134. //component:()=> import(`@/views/${item.component}.vue`),
  135. meta: {
  136. title:item.meta.title ,
  137. icon: item.meta.icon,
  138. url:item.meta.url ,
  139. permissionList:item.meta.permissionList,
  140. keepAlive:item.meta.keepAlive,
  141. /*update_begin author:wuxianquan date:20190908 for:赋值 */
  142. internalOrExternal:item.meta.internalOrExternal
  143. /*update_end author:wuxianquan date:20190908 for:赋值 */
  144. }
  145. }
  146. if(item.alwaysShow){
  147. menu.alwaysShow = true;
  148. menu.redirect = menu.path;
  149. }
  150. if (item.children && item.children.length > 0) {
  151. menu.children = [...generateChildRouters( item.children)];
  152. }
  153. //--update-begin----author:scott---date:20190320------for:根据后台菜单配置,判断是否路由菜单字段,动态选择是否生成路由(为了支持参数URL菜单)------
  154. //判断是否生成路由
  155. if(item.route && item.route === '0'){
  156. //console.log(' 不生成路由 item.route: '+item.route);
  157. //console.log(' 不生成路由 item.path: '+item.path);
  158. }else{
  159. routers.push(menu);
  160. }
  161. //--update-end----author:scott---date:20190320------for:根据后台菜单配置,判断是否路由菜单字段,动态选择是否生成路由(为了支持参数URL菜单)------
  162. }
  163. return routers
  164. }
  165. /**
  166. * 深度克隆对象、数组
  167. * @param obj 被克隆的对象
  168. * @return 克隆后的对象
  169. */
  170. export function cloneObject(obj) {
  171. return JSON.parse(JSON.stringify(obj))
  172. }
  173. /**
  174. * 随机生成数字
  175. *
  176. * 示例:生成长度为 12 的随机数:randomNumber(12)
  177. * 示例:生成 3~23 之间的随机数:randomNumber(3, 23)
  178. *
  179. * @param1 最小值 | 长度
  180. * @param2 最大值
  181. * @return int 生成后的数字
  182. */
  183. export function randomNumber() {
  184. // 生成 最小值 到 最大值 区间的随机数
  185. const random = (min, max) => {
  186. return Math.floor(Math.random() * (max - min + 1) + min)
  187. }
  188. if (arguments.length === 1) {
  189. let [length] = arguments
  190. // 生成指定长度的随机数字,首位一定不是 0
  191. let nums = [...Array(length).keys()].map((i) => (i > 0 ? random(0, 9) : random(1, 9)))
  192. return parseInt(nums.join(''))
  193. } else if (arguments.length >= 2) {
  194. let [min, max] = arguments
  195. return random(min, max)
  196. } else {
  197. return Number.NaN
  198. }
  199. }
  200. /**
  201. * 随机生成字符串
  202. * @param length 字符串的长度
  203. * @param chats 可选字符串区间(只会生成传入的字符串中的字符)
  204. * @return string 生成的字符串
  205. */
  206. export function randomString(length, chats) {
  207. if (!length) length = 1
  208. if (!chats) chats = '0123456789qwertyuioplkjhgfdsazxcvbnm'
  209. let str = ''
  210. for (let i = 0; i < length; i++) {
  211. let num = randomNumber(0, chats.length - 1)
  212. str += chats[num]
  213. }
  214. return str
  215. }
  216. /**
  217. * 随机生成uuid
  218. * @return string 生成的uuid
  219. */
  220. export function randomUUID() {
  221. let chats = '0123456789abcdef'
  222. return randomString(32, chats)
  223. }
  224. /**
  225. * 下划线转驼峰
  226. * @param string
  227. * @returns {*}
  228. */
  229. export function underLine2CamelCase(string){
  230. return string.replace( /_([a-z])/g, function( all, letter ) {
  231. return letter.toUpperCase();
  232. });
  233. }
  234. /**
  235. * 判断是否显示办理按钮
  236. * @param bpmStatus
  237. * @returns {*}
  238. */
  239. export function showDealBtn(bpmStatus){
  240. if(bpmStatus!="1"&&bpmStatus!="3"&&bpmStatus!="4"){
  241. return true;
  242. }
  243. return false;
  244. }
  245. /**
  246. * 增强CSS,可以在页面上输出全局css
  247. * @param css 要增强的css
  248. * @param id style标签的id,可以用来清除旧样式
  249. */
  250. export function cssExpand(css, id) {
  251. let style = document.createElement('style')
  252. style.type = "text/css"
  253. style.innerHTML = `@charset "UTF-8"; ${css}`
  254. // 清除旧样式
  255. if (id) {
  256. let $style = document.getElementById(id)
  257. if ($style != null) $style.outerHTML = ''
  258. style.id = id
  259. }
  260. // 应用新样式
  261. document.head.appendChild(style)
  262. }
  263. /** 用于js增强事件,运行JS代码,可以传参 */
  264. // options 所需参数:
  265. // 参数名 类型 说明
  266. // vm VueComponent vue实例
  267. // event Object event对象
  268. // jsCode String 待执行的js代码
  269. // errorMessage String 执行出错后的提示(控制台)
  270. export function jsExpand(options = {}) {
  271. // 绑定到window上的keyName
  272. let windowKeyName = 'J_CLICK_EVENT_OPTIONS'
  273. if (typeof window[windowKeyName] != 'object') {
  274. window[windowKeyName] = {}
  275. }
  276. // 随机生成JS增强的执行id,防止冲突
  277. let id = randomString(16, 'qwertyuioplkjhgfdsazxcvbnm'.toUpperCase())
  278. // 封装按钮点击事件
  279. let code = `
  280. (function (o_${id}) {
  281. try {
  282. (function (globalEvent, vm) {
  283. ${options.jsCode}
  284. })(o_${id}.event, o_${id}.vm)
  285. } catch (e) {
  286. o_${id}.error(e)
  287. }
  288. o_${id}.done()
  289. })(window['${windowKeyName}']['EVENT_${id}'])
  290. `
  291. // 创建script标签
  292. const script = document.createElement('script')
  293. // 将需要传递的参数挂载到window对象上
  294. window[windowKeyName]['EVENT_' + id] = {
  295. vm: options.vm,
  296. event: options.event,
  297. // 当执行完成时,无论如何都会调用的回调事件
  298. done() {
  299. // 执行完后删除新增的 script 标签不会撤销执行结果(已产生的结果不会被撤销)
  300. script.outerHTML = ''
  301. delete window[windowKeyName]['EVENT_' + id]
  302. },
  303. // 当js运行出错的时候调用的事件
  304. error(e) {
  305. console.group(`${options.errorMessage || '用户自定义JS增强代码运行出错'}(${new Date()})`)
  306. console.error(e)
  307. console.groupEnd()
  308. }
  309. }
  310. // 将事件挂载到document中
  311. script.innerHTML = code
  312. document.body.appendChild(script)
  313. }
  314. /**
  315. * 重复值验证工具方法
  316. *
  317. * 使用示例:
  318. * { validator: (rule, value, callback) => validateDuplicateValue('sys_fill_rule', 'rule_code', value, this.model.id, callback) }
  319. *
  320. * @param tableName 被验证的表名
  321. * @param fieldName 被验证的字段名
  322. * @param fieldVal 被验证的值
  323. * @param dataId 数据ID,可空
  324. * @param callback
  325. */
  326. export function validateDuplicateValue(tableName, fieldName, fieldVal, dataId, callback) {
  327. if (fieldVal) {
  328. let params = { tableName, fieldName, fieldVal, dataId }
  329. api.duplicateCheck(params).then(res => {
  330. res['success'] ? callback() : callback(res['message'])
  331. }).catch(err => {
  332. callback(err.message || err)
  333. })
  334. } else {
  335. callback()
  336. }
  337. }
  338. /**
  339. * 根据编码校验规则code,校验传入的值是否合法
  340. *
  341. * 使用示例:
  342. * { validator: (rule, value, callback) => validateCheckRule('common', value, callback) }
  343. *
  344. * @param ruleCode 编码校验规则 code
  345. * @param value 被验证的值
  346. * @param callback
  347. */
  348. export function validateCheckRule(ruleCode, value, callback) {
  349. if (ruleCode && value) {
  350. value = encodeURIComponent(value)
  351. api.checkRuleByCode({ ruleCode, value }).then(res => {
  352. res['success'] ? callback() : callback(res['message'])
  353. }).catch(err => {
  354. callback(err.message || err)
  355. })
  356. } else {
  357. callback()
  358. }
  359. }
  360. /**
  361. * 如果值不存在就 push 进数组,反之不处理
  362. * @param array 要操作的数据
  363. * @param value 要添加的值
  364. * @param key 可空,如果比较的是对象,可能存在地址不一样但值实际上是一样的情况,可以传此字段判断对象中唯一的字段,例如 id。不传则直接比较实际值
  365. * @returns {boolean} 成功 push 返回 true,不处理返回 false
  366. */
  367. export function pushIfNotExist(array, value, key) {
  368. for (let item of array) {
  369. if (key && (item[key] === value[key])) {
  370. return false
  371. } else if (item === value) {
  372. return false
  373. }
  374. }
  375. array.push(value)
  376. return true
  377. }
  378. /**
  379. * 可用于判断是否成功
  380. * @type {symbol}
  381. */
  382. export const succeedSymbol = Symbol()
  383. /**
  384. * 可用于判断是否失败
  385. * @type {symbol}
  386. */
  387. export const failedSymbol = Symbol()
  388. /**
  389. * 使 promise 无论如何都会 resolve,除非传入的参数不是一个Promise对象或返回Promise对象的方法
  390. * 一般用在 Promise.all 中
  391. *
  392. * @param promise 可传Promise对象或返回Promise对象的方法
  393. * @returns {Promise<any>}
  394. */
  395. export function alwaysResolve(promise) {
  396. return new Promise((resolve, reject) => {
  397. let p = promise
  398. if (typeof promise === 'function') {
  399. p = promise()
  400. }
  401. if (p instanceof Promise) {
  402. p.then(data => {
  403. resolve({ type: succeedSymbol, data })
  404. }).catch(error => {
  405. resolve({ type: failedSymbol, error })
  406. })
  407. } else {
  408. reject('alwaysResolve: 传入的参数不是一个Promise对象或返回Promise对象的方法')
  409. }
  410. })
  411. }
  412. /**
  413. * 简单实现防抖方法
  414. *
  415. * 防抖(debounce)函数在第一次触发给定的函数时,不立即执行函数,而是给出一个期限值(delay),比如100ms。
  416. * 如果100ms内再次执行函数,就重新开始计时,直到计时结束后再真正执行函数。
  417. * 这样做的好处是如果短时间内大量触发同一事件,只会执行一次函数。
  418. *
  419. * @param fn 要防抖的函数
  420. * @param delay 防抖的毫秒数
  421. * @returns {Function}
  422. */
  423. export function simpleDebounce(fn, delay = 100) {
  424. let timer = null
  425. return function () {
  426. let args = arguments
  427. if (timer) {
  428. clearTimeout(timer)
  429. }
  430. timer = setTimeout(() => {
  431. fn.apply(null, args)
  432. }, delay)
  433. }
  434. }
  435. /**
  436. * 不用正则的方式替换所有值
  437. * @param text 被替换的字符串
  438. * @param checker 替换前的内容
  439. * @param replacer 替换后的内容
  440. * @returns {String} 替换后的字符串
  441. */
  442. export function replaceAll(text, checker, replacer) {
  443. let lastText = text
  444. text = text.replace(checker, replacer)
  445. if (lastText !== text) {
  446. return replaceAll(text, checker, replacer)
  447. }
  448. return text
  449. }