action.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 { Message, MessageBox } from 'element-ui'
  12. import Utils from '@/utils/util'
  13. import I18n from '@/utils/i18n'
  14. import common from '@/constants/common.js'
  15. const action = {
  16. msg: function(message, options) {
  17. Message.closeAll()
  18. if (!options) {
  19. options = options || {}
  20. }
  21. options.message = message
  22. Message(options)
  23. },
  24. /**
  25. * 操作警告提示
  26. */
  27. warning: function(message) {
  28. this.msg(message, {
  29. type: 'warning'
  30. })
  31. },
  32. /**
  33. * 操作成功提示
  34. */
  35. success: function(message) {
  36. this.msg(message, {
  37. type: 'success'
  38. })
  39. },
  40. error: function(message) {
  41. this.msg(message, {
  42. type: 'error'
  43. })
  44. },
  45. /**
  46. * 默认操作成功提示
  47. * @param {*} message
  48. */
  49. successMessage: function(message = I18n.t('common.dialog.operateSuccess')) {
  50. this.success(message)
  51. },
  52. /**
  53. * 删除成功提示
  54. * @param {*} message
  55. */
  56. removeSuccessMessage: function(message = I18n.t('common.dialog.removeSuccess')) {
  57. this.success(message)
  58. },
  59. /**
  60. * 获取选择行的id
  61. * @param {*} rows
  62. * @param {*} pkKey
  63. */
  64. getSelectedIds: function(rows, pkKey = 'id') {
  65. const ids = []
  66. rows.forEach(row => {
  67. ids.push(row[pkKey])
  68. })
  69. return ids
  70. },
  71. /**
  72. * 选择记录
  73. * 只能选择一个记录
  74. * [一般用于编辑、明细等只选择一个记录]
  75. *
  76. * @param {*} selection
  77. */
  78. selectedRecord: function(selection) {
  79. return new Promise((resolve, reject) => {
  80. if (Utils.isEmpty(selection)) {
  81. this.warning(I18n.t('common.dialog.selectedRecords'))
  82. return reject(selection)
  83. }
  84. if (Array.isArray(selection) && selection.length > 1) {
  85. this.warning(I18n.t('common.dialog.multipleSelected'))
  86. return reject(selection)
  87. }
  88. if (Array.isArray(selection)) {
  89. resolve(selection[0])
  90. } else {
  91. resolve(selection)
  92. }
  93. })
  94. },
  95. /**
  96. * 选择多个记录
  97. * [一般用于删除等选择多个记录]
  98. *
  99. * @param {*} selection 选中的值
  100. * @param {*} isArray 是否数组格式返回 默认 false
  101. * @param {*} separator 分割符 默认 `,`
  102. */
  103. selectedMultiRecord: function(selection, isArray = false, separator = ',') {
  104. return new Promise((resolve, reject) => {
  105. if (Utils.isEmpty(selection)) {
  106. this.warning(I18n.t('common.dialog.selectedRecords'))
  107. return reject(selection)
  108. }
  109. if (!Array.isArray(selection)) {
  110. selection = selection.split(separator)
  111. }
  112. if (!isArray) { // 不是数组返回
  113. selection = selection.join(separator)
  114. }
  115. resolve(selection)
  116. })
  117. },
  118. /**
  119. * 删除记录
  120. * @param {*} rows
  121. * @param {*} pkKey
  122. */
  123. removeRecord: function(selection, confirmMsg = I18n.t('common.dialog.removeRecord'), isArray = false, separator = ',') {
  124. return new Promise((resolve, reject) => {
  125. if (Utils.isEmpty(selection)) {
  126. this.warning(I18n.t('common.dialog.selectedRecords'))
  127. return reject(selection)
  128. }
  129. if (!Array.isArray(selection)) {
  130. selection = selection.split(separator)
  131. }
  132. if (!isArray) { // 不是数组返回
  133. selection = selection.join(separator)
  134. }
  135. MessageBox.confirm(confirmMsg, I18n.t('common.dialog.title'), {
  136. type: 'warning'
  137. }).then(() => {
  138. resolve(selection)
  139. }).catch((err) => {
  140. reject(err)
  141. })
  142. })
  143. },
  144. /**
  145. * 保存成功提示
  146. */
  147. saveSuccessMessage: function(message = I18n.t('common.dialog.operateSuccess'), callback) {
  148. message = Utils.isNotEmpty(message) ? message : I18n.t('common.dialog.operateSuccess')
  149. MessageBox.confirm(message,
  150. I18n.t('common.dialog.title'),
  151. {
  152. type: 'success',
  153. confirmButtonText: I18n.t('common.dialog.saveConfirmButtonText'),
  154. cancelButtonText: I18n.t('common.dialog.saveCancelButtonText'),
  155. closeOnClickModal: false,
  156. callback: (action) => {
  157. const flag = action !== 'confirm'
  158. callback(flag)
  159. }
  160. })
  161. },
  162. /**
  163. * 保存成功提示
  164. */
  165. saveSuccessAlert: function(message = I18n.t('common.dialog.operateSuccess'), callback) {
  166. MessageBox.alert(message,
  167. I18n.t('common.dialog.title'),
  168. {
  169. type: 'success',
  170. closeOnClickModal: false,
  171. showClose: false,
  172. callback: (action) => {
  173. callback(action)
  174. }
  175. })
  176. },
  177. /**
  178. * 保存失败
  179. */
  180. saveErrorMessage: function(message, callback) {
  181. this.warning(message || I18n.t('common.dialog.saveError'))
  182. if (callback) { callback() }
  183. },
  184. /**
  185. * 处理列表数据
  186. * @param vm 当前对象
  187. * @param data 后台返回的列表数据
  188. * @param options 参数
  189. * dataResultKey 默认 dataResult
  190. * pageResultKey 默认 pageResult
  191. * resultKey 结果key 默认 listData
  192. * pageKey 分页key 默认 pagination
  193. */
  194. handleListData: function(vm, data, options = {}) {
  195. const dataResultKey = options.dataResultKey || 'dataResult'
  196. const pageResultKey = options.pageResultKey || 'pageResult'
  197. const resultKey = options.resultKey || 'listData'
  198. const pageKey = options.pageKey || 'pagination'
  199. vm[resultKey] = data ? data[dataResultKey] || [] : []
  200. vm[pageKey] = data ? data[pageResultKey] || {} : {}
  201. },
  202. /**
  203. * 设置分页设置
  204. */
  205. setPagination: function(pagination, defaultPagination) {
  206. // 修复通过查询
  207. if (!defaultPagination) {
  208. defaultPagination = {
  209. page: common.PAGE,
  210. limit: pagination.limit || common.LIMIT
  211. }
  212. }
  213. pagination.page = defaultPagination ? (defaultPagination.page || common.PAGE) : (pagination.page || common.PAGE)
  214. pagination.limit = defaultPagination ? (defaultPagination.limit || common.LIMIT) : (pagination.limit || common.LIMIT)
  215. },
  216. /**
  217. * 设置分页第一页设置
  218. * @param {*} pagination
  219. * @param {*} defaultPagination
  220. */
  221. setFirstPagination: function(pagination) {
  222. pagination.page = common.PAGE
  223. },
  224. /**
  225. * 设置排序
  226. */
  227. setSorts: function(sorts, sort, defaultSorts = {}) {
  228. const defaultSortsData = function() {
  229. for (const key in defaultSorts) {
  230. sorts[key] = defaultSorts[key]
  231. }
  232. }
  233. // 清空属性
  234. for (const key in sorts) {
  235. delete sorts[key]
  236. }
  237. if (sort) {
  238. const { name, sortBy, order } = sort
  239. if (name && order) {
  240. sorts[name] = order === 'ascending' ? 'ASC' : 'DESC'
  241. } else if (sortBy && order) {
  242. sorts[sortBy] = order === 'ascending' ? 'ASC' : 'DESC'
  243. } else {
  244. defaultSortsData()
  245. }
  246. } else {
  247. defaultSortsData()
  248. }
  249. },
  250. /**
  251. * 格式分页数据
  252. * @param {} params 查询的参数
  253. * @param {} page 分页
  254. * @param {} sorts 排序
  255. */
  256. formatParams: function(params, page, sorts) {
  257. const results = {}
  258. if (params) {
  259. results.parameters = Object.keys(params).map((k) => {
  260. return k === 'arg' ? params[k] : {
  261. 'key': k,
  262. 'value': params[k]
  263. }
  264. })
  265. }
  266. if (page) {
  267. results.requestPage = {
  268. 'pageNo': page.page || common.PAGE,
  269. 'limit': page.limit || common.LIMIT
  270. }
  271. if (Utils.isNotEmpty(page.totalCount)) { // mock 数据时候要传
  272. results.requestPage['totalCount'] = page.totalCount
  273. }
  274. }
  275. if (sorts) {
  276. results.sorts = Object.keys(sorts).map((k) => {
  277. return {
  278. 'field': k,
  279. 'order': sorts[k]
  280. }
  281. })
  282. }
  283. return results
  284. },
  285. /**
  286. * 下载
  287. */
  288. download: function(data, fileName, responseType = 'application/octet-stream') {
  289. const blob = data instanceof Blob ? data : new Blob([data], { type: responseType })
  290. if ('download' in document.createElement('a')) { // 非IE下载
  291. const url = window.URL.createObjectURL(blob)
  292. const link = document.createElement('a')
  293. link.style.display = 'none'
  294. link.href = url
  295. link.setAttribute('download', fileName)
  296. document.body.appendChild(link)
  297. link.click()
  298. window.URL.revokeObjectURL(link.href)
  299. document.body.removeChild(link)
  300. } else { // IE10+下载
  301. navigator.msSaveBlob(blob, fileName)
  302. }
  303. },
  304. /**
  305. * 导出文件
  306. */
  307. exportFile: function(data, fileName, responseType = 'application/octet-stream') {
  308. this.download(data, fileName, responseType)
  309. }
  310. }
  311. export default action