hlsUtil.js 21.4 KB
Newer Older
李晓兵's avatar
李晓兵 committed
1
export default {
JingChao's avatar
JingChao committed
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

  city: {
    11: '北京',
    12: '天津',
    13: '河北',
    14: '山西',
    15: '内蒙古',
    21: '辽宁',
    22: '吉林',
    23: '黑龙江',
    31: '上海',
    32: '江苏',
    33: '浙江',
    34: '安徽',
    35: '福建',
    36: '江西',
    37: '山东',
    41: '河南',
    42: '湖北',
    43: '湖南',
    44: '广东',
    45: '广西',
    46: '海南',
    50: '重庆',
    51: '四川',
    52: '贵州',
    53: '云南',
    54: '西藏',
    61: '陕西',
    62: '甘肃',
    63: '青海',
    64: '宁夏',
    65: '新疆',
    71: '台湾',
    81: '香港',
    82: '澳门',
    91: '国外',
  },

  // 检查号码是否符合规范,包括长度,类型
  isCardNo (card) {
李晓兵's avatar
李晓兵 committed
43
    // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
JingChao's avatar
JingChao committed
44 45 46 47
    card = card.toUpperCase()
    let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/

    return reg.test(card)
李晓兵's avatar
李晓兵 committed
48
  },
JingChao's avatar
JingChao committed
49

李晓兵's avatar
李晓兵 committed
50 51
  // 取身份证前两位,校验省份
  checkProvince: function (card) {
JingChao's avatar
JingChao committed
52 53 54 55 56 57
    card = card.toUpperCase()
    let province = card.substr(0, 2)
    if (this.city[province] === undefined) {
      return false
    }
    return true
李晓兵's avatar
李晓兵 committed
58
  },
JingChao's avatar
JingChao committed
59

李晓兵's avatar
李晓兵 committed
60 61
  // 检查生日是否正确
  checkBirthday: function (card) {
JingChao's avatar
JingChao committed
62 63 64 65 66 67 68
    card = card.toUpperCase()
    let len = card.length
    let arrData
    let year
    let month
    let day
    let birthday
李晓兵's avatar
李晓兵 committed
69
    // 身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字
JingChao's avatar
JingChao committed
70 71 72 73 74 75 76 77
    if (len === 15) {
      let reFifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/
      arrData = card.match(reFifteen)
      year = arrData[2]
      month = arrData[3]
      day = arrData[4]
      birthday = new Date('19' + year + '/' + month + '/' + day)
      return this.verifyBirthday('19' + year, month, day, birthday)
李晓兵's avatar
李晓兵 committed
78 79
    }
    // 身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X
JingChao's avatar
JingChao committed
80 81 82 83 84 85 86 87
    if (len === 18) {
      let reEighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/
      arrData = card.match(reEighteen)
      year = arrData[2]
      month = arrData[3]
      day = arrData[4]
      birthday = new Date(year + '/' + month + '/' + day)
      return this.verifyBirthday(year, month, day, birthday)
李晓兵's avatar
李晓兵 committed
88
    }
JingChao's avatar
JingChao committed
89
    return false
李晓兵's avatar
李晓兵 committed
90
  },
JingChao's avatar
JingChao committed
91

李晓兵's avatar
李晓兵 committed
92 93 94
  // 校验日期
  verifyBirthday: function (year, month, day, birthday) {
    // 年月日是否合理
JingChao's avatar
JingChao committed
95 96
    return (birthday.getFullYear().toString() === year && ((birthday.getMonth() + 1) < 10 ? '0' + (birthday.getMonth() + 1) : (birthday.getMonth() + 1).toString()) === month &&
      ((birthday.getDate()) < 10 ? '0' + (birthday.getDate()).toString() : birthday.getDate().toString()) === day)
李晓兵's avatar
李晓兵 committed
97
  },
JingChao's avatar
JingChao committed
98

李晓兵's avatar
李晓兵 committed
99
  // 校验位的检测
JingChao's avatar
JingChao committed
100
  checkParity (card) {
李晓兵's avatar
李晓兵 committed
101
    // 15位转18位
JingChao's avatar
JingChao committed
102 103 104 105 106 107 108 109
    card = this.changeFivteenToEighteen(card)
    var len = card.length
    if (len === 18) {
      let arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
      let arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2')
      let cardTemp = 0
      for (let i = 0; i < 17; i++) {
        cardTemp += card.substr(i, 1) * arrInt[i]
李晓兵's avatar
李晓兵 committed
110
      }
JingChao's avatar
JingChao committed
111 112 113
      let valnum = arrCh[cardTemp % 11]
      if (valnum === card.substr(17, 1)) {
        return true
李晓兵's avatar
李晓兵 committed
114
      }
JingChao's avatar
JingChao committed
115
      return false
李晓兵's avatar
李晓兵 committed
116
    }
JingChao's avatar
JingChao committed
117
    return false
李晓兵's avatar
李晓兵 committed
118 119
  },

JingChao's avatar
JingChao committed
120 121 122 123 124 125 126 127 128
  // 15位转18位身份证号
  changeFivteenToEighteen (card) {
    if (card.length === 15) {
      let arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
      let arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2')
      let cardTemp = 0
      card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6)
      for (let i = 0; i < 17; i++) {
        cardTemp += card.substr(i, 1) * arrInt[i]
李晓兵's avatar
李晓兵 committed
129
      }
JingChao's avatar
JingChao committed
130 131
      card += arrCh[cardTemp % 11]
      return card
李晓兵's avatar
李晓兵 committed
132
    }
JingChao's avatar
JingChao committed
133
    return card
李晓兵's avatar
李晓兵 committed
134 135 136 137 138 139
  },

  /**
   * 检验身份证号码
   */

JingChao's avatar
JingChao committed
140 141
  isCardID (card) {
    card = card.toUpperCase()
李晓兵's avatar
李晓兵 committed
142
    if (this.isCardNo(card) === false) {
JingChao's avatar
JingChao committed
143
      return '你输入的身份证长度或格式错误'
李晓兵's avatar
李晓兵 committed
144 145 146
    }
    // 检查省份
    if (this.checkProvince(card) === false) {
JingChao's avatar
JingChao committed
147
      return '你的身份证地区非法'
李晓兵's avatar
李晓兵 committed
148 149 150
    }
    // 校验生日
    if (this.checkBirthday(card) === false) {
JingChao's avatar
JingChao committed
151
      return '身份证上的出生日期非法'
李晓兵's avatar
李晓兵 committed
152 153 154
    }
    // 检验位的检测
    if (this.checkParity(card) === false) {
JingChao's avatar
JingChao committed
155
      return '你输入的身份证号非法'
李晓兵's avatar
李晓兵 committed
156
    }
JingChao's avatar
JingChao committed
157
    return ''
李晓兵's avatar
李晓兵 committed
158 159
  },

JingChao's avatar
JingChao committed
160
  // 银行卡号校验
李晓兵's avatar
李晓兵 committed
161 162
  isBankAccount: function (bankno) {
    if (bankno.length < 16 || bankno.length > 19) {
JingChao's avatar
JingChao committed
163 164
      /* 银行卡号长度必须在16到19之间 */
      return '银行卡号长度必须在16到19之间'
李晓兵's avatar
李晓兵 committed
165
    }
JingChao's avatar
JingChao committed
166
    let num = /^\d*$/// 全数字
李晓兵's avatar
李晓兵 committed
167
    if (!num.exec(bankno)) {
JingChao's avatar
JingChao committed
168 169
      /* 银行卡号必须全为数字 */
      return '银行卡号必须全为数字'
李晓兵's avatar
李晓兵 committed
170
    }
JingChao's avatar
JingChao committed
171
    var strBin = '10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99'
李晓兵's avatar
李晓兵 committed
172
    if (strBin.indexOf(bankno.substring(0, 2)) === -1) {
JingChao's avatar
JingChao committed
173 174
      /* 银行卡号开头6位不符合规范 */
      return '银行卡号开头6位不符合规范'
李晓兵's avatar
李晓兵 committed
175
    }
JingChao's avatar
JingChao committed
176
    // Luhn校验
李晓兵's avatar
李晓兵 committed
177
    if (!this.luhnCheck(bankno)) {
JingChao's avatar
JingChao committed
178
      return '银行卡号有误,请从新输入'
李晓兵's avatar
李晓兵 committed
179
    }
JingChao's avatar
JingChao committed
180
    // return true;
李晓兵's avatar
李晓兵 committed
181 182 183
  },

  luhnCheck: function (bankno) {
JingChao's avatar
JingChao committed
184 185 186 187 188 189 190 191 192
    let lastNum = Number(bankno.substr(bankno.length - 1, 1))// 取出最后一位(与luhn进行比较)
    let first15Num = bankno.substr(0, bankno.length - 1)// 前15或18位
    let newArr = []
    for (let i = first15Num.length - 1; i > -1; i--) { // 前15或18位倒序存进数组
      newArr.push(first15Num.substr(i, 1))
    }
    let arrJiShu = [] // 奇数位*2的积 <9
    let arrJiShu2 = [] // 奇数位*2的积 >9
    let arrOuShu = [] // 偶数位数组
李晓兵's avatar
李晓兵 committed
193
    for (let j = 0; j < newArr.length; j++) {
JingChao's avatar
JingChao committed
194 195 196
      if ((j + 1) % 2 === 1) { // 奇数位
        if (parseInt(newArr[j]) * 2 < 9) { arrJiShu.push(parseInt(newArr[j]) * 2) } else { arrJiShu2.push(parseInt(newArr[j]) * 2) }
      } else { arrOuShu.push(newArr[j]) }
李晓兵's avatar
李晓兵 committed
197
    }
JingChao's avatar
JingChao committed
198 199
    let jishuChild1 = []// 奇数位*2 >9 的分割之后的数组个位数
    let jishuChild2 = []// 奇数位*2 >9 的分割之后的数组十位数
李晓兵's avatar
李晓兵 committed
200
    for (let h = 0; h < arrJiShu2.length; h++) {
JingChao's avatar
JingChao committed
201 202
      jishuChild1.push(parseInt(arrJiShu2[h]) % 10)
      jishuChild2.push(parseInt(arrJiShu2[h]) / 10)
李晓兵's avatar
李晓兵 committed
203
    }
JingChao's avatar
JingChao committed
204 205 206 207
    let sumJiShu = 0 // 奇数位*2 < 9 的数组之和
    let sumOuShu = 0 // 偶数位数组之和
    let sumJiShuChild1 = 0 // 奇数位*2 >9 的分割之后的数组个位数之和
    let sumJiShuChild2 = 0 // 奇数位*2 >9 的分割之后的数组十位数之和
李晓兵's avatar
李晓兵 committed
208
    for (let m = 0; m < arrJiShu.length; m++) {
JingChao's avatar
JingChao committed
209
      sumJiShu = sumJiShu + parseInt(arrJiShu[m])
李晓兵's avatar
李晓兵 committed
210 211
    }
    for (let n = 0; n < arrOuShu.length; n++) {
JingChao's avatar
JingChao committed
212 213 214 215 216 217 218 219 220 221 222 223 224
      sumOuShu = sumOuShu + parseInt(arrOuShu[n])
    }
    for (let p = 0; p < jishuChild1.length; p++) {
      sumJiShuChild1 = sumJiShuChild1 + parseInt(jishuChild1[p])
      sumJiShuChild2 = sumJiShuChild2 + parseInt(jishuChild2[p])
    }
    // 计算总和
    let sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2)
    // 计算luhn值
    let k = parseInt(sumTotal) % 10 === 0 ? 10 : parseInt(sumTotal) % 10
    let luhn = 10 - k
    if (lastNum === luhn) {
      return true
李晓兵's avatar
李晓兵 committed
225
    } else {
JingChao's avatar
JingChao committed
226 227
      /* 银行卡号必须符合luhn校验 */
      return false
李晓兵's avatar
李晓兵 committed
228 229 230 231 232 233 234 235 236
    }
  },

  /**
   * 判断输入是否为十一位电话号码
   * @param str 字符串
   * @returns {boolean}
   */
  phoneNumber: function (str) {
JingChao's avatar
JingChao committed
237
    // ^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$
李晓兵's avatar
李晓兵 committed
238

JingChao's avatar
JingChao committed
239 240
    let reg = /^((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|166|198|199|(147))+\d{8}$/
    return reg.test(str)
李晓兵's avatar
李晓兵 committed
241 242 243 244 245 246 247
  },
  /**
   * 判断+86后11位电话号码
   * @param str 字符串
   * @returns {boolean}
   */
  phoneNumber86: function (str) {
JingChao's avatar
JingChao committed
248 249
    let reg = /^(\+86|\+86+\s)+(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|166|198|199|(147))+\d{8})$/
    return reg.test(str)
李晓兵's avatar
李晓兵 committed
250 251 252 253 254 255 256
  },
  /**
   * 是否是邮件格式
   * @param str
   * @returns {boolean|*}
   */
  isEmailAddress: function (str) {
JingChao's avatar
JingChao committed
257 258
    let pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
    return pattern.test(str)
李晓兵's avatar
李晓兵 committed
259 260 261 262 263 264 265
  },
  /**
   *
   * @param cameraoption 对象{quality:质量默认50,allowEdit:true||false,width:宽度,height:高度}
   * @param onSuccess 成功回调函数
   * @param onFail 失败回调函数
   */
JingChao's avatar
JingChao committed
266 267

  /* eslint-disable */
李晓兵's avatar
李晓兵 committed
268
  openCamera: function (cameraoption, onSuccess, onFail) {
JingChao's avatar
JingChao committed
269
    if (typeof onSuccess === 'function' && typeof onFail === 'function') {
李晓兵's avatar
李晓兵 committed
270 271 272 273 274 275 276 277 278 279
      let options = {
        quality: cameraoption.quality || 50,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.CAMERA,
        allowEdit: cameraoption.allowEdit || false,
        targetWidth: cameraoption.width || 1024,
        targetHeight: cameraoption.height || 768,
        encodingType: Camera.EncodingType.JPEG,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: window.localStorage.savePhoto || false,
JingChao's avatar
JingChao committed
280 281
        correctOrientation: true,
      }
李晓兵's avatar
李晓兵 committed
282
      navigator.camera.getPicture(onSuccess, onFail, options)
李晓兵's avatar
李晓兵 committed
283
    } else {
JingChao's avatar
JingChao committed
284
      window.hlsPopup.showLongCenter('参数有误!')
李晓兵's avatar
李晓兵 committed
285 286 287 288 289 290 291 292 293 294
    }
  },

  /**
   * 最多选取10张,返回图片的存储地址数组形式
   * @param obj {quality:质量默认50,width:宽度,height:高度}
   * @param successFunction 成功回调函数
   * @param errorFunction 失败函数
   */
  takePicture: function (obj, successFunction, errorFunction) {
JingChao's avatar
JingChao committed
295
    if (typeof successFunction === 'function' && typeof errorFunction === 'function') {
李晓兵's avatar
李晓兵 committed
296 297 298 299 300
      window.imagePicker.getPictures(
        successFunction, errorFunction, {
          maximumImagesCount: obj.maxCount || 10,
          quality: obj.quality || 50,
          width: obj.width || 1024,
JingChao's avatar
JingChao committed
301
          height: obj.height || 768,
李晓兵's avatar
李晓兵 committed
302 303
        }
      )
JingChao's avatar
JingChao committed
304 305
    } else {
      window.hlsPopup.showLongBottom('参数有误!')
李晓兵's avatar
李晓兵 committed
306 307 308 309 310 311 312 313
    }
  },

  /**
   * 拨打电话仅仅限制于手机
   * @param number
   */
  callPhone: function (number) {
JingChao's avatar
JingChao committed
314
    window.open('tel:' + number)
李晓兵's avatar
李晓兵 committed
315 316 317 318 319 320
  },
  /**
   * 发邮件
   * @param email
   */
  callEmail: function (email) {
JingChao's avatar
JingChao committed
321
    window.open('mailto:' + email)
李晓兵's avatar
李晓兵 committed
322 323 324 325 326 327 328
  },
  /**
   * 非ftp的上传方式
   * @param filePath
   * @param success
   */
  fileUploadHls: function (file, success) {
JingChao's avatar
JingChao committed
329 330 331 332 333
    let path = file.filePath
    let name = path.substr(path.lastIndexOf('/') + 1)
    let url = encodeURI(process.env.rootPath + '/app/fileUploadHls?sysName=HLS_APP&apiName=attment_file_upload')
    let options = new FileUploadOptions() // eslint-disable-line
    options.fileKey = 'file'
李晓兵's avatar
李晓兵 committed
334
    options.headers = {
JingChao's avatar
JingChao committed
335
      'Authorization': 'Bearer ' + window.localStorage.access_token,
李晓兵's avatar
李晓兵 committed
336 337
    }
    options.params = {
JingChao's avatar
JingChao committed
338 339 340 341 342 343 344 345 346
      'table_name': file.table_name,
      'table_pk_value': file.table_pk_value,
      'user_id': window.localStorage.user_id,
      'access_token': window.localStorage.access_token,
      'filePath': path,
    }
    options.fileName = name
    options.mimeType = 'multipart/form-date'
    let ft = new FileTransfer() // eslint-disable-line
李晓兵's avatar
李晓兵 committed
347 348
    ft.onprogress = function (progressEvent) {
      if (progressEvent.lengthComputable) {
JingChao's avatar
JingChao committed
349
        loadingStatus.setPercentage(progressEvent.loaded / progressEvent.total) // eslint-disable-line
李晓兵's avatar
李晓兵 committed
350
      } else {
JingChao's avatar
JingChao committed
351
        loadingStatus.increment() // eslint-disable-line
李晓兵's avatar
李晓兵 committed
352 353 354
      }
    }

JingChao's avatar
JingChao committed
355 356 357 358 359 360 361 362
    function uploadSuccess (result) {
      success(JSON.parse(result.response))
    }

    function fileError () {
      window.hlsPopup.hideLoading()
      console.log('upload error source ' + error.source)
      console.log('upload error target ' + error.target)
李晓兵's avatar
李晓兵 committed
363 364
    }

JingChao's avatar
JingChao committed
365
    ft.upload(path, url, uploadSuccess, fileError, options)
李晓兵's avatar
李晓兵 committed
366 367 368 369 370 371 372
  },
  /**
   * 调用系统svc进行上传
   * @param filePath
   * @param success
   */
  fileUploadSvc: function (file, success) {
李晓兵's avatar
李晓兵 committed
373
    debugger
李晓兵's avatar
李晓兵 committed
374
    let path = file.filePath
李晓兵's avatar
李晓兵 committed
375
    var name = path.substr(path.lastIndexOf('/') + 1)
李晓兵's avatar
李晓兵 committed
376
    let url = encodeURI(process.env.rootPath + '/app/fileUploadSvc?sysName=XCMG_DEV&apiName=attachment_upload')
JingChao's avatar
JingChao committed
377 378
    let options = new FileUploadOptions() // eslint-disable-line
    options.fileKey = 'file'
李晓兵's avatar
李晓兵 committed
379
    options.headers = {
JingChao's avatar
JingChao committed
380
      'Authorization': 'Bearer ' + window.localStorage.access_token,
李晓兵's avatar
李晓兵 committed
381 382
    }
    options.params = {
李晓兵's avatar
李晓兵 committed
383 384 385 386
      'source_type': file.source_type,
      'pkvalue': file.pkvalue,
      'user_id': file.user_id,
      'check_id': file.check_id,
JingChao's avatar
JingChao committed
387 388 389
      'access_token': window.localStorage.access_token,
      'filePath': path,
    }
李晓兵's avatar
李晓兵 committed
390
    if(file.fileName){
李晓兵's avatar
李晓兵 committed
391
      options.fileName = file.fileName + name.substr(name.lastIndexOf('.'))
李晓兵's avatar
李晓兵 committed
392 393 394 395
    }
    else{
      options.fileName = name
    }
JingChao's avatar
JingChao committed
396 397 398 399 400 401
    options.mimeType = 'multipart/form-date'
    let ft = new FileTransfer() // eslint-disable-line

    function uploadSuccess (result) {
      success(JSON.parse(result.response))
    }
李晓兵's avatar
李晓兵 committed
402

JingChao's avatar
JingChao committed
403 404 405 406
    function fileError () {
      window.hlsPopup.hideLoading()
      console.log('upload error source ' + error.source)
      console.log('upload error target ' + error.target)
李晓兵's avatar
李晓兵 committed
407 408
    }

JingChao's avatar
JingChao committed
409
    ft.upload(path, url, uploadSuccess, fileError, options)
李晓兵's avatar
李晓兵 committed
410 411 412 413 414 415 416 417 418
  },
  /**
   * 汉王识别
   * @param file
   * @param url
   * @param success
   */
  hangwan: function (fileUrl, postUrl, success) {
    if (!fileUrl) {
JingChao's avatar
JingChao committed
419
      return
李晓兵's avatar
李晓兵 committed
420
    }
JingChao's avatar
JingChao committed
421 422 423 424
    let path = fileUrl
    let name = fileUrl.substr(fileUrl.lastIndexOf('/') + 1)
    let url = encodeURI(postUrl)
    let options = new FileUploadOptions() // eslint-disable-line
李晓兵's avatar
李晓兵 committed
425
    options.headers = {
JingChao's avatar
JingChao committed
426
      'Authorization': 'Bearer ' + window.localStorage.access_token,
李晓兵's avatar
李晓兵 committed
427
    }
JingChao's avatar
JingChao committed
428 429 430 431
    options.fileKey = 'file'
    options.fileName = name
    options.mimeType = 'multipart/form-date'
    let ft = new FileTransfer() // eslint-disable-line
李晓兵's avatar
李晓兵 committed
432

JingChao's avatar
JingChao committed
433 434 435
    function uploadSuccess (message) {
      let res = JSON.parse(message.response)
      success(res)
李晓兵's avatar
李晓兵 committed
436 437
    }

JingChao's avatar
JingChao committed
438 439
    function error (e) {
      this.hlsPopup.showLongCenter('汉王识别失败')
李晓兵's avatar
李晓兵 committed
440 441
    }

JingChao's avatar
JingChao committed
442
    ft.upload(path, url, uploadSuccess, error, options)
李晓兵's avatar
李晓兵 committed
443
  },
李晓兵's avatar
李晓兵 committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
  /**
   * 百度OCR识别
   * @param file
   * @param url
   * @param success
   */
  baiduOcr: function (fileUrl, postUrl, success) {
    if (!fileUrl) {
      return
    }
    let path = fileUrl
    let name = fileUrl.substr(fileUrl.lastIndexOf('/') + 1)
    let url = encodeURI(postUrl)
    let options = new FileUploadOptions() // eslint-disable-line
    options.headers = {
      'Authorization': 'Bearer ' + window.localStorage.access_token,
    }
    options.fileKey = 'file'
    options.fileName = name
    options.mimeType = 'multipart/form-date'
    let ft = new FileTransfer() // eslint-disable-line

    function uploadSuccess (message) {
      let res = JSON.parse(message.response)
      success(res)
    }

    function error (e) {
      this.hlsPopup.showLongCenter('识别失败')
    }

    ft.upload(path, url, uploadSuccess, error, options)
  },
李晓兵's avatar
李晓兵 committed
477 478 479 480 481 482
  /**
   *
   * @param x 输入的数字
   * @returns {Number} 返回数字
   */
  toDecimal: function (x) {
JingChao's avatar
JingChao committed
483 484
    // hlsPopup.showLongCenter(baseConfig.debug);
    let f = parseFloat(x)
李晓兵's avatar
李晓兵 committed
485
    if (isNaN(f)) {
JingChao's avatar
JingChao committed
486
      return 0
李晓兵's avatar
李晓兵 committed
487
    }
JingChao's avatar
JingChao committed
488 489
    f = Math.round(x * 100) / 100
    return f
李晓兵's avatar
李晓兵 committed
490 491 492
  },

  formatFloat: function (f, digit) {
JingChao's avatar
JingChao committed
493 494
    let m = Math.pow(100000, digit)
    return parseInt(f * m, 100000) / m
李晓兵's avatar
李晓兵 committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
  },

  /**
   *
   * @param ir  interest rate per month
   * @param np  number of periods (months)
   * @param pv  present value
   * @param fv  future value (residual value)
   * @returns {number} 计算结果;
   * @constructor
   */
  PMT: function (ir, np, pv, fv, type) {
    /*
     ir - interest rate per month
     np - number of periods (months)
     pv - present value
     fv - future value (residual value)
     type - 0 or 1 need to implement that
     */
    if (!type) {
JingChao's avatar
JingChao committed
515
      type = 0
李晓兵's avatar
李晓兵 committed
516
    }
JingChao's avatar
JingChao committed
517 518
    if (ir === 0) {
      return -pv / np
李晓兵's avatar
李晓兵 committed
519
    }
JingChao's avatar
JingChao committed
520
    let r1 = 1 + ir
李晓兵's avatar
李晓兵 committed
521

JingChao's avatar
JingChao committed
522 523 524
    // let pmt = -( ir * ( pv * Math.pow((ir + 1), np) + fv ) ) / ( ( ir + 1 ) * ( Math.pow((ir + 1), np) - 1 ) );
    let pmt = -(pv * Math.pow(r1, np) + fv) * ir / ((1 + ir * type) * (Math.pow(r1, np) - 1))
    return this.toDecimal(pmt)
李晓兵's avatar
李晓兵 committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538
  },

  /**
   *
   * @param rate
   * @param per
   * @param nper
   * @param pv
   * @param fv
   * @param type
   * @returns {*}
   * @constructor
   */
  IPMT: function (rate, per, nper, pv, fv, type) {
JingChao's avatar
JingChao committed
539 540 541 542 543 544 545 546
    let ipmt = 0
    let r = rate
    let R1 = 1 + r
    let t = per
    let n = nper
    let ct
    if (rate === 0) {
      return 0
李晓兵's avatar
李晓兵 committed
547 548
    }
    if (fv) {
JingChao's avatar
JingChao committed
549
      fv = -fv
李晓兵's avatar
李晓兵 committed
550
    } else {
JingChao's avatar
JingChao committed
551
      fv = 0
李晓兵's avatar
李晓兵 committed
552 553
    }
    if (type) {
JingChao's avatar
JingChao committed
554
      ct = type
李晓兵's avatar
李晓兵 committed
555
    } else {
JingChao's avatar
JingChao committed
556
      ct = 0
李晓兵's avatar
李晓兵 committed
557
    }
JingChao's avatar
JingChao committed
558 559 560
    if (type === 1 && per === 1) {
      return ipmt
    } else {
李晓兵's avatar
李晓兵 committed
561 562 563 564
      ipmt = -(pv * Math.pow(R1, n) + fv) * r /
        ((1 + r * ct) * (Math.pow(R1, n) - 1)) -
        (pv / (1 + r * ct) - (pv * Math.pow(R1, n) + fv) /
          ((1 + r * ct) * (Math.pow(R1, n) - 1))) *
JingChao's avatar
JingChao committed
565 566
        (Math.pow(R1, t) - Math.pow(R1, (t - 1)))
      return this.toDecimal(ipmt)
李晓兵's avatar
李晓兵 committed
567 568 569 570 571 572 573 574 575 576 577 578 579
    }
  },

  /**
   *
   * @param p_rate
   * @param p_nper
   * @param p_pmt
   * @param p_fv
   * @param p_type
   * @returns {number|*}
   * @constructor
   */
JingChao's avatar
JingChao committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593
  PV: function (pRate, pNper, pPmt, pFv, pType) {
    let pv
    let r
    let R1
    let n
    let pmt
    let fv
    let ct

    r = pRate
    R1 = 1 + r
    n = pNper
    if (pPmt) {
      pmt = pPmt
李晓兵's avatar
李晓兵 committed
594 595 596
    } else {
      pmt = 0
    }
JingChao's avatar
JingChao committed
597 598
    if (pFv) {
      fv = pFv
李晓兵's avatar
李晓兵 committed
599
    } else {
JingChao's avatar
JingChao committed
600
      fv = 0
李晓兵's avatar
李晓兵 committed
601
    }
JingChao's avatar
JingChao committed
602 603
    if (pType) {
      ct = pType
李晓兵's avatar
李晓兵 committed
604
    } else {
JingChao's avatar
JingChao committed
605
      ct = 0
李晓兵's avatar
李晓兵 committed
606 607
    }
    pv = -(fv + pmt * (1 + r * ct) * (Math.pow(R1, n) - 1) / r) /
JingChao's avatar
JingChao committed
608 609
      Math.pow(R1, n)
    return this.toDecimal(pv)
李晓兵's avatar
李晓兵 committed
610 611 612 613 614 615 616 617 618 619 620 621
  },

  /**
   *
   * @param p_rate
   * @param p_nper
   * @param p_pmt
   * @param p_pv
   * @param p_type
   * @returns {number|*}
   * @constructor
   */
JingChao's avatar
JingChao committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635
  FV: function (pRate, pNper, pPmt, pPv, pType) {
    let fv
    let r
    let R1
    let n
    let pmt
    let pv
    let ct

    r = pRate
    R1 = 1 + r
    n = pNper
    if (pPmt) {
      pmt = pPmt
李晓兵's avatar
李晓兵 committed
636
    } else {
JingChao's avatar
JingChao committed
637
      pmt = 0
李晓兵's avatar
李晓兵 committed
638 639
    }

JingChao's avatar
JingChao committed
640 641
    if (pPv) {
      pv = pPv
李晓兵's avatar
李晓兵 committed
642
    } else {
JingChao's avatar
JingChao committed
643
      pv = 0
李晓兵's avatar
李晓兵 committed
644
    }
JingChao's avatar
JingChao committed
645 646
    if (pType) {
      ct = pType
李晓兵's avatar
李晓兵 committed
647
    } else {
JingChao's avatar
JingChao committed
648
      ct = 0
李晓兵's avatar
李晓兵 committed
649
    }
JingChao's avatar
JingChao committed
650 651
    fv = -pv * Math.pow(R1, n) - pmt * (1 + r * ct) * (Math.pow(R1, n) - 1) / r
    return this.toDecimal(fv)
李晓兵's avatar
李晓兵 committed
652 653 654 655 656 657 658 659 660 661
  },

  /**
   *
   * @param args
   * @param rate
   * @returns {*}
   * @constructor
   */
  NPV: function (args, rate) {
JingChao's avatar
JingChao committed
662 663
    let rrate = (1 + rate / 100)
    let npv = args[0]
李晓兵's avatar
李晓兵 committed
664
    for (let i = 1; i < args.length; i++) {
JingChao's avatar
JingChao committed
665
      npv += (args[i] / Math.pow(rrate, i))
李晓兵's avatar
李晓兵 committed
666
    }
JingChao's avatar
JingChao committed
667
    return npv
李晓兵's avatar
李晓兵 committed
668 669 670 671 672 673 674 675
  },

  /**
   *
   * @param fn
   * @returns {number}
   */
  seekZero: function (fn) {
JingChao's avatar
JingChao committed
676
    let x = 1
李晓兵's avatar
李晓兵 committed
677
    while (fn(x) > 0) {
JingChao's avatar
JingChao committed
678
      x += 1
李晓兵's avatar
李晓兵 committed
679 680
    }
    while (fn(x) < 0) {
JingChao's avatar
JingChao committed
681
      x -= 0.01
李晓兵's avatar
李晓兵 committed
682
    }
JingChao's avatar
JingChao committed
683
    return x + 0.01
李晓兵's avatar
李晓兵 committed
684 685 686 687 688 689 690 691 692
  },

  /**
   *
   * @param array
   * @returns {number}
   * @constructor
   */
  IRR: function (array) {
JingChao's avatar
JingChao committed
693 694
    let args = array
    let numberOfTries = 1
李晓兵's avatar
李晓兵 committed
695
    // Cash flow values must contain at least one positive value and one negative value
JingChao's avatar
JingChao committed
696
    let positive, negative
李晓兵's avatar
李晓兵 committed
697
    Array.prototype.slice.call(args).forEach(function (value) {
JingChao's avatar
JingChao committed
698 699
      if (value > 0) positive = true
      if (value < 0) negative = true
李晓兵's avatar
李晓兵 committed
700
    })
JingChao's avatar
JingChao committed
701
    if (!positive || !negative) throw new Error('IRR requires at least one positive value and one negative value')
李晓兵's avatar
李晓兵 committed
702

JingChao's avatar
JingChao committed
703 704
    function npv (rate) {
      numberOfTries++
李晓兵's avatar
李晓兵 committed
705
      if (numberOfTries > 1000) {
JingChao's avatar
JingChao committed
706
        throw new Error('IRR can\'t find a result')
李晓兵's avatar
李晓兵 committed
707
      }
JingChao's avatar
JingChao committed
708 709
      let rrate = (1 + rate / 100)
      let npv = args[0]
李晓兵's avatar
李晓兵 committed
710
      for (let i = 1; i < args.length; i++) {
JingChao's avatar
JingChao committed
711
        npv += (args[i] / Math.pow(rrate, i))
李晓兵's avatar
李晓兵 committed
712
      }
JingChao's avatar
JingChao committed
713
      return npv
李晓兵's avatar
李晓兵 committed
714 715
    }

JingChao's avatar
JingChao committed
716
    return this.seekZero(npv)
李晓兵's avatar
李晓兵 committed
717 718
  },

JingChao's avatar
JingChao committed
719
  // Returns Sum of f(x)/f'(x)
李晓兵's avatar
李晓兵 committed
720
  sumEq: function (cfs, durs, guess) {
JingChao's avatar
JingChao committed
721 722
    let sumFx = 0
    let sumFdx = 0
李晓兵's avatar
李晓兵 committed
723
    for (let i = 0; i < cfs.length; i++) {
JingChao's avatar
JingChao committed
724
      sumFx = sumFx + (cfs[i] / Math.pow(1 + guess, durs[i]))
李晓兵's avatar
李晓兵 committed
725
    }
JingChao's avatar
JingChao committed
726 727
    for (let i = 0; i < cfs.length; i++) {
      sumFdx = sumFdx + (-cfs[i] * durs[i] * Math.pow(1 + guess, -1 - durs[i]))
李晓兵's avatar
李晓兵 committed
728
    }
JingChao's avatar
JingChao committed
729
    return sumFx / sumFdx
李晓兵's avatar
李晓兵 committed
730 731
  },
  durYear: function (first, last) {
JingChao's avatar
JingChao committed
732
    return (Math.abs(last.getTime() - first.getTime()) / (1000 * 3600 * 24 * 365))
李晓兵's avatar
李晓兵 committed
733 734 735 736 737 738 739 740 741 742 743
  },

  /**
   *
   * @param cfs
   * @param dts
   * @param guess
   * @returns {number}
   * @constructor
   */
  XIRR: function (cfs, dts, guess) {
JingChao's avatar
JingChao committed
744
    if (cfs.length !== dts.length) throw new Error('Number of cash flows and dates should match')
李晓兵's avatar
李晓兵 committed
745

JingChao's avatar
JingChao committed
746
    let positive, negative
李晓兵's avatar
李晓兵 committed
747
    Array.prototype.slice.call(cfs).forEach(function (value) {
JingChao's avatar
JingChao committed
748 749 750 751 752 753 754 755 756 757
      if (value > 0) positive = true
      if (value < 0) negative = true
    })
    if (!positive || !negative) throw new Error('XIRR requires at least one positive value and one negative value')
    guess = guess || 0
    let limit = 100 // loop limit
    let guessLast
    let durs = []
    durs.push(0)
    // Create Array of durations from First date
李晓兵's avatar
李晓兵 committed
758
    for (let i = 1; i < dts.length; i++) {
JingChao's avatar
JingChao committed
759
      durs.push(this.durYear(dts[0], dts[i]))
李晓兵's avatar
李晓兵 committed
760 761
    }
    do {
JingChao's avatar
JingChao committed
762 763 764 765 766 767
      guessLast = guess
      guess = guessLast - this.sumEq(cfs, durs, guessLast)
      limit--
    } while (guessLast.toFixed(5) !== guess.toFixed(5) && limit > 0)
    let xirr = guessLast.toFixed(5) !== guess.toFixed(5) ? null : guess * 100
    return Math.round(xirr * 100) / 100
李晓兵's avatar
李晓兵 committed
768 769
  },

JingChao's avatar
JingChao committed
770
  // 判断是否为首次登录
李晓兵's avatar
李晓兵 committed
771 772 773
  isFirstTimeLogin: function (name) {
    if (window.localStorage.username) {
      if (window.localStorage.username !== name) {
JingChao's avatar
JingChao committed
774
        return true
李晓兵's avatar
李晓兵 committed
775
      } else {
JingChao's avatar
JingChao committed
776
        return false
李晓兵's avatar
李晓兵 committed
777 778
      }
    } else {
JingChao's avatar
JingChao committed
779 780 781
      return true
    }
  },
李晓兵's avatar
李晓兵 committed
782

JingChao's avatar
JingChao committed
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
  /**
   * 判断平台
   * @return {String} 平台
   */
  detectOS: function () {
    const ua = navigator.userAgent.toLowerCase()

    if (/MicroMessenger/i.test(ua)) {
      return 'weixin'
    } else if (/iPhone|iPad|iPod|iOS/i.test(ua)) {
      return 'ios'
    } else if (/Android/i.test(ua)) {
      return 'android'
    } else {
      return 'other'
    }
  },
李晓兵's avatar
李晓兵 committed
800 801

}