mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 08:20:25 +08:00 
			
		
		
		
	feat: linux文件上传成功通知调整
This commit is contained in:
		@@ -2,7 +2,9 @@ package ws
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"mayfly-go/base/global"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/gorilla/websocket"
 | 
			
		||||
)
 | 
			
		||||
@@ -17,11 +19,43 @@ var Upgrader = websocket.Upgrader{
 | 
			
		||||
 | 
			
		||||
var conns = make(map[uint64]*websocket.Conn, 100)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	checkConn()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 放置ws连接
 | 
			
		||||
func Put(userId uint64, conn *websocket.Conn) {
 | 
			
		||||
	Delete(userId)
 | 
			
		||||
	conns[userId] = conn
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func checkConn() {
 | 
			
		||||
	heartbeat := time.Duration(20) * time.Second
 | 
			
		||||
	tick := time.NewTicker(heartbeat)
 | 
			
		||||
	go func() {
 | 
			
		||||
		for range tick.C {
 | 
			
		||||
			// 遍历所有连接,ping失败的则删除掉
 | 
			
		||||
			for uid, conn := range conns {
 | 
			
		||||
				err := conn.WriteControl(websocket.PingMessage, []byte("ping"), time.Now().Add(heartbeat/2))
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					global.Log.Info("删除ping失败的websocket连接:uid = ", uid)
 | 
			
		||||
					Delete(uid)
 | 
			
		||||
					return
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 删除ws连接
 | 
			
		||||
func Delete(userid uint64) {
 | 
			
		||||
	conn := conns[userid]
 | 
			
		||||
	if conn != nil {
 | 
			
		||||
		conn.Close()
 | 
			
		||||
		delete(conns, userid)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 对指定用户发送消息
 | 
			
		||||
func SendMsg(userId uint64, msg *Msg) {
 | 
			
		||||
	conn := conns[userId]
 | 
			
		||||
 
 | 
			
		||||
@@ -34,6 +34,7 @@ export default {
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
                ElNotification({
 | 
			
		||||
                    duration: 0,
 | 
			
		||||
                    title: message.title,
 | 
			
		||||
                    message: message.msg,
 | 
			
		||||
                    type: mtype as any,
 | 
			
		||||
 
 | 
			
		||||
@@ -79,6 +79,7 @@
 | 
			
		||||
                                />
 | 
			
		||||
 | 
			
		||||
                                <el-upload
 | 
			
		||||
                                :before-upload="beforeUpload"
 | 
			
		||||
                                    :on-success="uploadSuccess"
 | 
			
		||||
                                    :headers="{ token }"
 | 
			
		||||
                                    :data="{
 | 
			
		||||
@@ -456,6 +457,10 @@ export default defineComponent({
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        const beforeUpload = (file: File) => {
 | 
			
		||||
            ElMessage.success(`'${file.name}' 上传中,请关注结果通知`)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const dontOperate = (data: any) => {
 | 
			
		||||
            const path = data.path;
 | 
			
		||||
            const ls = [
 | 
			
		||||
@@ -518,6 +523,7 @@ export default defineComponent({
 | 
			
		||||
            deleteFile,
 | 
			
		||||
            downloadFile,
 | 
			
		||||
            getUploadFile,
 | 
			
		||||
            beforeUpload,
 | 
			
		||||
            uploadSuccess,
 | 
			
		||||
            dontOperate,
 | 
			
		||||
            formatFileSize,
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,6 @@ import (
 | 
			
		||||
	"mayfly-go/base/ctx"
 | 
			
		||||
	"mayfly-go/base/ginx"
 | 
			
		||||
	"mayfly-go/base/utils"
 | 
			
		||||
	"mayfly-go/base/ws"
 | 
			
		||||
	"mayfly-go/server/devops/api/form"
 | 
			
		||||
	"mayfly-go/server/devops/api/vo"
 | 
			
		||||
	"mayfly-go/server/devops/application"
 | 
			
		||||
@@ -120,10 +119,9 @@ func (m *MachineFile) UploadFile(rc *ctx.ReqCtx) {
 | 
			
		||||
	g := rc.GinCtx
 | 
			
		||||
	fid := GetMachineFileId(g)
 | 
			
		||||
	path := g.PostForm("path")
 | 
			
		||||
 | 
			
		||||
	fileheader, err := g.FormFile("file")
 | 
			
		||||
	biz.ErrIsNilAppendErr(err, "读取文件失败: %s")
 | 
			
		||||
	// 通知正在上传
 | 
			
		||||
	ws.SendMsg(rc.LoginAccount.Id, ws.NewMsg("文件上传", "文件上传中..."))
 | 
			
		||||
 | 
			
		||||
	file, _ := fileheader.Open()
 | 
			
		||||
	bytes, _ := ioutil.ReadAll(file)
 | 
			
		||||
 
 | 
			
		||||
@@ -151,7 +151,7 @@ func (m *machineFileAppImpl) UploadFile(la *model.LoginAccount, fileId uint64, p
 | 
			
		||||
 | 
			
		||||
	createfile.Write(content)
 | 
			
		||||
	// 保存消息并发送文件上传成功通知
 | 
			
		||||
	m.msgApp.CreateAndSend(la, ws.SuccessMsg("文件上传", fmt.Sprintf("[%s]文件已成功上传至[%s]", filename, path)))
 | 
			
		||||
	m.msgApp.CreateAndSend(la, ws.SuccessMsg("文件上传成功", fmt.Sprintf("[%s]文件已成功上传至[%s]", filename, path)))
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -30,6 +30,7 @@ func (s *System) ConnectWs(g *gin.Context) {
 | 
			
		||||
	if err = ctx.PermissionHandler(rc); err != nil {
 | 
			
		||||
		panic(biz.NewBizErr("没有权限"))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 登录账号信息
 | 
			
		||||
	la := rc.LoginAccount
 | 
			
		||||
	ws.Put(la.Id, wsConn)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user