notice.ts 4.0 KB
Newer Older
zcdtk's avatar
zcdtk committed
1
import { toastController, alertController } from '@ionic/core';
2
import store from '../../store';
zcdtk's avatar
zcdtk committed
3 4 5 6 7 8 9 10
/**
 * 消息提示
 *
 * @export
 * @class Notice
 */
export class Notice {

11 12 13
    /**
     * store
     */
14
    public store:any = null;
15

zcdtk's avatar
zcdtk committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    /**
     * 唯一实例
     *
     * @private
     * @static
     * @type {Notice}
     * @memberof Notice
     */
    private static readonly instance: Notice = new Notice();

    /**
     * Creates an instance of Notice.
     * @memberof Notice
     */
    constructor() {
31
        this.store = store;
zcdtk's avatar
zcdtk committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        if (Notice.instance) {
            return Notice.instance;
        }
    }

    /**
     * 消息提示
     *
     * @param {string} message
     * @param {number} [time]
     * @returns {Promise<any>}
     * @memberof Notice
     */
    public async info(message: string, time?: number): Promise<any> {
        const type = 'secondary';
        return this.createToast(type, message, time);
    }

    /**
     * 成功提示
     *
     * @param {string} message
     * @param {number} [time]
     * @returns {Promise<any>}
     * @memberof Notice
     */
    public async success(message: string, time?: number): Promise<any> {
        const type = 'success';
        return this.createToast(type, message, time);
    }

    /**
     * 警告提示
     *
     * @param {string} message
     * @param {number} [time]
     * @returns {Promise<any>}
     * @memberof Notice
     */
    public async warning(message: string, time?: number): Promise<any> {
        const type = 'warning';
        return this.createToast(type, message, time);
    }

    /**
     * 错误提示
     *
     * @param {string} message
     * @param {number} [time]
     * @returns {Promise<any>}
     * @memberof Notice
     */
    public async error(message: string, time?: number): Promise<any> {
        const type = 'danger';
        return this.createToast(type, message, time);
    }

    /**
     * 确认操作
     *
     * @param {string} message
     * @returns {Promise<any>}
     * @memberof Notice
     */
96
    public async confirm(title: string, message: string): Promise<boolean> {
97 98 99 100 101 102 103
      return new Promise(async (resolve, reject) => {
          const alert = await alertController.create({
              header: title ? title : '标题',
              message: message,
              buttons: [
                  {
                      text: '取消',
104
                      role: 'cancel',
105
                      handler: () => {
106 107
                          if (this.store && this.store.commit) {
                            this.store.commit('setNoticeStatus',true);
108
                          }
109 110 111 112 113 114 115
                          resolve(false);
                      }
                  },
                  {
                      text: '确认',
                      cssClass: 'secondary',
                      handler: () => {
116 117
                          if (this.store && this.store.commit) {
                            this.store.commit('setNoticeStatus',true);
118
                          }
119 120 121 122 123
                          resolve(true);
                      }
                  },
              ],
          });
124
          if (this.store && this.store.state && this.store.state.noticeStatus) {
zcdtk's avatar
zcdtk committed
125
            await alert.present();
126 127
            if (this.store && this.store.commit) {
              this.store.commit('setNoticeStatus',false);
128
            }
129
          }
130
      });
131
    }
zcdtk's avatar
zcdtk committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

    /**
     * 创建对象
     *
     * @private
     * @param {string} type
     * @param {string} message
     * @param {number} [time]
     * @memberof Notice
     */
    private async createToast(type: string, message: string, time?: number) {
        const toast = await toastController.create({
            position: 'top',
            color: type ? type : 'primary',
            duration: time ? time : 2000,
            message: message,
        });
        await toast.present();
    }

    /**
     * 获取实例
     *
     * @static
     * @returns {Notice}
     * @memberof Notice
     */
    public static getInstance(): Notice {
        return this.instance;
    }

}