oversea.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package http
  2. import (
  3. "encoding/json"
  4. "go-common/app/admin/main/videoup/model/oversea"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "strconv"
  9. "strings"
  10. )
  11. // policyGroups 策略组列表
  12. func policyGroups(c *bm.Context) {
  13. var (
  14. uid int64
  15. err error
  16. data *oversea.PolicyGroupData
  17. )
  18. v := new(struct {
  19. UName string `form:"username"`
  20. GroupID int64 `form:"group_id"`
  21. Type int8 `form:"type"`
  22. State int8 `form:"state" default:"-1"`
  23. Pn int64 `form:"pn" default:"1"`
  24. Ps int64 `form:"ps" default:"20"`
  25. Order string `form:"order"`
  26. Sort string `form:"sort"`
  27. })
  28. if err = c.Bind(v); err != nil {
  29. c.JSON(nil, ecode.RequestErr)
  30. return
  31. }
  32. if v.UName != "" {
  33. if uid, err = vdaSvc.GetUID(c, v.UName); err != nil {
  34. log.Warn("vdaSvc.GetUID(%s) error(%v)", v.UName, err)
  35. c.JSON(nil, ecode.RequestErr)
  36. return
  37. }
  38. }
  39. data, err = vdaSvc.PolicyGroups(c, uid, v.GroupID, v.Type, v.State, v.Ps, v.Pn, v.Order, v.Sort)
  40. if err != nil {
  41. log.Error("vdaSvc.PolicyGroups() err(%v)", err)
  42. c.JSON(nil, ecode.RequestErr)
  43. return
  44. }
  45. c.JSON(data, err)
  46. }
  47. // archiveGroups 稿件的策略组
  48. func archiveGroups(c *bm.Context) {
  49. var (
  50. err error
  51. )
  52. v := new(struct {
  53. Aid int64 `form:"aid" validate:"required"`
  54. })
  55. if err = c.Bind(v); err != nil {
  56. c.JSON(nil, ecode.RequestErr)
  57. return
  58. }
  59. groups, err := vdaSvc.ArchiveGroups(c, v.Aid)
  60. if err != nil {
  61. log.Error("vdaSvc.ArchiveGroups() err(%v)", err)
  62. c.JSON(nil, ecode.RequestErr)
  63. return
  64. }
  65. c.JSON(groups, err)
  66. }
  67. // addPolicyGroup 添加策略组
  68. func addPolicyGroup(c *bm.Context) {
  69. var (
  70. v = new(struct {
  71. Name string `form:"name" validate:"required"`
  72. Type int8 `form:"type" validate:"required"`
  73. Remark string `form:"remark" default:""`
  74. })
  75. group = &oversea.PolicyGroup{}
  76. uid, _ = getUIDName(c)
  77. err error
  78. )
  79. if uid == 0 {
  80. c.JSON(nil, ecode.Unauthorized)
  81. return
  82. }
  83. if err = c.Bind(v); err != nil {
  84. c.JSON(nil, ecode.RequestErr)
  85. return
  86. }
  87. if v.Name == "" {
  88. c.JSON(nil, ecode.RequestErr)
  89. return
  90. }
  91. group.Name = v.Name
  92. group.Type = v.Type
  93. group.UID = uid
  94. group.Remark = v.Remark
  95. err = vdaSvc.AddPolicyGroup(c, group)
  96. if err != nil {
  97. log.Error("vdaSvc.AddPolicyGroup() err(%v)", err)
  98. c.JSON(nil, ecode.RequestErr)
  99. return
  100. }
  101. c.JSON(group, nil)
  102. }
  103. // editPolicyGroup 编辑策略组
  104. func editPolicyGroup(c *bm.Context) {
  105. var (
  106. v = new(struct {
  107. ID int64 `form:"id" validate:"required"`
  108. Name string `form:"name" validate:"required"`
  109. Remark string `form:"remark" default:""`
  110. })
  111. attrs = make(map[string]interface{})
  112. uid, _ = getUIDName(c)
  113. err error
  114. )
  115. if uid == 0 {
  116. c.JSON(nil, ecode.Unauthorized)
  117. return
  118. }
  119. if err = c.Bind(v); err != nil {
  120. c.JSON(nil, ecode.RequestErr)
  121. return
  122. }
  123. attrs["name"] = v.Name
  124. attrs["uid"] = uid
  125. attrs["remark"] = v.Remark
  126. err = vdaSvc.UpdatePolicyGroup(c, v.ID, attrs)
  127. if err != nil {
  128. log.Error("vdaSvc.UpdatePolicyGroup() err(%v)", err)
  129. c.JSON(nil, ecode.RequestErr)
  130. return
  131. }
  132. c.JSON(nil, nil)
  133. }
  134. // delPolicyGroups 删除策略组
  135. func delPolicyGroups(c *bm.Context) {
  136. upPolicyGroupStates(c, oversea.StateDeleted)
  137. }
  138. // restorePolicyGroups 恢复策略组
  139. func restorePolicyGroups(c *bm.Context) {
  140. upPolicyGroupStates(c, oversea.StateOK)
  141. }
  142. // upPolicyGroupStates 修改策略组状态
  143. func upPolicyGroupStates(c *bm.Context, state int8) {
  144. var (
  145. v = new(struct {
  146. IDStr string `form:"ids" validate:"required"`
  147. })
  148. attrs = make(map[string]interface{})
  149. intIDs []int64
  150. strIDs []string
  151. uid, _ = getUIDName(c)
  152. err error
  153. )
  154. if uid == 0 {
  155. c.JSON(nil, ecode.Unauthorized)
  156. return
  157. }
  158. if err = c.Bind(v); err != nil {
  159. c.JSON(nil, ecode.RequestErr)
  160. return
  161. }
  162. strIDs = strings.Split(v.IDStr, ",")
  163. intIDs = make([]int64, len(strIDs))
  164. for i, id := range strIDs {
  165. intIDs[i], err = strconv.ParseInt(id, 10, 64)
  166. if err != nil {
  167. c.JSON(nil, ecode.RequestErr)
  168. return
  169. }
  170. }
  171. attrs["state"] = state
  172. attrs["uid"] = uid
  173. err = vdaSvc.UpdatePolicyGroups(c, intIDs, attrs)
  174. if err != nil {
  175. c.JSON(nil, ecode.RequestErr)
  176. return
  177. }
  178. c.JSON(nil, nil)
  179. }
  180. // policies 策略组下的策略
  181. func policies(c *bm.Context) {
  182. var (
  183. err error
  184. v = new(struct {
  185. Gid int64 `form:"group_id" validate:"required"`
  186. })
  187. )
  188. if err = c.Bind(v); err != nil {
  189. c.JSON(nil, ecode.RequestErr)
  190. return
  191. }
  192. if v.Gid == 0 {
  193. c.JSON(nil, ecode.RequestErr)
  194. return
  195. }
  196. items, err := vdaSvc.PolicyItems(c, v.Gid)
  197. if err != nil {
  198. log.Error("vdaSvc.PolicyItems() err(%v)", err)
  199. c.JSON(nil, ecode.RequestErr)
  200. return
  201. }
  202. c.JSON(items, nil)
  203. }
  204. // addPolicies 添加策略
  205. func addPolicies(c *bm.Context) {
  206. var (
  207. err error
  208. v = new(struct {
  209. Gid int64 `form:"group_id" validate:"required"`
  210. JSON string `form:"items" validate:"required"`
  211. })
  212. items []*oversea.PolicyParams
  213. uid, _ = getUIDName(c)
  214. )
  215. if err = c.Bind(v); err != nil {
  216. c.JSON(nil, ecode.RequestErr)
  217. return
  218. }
  219. if err = json.Unmarshal([]byte(v.JSON), &items); err != nil {
  220. c.JSON(nil, ecode.RequestErr)
  221. return
  222. }
  223. if v.Gid == 0 {
  224. c.JSON(nil, ecode.RequestErr)
  225. return
  226. }
  227. err = vdaSvc.AddPolicies(c, uid, v.Gid, items)
  228. if err != nil {
  229. log.Error("vdaSvc.AddPolicies() err(%v)", err)
  230. c.JSON(nil, ecode.RequestErr)
  231. return
  232. }
  233. c.JSON(nil, nil)
  234. }
  235. // delPolicies 删除策略
  236. func delPolicies(c *bm.Context) {
  237. var (
  238. v = new(struct {
  239. Gid int64 `form:"group_id" validate:"required"`
  240. IDStr string `form:"ids" validate:"required"`
  241. })
  242. uid, _ = getUIDName(c)
  243. intIDs []int64
  244. strIDs []string
  245. err error
  246. )
  247. if err = c.Bind(v); err != nil {
  248. c.JSON(nil, ecode.RequestErr)
  249. return
  250. }
  251. strIDs = strings.Split(v.IDStr, ",")
  252. intIDs = make([]int64, len(strIDs))
  253. for i, id := range strIDs {
  254. intIDs[i], err = strconv.ParseInt(id, 10, 64)
  255. if err != nil {
  256. c.JSON(nil, ecode.RequestErr)
  257. return
  258. }
  259. }
  260. err = vdaSvc.DelPolices(c, uid, v.Gid, intIDs)
  261. if err != nil {
  262. log.Error("vdaSvc.DelPolices() err(%v)", err)
  263. c.JSON(nil, ecode.RequestErr)
  264. return
  265. }
  266. c.JSON(nil, nil)
  267. }