Upload.js 2.52 KB
// components/Goods/Goods.js
import { commonApi, shopmallApi } from '~/api/api'
import { componentsMixin } from '../../mixins/index'
Component({
  behaviors: [componentsMixin],
  /**
   * 组件的属性列表
   */
  properties: {
    index: {
      type: Number,
      default: 0
    },
    sizeStyle: Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgUrls: [],
    IMGURL: wx.$imgUrl
  },

  /**
   * 组件的方法列表
   */
  methods: {
    jump(e) {
      let { item } = e.currentTarget.dataset
      let { id, activityInfo } = item
      let isActivity = false
      if (activityInfo) {
        id = activityInfo.id
        isActivity = true
      }
      wx.$router.go('/packageShopmall/packageMall/pages/detail/detail', {
        id,
        isActivity,
        notIntercept: true
      })
    },
    uploadImg() {
      console.log()
      if (this.data.imgUrls.length > 8) {
        console.log(333333333)
        wx.showToast({
          icon: 'error',
          title: '最多可上传9张图片',
          duration: 2000
        })
        return
      }
      let _this = this
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          const [tempFiles] = res.tempFilePaths
          console.log(tempFiles)
          let imgType = tempFiles.split('.')[1]
          let type = imgType == 'unknown' ? 'jpg' : imgType
          wx.getFileSystemManager().readFile({
            filePath: tempFiles, //选择图片返回的相对路径
            encoding: 'base64', //编码格式
            success(_res) {
              console.log(_res)
              wx.$api.showLoading('上传中...')
              commonApi
                .uploadImg({
                  file: `${type}@${_res.data}`
                })
                .then(img => {
                  wx.$api.hideLoading()
                  console.log(img)
                  _this.data.imgUrls.push(img)
                  _this.setData({
                    imgUrls: _this.data.imgUrls
                  })
                  _this.triggerEvent('change', _this.data.imgUrls)
                })
            }
          })
        }
      })
    },
    closeImg(e) {
      let { index } = e.currentTarget.dataset
      console.log(index)
      this.data.imgUrls.splice(index, 1)
      this.setData({
        imgUrls: this.data.imgUrls
      })
      console.log(this.data.imgUrls)
      this.triggerEvent('change', this.data.imgUrls)
    }
  },
  lifetimes: {
    attached() {}
  }
})