util.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. import { getData } from '@/api/platform/desktop/column'
  2. import { getFile } from '@/utils/avatar'
  3. import { mapState } from 'vuex'
  4. import { taskTypeOptions, dashboardStatus, genderOptions, favoritesOptions, noticeOptions, unreadMessageOptions, imgOptionsData } from '@/business/platform/bpmn/constants'
  5. import ActionUtils from '@/utils/action'
  6. import Utils from '@/utils/util'
  7. import { findAllByCurrUserId, saveCalendarInfos, removeCalendarInfos, delNavigation, addNavigation, getNavigation, sortNavigation } from '@/api/detection/newHomeApi'
  8. import { isEqual } from 'lodash'
  9. import Bus from '@/utils/EventBus'
  10. import newPng from '@/assets/images/homepage/new.png'
  11. /**
  12. * 创建组件
  13. */
  14. export function buildComponent (name, column, preview, vm) {
  15. try {
  16. return {
  17. name,
  18. components: {
  19. VueDraggable: () => import('vuedraggable')
  20. },
  21. props: {
  22. params: {
  23. type: Object,
  24. default: () => {}
  25. },
  26. height: {
  27. type: Number,
  28. default: column.height || 300
  29. },
  30. visible: {
  31. type: Boolean,
  32. default: false
  33. },
  34. fullScreen: {
  35. type: Boolean,
  36. default: false
  37. }
  38. },
  39. filters: {
  40. filterStatus (val, type) {
  41. if (Utils.isEmpty(val)) {
  42. return ''
  43. }
  44. const typeMap = {
  45. pending: taskTypeOptions,
  46. already: dashboardStatus,
  47. myRequest: dashboardStatus,
  48. gender: genderOptions,
  49. favorites: favoritesOptions,
  50. notice: noticeOptions,
  51. unreadMessage: unreadMessageOptions
  52. }
  53. if (!typeMap[type]) {
  54. return val
  55. }
  56. return typeMap[type].find(x => x['value'] === val) ? typeMap[type].find(x => x['value'] === val).label : val
  57. }
  58. },
  59. data () {
  60. const { first = '', second = '' } = this.$store.getters.level
  61. const { userList = [], deptList = [], menus, userInfo } = this.$store.getters
  62. const t1 = deptList.find(i => i.positionId === first) || {}
  63. const t2 = deptList.find(i => i.positionId === second) || {}
  64. const locationName = second ? t1.positionName + t2.positionName : t1.positionName
  65. return {
  66. userList,
  67. deptList,
  68. menus,
  69. locationName,
  70. newPng,
  71. imgOptionsData,
  72. positions: userInfo.positions,
  73. loading: false,
  74. title: `${column.name}`,
  75. alias: `${column.alias}`,
  76. attrs: this.getAttrs(),
  77. variables: {}, // 一些变量,比如分页信息
  78. data: null,
  79. totalCount: 0,
  80. quickNavigationData: [],
  81. navigationList: [],
  82. stautusOptions: [],
  83. bpmnFormrenderDialogVisible: false, // 表单
  84. editId: '',
  85. bodyShow: true,
  86. show: false,
  87. showHeight: '',
  88. cardHeight: '100%',
  89. activeName: 'innerMessage',
  90. unreadMessageOption: {},
  91. formName: 'quickNavform',
  92. dialogFormVisible: false,
  93. formLabelWidth: '120px',
  94. quickNavform: {
  95. urlName: '',
  96. urlAddr: '',
  97. display: '_blank'
  98. },
  99. defaultForm: {},
  100. pendingTabActiveName: 'user-type',
  101. bodyParams: ActionUtils.formatParams({}, {}, {}),
  102. pendingBusinessOption: {},
  103. bpmn: [],
  104. rules: {
  105. urlName: [
  106. {
  107. required: true,
  108. message: this.$t('validate.required')
  109. }
  110. ],
  111. urlAddr: [
  112. {
  113. required: true,
  114. message: this.$t('validate.required')
  115. }
  116. ],
  117. display: [
  118. {
  119. required: true,
  120. message: this.$t('validate.required')
  121. }
  122. ]
  123. },
  124. calendarDialogForm: {
  125. id: '',
  126. biaoTi: '',
  127. neiRong: '',
  128. kaiShiShiJian: '',
  129. jieShuShiJian: '',
  130. formDate: []
  131. },
  132. colorStatus: ['#e7505a', '#f3c200', '#578ebe', '#1BBC9B'],
  133. // const status = ['急', '重', '轻','缓']
  134. isDragging: false,
  135. draggableOptions: {
  136. handle: '.draggable',
  137. ghostClass: 'sortable-ghost',
  138. distance: 1,
  139. disabled: false,
  140. animation: 200,
  141. axis: 'y'
  142. },
  143. calendarToolbar: this.fullScreen ? [{ key: 'refresh' }] : [{ key: 'refresh' }, { key: 'fullscreen' }, { key: 'collapse' }]
  144. }
  145. },
  146. computed: {
  147. ...mapState({
  148. userInfo: state => state.ibps.user.info
  149. })
  150. },
  151. mounted () {
  152. this.defaultForm = JSON.parse(JSON.stringify(this.quickNavform))
  153. this.$nextTick(() => {
  154. this.fetchData()
  155. })
  156. },
  157. methods: {
  158. fetchData (columns, params = {}) {
  159. this.loading = true
  160. this.data = []
  161. this.showHeight = this.getHeight()
  162. const param = Utils.isNotEmpty(columns) && (column.alias === 'unreadMessage' || column.alias === 'pendingBusiness')
  163. ? { dataMode: column.dataMode, dataFrom: column.dataFrom }
  164. : column
  165. if (param.alias === 'myCalendar') {
  166. const { getFormatDate, getDate } = this.$common
  167. findAllByCurrUserId().then(res => {
  168. const { data = [] } = res || {}
  169. this.data = data.map(i => ({
  170. id: i.id,
  171. title: i.title,
  172. content: i.content,
  173. start: i.startTime,
  174. // 日期组件日程显示里,end的时间需要多加一天,end:2024-01-02时,日程条只到2024-01-01的日期位置上
  175. end: getFormatDate('string', 10, getDate('day', 1, i.endTime)),
  176. jieShuShiJian: i.endTime,
  177. zhuangTai: i.emergencyState,
  178. color: this.colorStatus[Number(i.emergencyState) - 1]
  179. }))
  180. }).catch(() => {
  181. this.$message.error('获取日历日程失败!')
  182. })
  183. } else if (param.alias === 'quickNavigation') {
  184. getNavigation().then(res => {
  185. this.quickNavigationData = res.data || []
  186. })
  187. } else {
  188. getData(param, params).then(res => {
  189. let { data } = res || {}
  190. if (Utils.isNotEmpty(data) && Utils.isString(data)) {
  191. data = Utils.parseData(res.data)
  192. }
  193. this.data = data && data.dataResult ? data.dataResult : data
  194. this.totalCount = data && data.pageResult ? data.pageResult.totalCount : 0
  195. // 更新小铃铛消息数量
  196. if (param.alias === 'unreadMessage') {
  197. Bus.$emit('getMessageCount', this.totalCount)
  198. }
  199. this.variables = res.variables
  200. this.loading = false
  201. }).catch((e) => {
  202. this.loading = false
  203. })
  204. }
  205. },
  206. getPhoto (photo) {
  207. return getFile(photo)
  208. },
  209. getTaskDesc (v) {
  210. if (!v.includes('#')) {
  211. return ''
  212. }
  213. return v.split('#')[1] || ''
  214. },
  215. getTaskInfo (val, arg) {
  216. const arr = val.split('#')
  217. if (!arr[2]) {
  218. return ''
  219. }
  220. const result = JSON.parse(`{${arr[2]}}`)
  221. if (!result.dept) {
  222. return ''
  223. }
  224. const depts = result.dept.split(',')
  225. const deptNames = []
  226. depts.forEach(item => {
  227. const t = this.deptList.find(i => i.positionId === item)
  228. deptNames.push(t ? t.positionName : result.dept)
  229. })
  230. result.deptName = deptNames.join(',')
  231. return result[arg]
  232. },
  233. transformData (val, dataset, from, to) {
  234. if (!val) {
  235. return ''
  236. }
  237. const temp = this[dataset].find(u => u[from] === val)
  238. return temp ? temp[to] : ''
  239. },
  240. getHeightNoUnit () {
  241. // 高度 - header - 边框
  242. if (!this.visible) {
  243. return this.height ? (this.height - 60 - 20) : 150
  244. } else {
  245. return 150
  246. }
  247. },
  248. getHeight (h = 20) {
  249. // 高度 - header - 边框
  250. if (!this.visible) {
  251. return this.height ? `${(this.height - 60 - h)}px` : '150px'
  252. } else {
  253. return '100%'
  254. }
  255. },
  256. getDashboardHeight () {
  257. return this.height ? `${this.height + 20}px` : '150px'
  258. },
  259. getAttrs () {
  260. const item = JSON.parse(JSON.stringify(column))
  261. item.templateHtml = null
  262. return item
  263. },
  264. /**
  265. * 构建首页日期组件的参数
  266. * @param {*} data
  267. * @returns
  268. */
  269. getFullCalendarConfig (data) {
  270. const events = data === null ? [] : Utils.parseJSON(data)
  271. const config = {
  272. height: preview ? '100%' : (this.height ? this.height : 180),
  273. // editable: true, // 允许拖动缩放,不写默认就是false
  274. selectable: true,
  275. dayMaxEvents: 1, // 最多显示3个日程
  276. locale: this.$i18n.locale ? this.$i18n.locale.toLowerCase() : 'zh-cn',
  277. events: events,
  278. buttonText: {
  279. today: '今天',
  280. dayGridMonth: '月',
  281. listMonth: '日程'
  282. // week: '周视图',
  283. // day: '日视图',
  284. // prev: '<i class="icon-chevron-left">后退</i>',
  285. // next: '<i class="icon-chevron-right">前进</i>'
  286. },
  287. dateClick: this.handleDateClick, // 日期点击
  288. eventClick: this.handleEventClick
  289. }
  290. if (preview) {
  291. config.headerToolbar = {
  292. start: '',
  293. center: 'title',
  294. end: 'prev,next,today'
  295. // end: 'prev,next,today,month,agendaWeek,agendaDay,listWeek'
  296. }
  297. delete config['dayMaxEvents']
  298. }
  299. return config
  300. },
  301. handleDateClick (param) {
  302. this.$emit(
  303. 'open',
  304. 'calendar',
  305. [param.dateStr, param.dateStr],
  306. this.data
  307. )
  308. },
  309. handleEventClick (param) {
  310. this.$emit(
  311. 'open',
  312. 'calendar',
  313. [param.event.startStr, param.event._def.extendedProps.jieShuShiJian],
  314. this.data,
  315. param.event.id
  316. )
  317. },
  318. refreshData () {
  319. this.fetchData()
  320. },
  321. /**
  322. * 处理按钮事件
  323. * @param {*} command
  324. * @param {*} position
  325. * @param {*} data
  326. * @param {*} actions
  327. */
  328. handleActionEvent (command, position, data, actions) {
  329. switch (command) {
  330. case 'refresh': // 刷新
  331. this.refreshData()
  332. break
  333. case 'fullscreen': // 全屏
  334. this.handleFullscreen()
  335. break
  336. case 'more': // 更多
  337. this.handleMore()
  338. break
  339. case 'collapse': // 收缩
  340. case 'expansion': // 展开
  341. this.handleCollapseExpand(command, data, actions)
  342. break
  343. case 'add': // 新增
  344. this.getFormData()
  345. break
  346. default:
  347. break
  348. }
  349. },
  350. emitActionEventHandler (command) {
  351. this.$emit('action-event', command, ...Array.from(arguments).slice(1))
  352. },
  353. // 公告栏目点击事件
  354. handleApprove (id, title) {
  355. this.$emit('action-event', 'approve', {
  356. id, title
  357. })
  358. },
  359. handleUnreadMessage (id, tableId, tableName) {
  360. this.$emit('action-event', 'unRead', { id, tableId, tableName })
  361. },
  362. // 处理全屏
  363. handleFullscreen () {
  364. this.emitActionEventHandler('fullscreen', { id: this.attrs.id })
  365. },
  366. openPlate (url) {
  367. const menuMap = {
  368. myTraining: 'rygl/rypx/wdpx',
  369. myTesting: 'rygl/kszx/wdks',
  370. myDevices: 'sbgls/mywh',
  371. notice: 'tygl/tzgg'
  372. }
  373. if (menuMap[url]) {
  374. const alias = menuMap[url].split('/')[0]
  375. const resInfo = this.menus.find(i => i.alias === alias)
  376. this.$store.dispatch('ibps/menu/activeHeaderSet', { activeHeader: resInfo.id, vm: this })
  377. }
  378. this.$router.push(`/${menuMap[url] || url}`)
  379. },
  380. /**
  381. * 处理更多
  382. */
  383. handleMore () {
  384. if (this.attrs.colUrl === '') {
  385. return this.$message.warning('未设置更多路径的url')
  386. }
  387. this.openPlate(this.attrs.colUrl)
  388. },
  389. // 未读消息
  390. handleClick (option) {
  391. this.unreadMessageOption = option
  392. option[this.activeName].dataMode = column.dataMode
  393. this.fetchData(option[this.activeName])
  394. },
  395. // 待办事务
  396. handleTabClick (option) {
  397. this.pendingBusinessOption = option
  398. option[this.pendingTabActiveName].dataMode = column.dataMode
  399. this.fetchData(option[this.pendingTabActiveName])
  400. },
  401. handleFlowClick (params) {
  402. params.$alias = this.alias
  403. this.emitActionEventHandler('flow', params)
  404. },
  405. handleCollapseExpand (command, data, actions) {
  406. this.bodyShow = !this.bodyShow
  407. const index = actions.findIndex((action) => action.key === data.key)
  408. actions[index].key = this.bodyShow ? 'collapse' : 'expansion'
  409. if (!this.visible) {
  410. this.emitActionEventHandler(command, {})
  411. return
  412. }
  413. this.showHeight = this.bodyShow ? this.getHeight() : 0
  414. this.$refs['toolbar'].callback(actions)
  415. },
  416. formValidate (formName) {
  417. this.$nextTick(() => {
  418. this.$refs[formName].validate(() => {})
  419. })
  420. },
  421. getFormData () {
  422. this.quickNavform = JSON.parse(JSON.stringify(this.defaultForm))
  423. this.formValidate('quickNavform')
  424. this.dialogFormVisible = true
  425. },
  426. handleNavRemove (navId, i) {
  427. this.$confirm('是否确认删除该快捷导航?', '提示', {
  428. confirmButtonText: '确认',
  429. cancelButtonText: '取消',
  430. type: 'warning',
  431. showClose: false,
  432. closeOnClickModal: false
  433. }).then(() => {
  434. delNavigation({ navigateIds: navId }).then(() => {
  435. this.$message.success('删除成功')
  436. this.quickNavigationData.splice(i, 1)
  437. })
  438. })
  439. },
  440. // 错误头像的照片
  441. errorAvatarHandler (data) {
  442. // data.photo = require('https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png')
  443. return true
  444. },
  445. close () {
  446. this.$refs[this.formName].resetFields()
  447. this.dialogFormVisible = false
  448. },
  449. saveQuickNav () {
  450. this.$refs[this.formName].validate(valid => {
  451. if (valid) {
  452. addNavigation({ id: '', ...this.quickNavform }).then(res => {
  453. this.$message.success('添加成功!')
  454. const { id } = res.variables || {}
  455. this.quickNavigationData.unshift({ id, ...this.quickNavform })
  456. this.dialogFormVisible = false
  457. })
  458. } else {
  459. ActionUtils.saveErrorMessage()
  460. }
  461. })
  462. },
  463. handleSortChange () {
  464. this.isDragging = false
  465. const newSort = this.quickNavigationData.map(i => i.id)
  466. if (isEqual(this.navigationList, newSort)) {
  467. return
  468. }
  469. this.navigationList = newSort
  470. sortNavigation({ orders: this.navigationList.join(',') }).then(() => {
  471. this.$message.success('排序成功!')
  472. })
  473. },
  474. // 父组件调用该方法给日程添加数据
  475. async setCalendarEvents (param) {
  476. const { userId = '', name = '' } = this.$store.getters
  477. const form = param.form
  478. const paramObject = {
  479. id: form.id ? form.id : '11111',
  480. // diDian: "string", // 地点
  481. userId: userId, // 用户id
  482. userName: name, // 用户名
  483. title: form.biaoTi, // 标题
  484. content: form.neiRong, // 内容
  485. startTime: form.formDate[0], // 开始时间
  486. endTime: form.formDate[1], // 结束时间
  487. emergencyState: form.zhuangTai // 紧急状态
  488. }
  489. await saveCalendarInfos(paramObject)
  490. this.refreshData()
  491. },
  492. async hanldeCalendardel (param) {
  493. if (param.form.id) {
  494. await removeCalendarInfos({
  495. calendarIds: param.form.id
  496. })
  497. }
  498. this.refreshData()
  499. },
  500. // 公告栏是否显示new图标
  501. showNewIcon (date, days) {
  502. const nowDate = new Date().getTime()
  503. const targetDate = new Date(date).getTime()
  504. return targetDate + days * 24 * 60 * 60 * 1000 > nowDate
  505. },
  506. handleOverflow (val, length) {
  507. if (val.length > length) {
  508. return val.slice(0, length - 2) + '...'
  509. }
  510. return val
  511. }
  512. },
  513. template: column.templateHtml !== '' ? `${column.templateHtml}` : `<div></div>`
  514. }
  515. } catch (error) {
  516. console.error(error)
  517. }
  518. }