action.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /**
  2. * 操作帮助类
  3. * 处理增删改查、分页等相关操作
  4. * <pre>
  5. * 作者:hugh zhuang
  6. * 邮箱:3378340995@qq.com
  7. * 日期:2018-10-08-下午3:29:34
  8. * 版权:广州流辰信息技术有限公司
  9. * </pre>
  10. */
  11. import { Notify, Dialog } from 'vant'
  12. import Utils from '@/utils/util'
  13. import I18n from '@/utils/i18n'
  14. import common from '@/constants/common.js'
  15. const action = {
  16. descDialog: function ({ title, message, className = 'ibps-dialog-desc', confirmButtonColor = '#3396FB', confirmButtonText = '知道了', overlay = true }) {
  17. Dialog.alert({
  18. title,
  19. message,
  20. confirmButtonColor,
  21. confirmButtonText,
  22. overlay,
  23. className,
  24. messageAlign: 'left'
  25. })
  26. },
  27. msg: function (message, options) {
  28. if (!options) {
  29. options = options || {}
  30. }
  31. options.message = message
  32. Notify(options)
  33. },
  34. /**
  35. * 操作警告提示
  36. */
  37. warning: function (message) {
  38. this.msg(message, {
  39. type: 'warning'
  40. })
  41. },
  42. /**
  43. * 操作成功提示
  44. */
  45. success: function (message) {
  46. this.msg(message, {
  47. type: 'success'
  48. })
  49. },
  50. error: function (message) {
  51. this.msg(message, {
  52. type: 'error'
  53. })
  54. },
  55. /**
  56. * 默认操作成功提示
  57. * @param {*} message
  58. */
  59. successMessage: function (message = I18n.t('common.dialog.operateSuccess')) {
  60. this.success(message)
  61. },
  62. /**
  63. * 删除成功提示
  64. * @param {*} message
  65. */
  66. removeSuccessMessage: function (message = I18n.t('common.dialog.removeSuccess')) {
  67. this.success(message)
  68. },
  69. /**
  70. * 获取选择行的id
  71. * @param {*} rows
  72. * @param {*} pkKey
  73. */
  74. getSelectedIds: function (rows, pkKey = 'id') {
  75. const ids = []
  76. rows.forEach(row => {
  77. ids.push(row[pkKey])
  78. })
  79. return ids
  80. },
  81. /**
  82. * 选择记录
  83. * 只能选择一个记录
  84. * [一般用于编辑、明细等只选择一个记录]
  85. *
  86. * @param {*} selection
  87. */
  88. selectedRecord: function (selection) {
  89. return new Promise((resolve, reject) => {
  90. if (Utils.isEmpty(selection)) {
  91. this.warning(I18n.t('common.dialog.selectedRecords'))
  92. return reject(selection)
  93. }
  94. if (Array.isArray(selection) && selection.length > 1) {
  95. this.warning(I18n.t('common.dialog.multipleSelected'))
  96. return reject(selection)
  97. }
  98. if (Array.isArray(selection)) {
  99. resolve(selection[0])
  100. } else {
  101. resolve(selection)
  102. }
  103. })
  104. },
  105. /**
  106. * 选择多个记录
  107. * [一般用于删除等选择多个记录]
  108. *
  109. * @param {*} selection 选中的值
  110. * @param {*} isArray 是否数组格式返回 默认 false
  111. * @param {*} separator 分割符 默认 `,`
  112. */
  113. selectedMultiRecord: function (selection, isArray = false, separator = ',') {
  114. return new Promise((resolve, reject) => {
  115. if (Utils.isEmpty(selection)) {
  116. this.warning(I18n.t('common.dialog.selectedRecords'))
  117. return reject(selection)
  118. }
  119. if (!Array.isArray(selection)) {
  120. selection = selection.split(separator)
  121. }
  122. if (!isArray) { // 不是数组返回
  123. selection = selection.join(separator)
  124. }
  125. resolve(selection)
  126. })
  127. },
  128. /**
  129. * 删除记录
  130. * @param {*} rows
  131. * @param {*} pkKey
  132. */
  133. removeRecord: function (selection, confirmMsg = I18n.t('common.dialog.removeRecord'), isArray = false, separator = ',') {
  134. return new Promise((resolve, reject) => {
  135. if (Utils.isEmpty(selection)) {
  136. this.warning(I18n.t('common.dialog.selectedRecords'))
  137. return reject(selection)
  138. }
  139. if (!Array.isArray(selection)) {
  140. selection = selection.split(separator)
  141. }
  142. if (!isArray) { // 不是数组返回
  143. selection = selection.join(separator)
  144. }
  145. Dialog.confirm({
  146. title: I18n.t('common.dialog.title'),
  147. message: confirmMsg
  148. }).then(() => {
  149. resolve(selection)
  150. }).catch((err) => {
  151. reject(err)
  152. })
  153. })
  154. },
  155. /**
  156. * 保存成功提示
  157. */
  158. saveSuccessMessage: function (message = I18n.t('common.dialog.operateSuccess'), callback) {
  159. message = Utils.isNotEmpty(message) ? message : I18n.t('common.dialog.operateSuccess')
  160. // MessageBox.confirm(message,
  161. // I18n.t('common.dialog.title'),
  162. // {
  163. // type: 'success',
  164. // confirmButtonText: I18n.t('common.dialog.saveConfirmButtonText'),
  165. // cancelButtonText: I18n.t('common.dialog.saveCancelButtonText'),
  166. // closeOnClickModal: false,
  167. // callback: (action) => {
  168. // const flag = action !== 'confirm'
  169. // callback(flag)
  170. // }
  171. // })
  172. },
  173. /**
  174. * 保存成功提示
  175. */
  176. saveSuccessAlert: function (message = I18n.t('common.dialog.operateSuccess'), callback) {
  177. // MessageBox.alert(message,
  178. // I18n.t('common.dialog.title'),
  179. // {
  180. // type: 'success',
  181. // closeOnClickModal: false,
  182. // callback: (action) => {
  183. // callback(action)
  184. // }
  185. // })
  186. },
  187. /**
  188. * 保存失败
  189. */
  190. saveErrorMessage: function (message, callback) {
  191. this.warning(message || I18n.t('common.dialog.saveError'))
  192. if (callback) { callback() }
  193. },
  194. initListData(vm, options = {}) {
  195. const resultTypeKey = options.resultTypeKey || 'resultType'
  196. const resultKey = options.resultKey || 'listData'
  197. const paginationKey = options.paginationKey || 'pagination'
  198. const pageKey = options.pageKey || 'page'
  199. const finishedKey = options.finishedKey || 'finished'
  200. vm[finishedKey] = false
  201. // 设置第一页,数据设置为空
  202. vm[paginationKey][pageKey] = 1
  203. vm[resultTypeKey] = 'init'
  204. vm[resultKey] = []
  205. },
  206. /**
  207. * 处理列表数据(带过滤)
  208. * @param vm 当前对象
  209. * @param data 后台返回的列表数据
  210. * @param options 参数
  211. * dataResultKey 默认 dataResult
  212. * pageResultKey 默认 pageResult
  213. * resultKey 结果key 默认 listData
  214. * pageKey 分页key 默认 pagination
  215. */
  216. handleListData: function (vm, data, options = {}) {
  217. const loadingKey = options.loadingKey || 'loading'
  218. const refreshingKey = options.refreshingKey || 'refreshing'
  219. const finishedKey = options.finishedKey || 'finished'
  220. const dataResultKey = options.dataResultKey || 'dataResult'
  221. const pageResultKey = options.pageResultKey || 'pageResult'
  222. const resultKey = options.resultKey || 'listData'
  223. const paginationKey = options.paginationKey || 'pagination'
  224. const pageKey = options.pageKey || 'page'
  225. const resultTypeKey = options.resultTypeKey || 'resultType'
  226. const resultMessageKey = options.resultMessageKey || 'resultMessage'
  227. const listData = data ? data[dataResultKey] || [] : []
  228. const pagination = data ? data[pageResultKey] || {} : {}
  229. if (vm[refreshingKey]) {
  230. vm[resultKey] = []
  231. vm[refreshingKey] = false
  232. }
  233. if (Utils.isNotEmpty(listData)) {
  234. pagination[pageKey] = pagination[pageKey] + 1
  235. vm[resultKey] = vm[resultKey].concat(listData)
  236. }
  237. vm[loadingKey] = false
  238. vm[finishedKey] = this.isFinished(pagination, vm[resultKey])
  239. vm[paginationKey] = pagination
  240. // 处理结果类型
  241. vm[resultTypeKey] = this.handleResultType(vm, options)
  242. vm[resultMessageKey] = null
  243. },
  244. /**
  245. * 处理列表数据(不过滤)
  246. * @param vm 当前对象
  247. * @param data 后台返回的列表数据
  248. * @param options 参数
  249. * dataResultKey 默认 dataResult
  250. * pageResultKey 默认 pageResult
  251. * resultKey 结果key 默认 listData
  252. * pageKey 分页key 默认 pagination
  253. */
  254. handleListData: function (vm, data, options = {}) {
  255. const loadingKey = options.loadingKey || 'loading'
  256. const refreshingKey = options.refreshingKey || 'refreshing'
  257. const finishedKey = options.finishedKey || 'finished'
  258. const dataResultKey = options.dataResultKey || 'dataResult'
  259. const pageResultKey = options.pageResultKey || 'pageResult'
  260. const resultKey = options.resultKey || 'listData'
  261. const paginationKey = options.paginationKey || 'pagination'
  262. const pageKey = options.pageKey || 'page'
  263. const resultTypeKey = options.resultTypeKey || 'resultType'
  264. const resultMessageKey = options.resultMessageKey || 'resultMessage'
  265. const listData = data ? data[dataResultKey] || [] : []
  266. const pagination = data ? data[pageResultKey] || {} : {}
  267. if (vm[refreshingKey]) {
  268. vm[resultKey] = []
  269. vm[refreshingKey] = false
  270. }
  271. if (Utils.isNotEmpty(listData)) {
  272. pagination[pageKey] = pagination[pageKey] + 1
  273. vm[resultKey] = vm[resultKey].concat(listData)
  274. }
  275. vm[loadingKey] = false
  276. vm[finishedKey] = this.isFinished(pagination, vm[resultKey])
  277. vm[paginationKey] = pagination
  278. // 处理结果类型
  279. vm[resultTypeKey] = this.handleResultType(vm, options)
  280. vm[resultMessageKey] = null
  281. },
  282. /**
  283. * 处理列表数据(带过滤)
  284. * @param vm 当前对象
  285. * @param data 后台返回的列表数据
  286. * @param options 参数
  287. * dataResultKey 默认 dataResult
  288. * pageResultKey 默认 pageResult
  289. * resultKey 结果key 默认 listData
  290. * pageKey 分页key 默认 pagination
  291. */
  292. existHandleListData: function (vm, data, filtration, options={}) {
  293. const loadingKey = options.loadingKey || 'loading'
  294. const refreshingKey = options.refreshingKey || 'refreshing'
  295. const finishedKey = options.finishedKey || 'finished'
  296. const dataResultKey = options.dataResultKey || 'dataResult'
  297. const pageResultKey = options.pageResultKey || 'pageResult'
  298. const resultKey = options.resultKey || 'listData'
  299. const paginationKey = options.paginationKey || 'pagination'
  300. const pageKey = options.pageKey || 'page'
  301. const resultTypeKey = options.resultTypeKey || 'resultType'
  302. const resultMessageKey = options.resultMessageKey || 'resultMessage'
  303. const listData = data ? data[dataResultKey] || [] : []
  304. const pagination = data ? data[pageResultKey] || {} : {}
  305. if (vm[refreshingKey]) {
  306. vm[resultKey] = []
  307. vm[refreshingKey] = false
  308. }
  309. if (Utils.isNotEmpty(listData)) {
  310. let mid = []
  311. listData.forEach((item,i)=>{
  312. const obj = filtration.findIndex(t=> t.liu_cheng_id_ === item.procDefId)
  313. if(obj != -1){
  314. mid.push(item)
  315. }
  316. })
  317. pagination[pageKey] = pagination[pageKey] + 1
  318. vm[resultKey] = vm[resultKey].concat(mid)
  319. }
  320. vm[loadingKey] = false
  321. vm[finishedKey] = Utils.isEmpty(listData) ? true : false
  322. vm[paginationKey] = pagination
  323. // 处理结果类型
  324. vm[resultTypeKey] = this.handleResultType(vm, options)
  325. vm[resultMessageKey] = null
  326. },
  327. /**
  328. * 处理通用接口列表数据
  329. * @param vm 当前对象
  330. * @param data 后台返回的列表数据
  331. * @param options 参数
  332. * dataResultKey 默认 dataResult
  333. * pageResultKey 默认 pageResult
  334. * resultKey 结果key 默认 listData
  335. * pageKey 分页key 默认 pagination
  336. */
  337. handleListDataCommon: function (vm, data, options = {}) {
  338. const loadingKey = options.loadingKey || 'loading'
  339. const refreshingKey = options.refreshingKey || 'refreshing'
  340. const finishedKey = options.finishedKey || 'finished'
  341. const dataResultKey = options.dataResultKey || 'data'
  342. const pageResultKey = options.pageResultKey || 'pageResult'
  343. const resultKey = options.resultKey || 'listData'
  344. const paginationKey = options.paginationKey || 'pagination'
  345. const pageKey = options.pageKey || 'page'
  346. const resultTypeKey = options.resultTypeKey || 'resultType'
  347. const resultMessageKey = options.resultMessageKey || 'resultMessage'
  348. const listData = data ? data[dataResultKey] || [] : []
  349. const pagination = data ? data[pageResultKey] || {} : {}
  350. if (vm[refreshingKey]) {
  351. vm[resultKey] = []
  352. vm[refreshingKey] = false
  353. }
  354. if (Utils.isNotEmpty(listData)) {
  355. vm[pageKey] = vm[pageKey] + 1
  356. vm[resultKey] = vm[resultKey].concat(listData)
  357. }
  358. vm[loadingKey] = false
  359. vm[finishedKey] = Utils.isEmpty(listData) ? true : false
  360. // vm[paginationKey] = pagination
  361. // 处理结果类型
  362. vm[resultTypeKey] = this.handleResultType(vm, options)
  363. vm[resultMessageKey] = null
  364. },
  365. handleResultType: function (vm, options) {
  366. const resultKey = options.resultKey || 'listData'
  367. const finishedKey = options.finishedKey || 'finished'
  368. const list = vm[resultKey]
  369. const finished = vm[finishedKey]
  370. if (list && list.length > 0) {
  371. if (finished) return 'finished'
  372. return
  373. }
  374. return 'empty'
  375. },
  376. isFinished({ totalCount }, listData) {
  377. // 异常终止
  378. if (Utils.isEmpty(listData)) {
  379. return true
  380. }
  381. return listData.length >= totalCount
  382. },
  383. handleErrorData: function (vm, e, options = {}) {
  384. const loadingKey = options.loadingKey || 'loading'
  385. const refreshingKey = options.refreshingKey || 'refreshing'
  386. const finishedKey = options.finishedKey || 'finished'
  387. const resultTypeKey = options.resultTypeKey || 'resultType'
  388. const resultMessageKey = options.resultMessageKey || 'resultMessage'
  389. vm[loadingKey] = false
  390. vm[refreshingKey] = false
  391. vm[finishedKey] = true
  392. vm[resultTypeKey] = e.state === 500 ? 'network' : 'error'
  393. vm[resultMessageKey] = e.message
  394. },
  395. /**
  396. * 设置分页设置
  397. */
  398. setPagination: function (pagination, defaultPagination) {
  399. pagination.page = defaultPagination ? defaultPagination.page || common.PAGE : common.PAGE
  400. pagination.limit = defaultPagination ? defaultPagination.limit || common.LIMIT : common.LIMIT
  401. },
  402. /**
  403. * 设置排序
  404. */
  405. setSorts: function (sorts, sort, defaultSorts = {}) {
  406. const defaultSortsData = function () {
  407. for (const key in defaultSorts) {
  408. sorts[key] = defaultSorts[key]
  409. }
  410. }
  411. // 清空属性
  412. for (const key in sorts) {
  413. delete sorts[key]
  414. }
  415. if (sort) {
  416. const { name, order } = sort
  417. if (name && order) {
  418. sorts[name] = order === 'ascending' ? 'ASC' : 'DESC'
  419. } else {
  420. defaultSortsData()
  421. }
  422. } else {
  423. defaultSortsData()
  424. }
  425. },
  426. /**
  427. * 格式分页数据
  428. * @param {} params 查询的参数
  429. * @param {} page 分页
  430. * @param {} sorts 排序
  431. */
  432. formatParams: function (params, pagination, sorts) {
  433. const results = {}
  434. if (params) {
  435. results.parameters = Object.keys(params).map((k) => {
  436. return {
  437. 'key': k,
  438. 'value': params[k] ? params[k] : ''
  439. }
  440. })
  441. }
  442. if (pagination) {
  443. const requestPage = {
  444. 'pageNo': pagination.page || common.PAGE,
  445. 'limit': pagination.limit || common.LIMIT
  446. }
  447. if (Utils.isNotEmpty(pagination.totalCount)) { // mock 数据时候要传
  448. requestPage['totalCount'] = pagination.totalCount
  449. }
  450. results.requestPage = requestPage
  451. }
  452. if (sorts) {
  453. results.sorts = Object.keys(sorts).map((k) => {
  454. return {
  455. 'field': k,
  456. 'order': sorts[k]
  457. }
  458. })
  459. }
  460. return results
  461. },
  462. /**
  463. * 下载
  464. */
  465. download: function (data, fileName, responseType = 'application/octet-stream') {
  466. const blob = data instanceof Blob ? data : new Blob([data], { type: responseType })
  467. if ('download' in document.createElement('a')) { // 非IE下载
  468. const url = window.URL.createObjectURL(blob)
  469. const link = document.createElement('a')
  470. link.style.display = 'none'
  471. link.href = url
  472. link.setAttribute('download', fileName)
  473. document.body.appendChild(link)
  474. link.click()
  475. window.URL.revokeObjectURL(link.href)
  476. document.body.removeChild(link)
  477. } else { // IE10+下载
  478. navigator.msSaveBlob(blob, fileName)
  479. }
  480. },
  481. /**
  482. * 导出文件
  483. */
  484. exportFile: function (data, fileName, responseType = 'application/octet-stream') {
  485. this.download(data, fileName, responseType)
  486. }
  487. }
  488. export default action