action.js 9.9 KB

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