<template>
  <nut-uploader
    v-model:file-list="fileList"
    :url="config.url"
    :maximum="max"
    :headers="config.headers"
    @success="success"
    @failure="failure"
    :multiple="multiple"
  >
  </nut-uploader>
</template>

<script setup lang="ts">
import { BASE_URL } from "@/utils/request";
import { computed, ref } from "vue";
import Taro from "@tarojs/taro";

const props = defineProps({
  list: {
    type: Array,
    default: () => [],
  },
  max: {
    type: Number,
    default: 1,
  },
  multiple: {
    type: Boolean,
    default: false,
  },
});

const emits = defineEmits(["update:list"]);

const fileList = computed({
  get: () => props.list,
  set: (val) => emits("update:list", val),
});

const config = ref({
  url: `${BASE_URL}/upload`,
  headers: {
    token: Taro.getStorageSync("token"),
  },
});

const success = (res: any) => {
  const data = JSON.parse(res.responseText.data);
  res.fileItem.url = data.data.data;
  Taro.showToast({
    title: "上传成功",
    icon: "success",
  });
};

const failure = () => {
  Taro.showToast({
    title: "上传失败",
    icon: "error",
  });
};
</script>

<style lang="scss"></style>
