fix: 客户端无法下载聊天文件的问题

This commit is contained in:
kuaifan 2022-01-24 14:52:50 +08:00
parent 870276fa48
commit 1ab8a19f8e
8 changed files with 119 additions and 13 deletions

View File

@ -11,6 +11,7 @@ use App\Models\WebSocketDialogMsgRead;
use App\Models\WebSocketDialogUser;
use App\Module\Base;
use Request;
use Response;
/**
* @apiDefine dialog
@ -327,4 +328,36 @@ class DialogController extends AbstractController
$read = WebSocketDialogMsgRead::whereMsgId($msg_id)->get();
return Base::retSuccess('success', $read ?: []);
}
/**
* @api {get} api/dialog/msg/download 08. 文件下载
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup dialog
* @apiName msg__download
*
* @apiParam {Number} msg_id 消息ID
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function msg__download()
{
User::auth();
//
$msg_id = intval(Request::input('msg_id'));
//
$msg = WebSocketDialogMsg::whereId($msg_id)->first();
if (empty($msg)) {
abort(403, "This file not exist.");
}
if ($msg->type != 'file') {
abort(403, "This file not support download.");
}
$array = Base::json2array($msg->getRawOriginal('msg'));
//
return Response::download(public_path($array['path']), $array['name']);
}
}

View File

@ -19,6 +19,7 @@ use App\Module\Base;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Request;
use Response;
/**
* @apiDefine project
@ -1082,6 +1083,40 @@ class ProjectController extends AbstractController
return Base::retSuccess('success', $file);
}
/**
* @api {get} api/project/task/filedown 22. 下载任务文件
*
* @apiDescription 需要token身份项目、任务负责人
* @apiVersion 1.0.0
* @apiGroup project
* @apiName task__filedown
*
* @apiParam {Number} file_id 文件ID
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function task__filedown()
{
User::auth();
//
$file_id = intval(Request::input('file_id'));
//
$file = ProjectTaskFile::find($file_id);
if (empty($file)) {
abort(403, "This file not exist.");
}
//
try {
ProjectTask::userTask($file->task_id, true, true);
} catch (\Exception $e) {
abort(403, $e->getMessage() ?: "This file not support download.");
}
//
return Response::download(public_path($file->getRawOriginal('path')), $file->name);
}
/**
* @api {post} api/project/task/add 23. 添加任务
*

View File

@ -355,6 +355,23 @@
*/
dialogCompleted(dialog) {
return this.dialogTags(dialog).find(({color}) => color == 'success');
},
/**
* 下载文件
* @param url
*/
downFile(url) {
if (!url) {
return
}
if ($A.Electron) {
$A.Electron.shell.openExternal(url).catch(() => {
$A.modalError("下载失败");
});
} else {
window.open(url)
}
}
});

View File

@ -9,7 +9,7 @@
<div v-else-if="msgData.type === 'loading'" class="dialog-content loading"><Loading/></div>
<!--文件-->
<div v-else-if="msgData.type === 'file'" :class="['dialog-content', msgData.msg.type]">
<a :href="msgData.msg.path" target="_blank">
<div class="dialog-file" @click="downFile">
<img v-if="msgData.msg.type === 'img'" class="file-img" :style="imageStyle(msgData.msg)" :src="msgData.msg.thumb"/>
<div v-else class="file-box">
<img class="file-thumb" :src="msgData.msg.thumb"/>
@ -18,7 +18,7 @@
<div class="file-size">{{$A.bytesToSize(msgData.msg.size)}}</div>
</div>
</div>
</a>
</div>
</div>
<!--未知-->
<div v-else class="dialog-content unknown">{{$L("未知的消息类型")}}</div>
@ -56,6 +56,7 @@
<script>
import WCircle from "../../../components/WCircle";
import {mapState} from "vuex";
export default {
name: "DialogView",
@ -84,6 +85,8 @@ export default {
},
computed: {
...mapState(['userToken']),
readList() {
return this.read_list.filter(({read_at}) => read_at)
},
@ -162,6 +165,17 @@ export default {
};
}
return {};
},
downFile() {
$A.modalConfirm({
title: '下载文件',
content: `${this.msgData.msg.name} (${$A.bytesToSize(this.msgData.msg.size)})`,
okText: '立即下载',
onOk: () => {
$A.downFile($A.apiUrl(`dialog/msg/download?msg_id=${this.msgData.id}&token=${this.userToken}`))
}
});
}
}
}

View File

@ -274,7 +274,7 @@
<li v-for="file in fileList">
<img v-if="file.id" class="file-ext" :src="file.thumb"/>
<Loading v-else class="file-load"/>
<a class="file-name" :href="file.path||'javascript:;'" target="_blank">{{file.name}}</a>
<div class="file-name" @click="downFile(file)">{{file.name}}</div>
<div class="file-size">{{$A.bytesToSize(file.size)}}</div>
<EPopover v-model="file._deling" class="file-delete">
<div class="task-detail-delete-file-popover">
@ -517,6 +517,7 @@ export default {
computed: {
...mapState([
'userId',
'userToken',
'cacheProjects',
'cacheColumns',
'cacheTasks',
@ -1131,6 +1132,17 @@ export default {
}
}, 100);
}
},
downFile(file) {
$A.modalConfirm({
title: '下载文件',
content: `${file.name} (${$A.bytesToSize(file.size)})`,
okText: '立即下载',
onOk: () => {
$A.downFile($A.apiUrl(`project/task/filedown?file_id=${file.id}&token=${this.userToken}`))
}
});
}
}
}

View File

@ -919,16 +919,7 @@ export default {
content: `${item.name}.${item.ext} (${$A.bytesToSize(item.size)})`,
okText: '立即下载',
onOk: () => {
let url = $A.apiUrl(`file/content?id=${item.id}&down=yes&token=${this.userToken}`);
if (this.$Electron) {
try {
this.$Electron.shell.openExternal(url);
} catch (e) {
$A.modalError("下载失败");
}
} else {
window.open(url)
}
$A.downFile($A.apiUrl(`file/content?id=${item.id}&down=yes&token=${this.userToken}`))
}
});
break;

View File

@ -121,6 +121,9 @@
padding: 8px;
min-width: 32px;
border-radius: 6px 6px 6px 0;
.dialog-file {
cursor: pointer;
}
> pre {
display: block;
margin: 0;

View File

@ -251,6 +251,7 @@
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
cursor: pointer;
&:hover {
color: $primary-color;
}