util.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 === 'notice') {
  197. const deptIds = this.deptList.map(i => i.positionId)
  198. this.data = data.filter(i => deptIds.includes(i.depId))
  199. }
  200. // 更新小铃铛消息数量
  201. if (param.alias === 'unreadMessage') {
  202. Bus.$emit('getMessageCount', this.totalCount)
  203. }
  204. this.variables = res.variables
  205. this.loading = false
  206. }).catch((e) => {
  207. this.loading = false
  208. })
  209. }
  210. },
  211. getPhoto (photo) {
  212. return getFile(photo)
  213. },
  214. getTaskDesc (v) {
  215. if (!v.includes('#')) {
  216. return ''
  217. }
  218. return v.split('#')[1] || ''
  219. },
  220. getTaskInfo (val, arg) {
  221. const arr = val.split('#')
  222. if (!arr[2]) {
  223. return ''
  224. }
  225. const result = JSON.parse(`{${arr[2]}}`)
  226. if (!result.dept) {
  227. return ''
  228. }
  229. const depts = result.dept.split(',')
  230. const deptNames = []
  231. depts.forEach(item => {
  232. const t = this.deptList.find(i => i.positionId === item)
  233. deptNames.push(t ? t.positionName : result.dept)
  234. })
  235. result.deptName = deptNames.join(',')
  236. return result[arg]
  237. },
  238. transformData (val, dataset, from, to) {
  239. if (!val) {
  240. return ''
  241. }
  242. const temp = this[dataset].find(u => u[from] === val)
  243. return temp ? temp[to] : ''
  244. },
  245. getHeightNoUnit () {
  246. // 高度 - header - 边框
  247. if (!this.visible) {
  248. return this.height ? (this.height - 60 - 20) : 150
  249. } else {
  250. return 150
  251. }
  252. },
  253. getHeight (h = 20) {
  254. // 高度 - header - 边框
  255. if (!this.visible) {
  256. return this.height ? `${(this.height - 60 - h)}px` : '150px'
  257. } else {
  258. return '100%'
  259. }
  260. },
  261. getDashboardHeight () {
  262. return this.height ? `${this.height + 20}px` : '150px'
  263. },
  264. getAttrs () {
  265. const item = JSON.parse(JSON.stringify(column))
  266. item.templateHtml = null
  267. return item
  268. },
  269. /**
  270. * 构建首页日期组件的参数
  271. * @param {*} data
  272. * @returns
  273. */
  274. getFullCalendarConfig (data) {
  275. const events = data === null ? [] : Utils.parseJSON(data)
  276. const config = {
  277. height: preview ? '100%' : (this.height ? this.height : 180),
  278. // editable: true, // 允许拖动缩放,不写默认就是false
  279. selectable: true,
  280. dayMaxEvents: 1, // 最多显示3个日程
  281. locale: this.$i18n.locale ? this.$i18n.locale.toLowerCase() : 'zh-cn',
  282. events: events,
  283. buttonText: {
  284. today: '今天',
  285. dayGridMonth: '月',
  286. listMonth: '日程'
  287. // week: '周视图',
  288. // day: '日视图',
  289. // prev: '<i class="icon-chevron-left">后退</i>',
  290. // next: '<i class="icon-chevron-right">前进</i>'
  291. },
  292. dateClick: this.handleDateClick, // 日期点击
  293. eventClick: this.handleEventClick
  294. }
  295. if (preview) {
  296. config.headerToolbar = {
  297. start: '',
  298. center: 'title',
  299. end: 'prev,next,today'
  300. // end: 'prev,next,today,month,agendaWeek,agendaDay,listWeek'
  301. }
  302. delete config['dayMaxEvents']
  303. }
  304. return config
  305. },
  306. handleDateClick (param) {
  307. this.$emit(
  308. 'open',
  309. 'calendar',
  310. [param.dateStr, param.dateStr],
  311. this.data
  312. )
  313. },
  314. handleEventClick (param) {
  315. this.$emit(
  316. 'open',
  317. 'calendar',
  318. [param.event.startStr, param.event._def.extendedProps.jieShuShiJian],
  319. this.data,
  320. param.event.id
  321. )
  322. },
  323. refreshData () {
  324. this.fetchData()
  325. },
  326. /**
  327. * 处理按钮事件
  328. * @param {*} command
  329. * @param {*} position
  330. * @param {*} data
  331. * @param {*} actions
  332. */
  333. handleActionEvent (command, position, data, actions) {
  334. switch (command) {
  335. case 'refresh': // 刷新
  336. this.refreshData()
  337. break
  338. case 'fullscreen': // 全屏
  339. this.handleFullscreen()
  340. break
  341. case 'more': // 更多
  342. this.handleMore()
  343. break
  344. case 'collapse': // 收缩
  345. case 'expansion': // 展开
  346. this.handleCollapseExpand(command, data, actions)
  347. break
  348. case 'add': // 新增
  349. this.getFormData()
  350. break
  351. default:
  352. break
  353. }
  354. },
  355. emitActionEventHandler (command) {
  356. this.$emit('action-event', command, ...Array.from(arguments).slice(1))
  357. },
  358. // 公告栏目点击事件
  359. handleApprove (id, title) {
  360. this.$emit('action-event', 'approve', {
  361. id, title
  362. })
  363. },
  364. handleUnreadMessage (id) {
  365. this.$emit('action-event', 'unRead', id)
  366. },
  367. // 处理全屏
  368. handleFullscreen () {
  369. this.emitActionEventHandler('fullscreen', { id: this.attrs.id })
  370. },
  371. openPlate (url) {
  372. const menuMap = {
  373. myTraining: 'rygl/rypx/wdpx',
  374. myTesting: 'rygl/kszx/wdks',
  375. myDevices: 'sbgls/mywh'
  376. }
  377. if (menuMap[url]) {
  378. const alias = menuMap[url].split('/')[0]
  379. const resInfo = this.menus.find(i => i.alias === alias)
  380. this.$store.dispatch('ibps/menu/activeHeaderSet', { activeHeader: resInfo.id, vm: this })
  381. }
  382. this.$router.push(`/${menuMap[url] || url}`)
  383. },
  384. /**
  385. * 处理更多
  386. */
  387. handleMore () {
  388. if (this.attrs.colUrl === '') {
  389. return this.$message.warning('未设置更多路径的url')
  390. }
  391. this.$router.push(`/${this.attrs.colUrl}`)
  392. },
  393. // 未读消息
  394. handleClick (option) {
  395. this.unreadMessageOption = option
  396. option[this.activeName].dataMode = column.dataMode
  397. this.fetchData(option[this.activeName])
  398. },
  399. // 待办事务
  400. handleTabClick (option) {
  401. this.pendingBusinessOption = option
  402. option[this.pendingTabActiveName].dataMode = column.dataMode
  403. this.fetchData(option[this.pendingTabActiveName])
  404. },
  405. handleFlowClick (params) {
  406. params.$alias = this.alias
  407. this.emitActionEventHandler('flow', params)
  408. },
  409. handleCollapseExpand (command, data, actions) {
  410. this.bodyShow = !this.bodyShow
  411. const index = actions.findIndex((action) => action.key === data.key)
  412. actions[index].key = this.bodyShow ? 'collapse' : 'expansion'
  413. if (!this.visible) {
  414. this.emitActionEventHandler(command, {})
  415. return
  416. }
  417. this.showHeight = this.bodyShow ? this.getHeight() : 0
  418. this.$refs['toolbar'].callback(actions)
  419. },
  420. formValidate (formName) {
  421. this.$nextTick(() => {
  422. this.$refs[formName].validate(() => {})
  423. })
  424. },
  425. getFormData () {
  426. this.quickNavform = JSON.parse(JSON.stringify(this.defaultForm))
  427. this.formValidate('quickNavform')
  428. this.dialogFormVisible = true
  429. },
  430. handleNavRemove (navId, i) {
  431. this.$confirm('是否确认删除该快捷导航?', '提示', {
  432. confirmButtonText: '确认',
  433. cancelButtonText: '取消',
  434. type: 'warning',
  435. showClose: false,
  436. closeOnClickModal: false
  437. }).then(() => {
  438. delNavigation({ navigateIds: navId }).then(() => {
  439. this.$message.success('删除成功')
  440. this.quickNavigationData.splice(i, 1)
  441. })
  442. })
  443. },
  444. // 错误头像的照片
  445. errorAvatarHandler (data) {
  446. // data.photo = require('https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png')
  447. return true
  448. },
  449. close () {
  450. this.$refs[this.formName].resetFields()
  451. this.dialogFormVisible = false
  452. },
  453. saveQuickNav () {
  454. this.$refs[this.formName].validate(valid => {
  455. if (valid) {
  456. addNavigation({ id: '', ...this.quickNavform }).then(res => {
  457. this.$message.success('添加成功!')
  458. const { id } = res.variables || {}
  459. this.quickNavigationData.unshift({ id, ...this.quickNavform })
  460. this.dialogFormVisible = false
  461. })
  462. } else {
  463. ActionUtils.saveErrorMessage()
  464. }
  465. })
  466. },
  467. handleSortChange () {
  468. this.isDragging = false
  469. const newSort = this.quickNavigationData.map(i => i.id)
  470. if (isEqual(this.navigationList, newSort)) {
  471. return
  472. }
  473. this.navigationList = newSort
  474. sortNavigation({ orders: this.navigationList.join(',') }).then(() => {
  475. this.$message.success('排序成功!')
  476. })
  477. },
  478. // 父组件调用该方法给日程添加数据
  479. async setCalendarEvents (param) {
  480. const { userId = '', name = '' } = this.$store.getters
  481. const form = param.form
  482. const paramObject = {
  483. id: form.id ? form.id : '11111',
  484. // diDian: "string", // 地点
  485. userId: userId, // 用户id
  486. userName: name, // 用户名
  487. title: form.biaoTi, // 标题
  488. content: form.neiRong, // 内容
  489. startTime: form.formDate[0], // 开始时间
  490. endTime: form.formDate[1], // 结束时间
  491. emergencyState: form.zhuangTai // 紧急状态
  492. }
  493. await saveCalendarInfos(paramObject)
  494. this.refreshData()
  495. },
  496. async hanldeCalendardel (param) {
  497. if (param.form.id) {
  498. await removeCalendarInfos({
  499. calendarIds: param.form.id
  500. })
  501. }
  502. this.refreshData()
  503. },
  504. // 公告栏是否显示new图标
  505. showNewIcon (date, days) {
  506. const nowDate = new Date().getTime()
  507. const targetDate = new Date(date).getTime()
  508. return targetDate + days * 24 * 60 * 60 * 1000 > nowDate
  509. },
  510. handleOverflow (val, length) {
  511. if (val.length > length) {
  512. return val.slice(0, length - 2) + '...'
  513. }
  514. return val
  515. }
  516. },
  517. template: column.templateHtml !== '' ? `${column.templateHtml}` : `<div></div>`
  518. }
  519. } catch (error) {
  520. console.error(error)
  521. }
  522. }