ab.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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, response.TranslateMsg(c, "ParamsError")+err.Error())
  66. return
  67. }
  68. abd := &requstform.AddressBookFormData{}
  69. err = json.Unmarshal([]byte(abf.Data), abd)
  70. if err != nil {
  71. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  72. return
  73. }
  74. tc := map[string]uint{}
  75. err = json.Unmarshal([]byte(abd.TagColors), &tc)
  76. if err != nil {
  77. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  78. return
  79. }
  80. user := service.AllService.UserService.CurUser(c)
  81. err = service.AllService.AddressBookService.UpdateAddressBook(abd.Peers, user.Id)
  82. if err != nil {
  83. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  84. return
  85. }
  86. service.AllService.TagService.UpdateTags(user.Id, tc)
  87. c.JSON(http.StatusOK, nil)
  88. }
  89. // Tags
  90. // @Tags 地址
  91. // @Summary 标签
  92. // @Description 标签
  93. // @Accept json
  94. // @Produce json
  95. // @Success 200 {object} []model.Tag
  96. // @Failure 500 {object} response.ErrorResponse
  97. // @Router /tags [post]
  98. // @Security BearerAuth
  99. func (a *Ab) Tags(c *gin.Context) {
  100. user := service.AllService.UserService.CurUser(c)
  101. tags := service.AllService.TagService.ListByUserId(user.Id)
  102. c.JSON(http.StatusOK, tags.Tags)
  103. }
  104. // TagAdd
  105. // @Tags 地址[Personal]
  106. // @Summary 标签添加
  107. // @Description 标签
  108. // @Accept json
  109. // @Produce json
  110. // @Success 200 {string} string
  111. // @Failure 500 {object} response.ErrorResponse
  112. // @Router /ab/add [post]
  113. // @Security BearerAuth
  114. func (a *Ab) TagAdd(c *gin.Context) {
  115. t := &model.Tag{}
  116. err := c.ShouldBindJSON(t)
  117. if err != nil {
  118. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  119. return
  120. }
  121. u := service.AllService.UserService.CurUser(c)
  122. tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.Name)
  123. if tag != nil && tag.Id != 0 {
  124. response.Error(c, response.TranslateMsg(c, "ItemExists"))
  125. return
  126. }
  127. t.UserId = u.Id
  128. err = service.AllService.TagService.Create(t)
  129. if err != nil {
  130. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  131. return
  132. }
  133. c.String(http.StatusOK, "")
  134. }
  135. // TagRename
  136. // @Tags 地址[Personal]
  137. // @Summary 标签重命名
  138. // @Description 标签
  139. // @Accept json
  140. // @Produce json
  141. // @Success 200 {string} string
  142. // @Failure 500 {object} response.ErrorResponse
  143. // @Router /ab/tag/rename/{guid} [put]
  144. // @Security BearerAuth
  145. func (a *Ab) TagRename(c *gin.Context) {
  146. t := &requstform.TagRenameForm{}
  147. err := c.ShouldBindJSON(t)
  148. if err != nil {
  149. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  150. return
  151. }
  152. u := service.AllService.UserService.CurUser(c)
  153. tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.Old)
  154. if tag == nil || tag.Id == 0 {
  155. response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
  156. return
  157. }
  158. ntag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.New)
  159. if ntag != nil && ntag.Id != 0 {
  160. response.Error(c, response.TranslateMsg(c, "ItemExists"))
  161. return
  162. }
  163. tag.Name = t.New
  164. err = service.AllService.TagService.Update(tag)
  165. if err != nil {
  166. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  167. return
  168. }
  169. c.String(http.StatusOK, "")
  170. }
  171. // TagUpdate
  172. // @Tags 地址[Personal]
  173. // @Summary 标签修改颜色
  174. // @Description 标签
  175. // @Accept json
  176. // @Produce json
  177. // @Success 200 {string} string
  178. // @Failure 500 {object} response.ErrorResponse
  179. // @Router /ab/tag/update/{guid} [put]
  180. // @Security BearerAuth
  181. func (a *Ab) TagUpdate(c *gin.Context) {
  182. t := &requstform.TagColorForm{}
  183. err := c.ShouldBindJSON(t)
  184. if err != nil {
  185. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  186. return
  187. }
  188. u := service.AllService.UserService.CurUser(c)
  189. tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.Name)
  190. if tag == nil || tag.Id == 0 {
  191. response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
  192. return
  193. }
  194. tag.Color = t.Color
  195. err = service.AllService.TagService.Update(tag)
  196. if err != nil {
  197. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  198. return
  199. }
  200. c.String(http.StatusOK, "")
  201. }
  202. // TagDel
  203. // @Tags 地址[Personal]
  204. // @Summary 标签删除
  205. // @Description 标签
  206. // @Accept json
  207. // @Produce json
  208. // @Success 200 {string} string
  209. // @Failure 500 {object} response.ErrorResponse
  210. // @Router /ab/tag/{guid} [delete]
  211. // @Security BearerAuth
  212. func (a *Ab) TagDel(c *gin.Context) {
  213. t := &[]string{}
  214. err := c.ShouldBind(t)
  215. if err != nil {
  216. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  217. return
  218. }
  219. //fmt.Println(t)
  220. u := service.AllService.UserService.CurUser(c)
  221. for _, name := range *t {
  222. tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, name)
  223. if tag == nil || tag.Id == 0 {
  224. response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
  225. return
  226. }
  227. err = service.AllService.TagService.Delete(tag)
  228. if err != nil {
  229. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  230. return
  231. }
  232. }
  233. c.String(http.StatusOK, "")
  234. }
  235. // Personal
  236. // @Tags 地址[Personal]
  237. // @Summary 个人地址
  238. // @Description 个人地址
  239. // @Accept json
  240. // @Produce json
  241. // @Param string body string false "string valid"
  242. // @Success 200 {object} response.Response
  243. // @Failure 500 {object} response.Response
  244. // @Router /ab/personal [post]
  245. // @Security BearerAuth
  246. func (a *Ab) Personal(c *gin.Context) {
  247. user := service.AllService.UserService.CurUser(c)
  248. /**
  249. guid = json['guid'] ?? '',
  250. name = json['name'] ?? '',
  251. owner = json['owner'] ?? '',
  252. note = json['note'] ?? '',
  253. rule = json['rule'] ?? 0;
  254. */
  255. if global.Config.Rustdesk.Personal == 1 {
  256. guid := strconv.Itoa(int(user.GroupId)) + "-" + strconv.Itoa(int(user.Id))
  257. //如果返回了guid,后面的请求会有变化
  258. c.JSON(http.StatusOK, gin.H{
  259. "guid": guid,
  260. "name": user.Username,
  261. "rule": 0,
  262. })
  263. } else {
  264. c.JSON(http.StatusOK, nil)
  265. }
  266. }
  267. // Settings
  268. // @Tags 地址[Personal]
  269. // @Summary 设置
  270. // @Description 设置
  271. // @Accept json
  272. // @Produce json
  273. // @Param string body string false "string valid"
  274. // @Success 200 {object} response.Response
  275. // @Failure 500 {object} response.Response
  276. // @Router /ab/settings [post]
  277. // @Security BearerAuth
  278. func (a *Ab) Settings(c *gin.Context) {
  279. c.JSON(http.StatusOK, gin.H{
  280. "max_peer_one_ab": 0, //最大peer数,0表示不限制
  281. })
  282. }
  283. // SharedProfiles
  284. // @Tags 地址[Personal]
  285. // @Summary 共享地址簿
  286. // @Description 共享
  287. // @Accept json
  288. // @Produce json
  289. // @Param string body string false "string valid"
  290. // @Success 200 {object} response.Response
  291. // @Failure 500 {object} response.Response
  292. // @Router /ab/shared/profiles [post]
  293. // @Security BearerAuth
  294. func (a *Ab) SharedProfiles(c *gin.Context) {
  295. //AbProfile.fromJson(Map<String, dynamic> json)
  296. //: guid = json['guid'] ?? '',
  297. // name = json['name'] ?? '',
  298. // owner = json['owner'] ?? '',
  299. // note = json['note'] ?? '',
  300. // rule = json['rule'] ?? 0;
  301. //暂时没必要返回数据,可能是为了共享地址簿
  302. /*item := map[string]interface{}{
  303. "guid": "1",
  304. "name": "admin",
  305. "owner": "admin",
  306. "note": "admin11",
  307. "rule": 0,
  308. }
  309. item2 := map[string]interface{}{
  310. "guid": "2",
  311. "name": "admin2",
  312. "owner": "admin2",
  313. "note": "admin22",
  314. "rule": 0,
  315. }
  316. c.JSON(http.StatusOK, gin.H{
  317. "total": 2,
  318. "data": []interface{}{item, item2},
  319. })*/
  320. c.JSON(http.StatusOK, gin.H{
  321. "total": 0,
  322. "data": nil,
  323. })
  324. }
  325. // Peers
  326. // @Tags 地址[Personal]
  327. // @Summary 地址列表
  328. // @Description 地址
  329. // @Accept json
  330. // @Produce json
  331. // @Param string body string false "string valid"
  332. // @Success 200 {object} response.Response
  333. // @Failure 500 {object} response.Response
  334. // @Router /ab/peers [post]
  335. // @Security BearerAuth
  336. func (a *Ab) Peers(c *gin.Context) {
  337. user := service.AllService.UserService.CurUser(c)
  338. al := service.AllService.AddressBookService.ListByUserId(user.Id, 1, 1000)
  339. c.JSON(http.StatusOK, gin.H{
  340. "total": al.Total,
  341. "data": al.AddressBooks,
  342. "licensed_devices": 99999,
  343. })
  344. }
  345. // PTags
  346. // @Tags 地址[Personal]
  347. // @Summary 标签
  348. // @Description 标签
  349. // @Accept json
  350. // @Produce json
  351. // @Param id path string true "id"
  352. // @Success 200 {object} model.TagList
  353. // @Failure 500 {object} response.ErrorResponse
  354. // @Router /ab/tags/{guid} [post]
  355. // @Security BearerAuth
  356. func (a *Ab) PTags(c *gin.Context) {
  357. user := service.AllService.UserService.CurUser(c)
  358. tags := service.AllService.TagService.ListByUserId(user.Id)
  359. c.JSON(http.StatusOK, tags.Tags)
  360. }
  361. // PeerAdd
  362. // @Tags 地址[Personal]
  363. // @Summary 添加地址
  364. // @Description 添加地址
  365. // @Accept json
  366. // @Produce json
  367. // @Param id path string true "id"
  368. // @Success 200 {string} string
  369. // @Failure 500 {object} response.ErrorResponse
  370. // @Router /ab/peer/add/{guid} [post]
  371. // @Security BearerAuth
  372. func (a *Ab) PeerAdd(c *gin.Context) {
  373. // forceAlwaysRelay永远是字符串"false",真是坑
  374. //f := &gin.H{}
  375. f := &requstform.PersonalAddressBookForm{}
  376. err := c.ShouldBindJSON(f)
  377. if err != nil {
  378. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  379. return
  380. }
  381. fmt.Println(f)
  382. u := service.AllService.UserService.CurUser(c)
  383. f.UserId = u.Id
  384. ab := f.ToAddressBook()
  385. err = service.AllService.AddressBookService.AddAddressBook(ab)
  386. if err != nil {
  387. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  388. return
  389. }
  390. c.String(http.StatusOK, "")
  391. }
  392. // PeerDel
  393. // @Tags 地址[Personal]
  394. // @Summary 删除地址
  395. // @Description 删除地址
  396. // @Accept json
  397. // @Produce json
  398. // @Param id path string true "id"
  399. // @Success 200 {string} string
  400. // @Failure 500 {object} response.ErrorResponse
  401. // @Router /ab/peer/add/{guid} [delete]
  402. // @Security BearerAuth
  403. func (a *Ab) PeerDel(c *gin.Context) {
  404. f := &[]string{}
  405. err := c.ShouldBind(f)
  406. if err != nil {
  407. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  408. return
  409. }
  410. u := service.AllService.UserService.CurUser(c)
  411. for _, id := range *f {
  412. ab := service.AllService.AddressBookService.InfoByUserIdAndId(u.Id, id)
  413. if ab == nil || ab.RowId == 0 {
  414. response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
  415. return
  416. }
  417. err = service.AllService.AddressBookService.Delete(ab)
  418. if err != nil {
  419. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  420. return
  421. }
  422. }
  423. c.String(http.StatusOK, "")
  424. }
  425. // PeerUpdate
  426. // @Tags 地址[Personal]
  427. // @Summary 更新地址
  428. // @Description 更新地址
  429. // @Accept json
  430. // @Produce json
  431. // @Param id path string true "id"
  432. // @Success 200 {string} string
  433. // @Failure 500 {object} response.ErrorResponse
  434. // @Router /ab/peer/update/{guid} [put]
  435. // @Security BearerAuth
  436. func (a *Ab) PeerUpdate(c *gin.Context) {
  437. //f := &gin.H{}
  438. f := &requstform.PersonalAddressBookForm{}
  439. err := c.ShouldBindJSON(f)
  440. if err != nil {
  441. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  442. return
  443. }
  444. //fmt.Println(f)
  445. //return
  446. u := service.AllService.UserService.CurUser(c)
  447. ab := service.AllService.AddressBookService.InfoByUserIdAndId(u.Id, f.Id)
  448. if ab == nil || ab.RowId == 0 {
  449. response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
  450. return
  451. }
  452. nab := f.ToAddressBook()
  453. nab.RowId = ab.RowId
  454. err = service.AllService.AddressBookService.Update(nab)
  455. if err != nil {
  456. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  457. return
  458. }
  459. c.String(http.StatusOK, "")
  460. }