picture.js 5.2 KB
// packageStoreAssistant/packageEquityClaims/components/picture/picture.js
import { commonApi } from '~/api/api'
import { getSystemInfoSync, getSystemInfo } from '~/utils/systemInfo'
Component({
  /**
   * 组件的属性列表
   */
  externalClasses: ['custom-class'],
  properties: {
    configOptions: {
        type: Object,
        value: ()=> {
            
        }
    },
    url: {
        type: String,
        value: ''
    },
    exampleUrl: {
        type: String,
        value: ''
    },
    disabled: {
        type: Boolean,
        value: ''
    },
    batchFlag: {
      type: Boolean,
      value: false
    },
    fileList: {
      type: Object,
      value: () => {}
    },
    batchIndex: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    curUrl: '',
    previewVisible: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    previewImg(e, viewUrl) {
      const { batchFlag, fileList, batchIndex } = this.data
      const { url } = e.currentTarget.dataset
      let current, urls
      if (batchFlag) { // 批量预览
        urls = fileList
          ?.filter(item => item.attachmentFile)
          ?.map(item => item.attachmentFile)
        current = urls[batchIndex]
      } else { // viewUrl是示例图片预览
        current = viewUrl || url
        urls = viewUrl ? [viewUrl] : [url]
      }
      wx.previewImage({
        current,
        urls
      })
    },
    viewExample(e) {
        this.previewImg(e, this.data.exampleUrl)
    },
    stop() {},
    chooseFile(e){
        let { batchFlag, fileList } = this.data // 其他资料支持批量上传 最大50张
        if(this.data.configOptions.onlyExample) {
            return
        }
        // this.triggerEvent('upload', {url: 'https://img02.glsx.com.cn/weapp/resource/saas/scrm/assets/glsx/logo_collapse_new_common.png',type : 'png'});
        // if(e) return
        const {count} = this.data.configOptions
        let sysInfo = getSystemInfoSync()
        let maxNum = sysInfo?.environment ? 9 : 20 // 企微打开的话最大是9张。如果count大于9 会无法上传有异常
        let aleadyLength = 0
        if (batchFlag) {
          aleadyLength = fileList?.filter(val => val.attachmentFile)?.length || 0
        }
        wx.chooseMedia({
            count: batchFlag ? ((50 - aleadyLength) > maxNum ? maxNum : 50 - aleadyLength) : (count || 1),
            sizeType: ['original'],
            sourceType: ['album'],
            mediaType: ['image'],
            success: (res)=> {
              const uploadImages = async (tempFiles) => {
                wx.showLoading({
                  title: '上传中',
                  mask: true
                })
                try {
                  const uploadResults = [] // 存储所有上传结果
                  for (const item of tempFiles) {
                    const val = item.tempFilePath
                    const imgType = val.split('.')[1]
                    const type = imgType === 'unknown' ? 'jpg' : imgType
                    // 读取文件
                    const _res = await new Promise((resolve, reject) => {
                      wx.getFileSystemManager().readFile({
                        filePath: val,
                        encoding: 'base64',
                        success: resolve,
                        fail: reject,
                      })
                    })
                    // 上传图片
                    const url = await commonApi.uploadImg({
                      file: `${type}@${_res.data}`,
                    })
                    // 存储当前上传结果
                    uploadResults.push({
                      url,
                      type
                    })
                  }
                  // 所有上传完成后,统一触发事件
                  wx.hideLoading()
                  this.triggerEvent('upload', uploadResults);
                } catch (error) {
                  wx.$api.showToast('上传失败,请重试');
                  // showToast会将loading关闭,重新显示loading
                  wx.showLoading({
                    title: '上传中',
                    mask: true
                  })
                } finally {
                  wx.hideLoading()
                }
              }
              uploadImages(res?.tempFiles)
              //   const [tempFiles] = res.tempFilePaths
              //   let imgType = tempFiles.split('.')[1]
              //   let type = imgType == 'unknown' ? 'jpg' : imgType
              //   wx.getFileSystemManager().readFile({
              //   filePath: tempFiles,
              //   encoding: 'base64', 
              //   success: (_res)=> {
              //       wx.showLoading('上传中...')
              //       commonApi.uploadImg({
              //       file: `${type}@${_res.data}`
              //       }).then(url => {
              //       wx.hideLoading()
              //       this.triggerEvent('upload', {url, type});
              //       })
              //   }
              //   })
            }
        })
        // console.log('上传')
        // this.triggerEvent('upload', this.data.fileObj);
    },
    delImg(e) {
        console.log(2)
        this.triggerEvent('del');
    },
  }
})