step2.js 4.67 KB
// packageUsedCar/pages/deliveryNote/components/step2/step2.js
import { getSystemInfoSync } from '~/utils/systemInfo'
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'
import newApi from '~/packageUsedCar/api'
import { uploadFile, getImageDisplayUrl } from "~/packageUsedCar/utils/oss/ossUpload.js";
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 当前步骤
    step: {
      type: [String, Number],
      value: 2
    },
    // 详情数据
    detail: {
      type: Object,
      value: {}
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    IMG_URL,
    imgList: [], // 图片列表
    remark: '', // 备注
    MAX_IMG_COUNT: 10, // 最大图片数量
    MAX_IMAGE_SIZE: 10 * 1024 * 1024 // 图片最大10MB
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 删除图片
     */
    delImage: function(e) {
      const index = e.currentTarget.dataset.index
      let imgList = this.data.imgList
      imgList.splice(index, 1)
      this.setData({
        imgList: imgList
      })
    },

    /**
     * 选择图片并上传到 OSS(只存直链)
     */
    chooseImage: function() {
      let _this = this
      const { imgList, MAX_IMG_COUNT, MAX_IMAGE_SIZE } = _this.data
      const count = MAX_IMG_COUNT - imgList.length
      let sysInfo = getSystemInfoSync()
      if (count <= 0) {
        wx.showToast({
          title: '最多只能上传10张图片',
          icon: 'none'
        })
        return
      }

      wx.chooseImage({
        count: count,
        mediaType: ['image'],
        success(res) {
          for (const file of res.tempFiles) {
            if (file.size > MAX_IMAGE_SIZE) {
              return wx.showToast({
                title: '图片上传最大10MB',
                icon: 'none'
              })
            }
          }
          _this.setData({
            loadingFlag: true
          })
          const uploadImages = async (tempFiles) => {
            wx.$util.showLoading('上传中')
            try {
              for (const item of tempFiles) {
                const fileObj = {
                  tempFilePath: item.tempFilePath || item.path,
                  url: item.tempFilePath || item.path,
                  size: item.size,
                };
                const url = await uploadFile(fileObj);
                imgList.push(url);
                _this.setData({
                  imgList
                });
              }
              wx.hideLoading();
            } catch (error) {
              wx.$util.showToast("上传失败,请重试")
            } finally {
              _this.setData({
                loadingFlag: false
              })
            }
          }
          uploadImages(res?.tempFiles)
        }
      })
    },

    /**
     * 确认提车
     */
    async confirm() {
      try {
        const { imgList, step, remark, detail } = this.data
        if (imgList.length === 0) return
        // 拼接图片URL(用逗号分隔)
        const confirmImages = imgList.join(',')
        // 埋点
        wx.$track.setTrackData(0, 'delivery_arrive_shop', {}, '', 7)
        // 显示加载
        wx.$util.showLoading('提交中...')
        const res = await newApi.deliveryConfirmDelivery({
          confirmImages,
          deliveryOrderId: detail?.deliveryOrderId,
          remark
        })
        console.log(res)
        wx.hideLoading()
        let { code, msg } = res.data
        if (code == 0) {
          this.triggerEvent('confirm', step)
          this.triggerEvent('refreshlog')
        } else {
          throw msg
        }
      } catch (error) {
        this.triggerEvent('openError', error)
      }
    },

    /**
     * 预览媒体(图片预览时使用展示 URL,带 OSS 处理参数)
     */
    previewMedia: function(e) {
      const url = e.currentTarget.dataset.url
      const type = e.currentTarget.dataset.type
      const rawList = this.data.imgList || []
      const urls = rawList.map(u => getImageDisplayUrl(u, false))
      const current = getImageDisplayUrl(url, false)

      if (!urls.length) return

      // 预览图片
      wx.previewImage({
        current: current,
        urls: urls
      })
    },

    /**
     * 备注输入事件
     */
    onRemarkInput: function(e) {
      this.setData({
        remark: e.detail.value
      })
    },

    /**
     * 重置组件数据(供外部调用)
     */
    resetData: function() {
      this.setData({
        imgList: [],
        remark: ''
      })
    },

    /**
     * 获取组件数据(供外部调用)
     */
    getFormData: function() {
      return {
        imgList: this.data.imgList,
        remark: this.data.remark
      }
    }
  }
})