mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2026-04-22 22:57:15 +08:00
ws需要登陆鉴权
This commit is contained in:
@@ -97,7 +97,8 @@ import { getWorkbench } from '@/api/app'
|
||||
import '@/utils/echart'
|
||||
import vCharts from 'vue-echarts'
|
||||
import feedback from '@/utils/feedback'
|
||||
|
||||
import useUserStore from '@/stores/modules/user'
|
||||
const userStore = useUserStore()
|
||||
defineOptions({
|
||||
name: 'workbench'
|
||||
})
|
||||
@@ -178,7 +179,7 @@ function updateChart(val) {
|
||||
// }, 1000)
|
||||
}
|
||||
// // 用户 A,加入 room1
|
||||
const wsA = new WebSocket('ws://localhost:8080/api/ws?uid=userA&room=room1')
|
||||
const wsA = new WebSocket(`ws://localhost:8080/api/ws?token=${userStore.token}&room=room1`)
|
||||
|
||||
// // 用户 B,加入 room1
|
||||
// const wsB = new WebSocket('ws://localhost:8080/api/ws?uid=userB&room=room1')
|
||||
|
||||
@@ -3,6 +3,8 @@ package controller
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"x_admin/config"
|
||||
"x_admin/core"
|
||||
"x_admin/util"
|
||||
"x_admin/util/ws_util"
|
||||
@@ -17,13 +19,20 @@ var upgrader = websocket.Upgrader{
|
||||
},
|
||||
}
|
||||
|
||||
// @Summary websocket连接
|
||||
// @Tags 公共接口
|
||||
// @Router /api/ws [get]
|
||||
// @Param token header string true "token"
|
||||
// @Param uid query string true "用户ID"
|
||||
// @Param room query string true "房间ID"
|
||||
// @Schemes ws
|
||||
func WsHandler(c *gin.Context) {
|
||||
uuid := util.ToolsUtil.MakeUuidV7()
|
||||
// 从查询参数获取用户ID和房间ID(实际项目中应通过认证获取)
|
||||
uid := c.Query("uid")
|
||||
var adminId = config.AdminConfig.GetAdminId(c)
|
||||
roomID := c.Query("room")
|
||||
if uid == "" {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "id is required"})
|
||||
if adminId == 0 {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "adminId is required"})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -33,7 +42,7 @@ func WsHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
client := ws_util.NewClient(uuid, uid, roomID, conn, core.Ws)
|
||||
client := ws_util.NewClient(uuid, strconv.Itoa(int(adminId)), roomID, conn, core.Ws)
|
||||
core.Ws.Register <- client
|
||||
|
||||
// 启动读写协程
|
||||
|
||||
@@ -139,6 +139,7 @@ func TokenAuth() gin.HandlerFunc {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
// 超管权限
|
||||
if config.AdminConfig.GetAdminId(c) == config.AdminConfig.SuperAdminId {
|
||||
c.Next()
|
||||
return
|
||||
|
||||
+36
-4
@@ -1,18 +1,23 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"x_admin/app/controller"
|
||||
"x_admin/app/controller/admin_ctl/commonController"
|
||||
"x_admin/config"
|
||||
"x_admin/core/response"
|
||||
"x_admin/docs"
|
||||
"x_admin/middleware"
|
||||
"x_admin/routes/adminRoute"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterRoute(api *gin.RouterGroup, rootRouter *gin.Engine) {
|
||||
// @Summary 获取所有接口
|
||||
// @Tags 公共接口
|
||||
// @Router /api/admin/apiList [get]
|
||||
func apiList(api *gin.RouterGroup, rootRouter *gin.Engine) {
|
||||
|
||||
// /api/admin/apiList 获取所有接口
|
||||
api.GET("/admin/apiList", middleware.TokenAuth(), func(ctx *gin.Context) {
|
||||
var path = []string{}
|
||||
for _, route := range rootRouter.Routes() {
|
||||
@@ -21,8 +26,35 @@ func RegisterRoute(api *gin.RouterGroup, rootRouter *gin.Engine) {
|
||||
}
|
||||
response.Result(ctx, response.Success, path)
|
||||
})
|
||||
// /api/ws websocket
|
||||
api.GET("/ws", controller.WsHandler)
|
||||
}
|
||||
|
||||
// @Summary swagger文档数据
|
||||
// @Tags 公共接口
|
||||
// @Router /swagger/doc.json [get]
|
||||
func swaggerJson(api *gin.RouterGroup) {
|
||||
api.GET("/swagger/doc.json", func(c *gin.Context) {
|
||||
// 获取域名和端口号
|
||||
host := c.Request.Host
|
||||
// port := c.Request.Port
|
||||
// docs.SwaggerInfo.Host = fmt.Sprintf("%v:%v", host, config.AppConfig.Port)
|
||||
docs.SwaggerInfo.Host = fmt.Sprintf("%v", host)
|
||||
docs.SwaggerInfo.Title = config.AppConfig.AppName
|
||||
docs.SwaggerInfo.Version = config.AppConfig.Version
|
||||
c.String(200, docs.SwaggerInfo.ReadDoc())
|
||||
})
|
||||
}
|
||||
|
||||
func wsHandler(api *gin.RouterGroup) {
|
||||
api.GET("/ws", middleware.LoginAuth(), controller.WsHandler)
|
||||
}
|
||||
|
||||
func RegisterRoute(api *gin.RouterGroup, rootRouter *gin.Engine) {
|
||||
|
||||
apiList(api, rootRouter)
|
||||
|
||||
swaggerJson(api)
|
||||
|
||||
wsHandler(api)
|
||||
// /api/admin
|
||||
adminRoute.RegisterRoute(api)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user