| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <template>
- <u-upload :action="action" :fileList="fileListData"
- v-bind="$attrs"
- @on-remove="onRemove"
- @on-success="afterRead"
- @on-list-change="onListChange">
- <slot></slot>
- </u-upload>
- </template>
- <script>
- import configService from '@/common/service/config.service';
- export default {
- name: "",
- props: {
- api: {
- type: String,
- default: '/sys/common/upload'
- },
- fileList: {
- type: Array,
- default: () => []
- },
- },
- data() {
- return {
- fileListData: [],
- };
- },
- watch: {
- fileList: {
- immediate: true,
- handler(n) {
- if (n.length > 0) {
- this.fileListData = n;
- }
- }
- }
- },
- computed: {
- action() {
- return configService.apiUrl + this.api
- }
- },
- mounted() {
-
- },
- beforeDestroy() {
-
- },
- methods: {
- afterRead(event, index, list) {
- // url是组件的固定字段
- this.fileListData[index] = {
- ...event,
- url: list[index].url
- };
- this.$emit('update:fileList', this.fileListData);
- this.$emit('complete', this.fileListData);
- },
- onRemove(index, lists) {
- this.fileListData.splice(index, 1);
- this.$emit('update:fileList', this.fileListData);
- this.$emit('complete', this.fileListData);
- },
- // onListChange早于onSuccess
- onListChange(lists) {
- // this.$emit('complete', lists);
- },
- // 手动上传
- uploadFilePromise(url) {
- return new Promise((resolve, reject) => {
- let a = uni.uploadFile({
- url: this.action,
- name: "file",
- header:{
- 'AppType': '5',
- 'X-Access-Token': uni.getStorageSync('token'),
- },
- filePath: url,
- // formData: {
- // user: "test",
- // },
- success: (res) => {
- uni.hideLoading();
- let data = JSON.parse(res.data);
- if (data.success) {
- resolve(data);
- } else {
- uni.showToast({
- icon: 'none',
- title: data.message || '文件导入失败',
- });
- }
- },
- });
- });
- },
- }
- }
- </script>
- <style lang="scss" scoped>
- </style>
|