ab.go 13 KB

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