| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- package api
- import (
- "Gwen/global"
- requstform "Gwen/http/request/api"
- "Gwen/http/response"
- "Gwen/http/response/api"
- "Gwen/model"
- "Gwen/service"
- "encoding/json"
- "github.com/gin-gonic/gin"
- "net/http"
- "strconv"
- )
- type Ab struct {
- }
- // Ab
- // @Tags 地址
- // @Summary 地址列表
- // @Description 地址列表
- // @Accept json
- // @Produce json
- // @Success 200 {object} response.Response
- // @Failure 500 {object} response.ErrorResponse
- // @Router /ab [get]
- // @Security BearerAuth
- func (a *Ab) Ab(c *gin.Context) {
- user := service.AllService.UserService.CurUser(c)
- al := service.AllService.AddressBookService.ListByUserId(user.Id, 1, 1000)
- tags := service.AllService.TagService.ListByUserId(user.Id)
- tagColors := map[string]uint{}
- //将tags中的name转成一个以逗号分割的字符串
- var tagNames []string
- for _, tag := range tags.Tags {
- tagNames = append(tagNames, tag.Name)
- tagColors[tag.Name] = tag.Color
- }
- tgc, _ := json.Marshal(tagColors)
- res := &api.AbList{
- Peers: al.AddressBooks,
- Tags: tagNames,
- TagColors: string(tgc),
- }
- data, _ := json.Marshal(res)
- c.JSON(http.StatusOK, gin.H{
- "data": string(data),
- //"licensed_devices": 999,
- })
- }
- // UpAb
- // @Tags 地址
- // @Summary 地址更新
- // @Description 地址更新
- // @Accept json
- // @Produce json
- // @Param body body requstform.AddressBookForm true "地址表单"
- // @Success 200 {string} string "null"
- // @Failure 500 {object} response.ErrorResponse
- // @Router /ab [post]
- // @Security BearerAuth
- func (a *Ab) UpAb(c *gin.Context) {
- abf := &requstform.AddressBookForm{}
- err := c.ShouldBindJSON(&abf)
- if err != nil {
- response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
- return
- }
- abd := &requstform.AddressBookFormData{}
- err = json.Unmarshal([]byte(abf.Data), abd)
- if err != nil {
- response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
- return
- }
- tc := map[string]uint{}
- err = json.Unmarshal([]byte(abd.TagColors), &tc)
- if err != nil {
- response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
- return
- }
- user := service.AllService.UserService.CurUser(c)
- err = service.AllService.AddressBookService.UpdateAddressBook(abd.Peers, user.Id)
- if err != nil {
- response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
- return
- }
- service.AllService.TagService.UpdateTags(user.Id, tc)
- c.JSON(http.StatusOK, nil)
- }
- // Tags
- // @Tags 地址
- // @Summary 标签
- // @Description 标签
- // @Accept json
- // @Produce json
- // @Success 200 {object} []model.Tag
- // @Failure 500 {object} response.ErrorResponse
- // @Router /tags [post]
- // @Security BearerAuth
- func (a *Ab) Tags(c *gin.Context) {
- user := service.AllService.UserService.CurUser(c)
- tags := service.AllService.TagService.ListByUserId(user.Id)
- c.JSON(http.StatusOK, tags.Tags)
- }
- // TagAdd
- // @Tags 地址[Personal]
- // @Summary 标签添加
- // @Description 标签
- // @Accept json
- // @Produce json
- // @Success 200 {string} string
- // @Failure 500 {object} response.ErrorResponse
- // @Router /ab/add [post]
- // @Security BearerAuth
- func (a *Ab) TagAdd(c *gin.Context) {
- t := &model.Tag{}
- err := c.ShouldBindJSON(t)
- if err != nil {
- response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
- 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, response.TranslateMsg(c, "ItemExists"))
- return
- }
- t.UserId = u.Id
- err = service.AllService.TagService.Create(t)
- if err != nil {
- response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
- 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, response.TranslateMsg(c, "ParamsError")+err.Error())
- 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, response.TranslateMsg(c, "ItemNotFound"))
- return
- }
- ntag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.New)
- if ntag != nil && ntag.Id != 0 {
- response.Error(c, response.TranslateMsg(c, "ItemExists"))
- return
- }
- tag.Name = t.New
- err = service.AllService.TagService.Update(tag)
- if err != nil {
- response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
- 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, response.TranslateMsg(c, "ParamsError")+err.Error())
- 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, response.TranslateMsg(c, "ItemNotFound"))
- return
- }
- tag.Color = t.Color
- err = service.AllService.TagService.Update(tag)
- if err != nil {
- response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
- 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, response.TranslateMsg(c, "ParamsError")+err.Error())
- 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, response.TranslateMsg(c, "ItemNotFound"))
- return
- }
- err = service.AllService.TagService.Delete(tag)
- if err != nil {
- response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
- 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, response.TranslateMsg(c, "ParamsError")+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, response.TranslateMsg(c, "OperationFailed")+err.Error())
- 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, response.TranslateMsg(c, "ParamsError")+err.Error())
- 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, response.TranslateMsg(c, "ItemNotFound"))
- return
- }
- err = service.AllService.AddressBookService.Delete(ab)
- if err != nil {
- response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
- 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, response.TranslateMsg(c, "ParamsError")+err.Error())
- 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, response.TranslateMsg(c, "ItemNotFound"))
- return
- }
- nab := f.ToAddressBook()
- nab.RowId = ab.RowId
- err = service.AllService.AddressBookService.Update(nab)
- if err != nil {
- response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
- return
- }
- c.String(http.StatusOK, "")
- }
|