hlsPopup.js 9.5 KB
Newer Older
李晓兵's avatar
李晓兵 committed
1 2
import Vue from 'vue'
import {ToastPlugin, AlertPlugin, ConfirmPlugin, LoadingPlugin, DatetimePlugin} from 'vux'
JingChao's avatar
JingChao committed
3
import {ActionSheetPlugin, ShowPicturePlugin, SelectPlugin, NotifyPlugin, NumberKeyboardPlugin} from 'hls-easy-ui'
李晓兵's avatar
李晓兵 committed
4 5 6 7 8
Vue.use(ToastPlugin)
Vue.use(AlertPlugin)
Vue.use(ConfirmPlugin)
Vue.use(LoadingPlugin)
Vue.use(DatetimePlugin)
JingChao's avatar
JingChao committed
9 10 11 12 13
Vue.use(ActionSheetPlugin)
Vue.use(ShowPicturePlugin)
Vue.use(SelectPlugin)
Vue.use(NumberKeyboardPlugin)
Vue.use(NotifyPlugin)
李晓兵's avatar
李晓兵 committed
14 15 16 17 18 19

export default {

  isLoading: false,
  SHOW_TIMES: 2000,
  IS_SHOW_MASK: true,
李晓兵's avatar
李晓兵 committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
  /**
   * 移除数组的某个元素
   * @param dx 下标
   * @returns {boolean}
   */
  remove: function (dx) {
    if (isNaN(dx) || dx > this.length) {
      return false
    }
    for (var i = 0, n = 0; i < this.length; i++) {
      if (this[i] != this[dx]) {
        this[n++] = this[i]
      }
    }
    this.length -= 1
  },
李晓兵's avatar
李晓兵 committed
36 37 38 39 40 41
  /**
   * 锁屏函数 超过10s后自动解屏用于防止屏幕锁死
   * 自动截屏成弹出错误提示框
   * @param content 锁屏内容
   */
  showLoading: function (content) {
JingChao's avatar
JingChao committed
42
    let vm = this
李晓兵's avatar
李晓兵 committed
43
    Vue.$vux.loading.show({
JingChao's avatar
JingChao committed
44
      text: content || 'Loading',
李晓兵's avatar
李晓兵 committed
45
    })
JingChao's avatar
JingChao committed
46 47
    this.isLoading = true
    // 10s后自动解屏用于防止屏幕锁死
李晓兵's avatar
李晓兵 committed
48 49
    setTimeout(() => {
      if (vm.isLoading) {
JingChao's avatar
JingChao committed
50 51 52 53
        Vue.$vux.loading.hide()
        vm.isLoading = false
        // 弹出操作失败
        /* Vue.$vux.toast.show({
李晓兵's avatar
李晓兵 committed
54
          text: '操作失败',
JingChao's avatar
JingChao committed
55
          type: 'warn',
李晓兵's avatar
李晓兵 committed
56 57
          time: vm.SHOW_TIMES,
          isShowMask: vm.IS_SHOW_MASK,
JingChao's avatar
JingChao committed
58 59
          position: 'middle',
        }) */
李晓兵's avatar
李晓兵 committed
60
      }
linxin's avatar
linxin committed
61
    }, 120000)
李晓兵's avatar
李晓兵 committed
62 63 64 65 66 67
  },
  /**
   * 隐藏
   */
  hideLoading: function () {
    Vue.$vux.loading.hide()
JingChao's avatar
JingChao committed
68
    this.isLoading = false
李晓兵's avatar
李晓兵 committed
69 70 71 72 73 74 75
  },

  /**
   * 长时间顶部提示toast
   * @param content
   */
  showLongTop: function (content) {
JingChao's avatar
JingChao committed
76 77 78 79 80 81 82 83 84
    let vm = this
    let text = content || '操作失败'
    Vue.$vux.toast.show({
      text: text,
      type: 'text',
      time: vm.SHOW_TIMES,
      isShowMask: vm.IS_SHOW_MASK,
      position: 'top',
    })
李晓兵's avatar
李晓兵 committed
85 86 87 88 89 90 91
  },

  /**
   * 长时间中部提示toast
   * @param content
   */
  showLongCenter: function (content) {
JingChao's avatar
JingChao committed
92
    let vm = this
李晓兵's avatar
李晓兵 committed
93
    // let text = content || '操作失败'
李晓兵's avatar
李晓兵 committed
94 95
    let imgUrl = require('../assets/image/waring.png')
    let text = '<div><img src=' + imgUrl + '></div>' + content
JingChao's avatar
JingChao committed
96 97 98 99 100 101 102
    Vue.$vux.toast.show({
      text: text,
      type: 'text',
      time: vm.SHOW_TIMES,
      isShowMask: vm.IS_SHOW_MASK,
      position: 'middle',
    })
李晓兵's avatar
李晓兵 committed
103 104 105 106 107 108
  },
  /**
   * 长时间中部提示toast
   * @param content
   */
  showLongBottom: function (content) {
JingChao's avatar
JingChao committed
109 110 111 112 113 114 115 116 117
    let vm = this
    let text = content || '操作失败'
    Vue.$vux.toast.show({
      text: text,
      time: vm.SHOW_TIMES,
      type: 'text',
      isShowMask: vm.IS_SHOW_MASK,
      position: 'bottom',
    })
李晓兵's avatar
李晓兵 committed
118 119 120 121 122 123
  },
  /**
   * 成功提示框
   * @param content
   */
  showSuccess: function (content) {
JingChao's avatar
JingChao committed
124
    let vm = this
李晓兵's avatar
李晓兵 committed
125 126 127 128
    Vue.$vux.toast.show({
      text: content || '操作成功',
      time: vm.SHOW_TIMES,
      isShowMask: vm.IS_SHOW_MASK,
JingChao's avatar
JingChao committed
129 130
      type: 'success',
      position: 'middle',
李晓兵's avatar
李晓兵 committed
131 132 133 134 135 136 137
    })
  },
  /**
   * 成功提示框
   * @param content
   */
  showError: function (content) {
JingChao's avatar
JingChao committed
138
    let vm = this
李晓兵's avatar
李晓兵 committed
139 140
    Vue.$vux.toast.show({
      text: content || '操作失败',
JingChao's avatar
JingChao committed
141
      type: 'warn',
李晓兵's avatar
李晓兵 committed
142 143
      isShowMask: vm.IS_SHOW_MASK,
      time: vm.SHOW_TIMES,
JingChao's avatar
JingChao committed
144
      position: 'middle',
李晓兵's avatar
李晓兵 committed
145 146 147 148 149 150 151
    })
  },
  /**
   * 弹出是否确认的窗口
   * @param confirmObject.title 标题
   * @param confirmObject.content 内容
   * @param confirmObject.onConfirm 确定函数
Jennie Shi's avatar
Jennie Shi committed
152 153
   * @param confirmObject.confirmText 确定函数
   * @param confirmObject.cancelText 确定函数
李晓兵's avatar
李晓兵 committed
154 155
   */
  showConfirm: function (confirmObject) {
JingChao's avatar
JingChao committed
156 157 158
    let def = {
      title: confirmObject.title || '提示',
      content: confirmObject.content || '',
Jennie Shi's avatar
Jennie Shi committed
159 160
      confirmText: confirmObject.confirmText || '确定',
      cancelText: confirmObject.cancelText || '取消',
JingChao's avatar
JingChao committed
161 162 163 164 165 166
      onConfirm: () => {
        confirmObject.onConfirm(1)
      },
      onCancel: () => {
        confirmObject.onConfirm(0)
      },
李晓兵's avatar
李晓兵 committed
167
    }
JingChao's avatar
JingChao committed
168
    Vue.$vux.confirm.show(def)
李晓兵's avatar
李晓兵 committed
169 170 171 172 173 174 175 176 177
  },
  /*
   * 弹出确认的窗口
   * @param confirmObject.title 标题
   * @param confirmObject.content 内容
   * @param confirmObject.onConfirm 确定函数
   *
   */
  showPopup: function (confirmObject) {
JingChao's avatar
JingChao committed
178 179 180 181 182 183 184 185
    let def = {
      title: confirmObject.title || '提示',
      content: confirmObject.content || '',
      confirmText: '确定',
      showCancelButton: false,
      onConfirm: () => {
        confirmObject.onConfirm()
      },
李晓兵's avatar
李晓兵 committed
186
    }
JingChao's avatar
JingChao committed
187
    Vue.$vux.confirm.show(def)
李晓兵's avatar
李晓兵 committed
188
  },
李晓兵's avatar
李晓兵 committed
189 190 191
  hideConfirm: function () {
    Vue.$vux.confirm.hide()
  },
李晓兵's avatar
李晓兵 committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  /**
   * @param actionObject.titleText 弹出框的标题可空
   * @param actionObject.callback 点击按钮的回调函数 回传buttonArray数组下标
   * @param actionObject.buttonArray 按钮数组支持[string,string],[object,object],
   *        当为后一种是 object为{text:'拍照',type:'primary'},type支持 primary,warn,disabled
   *
   *  {
   *      titleText: '照片',
   *      buttonArray: [{text:'拍照',type:'warn'}, {text:'从相册取',type:'primary'}],
   *      callback: (index) => {
   *        alert(index);
   *      }
   *   }
   *
   *    {
   *      buttonArray: ['拍照','从相册取'],
   *      callback: (index) => {
   *        alert(index);
   *      }
   *   }
   *
   */
  showActionSheet: function (actionObject) {
    if (typeof actionObject === 'object') {
JingChao's avatar
JingChao committed
216
      let buttons = []
李晓兵's avatar
李晓兵 committed
217 218 219 220 221
      for (let i = 0; i < actionObject.buttonArray.length; i++) {
        if (typeof actionObject.buttonArray[i] === 'object') {
          buttons.push({
            text: actionObject.buttonArray[i].text,
            type: actionObject.buttonArray[i].type,
JingChao's avatar
JingChao committed
222
            callback: actionObject.callback,
李晓兵's avatar
李晓兵 committed
223 224 225 226
          })
        } else {
          buttons.push({
            text: actionObject.buttonArray[i],
JingChao's avatar
JingChao committed
227
            callback: actionObject.callback,
李晓兵's avatar
李晓兵 committed
228 229 230
          })
        }
      }
JingChao's avatar
JingChao committed
231
      ActionSheetPlugin.show({
李晓兵's avatar
李晓兵 committed
232
        title: actionObject.titleText || '',
JingChao's avatar
JingChao committed
233
        buttons: buttons,
李晓兵's avatar
李晓兵 committed
234 235 236 237 238 239 240 241 242 243 244 245
      })
    }
  },

  /**
   * 时间选择函数
   * @param timeObject.nowDate 当前展示的时间 可不填
   * @param timeObject.format 时间格式支持不支持秒
   * @param timeObject.callback 点击确定的回调函数
   *
   */
  showTime: function (timeObject) {
JingChao's avatar
JingChao committed
246 247
    let date = new Date().format('yyyy-MM-dd')
    let format = 'YYYY-MM-DD'
李晓兵's avatar
李晓兵 committed
248 249 250 251 252 253 254 255 256 257 258 259 260
    if (timeObject.nowDate) {
      date = timeObject.nowDate
    }
    if (timeObject.format) {
      format = timeObject.format
    }
    Vue.$vux.datetime.show({
      cancelText: '取消',
      confirmText: '确定',
      minYear: '1900',
      maxYear: '2200',
      format: format,
      value: date,
JingChao's avatar
JingChao committed
261 262 263
      onConfirm (val) {
        timeObject.callback(val)
      },
李晓兵's avatar
李晓兵 committed
264 265 266 267 268 269 270 271
    })
  },
  /**
   * 图片放大预览
   * @param imgObject.imgUrl
   */
  showBigPicture: function (imgObject) {
    if (typeof imgObject === 'object') {
JingChao's avatar
JingChao committed
272
      ShowPicturePlugin.show({
李晓兵's avatar
李晓兵 committed
273
        imgUrl: imgObject.imgUrl,
JingChao's avatar
JingChao committed
274
        width: imgObject.width,
李晓兵's avatar
李晓兵 committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
      })
    }
  },

  /**
   * 下拉框 支持级联操作 需指定 parent 属性
   * @param selectOption.list Array [{"code": "NP","code_name": "个人"}]
   * @param selectOption.code String "bp_type"
   * @param selectOption.object Object 当前数据对象
   * @param selectOption.returnItem function 回调函数返回index与object
   * var bp_class_list = [
   *    {
   *      "code": "NP",
   *      "code_name": "个人"
   *    },{
   *       "code": "NP1",
   *       "code_name": "个人1"
   *     }];
   *  hlsPopup.selectList({
   *     list: bp_class_list,
   *     code: 'bp_type',
   *     object: {},
   *     returnItem: function (index, obj) {
   *        console.log(obj)
   *      }
   *   })
   *
   */
  selectList: function (selectOption) {
    if (typeof selectOption === 'object') {
JingChao's avatar
JingChao committed
305 306
      let list = []
      let length = selectOption.list.length
李晓兵's avatar
李晓兵 committed
307 308 309 310
      vum.forEach(selectOption.list, function (date, index, array) {
        list.push({
          value: date.code,
          name: date.code_name,
JingChao's avatar
JingChao committed
311
          parent: date.parent,
李晓兵's avatar
李晓兵 committed
312 313
        })
        if (index === (length - 1)) {
JingChao's avatar
JingChao committed
314
          SelectPlugin.show({
李晓兵's avatar
李晓兵 committed
315 316 317
            list: list,
            callBack: selectOption.returnItem,
            code: selectOption.code,
JingChao's avatar
JingChao committed
318
            object: selectOption.object,
李晓兵's avatar
李晓兵 committed
319 320
          })
        }
JingChao's avatar
JingChao committed
321
      })
李晓兵's avatar
李晓兵 committed
322 323 324 325 326 327 328 329 330 331 332
    }
  },
  /**
   * 弹出数字键盘
   * @param keyboardObject.title 键盘的title
   * @param keyboardObject.closeButtonText 键盘的关闭按钮文字
   * @param keyboardObject.keyDown 普通按键按下去事件
   * @param keyboardObject.keyDelete 删除按键按下去事件
   */
  showNumberKeyborad: function (keyboardObject) {
    if (typeof keyboardObject === 'object') {
JingChao's avatar
JingChao committed
333
      NumberKeyboardPlugin.show({
李晓兵's avatar
李晓兵 committed
334 335 336 337 338 339 340 341
        title: keyboardObject.title,
        closeButtonText: keyboardObject.closeButtonText,
        extraKey: keyboardObject.extraKey || '.',
        keyDown: function (text) {
          keyboardObject.keyDown(text)
        },
        keyDelete: function () {
          keyboardObject.keyDelete()
JingChao's avatar
JingChao committed
342 343
          // console.log('delete')
        },
李晓兵's avatar
李晓兵 committed
344 345 346 347 348 349 350 351 352 353 354 355
      })
    }
  },
  /**
   * 弹出Notify
   * @param notifyObject.content 内容
   * @param notifyObject.position 位置
   * @param notifyObject.time 显示时长
   * @param notifyObject.type 类型  success warning default
   */
  showNotify: function (notifyObject) {
    if (typeof notifyObject === 'object') {
JingChao's avatar
JingChao committed
356
      NotifyPlugin.show({
李晓兵's avatar
李晓兵 committed
357 358 359 360
        show: true,
        content: notifyObject.content,
        position: notifyObject.position || 'top',
        time: notifyObject.time || 3000,
JingChao's avatar
JingChao committed
361
        type: notifyObject.type || 'default',
李晓兵's avatar
李晓兵 committed
362 363
      })
    }
JingChao's avatar
JingChao committed
364
  },
李晓兵's avatar
李晓兵 committed
365
}