ab.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. package api
  2. import (
  3. "Gwen/global"
  4. requstform "Gwen/http/request/api"
  5. "Gwen/http/response"
  6. "Gwen/http/response/api"
  7. "Gwen/model"
  8. "Gwen/service"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "net/http"
  13. "strconv"
  14. )
  15. type Ab struct {
  16. }
  17. // Ab
  18. // @Tags 地址
  19. // @Summary 地址列表
  20. // @Description 地址列表
  21. // @Accept json
  22. // @Produce json
  23. // @Success 200 {object} response.Response
  24. // @Failure 500 {object} response.ErrorResponse
  25. // @Router /ab [get]
  26. // @Security BearerAuth
  27. func (a *Ab) Ab(c *gin.Context) {
  28. user := service.AllService.UserService.CurUser(c)
  29. al := service.AllService.AddressBookService.ListByUserId(user.Id, 1, 1000)
  30. tags := service.AllService.TagService.ListByUserId(user.Id)
  31. tagColors := map[string]uint{}
  32. //将tags中的name转成一个以逗号分割的字符串
  33. var tagNames []string
  34. for _, tag := range tags.Tags {
  35. tagNames = append(tagNames, tag.Name)
  36. tagColors[tag.Name] = tag.Color
  37. }
  38. tgc, _ := json.Marshal(tagColors)
  39. res := &api.AbList{
  40. Peers: al.AddressBooks,
  41. Tags: tagNames,
  42. TagColors: string(tgc),
  43. }
  44. data, _ := json.Marshal(res)
  45. c.JSON(http.StatusOK, gin.H{
  46. "data": string(data),
  47. //"licensed_devices": 999,
  48. })
  49. }
  50. // UpAb
  51. // @Tags 地址
  52. // @Summary 地址更新
  53. // @Description 地址更新
  54. // @Accept json
  55. // @Produce json
  56. // @Param body body requstform.AddressBookForm true "地址表单"
  57. // @Success 200 {string} string "null"
  58. // @Failure 500 {object} response.ErrorResponse
  59. // @Router /ab [post]
  60. // @Security BearerAuth
  61. func (a *Ab) UpAb(c *gin.Context) {
  62. abf := &requstform.AddressBookForm{}
  63. err := c.ShouldBindJSON(&abf)
  64. if err != nil {
  65. response.Error(c, "参数错误")
  66. return
  67. }
  68. abd := &requstform.AddressBookFormData{}
  69. err = json.Unmarshal([]byte(abf.Data), abd)
  70. if err != nil {
  71. response.Error(c, "系统错误")
  72. return
  73. }
  74. //fmt.Println(abd)
  75. //for _, peer := range abd.Peers {
  76. // fmt.Println(peer)
  77. //}
  78. user := service.AllService.UserService.CurUser(c)
  79. err = service.AllService.AddressBookService.UpdateAddressBook(abd.Peers, user.Id)
  80. if err != nil {
  81. c.Abort()
  82. return
  83. }
  84. tc := map[string]uint{}
  85. err = json.Unmarshal([]byte(abd.TagColors), &tc)
  86. if err != nil {
  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 地址[Personal]
  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. response.Error(c, "参数错误")
  124. return
  125. }
  126. u := service.AllService.UserService.CurUser(c)
  127. tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.Name)
  128. if tag != nil && tag.Id != 0 {
  129. response.Error(c, "已存在")
  130. return
  131. }
  132. t.UserId = u.Id
  133. err = service.AllService.TagService.Create(t)
  134. if err != nil {
  135. response.Error(c, "操作失败")
  136. return
  137. }
  138. c.String(http.StatusOK, "")
  139. }
  140. // TagRename
  141. // @Tags 地址[Personal]
  142. // @Summary 标签重命名
  143. // @Description 标签
  144. // @Accept json
  145. // @Produce json
  146. // @Success 200 {string} string
  147. // @Failure 500 {object} response.ErrorResponse
  148. // @Router /ab/tag/rename/{guid} [put]
  149. // @Security BearerAuth
  150. func (a *Ab) TagRename(c *gin.Context) {
  151. t := &requstform.TagRenameForm{}
  152. err := c.ShouldBindJSON(t)
  153. if err != nil {
  154. response.Error(c, "参数错误")
  155. return
  156. }
  157. u := service.AllService.UserService.CurUser(c)
  158. tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.Old)
  159. if tag == nil || tag.Id == 0 {
  160. response.Error(c, "参数错误")
  161. return
  162. }
  163. ntag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.New)
  164. if ntag != nil && ntag.Id != 0 {
  165. response.Error(c, "已存在")
  166. return
  167. }
  168. tag.Name = t.New
  169. err = service.AllService.TagService.Update(tag)
  170. if err != nil {
  171. response.Error(c, "操作失败")
  172. return
  173. }
  174. c.String(http.StatusOK, "")
  175. }
  176. // TagUpdate
  177. // @Tags 地址[Personal]
  178. // @Summary 标签修改颜色
  179. // @Description 标签
  180. // @Accept json
  181. // @Produce json
  182. // @Success 200 {string} string
  183. // @Failure 500 {object} response.ErrorResponse
  184. // @Router /ab/tag/update/{guid} [put]
  185. // @Security BearerAuth
  186. func (a *Ab) TagUpdate(c *gin.Context) {
  187. t := &requstform.TagColorForm{}
  188. err := c.ShouldBindJSON(t)
  189. if err != nil {
  190. response.Error(c, "参数错误")
  191. return
  192. }
  193. u := service.AllService.UserService.CurUser(c)
  194. tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.Name)
  195. if tag == nil || tag.Id == 0 {
  196. response.Error(c, "参数错误")
  197. return
  198. }
  199. tag.Color = t.Color
  200. err = service.AllService.TagService.Update(tag)
  201. if err != nil {
  202. response.Error(c, "操作失败")
  203. return
  204. }
  205. c.String(http.StatusOK, "")
  206. }
  207. // TagDel
  208. // @Tags 地址[Personal]
  209. // @Summary 标签删除
  210. // @Description 标签
  211. // @Accept json
  212. // @Produce json
  213. // @Success 200 {string} string
  214. // @Failure 500 {object} response.ErrorResponse
  215. // @Router /ab/tag/{guid} [delete]
  216. // @Security BearerAuth
  217. func (a *Ab) TagDel(c *gin.Context) {
  218. t := &[]string{}
  219. err := c.ShouldBind(t)
  220. if err != nil {
  221. response.Error(c, "参数错误")
  222. return
  223. }
  224. //fmt.Println(t)
  225. u := service.AllService.UserService.CurUser(c)
  226. for _, name := range *t {
  227. tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, name)
  228. if tag == nil || tag.Id == 0 {
  229. response.Error(c, "参数错误")
  230. return
  231. }
  232. err = service.AllService.TagService.Delete(tag)
  233. if err != nil {
  234. response.Error(c, "操作失败")
  235. return
  236. }
  237. }
  238. c.String(http.StatusOK, "")
  239. }
  240. // Personal
  241. // @Tags 地址[Personal]
  242. // @Summary 个人地址
  243. // @Description 个人地址
  244. // @Accept json
  245. // @Produce json
  246. // @Param string body string false "string valid"
  247. // @Success 200 {object} response.Response
  248. // @Failure 500 {object} response.Response
  249. // @Router /ab/personal [post]
  250. // @Security BearerAuth
  251. func (a *Ab) Personal(c *gin.Context) {
  252. user := service.AllService.UserService.CurUser(c)
  253. /**
  254. guid = json['guid'] ?? '',
  255. name = json['name'] ?? '',
  256. owner = json['owner'] ?? '',
  257. note = json['note'] ?? '',
  258. rule = json['rule'] ?? 0;
  259. */
  260. if global.Config.Rustdesk.Personal == 1 {
  261. guid := strconv.Itoa(int(user.GroupId)) + "-" + strconv.Itoa(int(user.Id))
  262. //如果返回了guid,后面的请求会有变化
  263. c.JSON(http.StatusOK, gin.H{
  264. "guid": guid,
  265. "name": user.Username,
  266. "rule": 0,
  267. })
  268. } else {
  269. c.JSON(http.StatusOK, nil)
  270. }
  271. }
  272. // Settings
  273. // @Tags 地址[Personal]
  274. // @Summary 设置
  275. // @Description 设置
  276. // @Accept json
  277. // @Produce json
  278. // @Param string body string false "string valid"
  279. // @Success 200 {object} response.Response
  280. // @Failure 500 {object} response.Response
  281. // @Router /ab/settings [post]
  282. // @Security BearerAuth
  283. func (a *Ab) Settings(c *gin.Context) {
  284. c.JSON(http.StatusOK, gin.H{
  285. "max_peer_one_ab": 0, //最大peer数,0表示不限制
  286. })
  287. }
  288. // SharedProfiles
  289. // @Tags 地址[Personal]
  290. // @Summary 共享地址簿
  291. // @Description 共享
  292. // @Accept json
  293. // @Produce json
  294. // @Param string body string false "string valid"
  295. // @Success 200 {object} response.Response
  296. // @Failure 500 {object} response.Response
  297. // @Router /ab/shared/profiles [post]
  298. // @Security BearerAuth
  299. func (a *Ab) SharedProfiles(c *gin.Context) {
  300. //AbProfile.fromJson(Map<String, dynamic> json)
  301. //: guid = json['guid'] ?? '',
  302. // name = json['name'] ?? '',
  303. // owner = json['owner'] ?? '',
  304. // note = json['note'] ?? '',
  305. // rule = json['rule'] ?? 0;
  306. //暂时没必要返回数据,可能是为了共享地址簿
  307. /*item := map[string]interface{}{
  308. "guid": "1",
  309. "name": "admin",
  310. "owner": "admin",
  311. "note": "admin11",
  312. "rule": 0,
  313. }
  314. item2 := map[string]interface{}{
  315. "guid": "2",
  316. "name": "admin2",
  317. "owner": "admin2",
  318. "note": "admin22",
  319. "rule": 0,
  320. }
  321. c.JSON(http.StatusOK, gin.H{
  322. "total": 2,
  323. "data": []interface{}{item, item2},
  324. })*/
  325. c.JSON(http.StatusOK, gin.H{
  326. "total": 0,
  327. "data": nil,
  328. })
  329. }
  330. // Peers
  331. // @Tags 地址[Personal]
  332. // @Summary 地址列表
  333. // @Description 地址
  334. // @Accept json
  335. // @Produce json
  336. // @Param string body string false "string valid"
  337. // @Success 200 {object} response.Response
  338. // @Failure 500 {object} response.Response
  339. // @Router /ab/peers [post]
  340. // @Security BearerAuth
  341. func (a *Ab) Peers(c *gin.Context) {
  342. user := service.AllService.UserService.CurUser(c)
  343. al := service.AllService.AddressBookService.ListByUserId(user.Id, 1, 1000)
  344. c.JSON(http.StatusOK, gin.H{
  345. "total": al.Total,
  346. "data": al.AddressBooks,
  347. "licensed_devices": 99999,
  348. })
  349. }
  350. // PTags
  351. // @Tags 地址[Personal]
  352. // @Summary 标签
  353. // @Description 标签
  354. // @Accept json
  355. // @Produce json
  356. // @Param id path string true "id"
  357. // @Success 200 {object} model.TagList
  358. // @Failure 500 {object} response.ErrorResponse
  359. // @Router /ab/tags/{guid} [post]
  360. // @Security BearerAuth
  361. func (a *Ab) PTags(c *gin.Context) {
  362. user := service.AllService.UserService.CurUser(c)
  363. tags := service.AllService.TagService.ListByUserId(user.Id)
  364. c.JSON(http.StatusOK, tags.Tags)
  365. }
  366. // PeerAdd
  367. // @Tags 地址[Personal]
  368. // @Summary 添加地址
  369. // @Description 添加地址
  370. // @Accept json
  371. // @Produce json
  372. // @Param id path string true "id"
  373. // @Success 200 {string} string
  374. // @Failure 500 {object} response.ErrorResponse
  375. // @Router /ab/peer/add/{guid} [post]
  376. // @Security BearerAuth
  377. func (a *Ab) PeerAdd(c *gin.Context) {
  378. // forceAlwaysRelay永远是字符串"false",真是坑
  379. //f := &gin.H{}
  380. f := &requstform.PersonalAddressBookForm{}
  381. err := c.ShouldBindJSON(f)
  382. if err != nil {
  383. response.Error(c, "参数错误"+err.Error())
  384. return
  385. }
  386. fmt.Println(f)
  387. u := service.AllService.UserService.CurUser(c)
  388. f.UserId = u.Id
  389. ab := f.ToAddressBook()
  390. err = service.AllService.AddressBookService.AddAddressBook(ab)
  391. if err != nil {
  392. response.Error(c, "操作失败")
  393. return
  394. }
  395. c.String(http.StatusOK, "")
  396. }
  397. // PeerDel
  398. // @Tags 地址[Personal]
  399. // @Summary 删除地址
  400. // @Description 删除地址
  401. // @Accept json
  402. // @Produce json
  403. // @Param id path string true "id"
  404. // @Success 200 {string} string
  405. // @Failure 500 {object} response.ErrorResponse
  406. // @Router /ab/peer/add/{guid} [delete]
  407. // @Security BearerAuth
  408. func (a *Ab) PeerDel(c *gin.Context) {
  409. f := &[]string{}
  410. err := c.ShouldBind(f)
  411. if err != nil {
  412. response.Error(c, "参数错误")
  413. return
  414. }
  415. u := service.AllService.UserService.CurUser(c)
  416. for _, id := range *f {
  417. ab := service.AllService.AddressBookService.InfoByUserIdAndId(u.Id, id)
  418. if ab == nil || ab.RowId == 0 {
  419. response.Error(c, "参数错误")
  420. return
  421. }
  422. err = service.AllService.AddressBookService.Delete(ab)
  423. if err != nil {
  424. response.Error(c, "操作失败")
  425. return
  426. }
  427. }
  428. c.String(http.StatusOK, "")
  429. }
  430. // PeerUpdate
  431. // @Tags 地址[Personal]
  432. // @Summary 更新地址
  433. // @Description 更新地址
  434. // @Accept json
  435. // @Produce json
  436. // @Param id path string true "id"
  437. // @Success 200 {string} string
  438. // @Failure 500 {object} response.ErrorResponse
  439. // @Router /ab/peer/update/{guid} [put]
  440. // @Security BearerAuth
  441. func (a *Ab) PeerUpdate(c *gin.Context) {
  442. //f := &gin.H{}
  443. f := &requstform.PersonalAddressBookForm{}
  444. err := c.ShouldBindJSON(f)
  445. if err != nil {
  446. response.Error(c, "参数错误")
  447. return
  448. }
  449. fmt.Println(f)
  450. //return
  451. u := service.AllService.UserService.CurUser(c)
  452. ab := service.AllService.AddressBookService.InfoByUserIdAndId(u.Id, f.Id)
  453. if ab == nil || ab.RowId == 0 {
  454. response.Error(c, "参数错误")
  455. return
  456. }
  457. nab := f.ToAddressBook()
  458. nab.RowId = ab.RowId
  459. err = service.AllService.AddressBookService.Update(nab)
  460. if err != nil {
  461. response.Error(c, "操作失败")
  462. return
  463. }
  464. c.String(http.StatusOK, "")
  465. }