ab.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package api
  2. import (
  3. requstform "Gwen/http/request/api"
  4. "Gwen/http/response"
  5. "Gwen/http/response/api"
  6. "Gwen/model"
  7. "Gwen/service"
  8. "encoding/json"
  9. "github.com/gin-gonic/gin"
  10. "net/http"
  11. )
  12. type Ab struct {
  13. }
  14. // Ab
  15. // @Tags 地址
  16. // @Summary 地址列表
  17. // @Description 地址列表
  18. // @Accept json
  19. // @Produce json
  20. // @Success 200 {object} response.Response
  21. // @Failure 500 {object} response.ErrorResponse
  22. // @Router /ab [get]
  23. // @Security BearerAuth
  24. func (a *Ab) Ab(c *gin.Context) {
  25. user := service.AllService.UserService.CurUser(c)
  26. al := service.AllService.AddressBookService.ListByUserId(user.Id, 1, 1000)
  27. tags := service.AllService.TagService.ListByUserId(user.Id)
  28. tagColors := map[string]uint{}
  29. //将tags中的name转成一个以逗号分割的字符串
  30. var tagNames []string
  31. for _, tag := range tags.Tags {
  32. tagNames = append(tagNames, tag.Name)
  33. tagColors[tag.Name] = tag.Color
  34. }
  35. tgc, _ := json.Marshal(tagColors)
  36. res := &api.AbList{
  37. Peers: al.AddressBooks,
  38. Tags: tagNames,
  39. TagColors: string(tgc),
  40. }
  41. data, _ := json.Marshal(res)
  42. c.JSON(http.StatusOK, gin.H{
  43. "data": string(data),
  44. //"licensed_devices": 999,
  45. })
  46. }
  47. // UpAb
  48. // @Tags 地址
  49. // @Summary 地址更新
  50. // @Description 地址更新
  51. // @Accept json
  52. // @Produce json
  53. // @Param body body requstform.AddressBookForm true "地址表单"
  54. // @Success 200 {string} string "null"
  55. // @Failure 500 {object} response.ErrorResponse
  56. // @Router /ab [post]
  57. // @Security BearerAuth
  58. func (a *Ab) UpAb(c *gin.Context) {
  59. abf := &requstform.AddressBookForm{}
  60. err := c.ShouldBindJSON(&abf)
  61. if err != nil {
  62. response.Error(c, "参数错误")
  63. return
  64. }
  65. abd := &requstform.AddressBookFormData{}
  66. err = json.Unmarshal([]byte(abf.Data), abd)
  67. if err != nil {
  68. response.Error(c, "系统错误")
  69. return
  70. }
  71. //fmt.Println(abd)
  72. //for _, peer := range abd.Peers {
  73. // fmt.Println(peer)
  74. //}
  75. user := service.AllService.UserService.CurUser(c)
  76. err = service.AllService.AddressBookService.UpdateAddressBook(abd.Peers, user.Id)
  77. if err != nil {
  78. c.Abort()
  79. return
  80. }
  81. tc := map[string]uint{}
  82. err = json.Unmarshal([]byte(abd.TagColors), &tc)
  83. if err != nil {
  84. response.Error(c, "系统错误")
  85. return
  86. } else {
  87. service.AllService.TagService.UpdateTags(user.Id, tc)
  88. }
  89. c.JSON(http.StatusOK, nil)
  90. }
  91. // Tags
  92. // @Tags 地址
  93. // @Summary 标签
  94. // @Description 标签
  95. // @Accept json
  96. // @Produce json
  97. // @Success 200 {object} []model.Tag
  98. // @Failure 500 {object} response.ErrorResponse
  99. // @Router /tags [post]
  100. // @Security BearerAuth
  101. func (a *Ab) Tags(c *gin.Context) {
  102. user := service.AllService.UserService.CurUser(c)
  103. tags := service.AllService.TagService.ListByUserId(user.Id)
  104. c.JSON(http.StatusOK, tags.Tags)
  105. }
  106. // TagAdd
  107. // @Tags 地址
  108. // @Summary 标签添加
  109. // @Description 标签
  110. // @Accept json
  111. // @Produce json
  112. // @Success 200 {string} string
  113. // @Failure 500 {object} response.ErrorResponse
  114. // @Router /ab/add [post]
  115. // @Security BearerAuth
  116. func (a *Ab) TagAdd(c *gin.Context) {
  117. t := &model.Tag{}
  118. err := c.ShouldBindJSON(t)
  119. if err != nil {
  120. response.Error(c, "参数错误")
  121. return
  122. }
  123. //u := service.AllService.UserService.CurUser(c)
  124. //err = service.AllService.TagService.UpdateTags(t.Name, t.Color, user.Id)
  125. //if err != nil {
  126. // response.Error(c, "操作失败")
  127. // return
  128. //}
  129. c.JSON(http.StatusOK, "")
  130. }