ab.go 3.3 KB

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