johnsen 6 месяцев назад
Родитель
Сommit
91059289ff
2 измененных файлов с 159 добавлено и 96 удалено
  1. 1 0
      src/main.js
  2. 158 96
      src/utils/action.js

+ 1 - 0
src/main.js

@@ -29,6 +29,7 @@ import Vant from 'vant'
 Vue.use(ibps)
 Vue.use(Vant) // 导入vant
 Vue.prototype.$action = ActionUtils // 全局action
+Vue.prototype.$message = ActionUtils // 兼容PC端
 Vue.prototype.$utils = utils // 全局帮助类
 Vue.prototype.$common = common // 通用请求
 Vue.prototype.$methCommon = methCommon

+ 158 - 96
src/utils/action.js

@@ -14,7 +14,14 @@ 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 }) {
+  descDialog: function({
+    title,
+    message,
+    className = 'ibps-dialog-desc',
+    confirmButtonColor = '#3396FB',
+    confirmButtonText = '知道了',
+    overlay = true
+  }) {
     Dialog.alert({
       title,
       message,
@@ -25,6 +32,16 @@ const action = {
       messageAlign: 'left'
     })
   },
+  confirm: function(message, title, options = {
+    className: 'ibps-dialog-desc',
+    confirmButtonColor: '#3396FB'
+  }) {
+    return Dialog.confirm({
+      title,
+      message,
+      ...options
+    })
+  },
   msg: function(message, options) {
     if (!options) {
       options = options || {}
@@ -64,28 +81,30 @@ const action = {
    * 删除成功提示
    * @param {*} message
    */
-  removeSuccessMessage: function(message = I18n.t('common.dialog.removeSuccess')) {
+  removeSuccessMessage: function(
+    message = I18n.t('common.dialog.removeSuccess')
+  ) {
     this.success(message)
   },
   /**
- * 获取选择行的id
- * @param {*} rows
- * @param {*} pkKey
- */
+   * 获取选择行的id
+   * @param {*} rows
+   * @param {*} pkKey
+   */
   getSelectedIds: function(rows, pkKey = 'id') {
     const ids = []
-    rows.forEach(row => {
+    rows.forEach((row) => {
       ids.push(row[pkKey])
     })
     return ids
   },
   /**
- * 选择记录
- * 只能选择一个记录
- * [一般用于编辑、明细等只选择一个记录]
- *
- * @param {*} selection
- */
+   * 选择记录
+   * 只能选择一个记录
+   * [一般用于编辑、明细等只选择一个记录]
+   *
+   * @param {*} selection
+   */
   selectedRecord: function(selection) {
     return new Promise((resolve, reject) => {
       if (Utils.isEmpty(selection)) {
@@ -104,13 +123,13 @@ const action = {
     })
   },
   /**
- * 选择多个记录
- * [一般用于删除等选择多个记录]
- *
- * @param {*} selection 选中的值
- * @param {*} isArray 是否数组格式返回 默认 false
- * @param {*} separator 分割符 默认 `,`
- */
+   * 选择多个记录
+   * [一般用于删除等选择多个记录]
+   *
+   * @param {*} selection 选中的值
+   * @param {*} isArray 是否数组格式返回 默认 false
+   * @param {*} separator 分割符 默认 `,`
+   */
   selectedMultiRecord: function(selection, isArray = false, separator = ',') {
     return new Promise((resolve, reject) => {
       if (Utils.isEmpty(selection)) {
@@ -121,18 +140,24 @@ const action = {
       if (!Array.isArray(selection)) {
         selection = selection.split(separator)
       }
-      if (!isArray) { // 不是数组返回
+      if (!isArray) {
+        // 不是数组返回
         selection = selection.join(separator)
       }
       resolve(selection)
     })
   },
   /**
- * 删除记录
- * @param {*} rows
- * @param {*} pkKey
- */
-  removeRecord: function(selection, confirmMsg = I18n.t('common.dialog.removeRecord'), isArray = false, separator = ',') {
+   * 删除记录
+   * @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'))
@@ -142,25 +167,33 @@ const action = {
       if (!Array.isArray(selection)) {
         selection = selection.split(separator)
       }
-      if (!isArray) { // 不是数组返回
+      if (!isArray) {
+        // 不是数组返回
         selection = selection.join(separator)
       }
       Dialog.confirm({
         title: I18n.t('common.dialog.title'),
         message: confirmMsg
-      }).then(() => {
-        resolve(selection)
-      }).catch((err) => {
-        reject(err)
       })
+        .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')
+   * 保存成功提示
+   */
+  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'),
@@ -177,9 +210,12 @@ const action = {
   },
 
   /**
- * 保存成功提示
- */
-  saveSuccessAlert: function(message = I18n.t('common.dialog.operateSuccess'), callback) {
+   * 保存成功提示
+   */
+  saveSuccessAlert: function(
+    message = I18n.t('common.dialog.operateSuccess'),
+    callback
+  ) {
     // MessageBox.alert(message,
     //   I18n.t('common.dialog.title'),
     //   {
@@ -191,11 +227,13 @@ const action = {
     //   })
   },
   /**
- * 保存失败
- */
+   * 保存失败
+   */
   saveErrorMessage: function(message, callback) {
     this.warning(message || I18n.t('common.dialog.saveError'))
-    if (callback) { callback() }
+    if (callback) {
+      callback()
+    }
   },
   initListData(vm, options = {}) {
     const resultTypeKey = options.resultTypeKey || 'resultType'
@@ -212,15 +250,15 @@ const action = {
     vm[resultKey] = []
   },
   /**
- * 处理列表数据(不过滤)
- * @param vm 当前对象
- * @param data 后台返回的列表数据
- * @param options 参数
- *   dataResultKey 默认 dataResult
- *  pageResultKey 默认 pageResult
- *  resultKey 结果key 默认 listData
- *  pageKey 分页key 默认 pagination
- */
+   * 处理列表数据(不过滤)
+   * @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'
@@ -251,20 +289,26 @@ const action = {
     vm[paginationKey] = pagination
     // 处理结果类型
     vm[resultTypeKey] = this.handleResultType(vm, options)
-    console.log('vm[resultTypeKey]===>', vm[dataResultKey], vm[pageResultKey], vm[resultKey])
     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 = {}) {
+   * 处理列表数据(带过滤)
+   * @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'
@@ -286,7 +330,7 @@ const action = {
     if (Utils.isNotEmpty(listData)) {
       const mid = []
       listData.forEach((item, i) => {
-        const obj = filtration.findIndex(t => t[tid] === item[itemid])
+        const obj = filtration.findIndex((t) => t[tid] === item[itemid])
         if (obj != -1) {
           mid.push(item)
         }
@@ -377,16 +421,20 @@ const action = {
     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
+    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) {
@@ -410,35 +458,39 @@ const action = {
   },
 
   /**
- * 格式分页数据
- * @param {} params 查询的参数
- * @param {} page 分页
- * @param {} sorts 排序
- */
-  formatParams: function(params, page, sorts) {
+   * 格式分页数据
+   * @param {} params 查询的参数
+   * @param {} page 分页
+   * @param {} sorts 排序
+   */
+  formatParams: function(params, pagination, sorts) {
     const results = {}
     if (params) {
       results.parameters = Object.keys(params).map((k) => {
-        return k === 'arg' ? params[k] : {
-          'key': k,
-          'value': params[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 (pagination) {
+      const requestPage = {
+        pageNo: pagination.page || common.PAGE,
+        limit: pagination.limit || common.LIMIT
       }
-      if (Utils.isNotEmpty(page.totalCount)) { // mock 数据时候要传
-        results.requestPage['totalCount'] = page.totalCount
+      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]
+          field: k,
+          order: sorts[k]
         }
       })
     }
@@ -446,11 +498,17 @@ const action = {
     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下载
+   * 下载
+   */
+  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'
@@ -460,18 +518,22 @@ const action = {
       link.click()
       window.URL.revokeObjectURL(link.href)
       document.body.removeChild(link)
-    } else { // IE10+下载
+    } else {
+      // IE10+下载
       navigator.msSaveBlob(blob, fileName)
     }
   },
 
   /**
- * 导出文件
- */
-  exportFile: function(data, fileName, responseType = 'application/octet-stream') {
+   * 导出文件
+   */
+  exportFile: function(
+    data,
+    fileName,
+    responseType = 'application/octet-stream'
+  ) {
     this.download(data, fileName, responseType)
   }
-
 }
 
 export default action