index.vue 6.63 KB
<template>
  <view class="upload-image-box">
    <view class="upload-image-box-choose">
      <text class="upload-image-box-choose-txt">请选着上传图片类型:</text>
      <view class="upload-image-box-choose-type">
        <u-radio-group v-model="putType" @change="groupChange" placement="row">
          <u-radio activeColor="#19be6b" :customStyle="{ marginBottom: '8px' }" size="28" labelSize="28"
            v-for="(item, index) in chooseList" :key="index" :label="item.name" :name="item.name" @change="radioChange">
          </u-radio>
        </u-radio-group>
      </view>
    </view>
    <view class="upload-image-box-bottom">
      <text class="upload-image-box-bottom-txt">{{ putType }}(最多上传10张)</text>
      <view class="upload-image-box-button-container">
        <u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="3" multiple :maxCount="10"
          :previewFullImage="true" width="200" height="150"></u-upload>
      </view>
    </view>
    <view class="upload-image-box-submit-box">
      <view class="upload-image-box-submit-box-button" @click="handleSubmit(orderId, putType,driver,carPlate)">
        <view class="upload-image-box-submit-box-button-container">
          <up-button color="#19be6b" type="primary" shape="square" text="确定"></up-button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { uploadFilePromise } from '@/apis/common.js';
import { uploadImageUrlByType } from '@/apis/order.js';
import { onLoad } from '@dcloudio/uni-app';
import { reactive, ref } from 'vue';
import { useMainStore } from '@/stores/index.js';
const store = useMainStore();
const putType = ref("装车图片")
const orderId = ref();
const carPlate = ref();
const driver = ref();
const fileList = ref([])
const chooseList = reactive([{ name: "装车图片" }, { name: "卸车图片" }])
// 删除图片
const deletePic = (event) => {
  fileList.value.splice(event.index, 1);
};

// 新增图片
const afterRead = async (event) => {
  // 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
  let lists = [].concat(event.file);
  let fileListLen = fileList.value.length;
  lists.map((item) => {
    fileList.value.push({
      ...item,
      status: 'uploading',
      message: '上传中',
    });
  });
  for (let i = 0; i < lists.length; i++) {
    let requestPath = import.meta.env.VITE_BASE_URL + import.meta.env.VITE_BASE_FILE_UPLOAD_PREFIX;
    const result = await uploadFilePromise(requestPath, lists[i].url);
    let item = fileList.value[fileListLen];
    fileList.value.splice(fileListLen, 1, {
      ...item,
      status: 'success',
      message: '',
      url: result.data.fileName,
    });
    fileListLen++;
  }
};
// 提交图片
const handleSubmit = async (id, type, driver, carPlate) => {
  try {
    // 获取定位信息
    const location = await new Promise((resolve, reject) => {
      uni.getLocation({
        type: 'wgs84',
        success: resolve,
        fail: reject
      });
    });
    
    if (!validateImage()) {
      uni.$u.toast("请等待图片上传~")
      return
    }

    const userPhone = store.userPhone;
    let params = {
      garOrderId: id,
      driver: driver,
      carPlate: carPlate,
      userPhone: userPhone,
      type: type == "装车图片" ? 1 : 2,
      imageUrls: fileList.value.map(item => item.url),
      // 添加定位信息
      latitude: location.latitude,
      longitude: location.longitude
    }
	
    if(params.imageUrls && params.imageUrls.length == 0){
      uni.$u.toast("请上传现场图片");
      return;
    }
    
    uploadImageUrlByType(params).then(res => {
      if (res.data.success) {
        uni.$u.toast("图片上传完毕!");
        setTimeout(() => {
          uni.$u.route({
            type: 'navigateBack',
            url: `pages/order-info/order-other/detail/index`,
          })
        }, 300)
      }
    })
    
  } catch (error) {
    console.error('定位获取失败:', error);
    uni.$u.toast("需要位置权限才能提交");
    // 可选:引导用户打开设置
    uni.openSetting({
      success(res) {
        console.log('授权设置:', res.authSetting);
      }
    });
  }
}
// 提交图片
// const handleSubmit = async (id, type,driver,carPlate) => {
//   if (!validateImage()) {
//     uni.$u.toast("请等待图片上传~")
//     return
//   }
//   const userPhone = store.userPhone;
//   let params = {
//     garOrderId: id,
// 	driver:driver,
// 	carPlate:carPlate,
// 	userPhone:userPhone,
//     type: type == "装车图片" ? 1 : 2,
//     imageUrls: fileList.value.map(item => item.url)
//   }
//   if(params.imageUrls && params.imageUrls.length == 0){
//     uni.$u.toast("请上传现场图片");
//     return;
//   }
  
//   uploadImageUrlByType(params).then(res => {
//     if (res.data.success) {
//       uni.$u.toast("图片上传完毕!");
//       setTimeout(() => {
//         uni.$u.route({
//           type: 'navigateBack',
//           url: `pages/order-info/order-other/detail/index`,
//         })
//       }, 300)
//     }
//   })
// }

const validateImage = () => {
  for (let index = 0; index < fileList.value.length; index++) {
    const str = fileList.value[index].url;
    if (!str.startsWith("/profile/upload")) {
      return false;
    }
  }
  return true;
}

onLoad((options) => {
  orderId.value = options.orderId;
  carPlate.value = options.carPlate;
  driver.value = options.driver;
  // putType.value = options.putType === "putOnImages" ? "装车图片" : "卸车图片";

})
</script>

<style lang="scss" scoped>
.upload-image-box {
  height: 100%;
  width: 100%;
  background-color: #ffffff;
  padding: 20rpx;
  box-sizing: border-box;
  line-height: 80rpx;

  .upload-image-box-choose {
    display: flex;

    .upload-image-box-choose-txt {
      white-space: nowrap;
    }

    .upload-image-box-choose-type {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }

  .upload-image-box-bottom {
    .upload-image-box-bottom-txt {
      color: $u-info;
    }

    .upload-image-box-button-container {
      min-height: 350rpx;
      min-width: 100%;
      padding: 20rpx;
      // background-color: $u-info-light;
      border: 2rpx solid $u-border-color;
      box-sizing: border-box;
      border-radius: 10rpx;
    }
  }

  .upload-image-box-submit-box {
    box-sizing: border-box;
    margin-top: 80rpx;
    width: 100%;

    .upload-image-box-submit-box-button {
      box-sizing: border-box;
      display: flex;
      justify-content: flex-end;
      align-items: center;

      .upload-image-box-submit-box-button-container {
        white-space: nowrap;
        box-sizing: border-box;
        width: 200rpx;
      }
    }
  }
}
</style>