hlsPopup.js 10.6 KB
Newer Older
Nature's avatar
Nature committed
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 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
import Vue from 'vue'
import {ToastPlugin, AlertPlugin, ConfirmPlugin, LoadingPlugin, DatetimePlugin} from 'vux'
import ActionSheet from '../../components/ActionSheet/index'
import ShowPicture from '../../components/ShowPicture/index'
import Select from '../../components/select/index'
import Notify from '../../components/Dialog/plugins/index'
import HlsModal from '../../components/Modal/index'

import numberKeyboard from '../../components/NumberKeyboard/index'

Vue.use(ToastPlugin)
Vue.use(AlertPlugin)
Vue.use(ConfirmPlugin)
Vue.use(LoadingPlugin)
Vue.use(DatetimePlugin)
Vue.use(ActionSheet)
Vue.use(ShowPicture)
Vue.use(Select)
Vue.use(numberKeyboard)
Vue.use(Notify)
Vue.use(HlsModal)
Vue.prototype.HlsModal = window.HlsModal = HlsModal

export default {

  isLoading: false,
  SHOW_TIMES: 2000,
  IS_SHOW_MASK: true,
  /**
   * 锁屏函数 超过10s后自动解屏用于防止屏幕锁死
   * 自动截屏成弹出错误提示框
   * @param content 锁屏内容
   */
  showLoading: function (content) {
    let vm = this
    Vue.$vux.loading.show({
      text: content || 'Loading',
    })
    this.isLoading = true
    // 10s后自动解屏用于防止屏幕锁死
    setTimeout(() => {
      if (vm.isLoading) {
        Vue.$vux.loading.hide()
        vm.isLoading = false
        // 弹出操作失败
        Vue.$vux.toast.show({
          text: '操作失败',
          type: 'warn',
          time: vm.SHOW_TIMES,
          isShowMask: vm.IS_SHOW_MASK,
          position: 'middle',
        })
      }
    }, 10000)
  },
  /**
   * 隐藏
   */
  hideLoading: function () {
    Vue.$vux.loading.hide()
    this.isLoading = false
  },

  /**
   * 长时间顶部提示toast
   * @param content
   */
  showLongTop: function (content) {
    let vm = this
    let text = content || '操作失败'
    if (!process.env.isMobilePlatform) {
      Vue.$vux.toast.show({
        text: text,
        type: 'text',
        time: vm.SHOW_TIMES,
        isShowMask: vm.IS_SHOW_MASK,
        position: 'top',
      })
    } else {
      window.plugins.toast.showLongTop(content, function (success) {
      }, function (error) { // eslint-disable-line
      })
    }
  },

  /**
   * 长时间中部提示toast
   * @param content
   */
  showLongCenter: function (content) {
    let vm = this
    let text = content || '操作失败'
    if (!process.env.isMobilePlatform) {
      Vue.$vux.toast.show({
        text: text,
        type: 'text',
        time: vm.SHOW_TIMES,
        isShowMask: vm.IS_SHOW_MASK,
        position: 'middle',
      })
    } else {
      window.plugins.toast.showLongCenter(content, function (success) {
      }, function (error) { // eslint-disable-line
      })
    }
  },
  /**
   * 长时间中部提示toast
   * @param content
   */
  showLongBottom: function (content) {
    let vm = this
    let text = content || '操作失败'
    if (!process.env.isMobilePlatform) {
      Vue.$vux.toast.show({
        text: text,
        time: vm.SHOW_TIMES,
        type: 'text',
        isShowMask: vm.IS_SHOW_MASK,
        position: 'bottom',
      })
    } else {
      window.plugins.toast.showLongBottom(content, function (success) {
      }, function (error) { // eslint-disable-line
      })
    }
  },
  /**
   * 成功提示框
   * @param content
   */
  showSuccess: function (content) {
    let vm = this
    Vue.$vux.toast.show({
      text: content || '操作成功',
      time: vm.SHOW_TIMES,
      isShowMask: vm.IS_SHOW_MASK,
      type: 'success',
      position: 'middle',
    })
  },
  /**
   * 成功提示框
   * @param content
   */
  showError: function (content) {
    let vm = this
    Vue.$vux.toast.show({
      text: content || '操作失败',
      type: 'warn',
      isShowMask: vm.IS_SHOW_MASK,
      time: vm.SHOW_TIMES,
      position: 'middle',
    })
  },
  /**
   * 弹出是否确认的窗口
   * @param confirmObject.title 标题
   * @param confirmObject.content 内容
   * @param confirmObject.onConfirm 确定函数
   */
  showConfirm: function (confirmObject) {
    if (!process.env.isMobilePlatform) {
      let def = {
        title: confirmObject.title || '提示',
        content: confirmObject.content || '',
        confirmText: '确定',
        cancelText: '取消',
        onConfirm: () => {
          confirmObject.onConfirm(1)
        },
        onCancel: () => {
          confirmObject.onConfirm(0)
        },
      }
      Vue.$vux.confirm.show(def)
    } else {
      let message = confirmObject.content || ''
      let onConfirm = function (index) {
        confirmObject.onConfirm(index)
      }
      let title = confirmObject.title
      navigator.notification.confirm(
        message, // message
        function (index) {
          onConfirm(index - 1)
        },
        title, // title
        ['取消', '确定'] // buttonLabels
      )
    }
  },
  /*
   * 弹出确认的窗口
   * @param confirmObject.title 标题
   * @param confirmObject.content 内容
   * @param confirmObject.onConfirm 确定函数
   *
   */
  showPopup: function (confirmObject) {
    if (!process.env.isMobilePlatform) {
      let def = {
        title: confirmObject.title || '提示',
        content: confirmObject.content || '',
        confirmText: '确定',
        showCancelButton: false,
        onConfirm: () => {
          confirmObject.onConfirm()
        },
      }
      Vue.$vux.confirm.show(def)
    } else {
      var alertDismissed = function (index) {
        confirmObject.onConfirm()
      }
      let title = confirmObject.title || '提示'
      var message = confirmObject.content || ''
      navigator.notification.alert(
        message, // message
        alertDismissed, // callback
        title || '提示', // title
        '确定' // buttonName
      )
    }
  },
  /**
   * @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') {
      let buttons = []
      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,
            callback: actionObject.callback,
          })
        } else {
          buttons.push({
            text: actionObject.buttonArray[i],
            callback: actionObject.callback,
          })
        }
      }
      ActionSheet.show({
        title: actionObject.titleText || '',
        buttons: buttons,
      })
    }
  },

  /**
   * 时间选择函数
   * @param timeObject.nowDate 当前展示的时间 可不填
   * @param timeObject.format 时间格式支持不支持秒
   * @param timeObject.callback 点击确定的回调函数
   *
   */
  showTime: function (timeObject) {
    let date = new Date().format('yyyy-MM-dd')
    let format = 'YYYY-MM-DD'
    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,
      onConfirm (val) {
        timeObject.callback(val)
      },
    })
  },
  /**
   * 图片放大预览
   * @param imgObject.imgUrl
   */
  showBigPicture: function (imgObject) {
    if (typeof imgObject === 'object') {
      ShowPicture.show({
        imgUrl: imgObject.imgUrl,
        width: imgObject.width,
      })
    }
  },

  /**
   * 下拉框 支持级联操作 需指定 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') {
      let list = []
      let length = selectOption.list.length
      vum.forEach(selectOption.list, function (date, index, array) {
        list.push({
          value: date.code,
          name: date.code_name,
          parent: date.parent,
        })
        if (index === (length - 1)) {
          Select.show({
            list: list,
            callBack: selectOption.returnItem,
            code: selectOption.code,
            object: selectOption.object,
          })
        }
      })
    }
  },
  /**
   * 弹出数字键盘
   * @param keyboardObject.title 键盘的title
   * @param keyboardObject.closeButtonText 键盘的关闭按钮文字
   * @param keyboardObject.keyDown 普通按键按下去事件
   * @param keyboardObject.keyDelete 删除按键按下去事件
   */
  showNumberKeyborad: function (keyboardObject) {
    if (typeof keyboardObject === 'object') {
      numberKeyboard.show({
        title: keyboardObject.title,
        closeButtonText: keyboardObject.closeButtonText,
        extraKey: keyboardObject.extraKey || '.',
        keyDown: function (text) {
          keyboardObject.keyDown(text)
        },
        keyDelete: function () {
          keyboardObject.keyDelete()
          // console.log('delete')
        },
      })
    }
  },
  /**
   * 弹出Notify
   * @param notifyObject.content 内容
   * @param notifyObject.position 位置
   * @param notifyObject.time 显示时长
   * @param notifyObject.type 类型  success warning default
   */
  showNotify: function (notifyObject) {
    if (typeof notifyObject === 'object') {
      Notify.show({
        show: true,
        content: notifyObject.content,
        position: notifyObject.position || 'top',
        time: notifyObject.time || 3000,
        type: notifyObject.type || 'default',
      })
    }
  },
}