| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- /**
- * 操作帮助类
- * 处理增删改查、分页等相关操作
- * <pre>
- * 作者:hugh zhuang
- * 邮箱:3378340995@qq.com
- * 日期:2018-10-08-下午3:29:34
- * 版权:广州流辰信息技术有限公司
- * </pre>
- */
- import { Message, MessageBox } from 'element-ui'
- import Utils from '@/utils/util'
- import I18n from '@/utils/i18n'
- import common from '@/constants/common.js'
- const action = {
- msg: function(message, options) {
- Message.closeAll()
- if (!options) {
- options = options || {}
- }
- options.message = message
- Message(options)
- },
- /**
- * 操作警告提示
- */
- warning: function(message) {
- this.msg(message, {
- type: 'warning'
- })
- },
- /**
- * 操作成功提示
- */
- success: function(message) {
- this.msg(message, {
- type: 'success'
- })
- },
- error: function(message) {
- this.msg(message, {
- type: 'error'
- })
- },
- /**
- * 默认操作成功提示
- * @param {*} message
- */
- successMessage: function(message = I18n.t('common.dialog.operateSuccess')) {
- this.success(message)
- },
- /**
- * 删除成功提示
- * @param {*} message
- */
- removeSuccessMessage: function(message = I18n.t('common.dialog.removeSuccess')) {
- this.success(message)
- },
- /**
- * 获取选择行的id
- * @param {*} rows
- * @param {*} pkKey
- */
- getSelectedIds: function(rows, pkKey = 'id') {
- const ids = []
- rows.forEach(row => {
- ids.push(row[pkKey])
- })
- return ids
- },
- /**
- * 选择记录
- * 只能选择一个记录
- * [一般用于编辑、明细等只选择一个记录]
- *
- * @param {*} selection
- */
- selectedRecord: function(selection) {
- return new Promise((resolve, reject) => {
- if (Utils.isEmpty(selection)) {
- this.warning(I18n.t('common.dialog.selectedRecords'))
- return reject(selection)
- }
- if (Array.isArray(selection) && selection.length > 1) {
- this.warning(I18n.t('common.dialog.multipleSelected'))
- return reject(selection)
- }
- if (Array.isArray(selection)) {
- resolve(selection[0])
- } else {
- resolve(selection)
- }
- })
- },
- /**
- * 选择多个记录
- * [一般用于删除等选择多个记录]
- *
- * @param {*} selection 选中的值
- * @param {*} isArray 是否数组格式返回 默认 false
- * @param {*} separator 分割符 默认 `,`
- */
- selectedMultiRecord: function(selection, isArray = false, separator = ',') {
- return new Promise((resolve, reject) => {
- if (Utils.isEmpty(selection)) {
- this.warning(I18n.t('common.dialog.selectedRecords'))
- return reject(selection)
- }
- if (!Array.isArray(selection)) {
- selection = selection.split(separator)
- }
- if (!isArray) { // 不是数组返回
- selection = selection.join(separator)
- }
- resolve(selection)
- })
- },
- /**
- * 删除记录
- * @param {*} rows
- * @param {*} pkKey
- */
- removeRecord: function(selection, confirmMsg = I18n.t('common.dialog.removeRecord'), isArray = false, separator = ',') {
- return new Promise((resolve, reject) => {
- if (Utils.isEmpty(selection)) {
- this.warning(I18n.t('common.dialog.selectedRecords'))
- return reject(selection)
- }
- if (!Array.isArray(selection)) {
- selection = selection.split(separator)
- }
- if (!isArray) { // 不是数组返回
- selection = selection.join(separator)
- }
- MessageBox.confirm(confirmMsg, I18n.t('common.dialog.title'), {
- type: 'warning'
- }).then(() => {
- resolve(selection)
- }).catch((err) => {
- reject(err)
- })
- })
- },
- /**
- * 保存成功提示
- */
- saveSuccessMessage: function(message = I18n.t('common.dialog.operateSuccess'), callback) {
- message = Utils.isNotEmpty(message) ? message : I18n.t('common.dialog.operateSuccess')
- MessageBox.confirm(message,
- I18n.t('common.dialog.title'),
- {
- type: 'success',
- confirmButtonText: I18n.t('common.dialog.saveConfirmButtonText'),
- cancelButtonText: I18n.t('common.dialog.saveCancelButtonText'),
- closeOnClickModal: false,
- callback: (action) => {
- const flag = action !== 'confirm'
- callback(flag)
- }
- })
- },
- /**
- * 保存成功提示
- */
- saveSuccessAlert: function(message = I18n.t('common.dialog.operateSuccess'), callback) {
- MessageBox.alert(message,
- I18n.t('common.dialog.title'),
- {
- type: 'success',
- closeOnClickModal: false,
- showClose: false,
- callback: (action) => {
- callback(action)
- }
- })
- },
- /**
- * 保存失败
- */
- saveErrorMessage: function(message, callback) {
- this.warning(message || I18n.t('common.dialog.saveError'))
- if (callback) { callback() }
- },
- /**
- * 处理列表数据
- * @param vm 当前对象
- * @param data 后台返回的列表数据
- * @param options 参数
- * dataResultKey 默认 dataResult
- * pageResultKey 默认 pageResult
- * resultKey 结果key 默认 listData
- * pageKey 分页key 默认 pagination
- */
- handleListData: function(vm, data, options = {}) {
- const dataResultKey = options.dataResultKey || 'dataResult'
- const pageResultKey = options.pageResultKey || 'pageResult'
- const resultKey = options.resultKey || 'listData'
- const pageKey = options.pageKey || 'pagination'
- vm[resultKey] = data ? data[dataResultKey] || [] : []
- vm[pageKey] = data ? data[pageResultKey] || {} : {}
- },
- /**
- * 设置分页设置
- */
- setPagination: function(pagination, defaultPagination) {
- // 修复通过查询
- if (!defaultPagination) {
- defaultPagination = {
- page: common.PAGE,
- limit: pagination.limit || common.LIMIT
- }
- }
- pagination.page = defaultPagination ? (defaultPagination.page || common.PAGE) : (pagination.page || common.PAGE)
- pagination.limit = defaultPagination ? (defaultPagination.limit || common.LIMIT) : (pagination.limit || common.LIMIT)
- },
- /**
- * 设置分页第一页设置
- * @param {*} pagination
- * @param {*} defaultPagination
- */
- setFirstPagination: function(pagination) {
- pagination.page = common.PAGE
- },
- /**
- * 设置排序
- */
- setSorts: function(sorts, sort, defaultSorts = {}) {
- const defaultSortsData = function() {
- for (const key in defaultSorts) {
- sorts[key] = defaultSorts[key]
- }
- }
- // 清空属性
- for (const key in sorts) {
- delete sorts[key]
- }
- if (sort) {
- const { name, sortBy, order } = sort
- if (name && order) {
- sorts[name] = order === 'ascending' ? 'ASC' : 'DESC'
- } else if (sortBy && order) {
- sorts[sortBy] = order === 'ascending' ? 'ASC' : 'DESC'
- } else {
- defaultSortsData()
- }
- } else {
- defaultSortsData()
- }
- },
- /**
- * 格式分页数据
- * @param {} params 查询的参数
- * @param {} page 分页
- * @param {} sorts 排序
- */
- formatParams: function(params, page, sorts) {
- const results = {}
- if (params) {
- results.parameters = Object.keys(params).map((k) => {
- return k === 'arg' ? params[k] : {
- 'key': k,
- 'value': params[k]
- }
- })
- }
- if (page) {
- results.requestPage = {
- 'pageNo': page.page || common.PAGE,
- 'limit': page.limit || common.LIMIT
- }
- if (Utils.isNotEmpty(page.totalCount)) { // mock 数据时候要传
- results.requestPage['totalCount'] = page.totalCount
- }
- }
- if (sorts) {
- results.sorts = Object.keys(sorts).map((k) => {
- return {
- 'field': k,
- 'order': sorts[k]
- }
- })
- }
- return results
- },
- /**
- * 下载
- */
- download: function(data, fileName, responseType = 'application/octet-stream') {
- const blob = data instanceof Blob ? data : new Blob([data], { type: responseType })
- if ('download' in document.createElement('a')) { // 非IE下载
- const url = window.URL.createObjectURL(blob)
- const link = document.createElement('a')
- link.style.display = 'none'
- link.href = url
- link.setAttribute('download', fileName)
- document.body.appendChild(link)
- link.click()
- window.URL.revokeObjectURL(link.href)
- document.body.removeChild(link)
- } else { // IE10+下载
- navigator.msSaveBlob(blob, fileName)
- }
- },
- /**
- * 导出文件
- */
- exportFile: function(data, fileName, responseType = 'application/octet-stream') {
- this.download(data, fileName, responseType)
- }
- }
- export default action
|