-
zhangwei
2024-08-14 ff120d19df422dc6aeb7a35e144d726582aef8f8
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { apiBaseUrl } from '@/common/setting/constVarsHelper.js';
import * as db from './dbHelper.js' //引入common
 
const showError = error => {
    let errorMsg = '';
    switch (error.status) {
        case 400:
            errorMsg = '请求参数错误';
            break;
        case 401:
            errorMsg = '未授权,请登录';
            break;
        case 403:
            errorMsg = '跨域拒绝访问';
            break;
        case 404:
            errorMsg = `请求地址出错: ${error.config.url}`;
            break;
        case 408:
            errorMsg = '请求超时';
            break;
        case 500:
            errorMsg = '服务器内部错误';
            break;
        case 501:
            errorMsg = '服务未实现';
            break;
        case 502:
            errorMsg = '网关错误';
            break;
        case 503:
            errorMsg = '服务不可用';
            break;
        case 504:
            errorMsg = '网关超时';
            break;
        case 505:
            errorMsg = 'HTTP版本不受支持';
            break;
        default:
            errorMsg = error.msg;
            break;
    }
    uni.showToast({
        title: errorMsg,
        icon: 'none',
        duration: 1000,
        complete: function () {
            setTimeout(function () {
                uni.hideToast();
            },
                1000);
        }
    });
};
 
// 文件上传
export const uploadFiles = (data, callback) => {
    // 获取用户token
    let userToken = db.get("userToken");
    if (!userToken) {
        this.$store.commit('showLoginTip', true);
        return false;
    };
    uni.chooseImage({
        success: (chooseImageRes) => {
            uni.showLoading({
                title: '上传中...'
            });
            const tempFilePaths = chooseImageRes.tempFilePaths;
            const uploadTask = uni.uploadFile({
                url: apiBaseUrl + '/Api/Common/UploadImages',
                filePath: tempFilePaths[0],
                fileType: 'image',
                name: 'file',
                header: {
                    'Accept': 'application/json',
                    'Content-Type': 'multipart/form-data',
                    'Authorization': 'Bearer ' + userToken
                },
                formData: {
                    'method': 'images.upload',
                    'upfile': tempFilePaths[0]
                },
                success: (uploadFileRes) => {
                    //console.log("交互成功");
                    //console.log(uploadFileRes);
                    callback(JSON.parse(uploadFileRes.data));
                },
                fail: (error) => {
                    console.log("交互失败");
                    console.log(error);
                    if (error && error.response) {
                        showError(error.response);
                    }
                },
                complete: () => {
                    setTimeout(function () {
                        uni.hideLoading();
                    },
                        250);
                }
            });
            //uploadTask.onProgressUpdate((res) => {
            //    console.log('上传进度' + res.progress);
            //    console.log('已经上传的数据长度' + res.totalBytesSent);
            //    console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
 
            //    // 测试条件,取消上传任务。
            //    if (res.progress > 50) {
            //        uploadTask.abort();
            //    }
            //});
        }
    });
};
 
// 上传图片
export const uploadImage = (num, callback) => {
 
    // 获取用户token
    let userToken = db.get("userToken");
    if (!userToken) {
        this.$store.commit('showLoginTip', true);
        return false;
    };
 
    uni.chooseImage({
        count: num,
        success: (res) => {
            uni.showLoading({
                title: '上传中...'
            });
            let tempFilePaths = res.tempFilePaths;
            for (var i = 0; i < tempFilePaths.length; i++) {
                uni.uploadFile({
                    url: apiBaseUrl + '/Api/Common/UploadImages',
                    filePath: tempFilePaths[i],
                    fileType: 'image',
                    name: 'file',
                    header: {
                        'Accept': 'application/json',
                        'Content-Type': 'multipart/form-data',
                        'Authorization': 'Bearer ' + userToken
                    },
                    formData: {
                        'method': 'images.upload',
                        'upfile': tempFilePaths[i]
                    },
                    success: (uploadFileRes) => {
                        callback(JSON.parse(uploadFileRes.data));
                    },
                    fail: (error) => {
                        if (error && error.response) {
                            showError(error.response);
                        }
                    },
                    complete: () => {
                        setTimeout(function () {
                            uni.hideLoading();
                        },
                            250);
                    },
                });
            }
        }
    });
};