add personal apis

This commit is contained in:
ljw
2024-09-24 14:43:27 +08:00
parent a161e3ba10
commit c485035e74
21 changed files with 1099 additions and 168 deletions

View File

@@ -1,14 +1,17 @@
package api
import (
"Gwen/global"
requstform "Gwen/http/request/api"
"Gwen/http/response"
"Gwen/http/response/api"
"Gwen/model"
"Gwen/service"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)
type Ab struct {
@@ -118,7 +121,7 @@ func (a *Ab) Tags(c *gin.Context) {
}
// TagAdd
// @Tags 地址
// @Tags 地址[Personal]
// @Summary 标签添加
// @Description 标签
// @Accept json
@@ -133,14 +136,359 @@ func (a *Ab) TagAdd(c *gin.Context) {
if err != nil {
response.Error(c, "参数错误")
return
}
//u := service.AllService.UserService.CurUser(c)
//err = service.AllService.TagService.UpdateTags(t.Name, t.Color, user.Id)
//if err != nil {
// response.Error(c, "操作失败")
// return
//}
c.JSON(http.StatusOK, "")
u := service.AllService.UserService.CurUser(c)
tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.Name)
if tag != nil && tag.Id != 0 {
response.Error(c, "已存在")
return
}
t.UserId = u.Id
err = service.AllService.TagService.Create(t)
if err != nil {
response.Error(c, "操作失败")
return
}
c.String(http.StatusOK, "")
}
// TagRename
// @Tags 地址[Personal]
// @Summary 标签重命名
// @Description 标签
// @Accept json
// @Produce json
// @Success 200 {string} string
// @Failure 500 {object} response.ErrorResponse
// @Router /ab/tag/rename/{guid} [put]
// @Security BearerAuth
func (a *Ab) TagRename(c *gin.Context) {
t := &requstform.TagRenameForm{}
err := c.ShouldBindJSON(t)
if err != nil {
response.Error(c, "参数错误")
return
}
u := service.AllService.UserService.CurUser(c)
tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.Old)
if tag == nil || tag.Id == 0 {
response.Error(c, "参数错误")
return
}
ntag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.New)
if ntag != nil && ntag.Id != 0 {
response.Error(c, "已存在")
return
}
tag.Name = t.New
err = service.AllService.TagService.Update(tag)
if err != nil {
response.Error(c, "操作失败")
return
}
c.String(http.StatusOK, "")
}
// TagUpdate
// @Tags 地址[Personal]
// @Summary 标签修改颜色
// @Description 标签
// @Accept json
// @Produce json
// @Success 200 {string} string
// @Failure 500 {object} response.ErrorResponse
// @Router /ab/tag/update/{guid} [put]
// @Security BearerAuth
func (a *Ab) TagUpdate(c *gin.Context) {
t := &requstform.TagColorForm{}
err := c.ShouldBindJSON(t)
if err != nil {
response.Error(c, "参数错误")
return
}
u := service.AllService.UserService.CurUser(c)
tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.Name)
if tag == nil || tag.Id == 0 {
response.Error(c, "参数错误")
return
}
tag.Color = t.Color
err = service.AllService.TagService.Update(tag)
if err != nil {
response.Error(c, "操作失败")
return
}
c.String(http.StatusOK, "")
}
// TagDel
// @Tags 地址[Personal]
// @Summary 标签删除
// @Description 标签
// @Accept json
// @Produce json
// @Success 200 {string} string
// @Failure 500 {object} response.ErrorResponse
// @Router /ab/tag/{guid} [delete]
// @Security BearerAuth
func (a *Ab) TagDel(c *gin.Context) {
t := &[]string{}
err := c.ShouldBind(t)
if err != nil {
response.Error(c, "参数错误")
return
}
//fmt.Println(t)
u := service.AllService.UserService.CurUser(c)
for _, name := range *t {
tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, name)
if tag == nil || tag.Id == 0 {
response.Error(c, "参数错误")
return
}
err = service.AllService.TagService.Delete(tag)
if err != nil {
response.Error(c, "操作失败")
return
}
}
c.String(http.StatusOK, "")
}
// Personal
// @Tags 地址[Personal]
// @Summary 个人地址
// @Description 个人地址
// @Accept json
// @Produce json
// @Param string body string false "string valid"
// @Success 200 {object} response.Response
// @Failure 500 {object} response.Response
// @Router /ab/personal [post]
// @Security BearerAuth
func (a *Ab) Personal(c *gin.Context) {
user := service.AllService.UserService.CurUser(c)
/**
guid = json['guid'] ?? '',
name = json['name'] ?? '',
owner = json['owner'] ?? '',
note = json['note'] ?? '',
rule = json['rule'] ?? 0;
*/
if global.Config.Rustdesk.Personal == 1 {
guid := strconv.Itoa(int(user.GroupId)) + "-" + strconv.Itoa(int(user.Id))
//如果返回了guid后面的请求会有变化
c.JSON(http.StatusOK, gin.H{
"guid": guid,
"name": user.Username,
"rule": 0,
})
} else {
c.JSON(http.StatusOK, nil)
}
}
// Settings
// @Tags 地址[Personal]
// @Summary 设置
// @Description 设置
// @Accept json
// @Produce json
// @Param string body string false "string valid"
// @Success 200 {object} response.Response
// @Failure 500 {object} response.Response
// @Router /ab/settings [post]
// @Security BearerAuth
func (a *Ab) Settings(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"max_peer_one_ab": 0, //最大peer数0表示不限制
})
}
// SharedProfiles
// @Tags 地址[Personal]
// @Summary 共享地址簿
// @Description 共享
// @Accept json
// @Produce json
// @Param string body string false "string valid"
// @Success 200 {object} response.Response
// @Failure 500 {object} response.Response
// @Router /ab/shared/profiles [post]
// @Security BearerAuth
func (a *Ab) SharedProfiles(c *gin.Context) {
//AbProfile.fromJson(Map<String, dynamic> json)
//: guid = json['guid'] ?? '',
// name = json['name'] ?? '',
// owner = json['owner'] ?? '',
// note = json['note'] ?? '',
// rule = json['rule'] ?? 0;
//暂时没必要返回数据,可能是为了共享地址簿
/*item := map[string]interface{}{
"guid": "1",
"name": "admin",
"owner": "admin",
"note": "admin11",
"rule": 0,
}
item2 := map[string]interface{}{
"guid": "2",
"name": "admin2",
"owner": "admin2",
"note": "admin22",
"rule": 0,
}
c.JSON(http.StatusOK, gin.H{
"total": 2,
"data": []interface{}{item, item2},
})*/
c.JSON(http.StatusOK, gin.H{
"total": 0,
"data": nil,
})
}
// Peers
// @Tags 地址[Personal]
// @Summary 地址列表
// @Description 地址
// @Accept json
// @Produce json
// @Param string body string false "string valid"
// @Success 200 {object} response.Response
// @Failure 500 {object} response.Response
// @Router /ab/peers [post]
// @Security BearerAuth
func (a *Ab) Peers(c *gin.Context) {
user := service.AllService.UserService.CurUser(c)
al := service.AllService.AddressBookService.ListByUserId(user.Id, 1, 1000)
c.JSON(http.StatusOK, gin.H{
"total": al.Total,
"data": al.AddressBooks,
"licensed_devices": 99999,
})
}
// PTags
// @Tags 地址[Personal]
// @Summary 标签
// @Description 标签
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Success 200 {object} model.TagList
// @Failure 500 {object} response.ErrorResponse
// @Router /ab/tags/{guid} [post]
// @Security BearerAuth
func (a *Ab) PTags(c *gin.Context) {
user := service.AllService.UserService.CurUser(c)
tags := service.AllService.TagService.ListByUserId(user.Id)
c.JSON(http.StatusOK, tags.Tags)
}
// PeerAdd
// @Tags 地址[Personal]
// @Summary 添加地址
// @Description 添加地址
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Success 200 {string} string
// @Failure 500 {object} response.ErrorResponse
// @Router /ab/peer/add/{guid} [post]
// @Security BearerAuth
func (a *Ab) PeerAdd(c *gin.Context) {
// forceAlwaysRelay永远是字符串"false",真是坑
//f := &gin.H{}
f := &requstform.PersonalAddressBookForm{}
err := c.ShouldBindJSON(f)
if err != nil {
response.Error(c, "参数错误"+err.Error())
return
}
fmt.Println(f)
u := service.AllService.UserService.CurUser(c)
f.UserId = u.Id
ab := f.ToAddressBook()
err = service.AllService.AddressBookService.AddAddressBook(ab)
if err != nil {
response.Error(c, "操作失败")
return
}
c.String(http.StatusOK, "")
}
// PeerDel
// @Tags 地址[Personal]
// @Summary 删除地址
// @Description 删除地址
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Success 200 {string} string
// @Failure 500 {object} response.ErrorResponse
// @Router /ab/peer/add/{guid} [delete]
// @Security BearerAuth
func (a *Ab) PeerDel(c *gin.Context) {
f := &[]string{}
err := c.ShouldBind(f)
if err != nil {
response.Error(c, "参数错误")
return
}
u := service.AllService.UserService.CurUser(c)
for _, id := range *f {
ab := service.AllService.AddressBookService.InfoByUserIdAndId(u.Id, id)
if ab == nil || ab.RowId == 0 {
response.Error(c, "参数错误")
return
}
err = service.AllService.AddressBookService.Delete(ab)
if err != nil {
response.Error(c, "操作失败")
return
}
}
c.String(http.StatusOK, "")
}
// PeerUpdate
// @Tags 地址[Personal]
// @Summary 更新地址
// @Description 更新地址
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Success 200 {string} string
// @Failure 500 {object} response.ErrorResponse
// @Router /ab/peer/update/{guid} [put]
// @Security BearerAuth
func (a *Ab) PeerUpdate(c *gin.Context) {
//f := &gin.H{}
f := &requstform.PersonalAddressBookForm{}
err := c.ShouldBindJSON(f)
if err != nil {
response.Error(c, "参数错误")
return
}
fmt.Println(f)
//return
u := service.AllService.UserService.CurUser(c)
ab := service.AllService.AddressBookService.InfoByUserIdAndId(u.Id, f.Id)
if ab == nil || ab.RowId == 0 {
response.Error(c, "参数错误")
return
}
nab := f.ToAddressBook()
nab.RowId = ab.RowId
err = service.AllService.AddressBookService.Update(nab)
if err != nil {
response.Error(c, "操作失败")
return
}
c.String(http.StatusOK, "")
}

View File

@@ -264,16 +264,3 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
c.String(http.StatusInternalServerError, "授权配置错误,请联系管理员")
}
// WebOauthLogin
// @Tags Oauth
// @Summary WebOauthLogin
// @Description WebOauthLogin
// @Accept json
// @Produce json
// @Success 200 {string} string
// @Failure 500 {string} string
// @Router /oauth/login [get]
func (o *Oauth) WebOauthLogin(c *gin.Context) {
}

View File

@@ -3,7 +3,6 @@ package api
import (
apiResp "Gwen/http/response/api"
"Gwen/service"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
@@ -42,33 +41,3 @@ func (u *User) Info(c *gin.Context) {
up := (&apiResp.UserPayload{}).FromUser(user)
c.JSON(http.StatusOK, up)
}
// Personal
// @Tags 用户
// @Summary 个人信息
// @Description 个人信息
// @Accept json
// @Produce json
// @Param string body string false "string valid"
// @Success 200 {object} response.Response
// @Failure 500 {object} response.Response
// @Router /ab/personal [post]
// @Security BearerAuth
func (u *User) Personal(c *gin.Context) {
//打印全部body
fmt.Println(c.Request.Body)
/**
guid = json['guid'] ?? '',
name = json['name'] ?? '',
owner = json['owner'] ?? '',
note = json['note'] ?? '',
rule = json['rule'] ?? 0;
*/
//如果返回了guid后面的请求会有变化
c.JSON(http.StatusOK, gin.H{
//"guid": "123456",
//"name": "admindddd",
//"rule": 1,
})
}

View File

@@ -8,6 +8,10 @@ import (
type Index struct {
}
func (i *Index) Index(c *gin.Context) {
c.Redirect(302, "/_admin/")
}
func (i *Index) ConfigJs(c *gin.Context) {
apiServer := global.Config.Rustdesk.ApiServer

View File

@@ -16,7 +16,7 @@ type AddressBookForm struct {
Tags []string `json:"tags"`
Hash string `json:"hash"`
UserId uint `json:"user_id"`
ForceAlwaysRelay bool `json:"force_always_relay"`
ForceAlwaysRelay bool `json:"forceAlwaysRelay"`
RdpPort string `json:"rdp_port"`
RdpUsername string `json:"rdp_username"`
Online bool `json:"online"`

View File

@@ -35,3 +35,39 @@ func (pf *PeerForm) ToPeer() *model.Peer {
Version: pf.Version,
}
}
// PersonalAddressBookForm 个人地址簿表单
type PersonalAddressBookForm struct {
model.AddressBook
ForceAlwaysRelay string `json:"forceAlwaysRelay"`
}
func (pabf *PersonalAddressBookForm) ToAddressBook() *model.AddressBook {
return &model.AddressBook{
RowId: pabf.RowId,
Id: pabf.Id,
Username: pabf.Username,
Password: pabf.Password,
Hostname: pabf.Hostname,
Alias: pabf.Alias,
Platform: pabf.Platform,
Tags: pabf.Tags,
Hash: pabf.Hash,
UserId: pabf.UserId,
ForceAlwaysRelay: pabf.ForceAlwaysRelay == "true",
RdpPort: pabf.RdpPort,
RdpUsername: pabf.RdpUsername,
Online: pabf.Online,
LoginName: pabf.LoginName,
SameServer: pabf.SameServer,
}
}
type TagRenameForm struct {
Old string `json:"old"`
New string `json:"new"`
}
type TagColorForm struct {
Name string `json:"name"`
Color uint `json:"color"`
}

View File

@@ -59,16 +59,13 @@ func (gpp *GroupPeerPayload) FromAddressBook(a *model.AddressBook, username stri
gpp.UserName = username
}
//func (gpp *GroupPeerPayload) FromPeer(p *model.Peer) {
// gpp.Id = p.Id
// gpp.Info = &PeerPayloadInfo{
// DeviceName: p.Hostname,
// Os: p.Os,
// Username: p.Username,
// }
// gpp.Note = ""
// if p.User.Id != 0 {
// //gpp.User = p.User.Username
// gpp.UserName = p.User.Username
// }
//}
func (gpp *GroupPeerPayload) FromPeer(p *model.Peer) {
gpp.Id = p.Id
gpp.Info = &PeerPayloadInfo{
DeviceName: p.Hostname,
Os: p.Os,
Username: p.Username,
}
gpp.Note = ""
gpp.UserName = p.User.Username
}

View File

@@ -74,7 +74,38 @@ func ApiInit(g *gin.Engine) {
//更新地址
frg.POST("/ab", ab.UpAb)
}
PersonalRoutes(frg)
//访问静态文件
g.StaticFS("/upload", http.Dir(global.Config.Gin.ResourcesPath+"/public/upload"))
}
func PersonalRoutes(frg *gin.RouterGroup) {
{
ab := &api.Ab{}
frg.POST("/ab/personal", ab.Personal)
//[method:POST] [uri:/api/ab/settings] Request
frg.POST("/ab/settings", ab.Settings)
// [method:POST] [uri:/api/ab/shared/profiles?current=1&pageSize=100]
frg.POST("/ab/shared/profiles", ab.SharedProfiles)
//[method:POST] [uri:/api/ab/peers?current=1&pageSize=100&ab=1]
frg.POST("/ab/peers", ab.Peers)
// [method:POST] [uri:/api/ab/tags/1]
frg.POST("/ab/tags/:guid", ab.PTags)
//[method:POST] api/ab/peer/add/1
frg.POST("/ab/peer/add/:guid", ab.PeerAdd)
//[method:DELETE] [uri:/api/ab/peer/1]
frg.DELETE("/ab/peer/:guid", ab.PeerDel)
//[method:PUT] [uri:/api/ab/peer/update/1]
frg.PUT("/ab/peer/update/:guid", ab.PeerUpdate)
//[method:POST] [uri:/api/ab/tag/add/1]
frg.POST("/ab/tag/add/:guid", ab.TagAdd)
//[method:PUT] [uri:/api/ab/tag/rename/1]
frg.PUT("/ab/tag/rename/:guid", ab.TagRename)
//[method:PUT] [uri:/api/ab/tag/update/1]
frg.PUT("/ab/tag/update/:guid", ab.TagUpdate)
//[method:DELETE] [uri:/api/ab/tag/1]
frg.DELETE("/ab/tag/:guid", ab.TagDel)
}
}

View File

@@ -9,6 +9,7 @@ import (
func WebInit(g *gin.Engine) {
i := &web.Index{}
g.GET("/", i.Index)
g.GET("/webclient-config/index.js", i.ConfigJs)
g.StaticFS("/webclient", http.Dir(global.Config.Gin.ResourcesPath+"/web"))
g.StaticFS("/_admin", http.Dir(global.Config.Gin.ResourcesPath+"/admin"))