| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- /**
- * 操作帮助类
- * 处理增删改查、分页等相关操作
- * <pre>
- * 作者:hugh zhuang
- * 邮箱:3378340995@qq.com
- * 日期:2018-10-08-下午3:29:34
- * 版权:广州流辰信息技术有限公司
- * </pre>
- */
- import { Notify, Dialog } from 'vant'
- import Utils from '@/utils/util'
- import I18n from '@/utils/i18n'
- import common from '@/constants/common.js'
- const action = {
- descDialog: function ({ title, message, className = 'ibps-dialog-desc', confirmButtonColor = '#3396FB', confirmButtonText = '知道了', overlay = true }) {
- Dialog.alert({
- title,
- message,
- confirmButtonColor,
- confirmButtonText,
- overlay,
- className,
- messageAlign: 'left'
- })
- },
- msg: function (message, options) {
- if (!options) {
- options = options || {}
- }
- options.message = message
- Notify(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)
- }
- Dialog.confirm({
- title: I18n.t('common.dialog.title'),
- message: confirmMsg
- }).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,
- // callback: (action) => {
- // callback(action)
- // }
- // })
- },
- /**
- * 保存失败
- */
- saveErrorMessage: function (message, callback) {
- this.warning(message || I18n.t('common.dialog.saveError'))
- if (callback) { callback() }
- },
- initListData(vm, options = {}) {
- const resultTypeKey = options.resultTypeKey || 'resultType'
- const resultKey = options.resultKey || 'listData'
- const paginationKey = options.paginationKey || 'pagination'
- const pageKey = options.pageKey || 'page'
- const finishedKey = options.finishedKey || 'finished'
- vm[finishedKey] = false
- // 设置第一页,数据设置为空
- vm[paginationKey][pageKey] = 1
- vm[resultTypeKey] = 'init'
- vm[resultKey] = []
- },
- /**
- * 处理列表数据(不过滤)
- * @param vm 当前对象
- * @param data 后台返回的列表数据
- * @param options 参数
- * dataResultKey 默认 dataResult
- * pageResultKey 默认 pageResult
- * resultKey 结果key 默认 listData
- * pageKey 分页key 默认 pagination
- */
- handleListData: function (vm, data, options = {}) {
- const loadingKey = options.loadingKey || 'loading'
- const refreshingKey = options.refreshingKey || 'refreshing'
- const finishedKey = options.finishedKey || 'finished'
- const dataResultKey = options.dataResultKey || 'dataResult'
- const pageResultKey = options.pageResultKey || 'pageResult'
- const resultKey = options.resultKey || 'listData'
- const paginationKey = options.paginationKey || 'pagination'
- const pageKey = options.pageKey || 'page'
- const resultTypeKey = options.resultTypeKey || 'resultType'
- const resultMessageKey = options.resultMessageKey || 'resultMessage'
- const listData = data ? data[dataResultKey] || [] : []
- const pagination = data ? data[pageResultKey] || {} : {}
- if (vm[refreshingKey]) {
- vm[resultKey] = []
- vm[refreshingKey] = false
- }
- if (Utils.isNotEmpty(listData)) {
- pagination[pageKey] = pagination[pageKey] + 1
- vm[resultKey] = vm[resultKey].concat(listData)
- }
- vm[loadingKey] = false
- vm[finishedKey] = this.isFinished(pagination, vm[resultKey])
- vm[paginationKey] = pagination
- // 处理结果类型
- vm[resultTypeKey] = this.handleResultType(vm, options)
- vm[resultMessageKey] = null
- },
- /**
- * 处理列表数据(带过滤)
- * @param vm 当前对象
- * @param data 后台返回的列表数据
- * @param options 参数
- * dataResultKey 默认 dataResult
- * pageResultKey 默认 pageResult
- * resultKey 结果key 默认 listData
- * pageKey 分页key 默认 pagination
- */
- existHandleListData: function (vm, data, filtration, itemid, tid, options={}) {
- const loadingKey = options.loadingKey || 'loading'
- const refreshingKey = options.refreshingKey || 'refreshing'
- const finishedKey = options.finishedKey || 'finished'
- const dataResultKey = options.dataResultKey || 'dataResult'
- const pageResultKey = options.pageResultKey || 'pageResult'
- const resultKey = options.resultKey || 'listData'
- const paginationKey = options.paginationKey || 'pagination'
- const pageKey = options.pageKey || 'page'
- const resultTypeKey = options.resultTypeKey || 'resultType'
- const resultMessageKey = options.resultMessageKey || 'resultMessage'
- const listData = data ? data[dataResultKey] || [] : []
- const pagination = data ? data[pageResultKey] || {} : {}
- if (vm[refreshingKey]) {
- vm[resultKey] = []
- vm[refreshingKey] = false
- }
- if (Utils.isNotEmpty(listData)) {
- let mid = []
- listData.forEach((item,i)=>{
- const obj = filtration.findIndex(t=> t[tid] === item[itemid])
- if(obj != -1){
- mid.push(item)
- }
- })
- pagination[pageKey] = pagination[pageKey] + 1
- vm[resultKey] = vm[resultKey].concat(mid)
-
- }
- vm[loadingKey] = false
- vm[finishedKey] = Utils.isEmpty(listData) ? true : false
- vm[paginationKey] = pagination
- // 处理结果类型
- vm[resultTypeKey] = this.handleResultType(vm, options)
- vm[resultMessageKey] = null
- },
- /**
- * 处理通用接口列表数据
- * @param vm 当前对象
- * @param data 后台返回的列表数据
- * @param options 参数
- * dataResultKey 默认 dataResult
- * pageResultKey 默认 pageResult
- * resultKey 结果key 默认 listData
- * pageKey 分页key 默认 pagination
- */
- handleListDataCommon: function (vm, data, options = {}) {
- const loadingKey = options.loadingKey || 'loading'
- const refreshingKey = options.refreshingKey || 'refreshing'
- const finishedKey = options.finishedKey || 'finished'
- const dataResultKey = options.dataResultKey || 'data'
- const pageResultKey = options.pageResultKey || 'pageResult'
- const resultKey = options.resultKey || 'listData'
- const paginationKey = options.paginationKey || 'pagination'
- const pageKey = options.pageKey || 'page'
- const resultTypeKey = options.resultTypeKey || 'resultType'
- const resultMessageKey = options.resultMessageKey || 'resultMessage'
- const listData = data ? data[dataResultKey] || [] : []
- const pagination = data ? data[pageResultKey] || {} : {}
- if (vm[refreshingKey]) {
- vm[resultKey] = []
- vm[refreshingKey] = false
- }
- if (Utils.isNotEmpty(listData)) {
- vm[pageKey] = vm[pageKey] + 1
- vm[resultKey] = vm[resultKey].concat(listData)
- }
- vm[loadingKey] = false
- vm[finishedKey] = Utils.isEmpty(listData) ? true : false
- // vm[paginationKey] = pagination
- // 处理结果类型
- vm[resultTypeKey] = this.handleResultType(vm, options)
- vm[resultMessageKey] = null
- },
- handleResultType: function (vm, options) {
- const resultKey = options.resultKey || 'listData'
- const finishedKey = options.finishedKey || 'finished'
- const list = vm[resultKey]
- const finished = vm[finishedKey]
- if (list && list.length > 0) {
- if (finished) return 'finished'
- return
- }
- return 'empty'
- },
- isFinished({ totalCount }, listData) {
- // 异常终止
- if (Utils.isEmpty(listData)) {
- return true
- }
- return listData.length >= totalCount
- },
- handleErrorData: function (vm, e, options = {}) {
- const loadingKey = options.loadingKey || 'loading'
- const refreshingKey = options.refreshingKey || 'refreshing'
- const finishedKey = options.finishedKey || 'finished'
- const resultTypeKey = options.resultTypeKey || 'resultType'
- const resultMessageKey = options.resultMessageKey || 'resultMessage'
- vm[loadingKey] = false
- vm[refreshingKey] = false
- vm[finishedKey] = true
- vm[resultTypeKey] = e.state === 500 ? 'network' : 'error'
- vm[resultMessageKey] = e.message
- },
- /**
- * 设置分页设置
- */
- setPagination: function (pagination, defaultPagination) {
- pagination.page = defaultPagination ? defaultPagination.page || common.PAGE : common.PAGE
- pagination.limit = defaultPagination ? defaultPagination.limit || common.LIMIT : common.LIMIT
- },
- /**
- * 设置排序
- */
- 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, order } = sort
- if (name && order) {
- sorts[name] = order === 'ascending' ? 'ASC' : 'DESC'
- } else {
- defaultSortsData()
- }
- } else {
- defaultSortsData()
- }
- },
- /**
- * 格式分页数据
- * @param {} params 查询的参数
- * @param {} page 分页
- * @param {} sorts 排序
- */
- formatParams: function (params, pagination, sorts) {
- const results = {}
- if (params) {
- results.parameters = Object.keys(params).map((k) => {
- return {
- 'key': k,
- 'value': params[k] ? params[k] : ''
- }
- })
- }
- if (pagination) {
- const requestPage = {
- 'pageNo': pagination.page || common.PAGE,
- 'limit': pagination.limit || common.LIMIT
- }
- if (Utils.isNotEmpty(pagination.totalCount)) { // mock 数据时候要传
- requestPage['totalCount'] = pagination.totalCount
- }
- results.requestPage = requestPage
- }
- 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
|