hookehuyr

fix 上传控件错误提示

1 <!-- 1 <!--
2 * @Date: 2022-08-31 16:16:49 2 * @Date: 2022-08-31 16:16:49
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2023-04-12 09:54:33 4 + * @LastEditTime: 2023-04-12 10:59:48
5 * @FilePath: /custom_form/src/components/FileUploaderField/index.vue 5 * @FilePath: /custom_form/src/components/FileUploaderField/index.vue
6 * @Description: 文件上传控件 6 * @Description: 文件上传控件
7 --> 7 -->
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
18 <div 18 <div
19 v-if="item.component_props.note" 19 v-if="item.component_props.note"
20 v-html="item.component_props.note" 20 v-html="item.component_props.note"
21 - style="font-size: 0.9rem; margin-left: 1rem; color: gray; padding-bottom: 0.5rem; padding-top: 0.25rem; white-space: pre-wrap;" 21 + style="font-size: 13px; margin-left: 1rem; color: gray; padding-bottom: 0.5rem; padding-top: 0.25rem; white-space: pre-wrap;"
22 /> 22 />
23 <!-- <div> 23 <!-- <div>
24 <p 24 <p
...@@ -132,7 +132,7 @@ const beforeXhrUpload = async (xhr, options) => { ...@@ -132,7 +132,7 @@ const beforeXhrUpload = async (xhr, options) => {
132 // 上传返回file数据结构 132 // 上传返回file数据结构
133 const resImgObj = await handleUpload(imgFile); 133 const resImgObj = await handleUpload(imgFile);
134 // 上传失败提示 134 // 上传失败提示
135 - if (!resImgObj.src) { 135 + if (!resImgObj) {
136 options.onFailure?.(resImgObj, options); 136 options.onFailure?.(resImgObj, options);
137 loading.value = false; 137 loading.value = false;
138 } else { 138 } else {
...@@ -162,6 +162,7 @@ const beforeXhrUpload = async (xhr, options) => { ...@@ -162,6 +162,7 @@ const beforeXhrUpload = async (xhr, options) => {
162 // 文件上传七牛云 162 // 文件上传七牛云
163 // 第一次上传 163 // 第一次上传
164 if (getToken.token) { 164 if (getToken.token) {
165 + loading.value = true;
165 // 自拍图片上传七牛服务器 166 // 自拍图片上传七牛服务器
166 Taro.uploadFile({ 167 Taro.uploadFile({
167 url: 'https://up.qbox.me', 168 url: 'https://up.qbox.me',
...@@ -180,9 +181,6 @@ const beforeXhrUpload = async (xhr, options) => { ...@@ -180,9 +181,6 @@ const beforeXhrUpload = async (xhr, options) => {
180 name: filename, 181 name: filename,
181 filekey: res.data.filekey, 182 filekey: res.data.filekey,
182 hash: file_info.digest, 183 hash: file_info.digest,
183 - // format: image_info.format,
184 - // height: image_info.height,
185 - // width: image_info.width,
186 }); 184 });
187 // 加入上传成功队列 185 // 加入上传成功队列
188 fileList.value.push({ 186 fileList.value.push({
...@@ -191,10 +189,13 @@ const beforeXhrUpload = async (xhr, options) => { ...@@ -191,10 +189,13 @@ const beforeXhrUpload = async (xhr, options) => {
191 size: file_info.size 189 size: file_info.size
192 }); 190 });
193 options.onSuccess?.(data, options); 191 options.onSuccess?.(data, options);
192 + loading.value = false;
194 } 193 }
195 }) 194 })
196 .catch((error) => { 195 .catch((error) => {
197 console.error(error) 196 console.error(error)
197 + options.onFailure?.(error, options);
198 + loading.value = false;
198 }) 199 })
199 } 200 }
200 // 重复上传 201 // 重复上传
...@@ -227,10 +228,13 @@ const uploadSuccess = async ({ data, fileItem, option, responseText }) => { ...@@ -227,10 +228,13 @@ const uploadSuccess = async ({ data, fileItem, option, responseText }) => {
227 228
228 // 上传失败回调 229 // 上传失败回调
229 const uploadFailure = async ({ data, fileItem, option, responseText }) => { 230 const uploadFailure = async ({ data, fileItem, option, responseText }) => {
231 + // 真实上传失败才会提示
232 + if (data) {
230 console.error("上传失败", "fail"); 233 console.error("上传失败", "fail");
231 toast_msg.value = '上传失败,请重新尝试!' 234 toast_msg.value = '上传失败,请重新尝试!'
232 toast_show.value = true; 235 toast_show.value = true;
233 toast_type.value = 'fail'; 236 toast_type.value = 'fail';
237 + }
234 }; 238 };
235 239
236 // 删除上传队列回调 240 // 删除上传队列回调
...@@ -254,48 +258,6 @@ const fileItemClick = (fileItem) => { ...@@ -254,48 +258,6 @@ const fileItemClick = (fileItem) => {
254 const onChange = ({ fileList }) => { 258 const onChange = ({ fileList }) => {
255 } 259 }
256 260
257 -// 上传前置处理
258 -const beforeRead = (file) => {
259 - // TODO: 需要file_type集合
260 - // 类型限制
261 - // const file_types = _.map(
262 - // props.item.component_props.file_type.split("/"),
263 - // (item) => `video/${item}`
264 - // );
265 - let flag = true;
266 - if (file.length + 1 > props.item.component_props.max_count) {
267 - // 数量限制
268 - flag = false;
269 - showToast(`最大上传数量为${props.item.component_props.max_count}个`);
270 - }
271 - if (fileList.value.length + 1 > props.item.component_props.max_count) {
272 - // 数量限制
273 - flag = false;
274 - showToast(`最大上传数量为${props.item.component_props.max_count}个`);
275 - }
276 - if ((file.size / 1024 / 1024).toFixed(2) > props.item.component_props.max_size) {
277 - // 体积限制
278 - flag = false;
279 - showToast(
280 - `最大文件体积为${props.item.component_props.max_size}MB`
281 - );
282 - }
283 - // if (_.isArray(file)) {
284 - // // 多张文件
285 - // const types = _.difference(_.uniq(_.map(file, (item) => item.type)), file_types); // 数组返回不能上传的类型
286 - // if (types.length) {
287 - // flag = false;
288 - // showFailToast("请上传指定格式文件");
289 - // }
290 - // } else {
291 - // if (!_.includes(file_types, file.type)) {
292 - // showFailToast("请上传指定格式文件");
293 - // flag = false;
294 - // }
295 - // }
296 - return flag;
297 -};
298 -
299 /********** 上传七牛云获取文件地址 ***********/ 261 /********** 上传七牛云获取文件地址 ***********/
300 const loading = ref(false); 262 const loading = ref(false);
301 const formCode = getUrlParams(location.href) ? getUrlParams(location.href).code : ''; // 表单code 263 const formCode = getUrlParams(location.href) ? getUrlParams(location.href).code : ''; // 表单code
...@@ -358,16 +320,21 @@ const uploadQiniu = async (file, token, name, md5) => { ...@@ -358,16 +320,21 @@ const uploadQiniu = async (file, token, name, md5) => {
358 } else { 320 } else {
359 qiniuUploadUrl = 'http://upload.qiniu.com'; 321 qiniuUploadUrl = 'http://upload.qiniu.com';
360 } 322 }
361 - const { filekey, hash, image_info } = await qiniuUploadAPI( 323 + const uploadData = await qiniuUploadAPI(
362 qiniuUploadUrl, 324 qiniuUploadUrl,
363 formData, 325 formData,
364 config 326 config
365 ); 327 );
366 - if (filekey) { 328 + // 上传失败处理
329 + if (!uploadData) {
330 + loading.value = false;
331 + return false;
332 + }
333 + if (uploadData.filekey) {
367 // 保存文件 334 // 保存文件
368 const { data } = await saveFileAPI({ 335 const { data } = await saveFileAPI({
369 name, 336 name,
370 - filekey, 337 + filekey: uploadData.filekey,
371 hash: md5, 338 hash: md5,
372 // format: image_info.format, 339 // format: image_info.format,
373 // height: image_info.height, 340 // height: image_info.height,
......
1 /* 1 /*
2 * @Date: 2022-09-19 14:11:06 2 * @Date: 2022-09-19 14:11:06
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2023-03-24 11:16:19 4 + * @LastEditTime: 2023-04-12 10:50:42
5 * @FilePath: /custom_form/src/utils/request.js 5 * @FilePath: /custom_form/src/utils/request.js
6 * @Description: 简单axios封装,后续按实际处理 6 * @Description: 简单axios封装,后续按实际处理
7 */ 7 */
...@@ -41,7 +41,7 @@ const service = axios.create({ ...@@ -41,7 +41,7 @@ const service = axios.create({
41 // 小程序跨域需要定义域名前缀,H5跨域有配置不需要 41 // 小程序跨域需要定义域名前缀,H5跨域有配置不需要
42 baseURL: process.env.TARO_ENV === 'weapp' ? BASE_URL : '', // url = base url + request url 42 baseURL: process.env.TARO_ENV === 'weapp' ? BASE_URL : '', // url = base url + request url
43 // withCredentials: true, // send cookies when cross-domain requests 43 // withCredentials: true, // send cookies when cross-domain requests
44 - timeout: 5000, // request timeout 44 + // timeout: 5000, // request timeout
45 }) 45 })
46 46
47 service.defaults.params = { 47 service.defaults.params = {
......