model-util.ts 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
import Vue from 'vue';
import { IModalUtil, ModalParams } from '@ibiz-template/runtime';
import { ModalConfig, ModalInstance } from 'view-design';

function convertParams(params: ModalParams): ModalConfig {
  const config: IData = { ...params };
  if (config.desc) {
    config.content = config.desc;
    delete config.desc;
  }
  return config;
}

/**
 * 简洁确认操作框
 *
 * @author chitanda
 * @date 2022-08-17 16:08:52
 * @export
 * @class ModalUtil
 * @implements {IModalUtil}
 */
export class ModalUtil implements IModalUtil {
  protected modal: ModalInstance = Vue.prototype.$Modal;

  async info(params: ModalParams): Promise<void> {
    this.modal.info(convertParams(params));
  }

  async success(params: ModalParams): Promise<void> {
    this.modal.success(convertParams(params));
  }

  async warning(params: ModalParams): Promise<void> {
    this.modal.warning(convertParams(params));
  }

  async error(params: ModalParams): Promise<void> {
    this.modal.error(convertParams(params));
  }

  async confirm(params: ModalParams): Promise<boolean> {
    return new Promise(resolve => {
      this.modal.confirm({
        ...convertParams(params),
        onOk: () => {
          resolve(true);
        },
        onCancel: () => {
          resolve(false);
        },
      });
    });
  }
}