重构,把原来的 index.js 单独放到 unitool/ 里
This commit is contained in:
		
							parent
							
								
									547d0f7183
								
							
						
					
					
						commit
						7bec07d04b
					
				
							
								
								
									
										1
									
								
								iconfont.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								iconfont.sh
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					sed -e "s/src: url('iconfont.*');.*$//" -e "s/url('iconfont.*').*$//g" -e "s/format('woff2'),/format('woff2');/" ./iconfont.css > App.iconfont.css
 | 
				
			||||||
							
								
								
									
										262
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										262
									
								
								index.js
									
									
									
									
									
								
							@ -1,261 +1,7 @@
 | 
				
			|||||||
// #ifdef H5
 | 
					const unitool = require('./unitool/')
 | 
				
			||||||
// import device from 'current-device' // https://github.com/matthewhudson/current-device
 | 
					const unisocket = require('./unisocket/')
 | 
				
			||||||
// #endif
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
  clog(...message) {
 | 
					  unitool,
 | 
				
			||||||
    console.log(
 | 
					  unisocket,
 | 
				
			||||||
      '【【【【【【【【【【',
 | 
					 | 
				
			||||||
      getCurrentPages().length > 0 ? getCurrentPages().pop().route : 'pages/Welcome', // 在首页时,getApp() 或 getCurrentPages() 有可能获取不到。
 | 
					 | 
				
			||||||
      ...message,
 | 
					 | 
				
			||||||
      '】】】】】】】】】】】'
 | 
					 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)),
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  async request({ method = 'POST', url, header = {}, data = {} }) {
 | 
					 | 
				
			||||||
    url = this.makeUrl(url)
 | 
					 | 
				
			||||||
    if (uni.getStorageSync('_passtoken')) {
 | 
					 | 
				
			||||||
      header._passtoken = uni.getStorageSync('_passtoken')
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    if (method === 'GET') {
 | 
					 | 
				
			||||||
      // 如果不是 POST 方法,要额外把参数JSON化
 | 
					 | 
				
			||||||
      for (let key in data) {
 | 
					 | 
				
			||||||
        data[key] = JSON.stringify(data[key])
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    console.log('👇 👇 👇 👇 < Request > 👇 👇 👇 👇 ', { method, url, header, data }, '👆 👆 👆 👆 < /Request > 👆 👆 👆 👆')
 | 
					 | 
				
			||||||
    let [error, response] = await uni.request({ method, url, header, data })
 | 
					 | 
				
			||||||
    console.log('⬇️ ⬇️ ⬇️ ⬇️ < Response > ⬇️ ⬇️ ⬇️ ⬇️ ', response, '⬆️ ⬆️ ⬆️ ⬆️ < /Response > ⬆️ ⬆️ ⬆️ ⬆️')
 | 
					 | 
				
			||||||
    return [error, response]
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  async pickupFile({
 | 
					 | 
				
			||||||
    mediaType = 'image',
 | 
					 | 
				
			||||||
    count = 1,
 | 
					 | 
				
			||||||
    sizeType = ['original', 'compressed'],
 | 
					 | 
				
			||||||
    sourceType = ['album', 'camera'],
 | 
					 | 
				
			||||||
    url,
 | 
					 | 
				
			||||||
    header = {},
 | 
					 | 
				
			||||||
    formData = {},
 | 
					 | 
				
			||||||
    name = 'file',
 | 
					 | 
				
			||||||
  } = {}) {
 | 
					 | 
				
			||||||
    let filePath
 | 
					 | 
				
			||||||
    if (mediaType === 'image') {
 | 
					 | 
				
			||||||
      let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType })
 | 
					 | 
				
			||||||
      filePath = tempFilePaths[0]
 | 
					 | 
				
			||||||
    } else if (mediaType === 'video') {
 | 
					 | 
				
			||||||
      let [errorChoose, { tempFilePath }] = await uni.chooseVideo({ sourceType })
 | 
					 | 
				
			||||||
      filePath = tempFilePath
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      return [{ _ERROR: 'UNKNOWN_MEDIATYPE' }, null]
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (filePath) {
 | 
					 | 
				
			||||||
      if (uni.getStorageSync('_passtoken')) {
 | 
					 | 
				
			||||||
        header._passtoken = uni.getStorageSync('_passtoken')
 | 
					 | 
				
			||||||
      } else {
 | 
					 | 
				
			||||||
        return [{ _ERROR: 'USER_OFFLINE', errMsg: 'offline user cannot upload files' }, null]
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      for (let key in formData) {
 | 
					 | 
				
			||||||
        // multer 不会自动处理 JSON 数据,必须前后端配合处理
 | 
					 | 
				
			||||||
        formData[key] = JSON.stringify(formData[key])
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      uni.showLoading()
 | 
					 | 
				
			||||||
      let [errorUpload, response] = await uni.uploadFile({ filePath: filePath, url: this.makeUrl(url), header, formData, name })
 | 
					 | 
				
			||||||
      uni.hideLoading()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (response && response.data) {
 | 
					 | 
				
			||||||
        try {
 | 
					 | 
				
			||||||
          response.data = JSON.parse(response.data)
 | 
					 | 
				
			||||||
        } catch (exception) {}
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      return [errorUpload, response]
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return [{ _ERROR: 'USER_CANCELED' }, null]
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  async uploadFile({ url, name = 'file', formData = {}, header = {} } = {}) {
 | 
					 | 
				
			||||||
    url = this.makeUrl(url)
 | 
					 | 
				
			||||||
    if (uni.getStorageSync('_passtoken')) {
 | 
					 | 
				
			||||||
      header._passtoken = uni.getStorageSync('_passtoken')
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      return [{ errMsg: 'offline user cannot upload files' }, null]
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (let key in formData) {
 | 
					 | 
				
			||||||
      // multer 不会自动处理 JSON 数据,必须前后端配合处理
 | 
					 | 
				
			||||||
      formData[key] = JSON.stringify(formData[key])
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let [error, response] = await uni.uploadFile({ url, name, formData, header })
 | 
					 | 
				
			||||||
    if (response && response.data) {
 | 
					 | 
				
			||||||
      try {
 | 
					 | 
				
			||||||
        response.data = JSON.parse(response.data)
 | 
					 | 
				
			||||||
      } catch (exception) {}
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return [error, response]
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  async pickupFile2Cloud({ mediaType = 'image', count = 1, sizeType = ['original', 'compressed'], sourceType = ['album', 'camera'] } = {}) {
 | 
					 | 
				
			||||||
    let filePath, cloudPath
 | 
					 | 
				
			||||||
    if (mediaType === 'image') {
 | 
					 | 
				
			||||||
      let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType })
 | 
					 | 
				
			||||||
      filePath = tempFilePaths[0]
 | 
					 | 
				
			||||||
      cloudPath = 'cloud.jpg'
 | 
					 | 
				
			||||||
      // #ifdef H5
 | 
					 | 
				
			||||||
      cloudPath = tempFiles[0].name
 | 
					 | 
				
			||||||
      // #endif
 | 
					 | 
				
			||||||
    } else if (mediaType === 'video') {
 | 
					 | 
				
			||||||
      let [errorChoose, { tempFilePath, tempFile, name }] = await uni.chooseVideo({ sourceType })
 | 
					 | 
				
			||||||
      filePath = tempFilePath
 | 
					 | 
				
			||||||
      cloudPath = 'cloud.mp4'
 | 
					 | 
				
			||||||
      // #ifdef H5
 | 
					 | 
				
			||||||
      cloudPath = name
 | 
					 | 
				
			||||||
      // #endif
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      return { _state: 'UNKNOWN_MEDIA_TYPE' }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (filePath) {
 | 
					 | 
				
			||||||
      uni.showLoading()
 | 
					 | 
				
			||||||
      const { fileID, requestId } = await uniCloud.uploadFile({
 | 
					 | 
				
			||||||
        filePath: filePath,
 | 
					 | 
				
			||||||
        cloudPath: cloudPath,
 | 
					 | 
				
			||||||
        fileType: mediaType,
 | 
					 | 
				
			||||||
        onUploadProgress: function (progressEvent) {
 | 
					 | 
				
			||||||
          console.log(progressEvent)
 | 
					 | 
				
			||||||
          var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
      })
 | 
					 | 
				
			||||||
      uni.hideLoading()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      console.log('文件上传结果:', { fileID, requestId })
 | 
					 | 
				
			||||||
      if (fileID) {
 | 
					 | 
				
			||||||
        return { _state: 'SUCCESS', fileID, requestId }
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return { _state: 'FAIL' }
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  openUrl(url) {
 | 
					 | 
				
			||||||
    // #ifdef APP-PLUS
 | 
					 | 
				
			||||||
    plus.runtime.openURL(url)
 | 
					 | 
				
			||||||
    // #endif
 | 
					 | 
				
			||||||
    // #ifdef H5
 | 
					 | 
				
			||||||
    window.open(url, '_blank')
 | 
					 | 
				
			||||||
    // #endif
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  getSystemInfo() {
 | 
					 | 
				
			||||||
    let systemInfo = uni.getSystemInfoSync()
 | 
					 | 
				
			||||||
    // #ifdef H5
 | 
					 | 
				
			||||||
    systemInfo.runtime = 'h5'
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // if (device.mobile()){
 | 
					 | 
				
			||||||
    //   systemInfo.platform = 'mobile'
 | 
					 | 
				
			||||||
    // }else if (device.desktop()){
 | 
					 | 
				
			||||||
    //   systemInfo.platform = 'desktop'
 | 
					 | 
				
			||||||
    // }else if (device.tablet()){
 | 
					 | 
				
			||||||
    //   systemInfo.platform = 'tablet'
 | 
					 | 
				
			||||||
    // }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (/MicroMessenger/.test(window.navigator.userAgent)) {
 | 
					 | 
				
			||||||
      // 微信内置浏览器
 | 
					 | 
				
			||||||
      systemInfo.browser = 'wechat'
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // #endif
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // #ifdef APP-PLUS || APP-PLUS-NVUE
 | 
					 | 
				
			||||||
    systemInfo.runtime = 'app'
 | 
					 | 
				
			||||||
    // 细分成 systemInfo.platform === ios or android
 | 
					 | 
				
			||||||
    // #endif
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // #ifdef MP
 | 
					 | 
				
			||||||
    systemInfo.runtime = 'mp'
 | 
					 | 
				
			||||||
    // 细分成 WEIXIN, ...
 | 
					 | 
				
			||||||
    // #endif
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return systemInfo
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  showToast({ type, icon, image, title, duration, ...rest }) {
 | 
					 | 
				
			||||||
    let pageNow = this.$store ? this : getCurrentPages().pop()
 | 
					 | 
				
			||||||
    if (pageNow.$refs && pageNow.$refs.toast) {
 | 
					 | 
				
			||||||
      // 在 ios app 里,虽然能获得 pageNow,但是不存在 pageNow.$refs,不知为何。android app 没有测试
 | 
					 | 
				
			||||||
      pageNow.$refs.toast.open({ type, content: title, duration, ...rest })
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      // #ifdef APP-PLUS
 | 
					 | 
				
			||||||
      if (uni.getSystemInfoSync().platform === 'android') {
 | 
					 | 
				
			||||||
        uni.showToast({ icon: 'none', title, duration, ...rest })
 | 
					 | 
				
			||||||
        return
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      // #endif
 | 
					 | 
				
			||||||
      if (!image) {
 | 
					 | 
				
			||||||
        image = `../static/Common.${type ? type : 'info'}.png`
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      uni.showToast({ icon, image, title, duration, ...rest })
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  setBarTitles({ windowTitle, pageTitle } = {}) {
 | 
					 | 
				
			||||||
    let page = this.$store ? this : getCurrentPages().pop()
 | 
					 | 
				
			||||||
    uni.setNavigationBarTitle({ title: pageTitle || page.i18nText[page.$store.state.i18n.activeLang].tPageTitle })
 | 
					 | 
				
			||||||
    // #ifdef H5
 | 
					 | 
				
			||||||
    document.title = windowTitle || page.$store.getters['i18n/getAppName'] || page.appName // 必须放在 setNavigationBarTitle 之后,否则会被其覆盖掉。
 | 
					 | 
				
			||||||
    // #endif
 | 
					 | 
				
			||||||
    if (page.$store._mutations['i18n/setTabbar']) page.$store.commit('i18n/setTabbar') // 必须要在有 tab 的页面里重置才有效果
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  localeText() {
 | 
					 | 
				
			||||||
    let page = this.$store ? this : getCurrentPages().pop()
 | 
					 | 
				
			||||||
    return page.i18nText[page.$store.state.i18n.activeLang]
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  formatMoney(value, decimal) {
 | 
					 | 
				
			||||||
    return Number(value).toFixed(decimal || 2)
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  formatPercent(value, decimal) {
 | 
					 | 
				
			||||||
    return `${Number(value * 100).toFixed(decimal || 2)}`
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  formatDate(date, format) {
 | 
					 | 
				
			||||||
    if (!(date instanceof Date)) {
 | 
					 | 
				
			||||||
      date = new Date(date)
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    if (!date.toJSON()) {
 | 
					 | 
				
			||||||
      date = new Date()
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    format = format && typeof format === 'string' ? format : 'yyyy-mm-dd HH:MM:SS'
 | 
					 | 
				
			||||||
    let o = {
 | 
					 | 
				
			||||||
      'm+': date.getMonth() + 1, //月份
 | 
					 | 
				
			||||||
      'q+': Math.floor((date.getMonth() + 3) / 3), //季度
 | 
					 | 
				
			||||||
      'd+': date.getDate(), //日
 | 
					 | 
				
			||||||
      'H+': date.getHours(), //小时
 | 
					 | 
				
			||||||
      'M+': date.getMinutes(), //分
 | 
					 | 
				
			||||||
      'S+': date.getSeconds(), //秒
 | 
					 | 
				
			||||||
      s: date.getMilliseconds(), //毫秒
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1, `${date.getFullYear()}`.substr(4 - RegExp.$1.length))
 | 
					 | 
				
			||||||
    for (var k in o) {
 | 
					 | 
				
			||||||
      if (new RegExp(`(${k})`).test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length))
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return format
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  hash(data, { hasher = 'sha256', salt, input = 'utf8', output = 'hex' } = {}) {
 | 
					 | 
				
			||||||
    if (typeof data !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView)) data = JSON.stringify(data)
 | 
					 | 
				
			||||||
    if (salt && typeof salt === 'string') data = data + salt
 | 
					 | 
				
			||||||
    let inputEncoding = input // my.INPUT_LIST.indexOf(option.input)>=0?option.input:my.INPUT // 'utf8', 'ascii' or 'latin1' for string data, default to utf8 if not specified; ignored for Buffer, TypedArray, or DataView.
 | 
					 | 
				
			||||||
    let outputEncoding = output === 'buf' ? undefined : output // (my.OUTPUT_LIST.indexOf(output)>=0?output:my.OUTPUT)  // option.output: 留空=》默认输出hex格式;或者手动指定 'buf', hex', 'latin1' or 'base64'
 | 
					 | 
				
			||||||
    return require('crypto').createHash(hasher).update(data, inputEncoding).digest(outputEncoding)
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										261
									
								
								unitool/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										261
									
								
								unitool/index.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,261 @@
 | 
				
			|||||||
 | 
					// #ifdef H5
 | 
				
			||||||
 | 
					// import device from 'current-device' // https://github.com/matthewhudson/current-device
 | 
				
			||||||
 | 
					// #endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports = {
 | 
				
			||||||
 | 
					  clog(...message) {
 | 
				
			||||||
 | 
					    console.log(
 | 
				
			||||||
 | 
					      '【【【【【【【【【【',
 | 
				
			||||||
 | 
					      getCurrentPages().length > 0 ? getCurrentPages().pop().route : 'pages/Welcome', // 在首页时,getApp() 或 getCurrentPages() 有可能获取不到。
 | 
				
			||||||
 | 
					      ...message,
 | 
				
			||||||
 | 
					      '】】】】】】】】】】】'
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  async request({ method = 'POST', url, header = {}, data = {} }) {
 | 
				
			||||||
 | 
					    url = this.makeUrl(url)
 | 
				
			||||||
 | 
					    if (uni.getStorageSync('_passtoken')) {
 | 
				
			||||||
 | 
					      header._passtoken = uni.getStorageSync('_passtoken')
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (method === 'GET') {
 | 
				
			||||||
 | 
					      // 如果不是 POST 方法,要额外把参数JSON化
 | 
				
			||||||
 | 
					      for (let key in data) {
 | 
				
			||||||
 | 
					        data[key] = JSON.stringify(data[key])
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    console.log('👇 👇 👇 👇 < Request > 👇 👇 👇 👇 ', { method, url, header, data }, '👆 👆 👆 👆 < /Request > 👆 👆 👆 👆')
 | 
				
			||||||
 | 
					    let [error, response] = await uni.request({ method, url, header, data })
 | 
				
			||||||
 | 
					    console.log('⬇️ ⬇️ ⬇️ ⬇️ < Response > ⬇️ ⬇️ ⬇️ ⬇️ ', response, '⬆️ ⬆️ ⬆️ ⬆️ < /Response > ⬆️ ⬆️ ⬆️ ⬆️')
 | 
				
			||||||
 | 
					    return [error, response]
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  async pickupFile({
 | 
				
			||||||
 | 
					    mediaType = 'image',
 | 
				
			||||||
 | 
					    count = 1,
 | 
				
			||||||
 | 
					    sizeType = ['original', 'compressed'],
 | 
				
			||||||
 | 
					    sourceType = ['album', 'camera'],
 | 
				
			||||||
 | 
					    url,
 | 
				
			||||||
 | 
					    header = {},
 | 
				
			||||||
 | 
					    formData = {},
 | 
				
			||||||
 | 
					    name = 'file',
 | 
				
			||||||
 | 
					  } = {}) {
 | 
				
			||||||
 | 
					    let filePath
 | 
				
			||||||
 | 
					    if (mediaType === 'image') {
 | 
				
			||||||
 | 
					      let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType })
 | 
				
			||||||
 | 
					      filePath = tempFilePaths[0]
 | 
				
			||||||
 | 
					    } else if (mediaType === 'video') {
 | 
				
			||||||
 | 
					      let [errorChoose, { tempFilePath }] = await uni.chooseVideo({ sourceType })
 | 
				
			||||||
 | 
					      filePath = tempFilePath
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      return [{ _ERROR: 'UNKNOWN_MEDIATYPE' }, null]
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (filePath) {
 | 
				
			||||||
 | 
					      if (uni.getStorageSync('_passtoken')) {
 | 
				
			||||||
 | 
					        header._passtoken = uni.getStorageSync('_passtoken')
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        return [{ _ERROR: 'USER_OFFLINE', errMsg: 'offline user cannot upload files' }, null]
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      for (let key in formData) {
 | 
				
			||||||
 | 
					        // multer 不会自动处理 JSON 数据,必须前后端配合处理
 | 
				
			||||||
 | 
					        formData[key] = JSON.stringify(formData[key])
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      uni.showLoading()
 | 
				
			||||||
 | 
					      let [errorUpload, response] = await uni.uploadFile({ filePath: filePath, url: this.makeUrl(url), header, formData, name })
 | 
				
			||||||
 | 
					      uni.hideLoading()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if (response && response.data) {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					          response.data = JSON.parse(response.data)
 | 
				
			||||||
 | 
					        } catch (exception) {}
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      return [errorUpload, response]
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return [{ _ERROR: 'USER_CANCELED' }, null]
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  async uploadFile({ url, name = 'file', formData = {}, header = {} } = {}) {
 | 
				
			||||||
 | 
					    url = this.makeUrl(url)
 | 
				
			||||||
 | 
					    if (uni.getStorageSync('_passtoken')) {
 | 
				
			||||||
 | 
					      header._passtoken = uni.getStorageSync('_passtoken')
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      return [{ errMsg: 'offline user cannot upload files' }, null]
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (let key in formData) {
 | 
				
			||||||
 | 
					      // multer 不会自动处理 JSON 数据,必须前后端配合处理
 | 
				
			||||||
 | 
					      formData[key] = JSON.stringify(formData[key])
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let [error, response] = await uni.uploadFile({ url, name, formData, header })
 | 
				
			||||||
 | 
					    if (response && response.data) {
 | 
				
			||||||
 | 
					      try {
 | 
				
			||||||
 | 
					        response.data = JSON.parse(response.data)
 | 
				
			||||||
 | 
					      } catch (exception) {}
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return [error, response]
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  async pickupFile2Cloud({ mediaType = 'image', count = 1, sizeType = ['original', 'compressed'], sourceType = ['album', 'camera'] } = {}) {
 | 
				
			||||||
 | 
					    let filePath, cloudPath
 | 
				
			||||||
 | 
					    if (mediaType === 'image') {
 | 
				
			||||||
 | 
					      let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType })
 | 
				
			||||||
 | 
					      filePath = tempFilePaths[0]
 | 
				
			||||||
 | 
					      cloudPath = 'cloud.jpg'
 | 
				
			||||||
 | 
					      // #ifdef H5
 | 
				
			||||||
 | 
					      cloudPath = tempFiles[0].name
 | 
				
			||||||
 | 
					      // #endif
 | 
				
			||||||
 | 
					    } else if (mediaType === 'video') {
 | 
				
			||||||
 | 
					      let [errorChoose, { tempFilePath, tempFile, name }] = await uni.chooseVideo({ sourceType })
 | 
				
			||||||
 | 
					      filePath = tempFilePath
 | 
				
			||||||
 | 
					      cloudPath = 'cloud.mp4'
 | 
				
			||||||
 | 
					      // #ifdef H5
 | 
				
			||||||
 | 
					      cloudPath = name
 | 
				
			||||||
 | 
					      // #endif
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      return { _state: 'UNKNOWN_MEDIA_TYPE' }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (filePath) {
 | 
				
			||||||
 | 
					      uni.showLoading()
 | 
				
			||||||
 | 
					      const { fileID, requestId } = await uniCloud.uploadFile({
 | 
				
			||||||
 | 
					        filePath: filePath,
 | 
				
			||||||
 | 
					        cloudPath: cloudPath,
 | 
				
			||||||
 | 
					        fileType: mediaType,
 | 
				
			||||||
 | 
					        onUploadProgress: function (progressEvent) {
 | 
				
			||||||
 | 
					          console.log(progressEvent)
 | 
				
			||||||
 | 
					          var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      })
 | 
				
			||||||
 | 
					      uni.hideLoading()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      console.log('文件上传结果:', { fileID, requestId })
 | 
				
			||||||
 | 
					      if (fileID) {
 | 
				
			||||||
 | 
					        return { _state: 'SUCCESS', fileID, requestId }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return { _state: 'FAIL' }
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  openUrl(url) {
 | 
				
			||||||
 | 
					    // #ifdef APP-PLUS
 | 
				
			||||||
 | 
					    plus.runtime.openURL(url)
 | 
				
			||||||
 | 
					    // #endif
 | 
				
			||||||
 | 
					    // #ifdef H5
 | 
				
			||||||
 | 
					    window.open(url, '_blank')
 | 
				
			||||||
 | 
					    // #endif
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  getSystemInfo() {
 | 
				
			||||||
 | 
					    let systemInfo = uni.getSystemInfoSync()
 | 
				
			||||||
 | 
					    // #ifdef H5
 | 
				
			||||||
 | 
					    systemInfo.runtime = 'h5'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // if (device.mobile()){
 | 
				
			||||||
 | 
					    //   systemInfo.platform = 'mobile'
 | 
				
			||||||
 | 
					    // }else if (device.desktop()){
 | 
				
			||||||
 | 
					    //   systemInfo.platform = 'desktop'
 | 
				
			||||||
 | 
					    // }else if (device.tablet()){
 | 
				
			||||||
 | 
					    //   systemInfo.platform = 'tablet'
 | 
				
			||||||
 | 
					    // }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (/MicroMessenger/.test(window.navigator.userAgent)) {
 | 
				
			||||||
 | 
					      // 微信内置浏览器
 | 
				
			||||||
 | 
					      systemInfo.browser = 'wechat'
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // #endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // #ifdef APP-PLUS || APP-PLUS-NVUE
 | 
				
			||||||
 | 
					    systemInfo.runtime = 'app'
 | 
				
			||||||
 | 
					    // 细分成 systemInfo.platform === ios or android
 | 
				
			||||||
 | 
					    // #endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // #ifdef MP
 | 
				
			||||||
 | 
					    systemInfo.runtime = 'mp'
 | 
				
			||||||
 | 
					    // 细分成 WEIXIN, ...
 | 
				
			||||||
 | 
					    // #endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return systemInfo
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  showToast({ type, icon, image, title, duration, ...rest }) {
 | 
				
			||||||
 | 
					    let pageNow = this.$store ? this : getCurrentPages().pop()
 | 
				
			||||||
 | 
					    if (pageNow.$refs && pageNow.$refs.toast) {
 | 
				
			||||||
 | 
					      // 在 ios app 里,虽然能获得 pageNow,但是不存在 pageNow.$refs,不知为何。android app 没有测试
 | 
				
			||||||
 | 
					      pageNow.$refs.toast.open({ type, content: title, duration, ...rest })
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      // #ifdef APP-PLUS
 | 
				
			||||||
 | 
					      if (uni.getSystemInfoSync().platform === 'android') {
 | 
				
			||||||
 | 
					        uni.showToast({ icon: 'none', title, duration, ...rest })
 | 
				
			||||||
 | 
					        return
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      // #endif
 | 
				
			||||||
 | 
					      if (!image) {
 | 
				
			||||||
 | 
					        image = `../static/Common.${type ? type : 'info'}.png`
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      uni.showToast({ icon, image, title, duration, ...rest })
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  setBarTitles({ windowTitle, pageTitle } = {}) {
 | 
				
			||||||
 | 
					    let page = this.$store ? this : getCurrentPages().pop()
 | 
				
			||||||
 | 
					    uni.setNavigationBarTitle({ title: pageTitle || page.i18nText[page.$store.state.i18n.activeLang].tPageTitle })
 | 
				
			||||||
 | 
					    // #ifdef H5
 | 
				
			||||||
 | 
					    document.title = windowTitle || page.$store.getters['i18n/getAppName'] || page.appName // 必须放在 setNavigationBarTitle 之后,否则会被其覆盖掉。
 | 
				
			||||||
 | 
					    // #endif
 | 
				
			||||||
 | 
					    if (page.$store._mutations['i18n/setTabbar']) page.$store.commit('i18n/setTabbar') // 必须要在有 tab 的页面里重置才有效果
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  localeText() {
 | 
				
			||||||
 | 
					    let page = this.$store ? this : getCurrentPages().pop()
 | 
				
			||||||
 | 
					    return page.i18nText[page.$store.state.i18n.activeLang]
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  formatMoney(value, decimal) {
 | 
				
			||||||
 | 
					    return Number(value).toFixed(decimal || 2)
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  formatPercent(value, decimal) {
 | 
				
			||||||
 | 
					    return `${Number(value * 100).toFixed(decimal || 2)}`
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  formatDate(date, format) {
 | 
				
			||||||
 | 
					    if (!(date instanceof Date)) {
 | 
				
			||||||
 | 
					      date = new Date(date)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (!date.toJSON()) {
 | 
				
			||||||
 | 
					      date = new Date()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    format = format && typeof format === 'string' ? format : 'yyyy-mm-dd HH:MM:SS'
 | 
				
			||||||
 | 
					    let o = {
 | 
				
			||||||
 | 
					      'm+': date.getMonth() + 1, //月份
 | 
				
			||||||
 | 
					      'q+': Math.floor((date.getMonth() + 3) / 3), //季度
 | 
				
			||||||
 | 
					      'd+': date.getDate(), //日
 | 
				
			||||||
 | 
					      'H+': date.getHours(), //小时
 | 
				
			||||||
 | 
					      'M+': date.getMinutes(), //分
 | 
				
			||||||
 | 
					      'S+': date.getSeconds(), //秒
 | 
				
			||||||
 | 
					      s: date.getMilliseconds(), //毫秒
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (/(y+)/.test(format)) format = format.replace(RegExp.$1, `${date.getFullYear()}`.substr(4 - RegExp.$1.length))
 | 
				
			||||||
 | 
					    for (var k in o) {
 | 
				
			||||||
 | 
					      if (new RegExp(`(${k})`).test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length))
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return format
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  hash(data, { hasher = 'sha256', salt, input = 'utf8', output = 'hex' } = {}) {
 | 
				
			||||||
 | 
					    if (typeof data !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView)) data = JSON.stringify(data)
 | 
				
			||||||
 | 
					    if (salt && typeof salt === 'string') data = data + salt
 | 
				
			||||||
 | 
					    let inputEncoding = input // my.INPUT_LIST.indexOf(option.input)>=0?option.input:my.INPUT // 'utf8', 'ascii' or 'latin1' for string data, default to utf8 if not specified; ignored for Buffer, TypedArray, or DataView.
 | 
				
			||||||
 | 
					    let outputEncoding = output === 'buf' ? undefined : output // (my.OUTPUT_LIST.indexOf(output)>=0?output:my.OUTPUT)  // option.output: 留空=》默认输出hex格式;或者手动指定 'buf', hex', 'latin1' or 'base64'
 | 
				
			||||||
 | 
					    return require('crypto').createHash(hasher).update(data, inputEncoding).digest(outputEncoding)
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user