action.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. existHandleListData: function (vm, data, filtration, itemid, tid, 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. let mid = []
  273. listData.forEach((item,i)=>{
  274. const obj = filtration.findIndex(t=> t[tid] === item[itemid])
  275. if(obj != -1){
  276. mid.push(item)
  277. }
  278. })
  279. pagination[pageKey] = pagination[pageKey] + 1
  280. vm[resultKey] = vm[resultKey].concat(mid)
  281. }
  282. vm[loadingKey] = false
  283. vm[finishedKey] = Utils.isEmpty(listData) ? true : false
  284. vm[paginationKey] = pagination
  285. // 处理结果类型
  286. vm[resultTypeKey] = this.handleResultType(vm, options)
  287. vm[resultMessageKey] = null
  288. },
  289. /**
  290. * 处理通用接口列表数据
  291. * @param vm 当前对象
  292. * @param data 后台返回的列表数据
  293. * @param options 参数
  294. * dataResultKey 默认 dataResult
  295. * pageResultKey 默认 pageResult
  296. * resultKey 结果key 默认 listData
  297. * pageKey 分页key 默认 pagination
  298. */
  299. handleListDataCommon: function (vm, data, options = {}) {
  300. const loadingKey = options.loadingKey || 'loading'
  301. const refreshingKey = options.refreshingKey || 'refreshing'
  302. const finishedKey = options.finishedKey || 'finished'
  303. const dataResultKey = options.dataResultKey || 'data'
  304. const pageResultKey = options.pageResultKey || 'pageResult'
  305. const resultKey = options.resultKey || 'listData'
  306. const paginationKey = options.paginationKey || 'pagination'
  307. const pageKey = options.pageKey || 'page'
  308. const resultTypeKey = options.resultTypeKey || 'resultType'
  309. const resultMessageKey = options.resultMessageKey || 'resultMessage'
  310. const listData = data ? data[dataResultKey] || [] : []
  311. const pagination = data ? data[pageResultKey] || {} : {}
  312. if (vm[refreshingKey]) {
  313. vm[resultKey] = []
  314. vm[refreshingKey] = false
  315. }
  316. if (Utils.isNotEmpty(listData)) {
  317. vm[pageKey] = vm[pageKey] + 1
  318. vm[resultKey] = vm[resultKey].concat(listData)
  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. handleResultType: function (vm, options) {
  328. const resultKey = options.resultKey || 'listData'
  329. const finishedKey = options.finishedKey || 'finished'
  330. const list = vm[resultKey]
  331. const finished = vm[finishedKey]
  332. if (list && list.length > 0) {
  333. if (finished) return 'finished'
  334. return
  335. }
  336. return 'empty'
  337. },
  338. isFinished({ totalCount }, listData) {
  339. // 异常终止
  340. if (Utils.isEmpty(listData)) {
  341. return true
  342. }
  343. return listData.length >= totalCount
  344. },
  345. handleErrorData: function (vm, e, options = {}) {
  346. const loadingKey = options.loadingKey || 'loading'
  347. const refreshingKey = options.refreshingKey || 'refreshing'
  348. const finishedKey = options.finishedKey || 'finished'
  349. const resultTypeKey = options.resultTypeKey || 'resultType'
  350. const resultMessageKey = options.resultMessageKey || 'resultMessage'
  351. vm[loadingKey] = false
  352. vm[refreshingKey] = false
  353. vm[finishedKey] = true
  354. vm[resultTypeKey] = e.state === 500 ? 'network' : 'error'
  355. vm[resultMessageKey] = e.message
  356. },
  357. /**
  358. * 设置分页设置
  359. */
  360. setPagination: function (pagination, defaultPagination) {
  361. pagination.page = defaultPagination ? defaultPagination.page || common.PAGE : common.PAGE
  362. pagination.limit = defaultPagination ? defaultPagination.limit || common.LIMIT : common.LIMIT
  363. },
  364. /**
  365. * 设置排序
  366. */
  367. setSorts: function (sorts, sort, defaultSorts = {}) {
  368. const defaultSortsData = function () {
  369. for (const key in defaultSorts) {
  370. sorts[key] = defaultSorts[key]
  371. }
  372. }
  373. // 清空属性
  374. for (const key in sorts) {
  375. delete sorts[key]
  376. }
  377. if (sort) {
  378. const { name, order } = sort
  379. if (name && order) {
  380. sorts[name] = order === 'ascending' ? 'ASC' : 'DESC'
  381. } else {
  382. defaultSortsData()
  383. }
  384. } else {
  385. defaultSortsData()
  386. }
  387. },
  388. /**
  389. * 格式分页数据
  390. * @param {} params 查询的参数
  391. * @param {} page 分页
  392. * @param {} sorts 排序
  393. */
  394. formatParams: function (params, pagination, sorts) {
  395. const results = {}
  396. if (params) {
  397. results.parameters = Object.keys(params).map((k) => {
  398. return {
  399. 'key': k,
  400. 'value': params[k] ? params[k] : ''
  401. }
  402. })
  403. }
  404. if (pagination) {
  405. const requestPage = {
  406. 'pageNo': pagination.page || common.PAGE,
  407. 'limit': pagination.limit || common.LIMIT
  408. }
  409. if (Utils.isNotEmpty(pagination.totalCount)) { // mock 数据时候要传
  410. requestPage['totalCount'] = pagination.totalCount
  411. }
  412. results.requestPage = requestPage
  413. }
  414. if (sorts) {
  415. results.sorts = Object.keys(sorts).map((k) => {
  416. return {
  417. 'field': k,
  418. 'order': sorts[k]
  419. }
  420. })
  421. }
  422. return results
  423. },
  424. /**
  425. * 下载
  426. */
  427. download: function (data, fileName, responseType = 'application/octet-stream') {
  428. const blob = data instanceof Blob ? data : new Blob([data], { type: responseType })
  429. if ('download' in document.createElement('a')) { // 非IE下载
  430. const url = window.URL.createObjectURL(blob)
  431. const link = document.createElement('a')
  432. link.style.display = 'none'
  433. link.href = url
  434. link.setAttribute('download', fileName)
  435. document.body.appendChild(link)
  436. link.click()
  437. window.URL.revokeObjectURL(link.href)
  438. document.body.removeChild(link)
  439. } else { // IE10+下载
  440. navigator.msSaveBlob(blob, fileName)
  441. }
  442. },
  443. /**
  444. * 导出文件
  445. */
  446. exportFile: function (data, fileName, responseType = 'application/octet-stream') {
  447. this.download(data, fileName, responseType)
  448. }
  449. }
  450. export default action