hlsPopup.js 9.18 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 36
  /**
   * 移除数组的某个元素
   * @param dx 下标
   * @returns {boolean}
   */
  remove: function (dx) {
    debugger
    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
37 38 39 40 41 42
  /**
   * 锁屏函数 超过10s后自动解屏用于防止屏幕锁死
   * 自动截屏成弹出错误提示框
   * @param content 锁屏内容
   */
  showLoading: function (content) {
JingChao's avatar
JingChao committed
43
    let vm = this
李晓兵's avatar
李晓兵 committed
44
    Vue.$vux.loading.show({
JingChao's avatar
JingChao committed
45
      text: content || 'Loading',
李晓兵's avatar
李晓兵 committed
46
    })
JingChao's avatar
JingChao committed
47 48
    this.isLoading = true
    // 10s后自动解屏用于防止屏幕锁死
李晓兵's avatar
李晓兵 committed
49 50
    setTimeout(() => {
      if (vm.isLoading) {
JingChao's avatar
JingChao committed
51 52 53 54
        Vue.$vux.loading.hide()
        vm.isLoading = false
        // 弹出操作失败
        /* Vue.$vux.toast.show({
李晓兵's avatar
李晓兵 committed
55
          text: '操作失败',
JingChao's avatar
JingChao committed
56
          type: 'warn',
李晓兵's avatar
李晓兵 committed
57 58
          time: vm.SHOW_TIMES,
          isShowMask: vm.IS_SHOW_MASK,
JingChao's avatar
JingChao committed
59 60
          position: 'middle',
        }) */
李晓兵's avatar
李晓兵 committed
61
      }
JingChao's avatar
JingChao committed
62
    }, 40000)
李晓兵's avatar
李晓兵 committed
63 64 65 66 67 68
  },
  /**
   * 隐藏
   */
  hideLoading: function () {
    Vue.$vux.loading.hide()
JingChao's avatar
JingChao committed
69
    this.isLoading = false
李晓兵's avatar
李晓兵 committed
70 71 72 73 74 75 76
  },

  /**
   * 长时间顶部提示toast
   * @param content
   */
  showLongTop: function (content) {
JingChao's avatar
JingChao committed
77 78 79 80 81 82 83 84 85
    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
86 87 88 89 90 91 92
  },

  /**
   * 长时间中部提示toast
   * @param content
   */
  showLongCenter: function (content) {
JingChao's avatar
JingChao committed
93 94 95 96 97 98 99 100 101
    let vm = this
    let text = content || '操作失败'
    Vue.$vux.toast.show({
      text: text,
      type: 'text',
      time: vm.SHOW_TIMES,
      isShowMask: vm.IS_SHOW_MASK,
      position: 'middle',
    })
李晓兵's avatar
李晓兵 committed
102 103 104 105 106 107
  },
  /**
   * 长时间中部提示toast
   * @param content
   */
  showLongBottom: function (content) {
JingChao's avatar
JingChao committed
108 109 110 111 112 113 114 115 116
    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
117 118 119 120 121 122
  },
  /**
   * 成功提示框
   * @param content
   */
  showSuccess: function (content) {
JingChao's avatar
JingChao committed
123
    let vm = this
李晓兵's avatar
李晓兵 committed
124 125 126 127
    Vue.$vux.toast.show({
      text: content || '操作成功',
      time: vm.SHOW_TIMES,
      isShowMask: vm.IS_SHOW_MASK,
JingChao's avatar
JingChao committed
128 129
      type: 'success',
      position: 'middle',
李晓兵's avatar
李晓兵 committed
130 131 132 133 134 135 136
    })
  },
  /**
   * 成功提示框
   * @param content
   */
  showError: function (content) {
JingChao's avatar
JingChao committed
137
    let vm = this
李晓兵's avatar
李晓兵 committed
138 139
    Vue.$vux.toast.show({
      text: content || '操作失败',
JingChao's avatar
JingChao committed
140
      type: 'warn',
李晓兵's avatar
李晓兵 committed
141 142
      isShowMask: vm.IS_SHOW_MASK,
      time: vm.SHOW_TIMES,
JingChao's avatar
JingChao committed
143
      position: 'middle',
李晓兵's avatar
李晓兵 committed
144 145 146 147 148 149 150 151 152
    })
  },
  /**
   * 弹出是否确认的窗口
   * @param confirmObject.title 标题
   * @param confirmObject.content 内容
   * @param confirmObject.onConfirm 确定函数
   */
  showConfirm: function (confirmObject) {
JingChao's avatar
JingChao committed
153 154 155 156 157 158 159 160 161 162 163
    let def = {
      title: confirmObject.title || '提示',
      content: confirmObject.content || '',
      confirmText: '确定',
      cancelText: '取消',
      onConfirm: () => {
        confirmObject.onConfirm(1)
      },
      onCancel: () => {
        confirmObject.onConfirm(0)
      },
李晓兵's avatar
李晓兵 committed
164
    }
JingChao's avatar
JingChao committed
165
    Vue.$vux.confirm.show(def)
李晓兵's avatar
李晓兵 committed
166 167 168 169 170 171 172 173 174
  },
  /*
   * 弹出确认的窗口
   * @param confirmObject.title 标题
   * @param confirmObject.content 内容
   * @param confirmObject.onConfirm 确定函数
   *
   */
  showPopup: function (confirmObject) {
JingChao's avatar
JingChao committed
175 176 177 178 179 180 181 182
    let def = {
      title: confirmObject.title || '提示',
      content: confirmObject.content || '',
      confirmText: '确定',
      showCancelButton: false,
      onConfirm: () => {
        confirmObject.onConfirm()
      },
李晓兵's avatar
李晓兵 committed
183
    }
JingChao's avatar
JingChao committed
184
    Vue.$vux.confirm.show(def)
李晓兵's avatar
李晓兵 committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  },
  /**
   * @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
210
      let buttons = []
李晓兵's avatar
李晓兵 committed
211 212 213 214 215
      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
216
            callback: actionObject.callback,
李晓兵's avatar
李晓兵 committed
217 218 219 220
          })
        } else {
          buttons.push({
            text: actionObject.buttonArray[i],
JingChao's avatar
JingChao committed
221
            callback: actionObject.callback,
李晓兵's avatar
李晓兵 committed
222 223 224
          })
        }
      }
JingChao's avatar
JingChao committed
225
      ActionSheetPlugin.show({
李晓兵's avatar
李晓兵 committed
226
        title: actionObject.titleText || '',
JingChao's avatar
JingChao committed
227
        buttons: buttons,
李晓兵's avatar
李晓兵 committed
228 229 230 231 232 233 234 235 236 237 238 239
      })
    }
  },

  /**
   * 时间选择函数
   * @param timeObject.nowDate 当前展示的时间 可不填
   * @param timeObject.format 时间格式支持不支持秒
   * @param timeObject.callback 点击确定的回调函数
   *
   */
  showTime: function (timeObject) {
JingChao's avatar
JingChao committed
240 241
    let date = new Date().format('yyyy-MM-dd')
    let format = 'YYYY-MM-DD'
李晓兵's avatar
李晓兵 committed
242 243 244 245 246 247 248 249 250 251 252 253 254
    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
255 256 257
      onConfirm (val) {
        timeObject.callback(val)
      },
李晓兵's avatar
李晓兵 committed
258 259 260 261 262 263 264 265
    })
  },
  /**
   * 图片放大预览
   * @param imgObject.imgUrl
   */
  showBigPicture: function (imgObject) {
    if (typeof imgObject === 'object') {
JingChao's avatar
JingChao committed
266
      ShowPicturePlugin.show({
李晓兵's avatar
李晓兵 committed
267
        imgUrl: imgObject.imgUrl,
JingChao's avatar
JingChao committed
268
        width: imgObject.width,
李晓兵's avatar
李晓兵 committed
269 270 271 272
      })
    }
  },

李晓兵's avatar
李晓兵 committed
273

李晓兵's avatar
李晓兵 committed
274 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
  /**
   * 下拉框 支持级联操作 需指定 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
300 301
      let list = []
      let length = selectOption.list.length
李晓兵's avatar
李晓兵 committed
302 303 304 305
      vum.forEach(selectOption.list, function (date, index, array) {
        list.push({
          value: date.code,
          name: date.code_name,
JingChao's avatar
JingChao committed
306
          parent: date.parent,
李晓兵's avatar
李晓兵 committed
307 308
        })
        if (index === (length - 1)) {
JingChao's avatar
JingChao committed
309
          SelectPlugin.show({
李晓兵's avatar
李晓兵 committed
310 311 312
            list: list,
            callBack: selectOption.returnItem,
            code: selectOption.code,
JingChao's avatar
JingChao committed
313
            object: selectOption.object,
李晓兵's avatar
李晓兵 committed
314 315
          })
        }
JingChao's avatar
JingChao committed
316
      })
李晓兵's avatar
李晓兵 committed
317 318 319 320 321 322 323 324 325 326 327
    }
  },
  /**
   * 弹出数字键盘
   * @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
328
      NumberKeyboardPlugin.show({
李晓兵's avatar
李晓兵 committed
329 330 331 332 333 334 335 336
        title: keyboardObject.title,
        closeButtonText: keyboardObject.closeButtonText,
        extraKey: keyboardObject.extraKey || '.',
        keyDown: function (text) {
          keyboardObject.keyDown(text)
        },
        keyDelete: function () {
          keyboardObject.keyDelete()
JingChao's avatar
JingChao committed
337 338
          // console.log('delete')
        },
李晓兵's avatar
李晓兵 committed
339 340 341 342 343 344 345 346 347 348 349 350
      })
    }
  },
  /**
   * 弹出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
351
      NotifyPlugin.show({
李晓兵's avatar
李晓兵 committed
352 353 354 355
        show: true,
        content: notifyObject.content,
        position: notifyObject.position || 'top',
        time: notifyObject.time || 3000,
JingChao's avatar
JingChao committed
356
        type: notifyObject.type || 'default',
李晓兵's avatar
李晓兵 committed
357 358
      })
    }
JingChao's avatar
JingChao committed
359
  },
李晓兵's avatar
李晓兵 committed
360
}