linwe
2024-08-08 3c738f4fe2762bba8087e5a22fc0dc06560eab0e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {
    prodUrl,
    clientId,
    clientSecret,
    uploadUrl
} from '@/common/setting'
 
import {
    Base64
} from '@/utils/base64.js';
export default {
    data() {
        return {
            form: {
                images: []
            },
 
            uploadConfig: {
                acceptImg: "image",
                acceptVideo: 'video',
                capture: ['album', 'camera'],
                multiple: true,
                maxCount: "5",
                previewFullImage: true,
                uploadText: "上传中",
                url: uploadUrl + "/blade-resource/oss/endpoint/put-file",
                header: {},
            },
        }
    },
 
    created() {
        this.getHeader()
    },
 
    methods: {
        //获取头部
        getHeader() {
            let accessToken = uni.getStorageSync('accessToken');
            let myHeader = {}
            if (accessToken) {
                myHeader['Blade-Auth'] = 'bearer ' + accessToken;
            }
            // 客户端认证参数
            myHeader['Authorization'] = 'Basic ' + Base64.encode(clientId + ':' + clientSecret);
            this.uploadConfig.header = myHeader
        },
 
        //上传方法
        uploadFilePromise(url) {
            return new Promise((resolve, reject) => {
                let a = uni.uploadFile({
                    url: this.uploadConfig.url, //接口地址
                    filePath: url,
                    name: 'file',
                    header: this.uploadConfig.header,
                    success: (res) => {
                        let data = JSON.parse(res.data)
                        setTimeout(() => {
                            resolve(data)
                        }, 1000)
                    }
                });
            })
        },
 
        //上传成功后对返回数据进行处理
        async afterReadImg(event, key = "images") {
            // console.log(key)
            // console.log(this.form[key]);
            this.showLoading()
            var that = this;
            // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
            let lists = [].concat(event.file)
            let fileListLen = this.form[key].length
            lists.map((item) => {
                this.form[key].push({
                    ...item,
                    status: 'uploading',
                    message: '上传中'
                })
            })
            for (let i = 0; i < lists.length; i++) {
                const result = await this.uploadFilePromise(lists[i].url)
                that.form[key].splice(fileListLen, 1, Object.assign({}, {
                    url: result.data.link,
                    name: result.data.name
                }))
                fileListLen++
            }
 
            // let fileListLen = this.form.images.length
            // lists.map((item) => {
            //     this.form.images.push({
            //         ...item,
            //         status: 'uploading',
            //         message: '上传中'
            //     })
            // })
            // for (let i = 0; i < lists.length; i++) {
            //     const result = await this.uploadFilePromise(lists[i].url)
            //     that.form.images.splice(fileListLen, 1, Object.assign({}, {
            //         url: result.data.link,
            //         name:result.data.name
            //     }))
            //     fileListLen++
            // }
 
 
            this.loadingClose()
        },
 
 
        //删除图片
        deletePic(event, key = "images") {
 
            console.log(111)
 
            this.form[key].splice(event.index, 1)
        },
 
        deletePics(event, key) {
            console.log(event, key);
            this[key].splice(event.index, 1)
        },
 
        showLoading() {
            uni.showLoading({
                mask: true,
                title: '上传中'
            })
        },
        loadingClose() {
            uni.hideLoading()
        },
    }
}