蜻蜓Q系统上传部分修改·调整为先上传再到服务端,此前为先到服务端会经常莫名其妙的超时

关于视频上传有个未解之谜,具体原因不细说了,可以写论文了。

优雅草蜻蜓系统已于2022年9月15日改变上传系统,目前默认为七牛云上传,曾经是可以在后台切换的如今如果需要切换上传机制需要前后端改:

改动文件,上传节点也需要此处修改。

/pages/upload-video/upload-video.vue

_uploadVideo() {
				// const uploadTask = uni.uploadFile({
				// 	url: `${BASE_URL}uploadVideo`,
				// 	header: {
				// 		authorization: getToken()
				// 	},
				// 	filePath: this.src,
				// 	name: 'file',
				// 	formData: {},
				// 	success: (uploadFileRes) => {
				// 		const { code, msg, data } = JSON.parse(uploadFileRes.data)
				// 		console.log(data)
				// 		if (code === 200) {
				// 			this.video = data
				// 			// this._checkVideo(data.url)
				// 		} else {
				// 			uni.showToast({
				// 				title: msg,
				// 				icon: 'none'
				// 			})
				// 		}
				// 	},
				// 	fail: (err) => {
				// 		console.log('视频上传失败', err)
				// 	}
				// })
				// uploadTask.onProgressUpdate((res) => {
				// 	this.uploadProgress = res.progress
				// })
			   var date = new Date();
		       getQtToken({}).then((res) => {
				//如果获取成功
				if (res.code == 200) {
					
					let token = res.data.token;
					var date = new Date();
					//开始上传
					const uploadTask = uni.uploadFile({
						url: 'https://upload-z1.qiniup.com',
						filePath: this.src,  
						dataType: 'json',
						name: 'file',
						formData: {
							'token': token,
							'key':1+ "/" + date.getTime()+ "." +'mp4'
						},
						success: (res) => {
						
							uni.hideLoading()
							this.uploadFile = false;
							this.progress=0;
							if (res.statusCode == 200) {
								var data = JSON.parse(res.data);
								if (!data.key) {
									uni.showToast({
										title: '上传失败,' + data.error,
										icon: 'none'
									});
									return;
								}
								uni.showToast({
									title: '上传成功',
								});
								this.video.url='https://qingtingcun.youyacao.com/'+data.key
								// this._checkImage(this.config.domain + data.key)
							}
						},
						fail(e) {}
					});
					uploadTask.onProgressUpdate((res) => {
				    	this.uploadProgress = res.progress
					});
				}
			})
				
			},
			_checkVideo(content) {
				this.testing = true
				Test('video', content).then(({ machine_result }) => {
					this.testing = false
					if (machine_result !== 'Normal') {
						const msg = this.machineResult[machine_result]
						uni.showToast({
							title: `检测到您的视频涉及${msg},请重新选择。`,
							icon: 'none'
						})
						this.src = ''
						this.video = ''
						this.uploadProgress = 0
					}
				}).catch(err => {
					this.testing = false
				})
			}
		},
		onHide() {
			if (this.videoContext) {
				this.videoContext.pause()
			}
		}

七牛云节点处需要修改, 拼接上传域名处也需要修改。

			this.video.url='https://qingtingcun.youyacao.com/'+data.key
								// this._checkImage(this.config.domain + data.key)
url: 'https://upload-z1.qiniup.com',
						filePath: this.src,  

服务端上传文件修改:

<?php
/**
 * 上传
 * @date    2020-01-01
 * @email   [email protected]
 * @version 1.0
 */
namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use Exception;

class UploadController extends ApiController
{
    public function qiniuToken(Request $request)
    {
        try {
            $token = app('upload')->qiniu_token();
            return $this->success('成功', [
                'token' => $token
            ]);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    // 上传文件
    public function up(Request $request)
    {
        try {
            if (!$request->hasFile('file')) {
                throw new Exception('无法获取上传文件');
            }
            $file = $request->file('file');
            if (!$file->isValid()) {
                throw new Exception('文件未通过验证');
            }
            $fileExtension = strtolower($file->getClientOriginalExtension());
            $filePath = $file->getRealPath();
            $filename = genRequestSn() . '.' . $fileExtension;
            // 文件原名
            $originaName = $file->getClientOriginalName();
            $data = app('upload')->qiniu_upload($filename, $filePath);
            $data['name'] = $originaName;
            return $this->success('保存成功', $data);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    // 上传图片
    public function upload(Request $request)
    {
        try {
            $data = app('upload')->up($request);
            return $this->success('保存成功', $data);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    // 上传视频
    public function upVideo(Request $request)
    {
        try {
            $data = app('upload')->upVideo($request);
            return $this->success('保存成功', $data);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}

猜你喜欢

转载自blog.csdn.net/dujiangdu123/article/details/126895775