package api import ( "gitee.ltd/lxh/logger/log" "github.com/gin-gonic/gin" "wireguard-dashboard/http/param" "wireguard-dashboard/model/entity" "wireguard-dashboard/queues" "wireguard-dashboard/repository" "wireguard-dashboard/utils" ) type clients struct{} func Client() clients { return clients{} } // List // @description: 客户端列表 // @receiver clients // @param c func (clients) List(c *gin.Context) { var p param.ClientList if err := c.ShouldBind(&p); err != nil { utils.GinResponse(c).FailedWithErr("参数错误", err) return } data, total, err := repository.Client().List(p) if err != nil { utils.GinResponse(c).FailedWithMsg("获取失败") return } utils.GinResponse(c).OkWithPage(data, total, p.Current, p.Size) } // Save // @description: 新增/更新客户端 // @receiver clients // @param c func (clients) Save(c *gin.Context) { var p param.SaveClient if err := c.ShouldBind(&p); err != nil { utils.GinResponse(c).FailedWithErr("参数错误", err) return } info, ok := c.Get("user") if !ok { utils.GinResponse(c).FailedWithMsg("获取信息失败") return } _, err := repository.Client().Save(p, info.(*entity.User).Id) if err != nil { utils.GinResponse(c).FailedWithMsg("操作失败") return } go func() { if err = queues.PutAsyncWireguardConfigFile(p.ServerId); err != nil { log.Errorf("[新增/编辑客户端]同步配置文件失败: %v", err.Error()) } }() utils.GinResponse(c).OK() }