ab.go 13 KB

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