task.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. package http
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "go-common/app/admin/main/aegis/model/business"
  11. "go-common/app/admin/main/aegis/model/common"
  12. taskmod "go-common/app/admin/main/aegis/model/task"
  13. "go-common/library/ecode"
  14. "go-common/library/log"
  15. bm "go-common/library/net/http/blademaster"
  16. libtime "go-common/library/time"
  17. )
  18. func taskDelay(c *bm.Context) {
  19. opt := &taskmod.DelayOptions{}
  20. if err := c.Bind(opt); err != nil {
  21. return
  22. }
  23. err := srv.Delay(c, opt)
  24. if err == ecode.AccessDenied || err == ecode.NothingFound {
  25. c.JSONMap(map[string]interface{}{"tips": "任务已被他人处理,毋需延迟"}, nil)
  26. return
  27. }
  28. c.JSON(nil, err)
  29. }
  30. func taskRelease(c *bm.Context) {
  31. opt := &common.BaseOptions{}
  32. if err := c.Bind(opt); err != nil {
  33. return
  34. }
  35. c.JSON(srv.Release(c, opt, false))
  36. }
  37. func taskUnDo(c *bm.Context) {
  38. opt := &common.BaseOptions{}
  39. if err := c.Bind(opt); err != nil {
  40. return
  41. }
  42. c.JSON(srv.UnDoStat(c, opt))
  43. }
  44. func taskStat(c *bm.Context) {
  45. opt := &common.BaseOptions{}
  46. if err := c.Bind(opt); err != nil {
  47. return
  48. }
  49. c.JSON(srv.TaskStat(c, opt))
  50. }
  51. func consumerOn(c *bm.Context) {
  52. opt := &common.BaseOptions{}
  53. if err := c.Bind(opt); err != nil {
  54. return
  55. }
  56. c.JSON(nil, srv.On(c, opt))
  57. }
  58. func consumerOff(c *bm.Context) {
  59. opt := new(struct {
  60. common.BaseOptions
  61. Delay bool `form:"delay" default:"true"`
  62. })
  63. if err := c.Bind(opt); err != nil {
  64. return
  65. }
  66. if err := srv.Off(c, &opt.BaseOptions); err != nil {
  67. c.JSON(nil, err)
  68. return
  69. }
  70. c.JSON(nil, nil)
  71. srv.Release(c, &opt.BaseOptions, opt.Delay)
  72. }
  73. func kickOut(c *bm.Context) {
  74. opt := new(struct {
  75. common.BaseOptions
  76. KickUID int64 `form:"kick_uid" validate:"required"`
  77. })
  78. if err := c.Bind(opt); err != nil {
  79. return
  80. }
  81. if opt.BaseOptions.Role != taskmod.TaskRoleLeader {
  82. httpCode(c, "组长才有权限踢人", ecode.RequestErr)
  83. return
  84. }
  85. if err := srv.KickOut(c, &opt.BaseOptions, opt.KickUID); err != nil {
  86. c.JSON(nil, err)
  87. return
  88. }
  89. c.JSON(nil, nil)
  90. srv.Release(c, &opt.BaseOptions, false)
  91. }
  92. func consumerWatcher(c *bm.Context) {
  93. opt := new(struct {
  94. BizID int64 `form:"business_id" validate:"required"`
  95. FlowID int64 `form:"flow_id" validate:"required"`
  96. Role int8 `form:"role"`
  97. })
  98. if err := c.Bind(opt); err != nil {
  99. return
  100. }
  101. c.JSON(srv.Watcher(c, opt.BizID, opt.FlowID, opt.Role))
  102. }
  103. func configEdit(c *bm.Context) {
  104. twc, _, err := readConfig(c)
  105. if err != nil || twc == nil {
  106. c.JSON(nil, ecode.RequestErr)
  107. return
  108. }
  109. if twc.ID <= 0 {
  110. httpCode(c, "id错误", ecode.RequestErr)
  111. return
  112. }
  113. c.JSON(nil, srv.UpdateConfig(c, twc))
  114. }
  115. func configAdd(c *bm.Context) {
  116. twc, confJSON, err := readConfig(c)
  117. if err != nil || twc == nil {
  118. c.JSON(nil, ecode.RequestErr)
  119. return
  120. }
  121. c.JSON(nil, srv.AddConfig(c, twc, confJSON))
  122. }
  123. func readConfig(c *bm.Context) (tc *taskmod.Config, confJSON interface{}, err error) {
  124. opt := &taskmod.ConfigOption{}
  125. if err = c.Bind(opt); err != nil {
  126. return nil, nil, err
  127. }
  128. switch opt.ConfType {
  129. case taskmod.TaskConfigAssign:
  130. type Assign struct {
  131. Mids string `json:"mids"`
  132. Uids string `json:"uids"`
  133. }
  134. confJSON = new(Assign)
  135. case taskmod.TaskConfigRangeWeight:
  136. confJSON = new(taskmod.RangeWeightConfig)
  137. case taskmod.TaskConfigEqualWeight:
  138. confJSON = new(taskmod.EqualWeightConfig)
  139. default:
  140. return nil, nil, ecode.RequestErr
  141. }
  142. tc = &taskmod.Config{
  143. ID: opt.ID,
  144. ConfType: opt.ConfType,
  145. BusinessID: opt.BusinessID,
  146. FlowID: opt.FlowID,
  147. Description: opt.Description,
  148. UID: opt.UID,
  149. Uname: opt.Uname,
  150. }
  151. bt, _ := time.ParseInLocation(common.TimeFormat, opt.Btime, time.Local)
  152. et, _ := time.ParseInLocation(common.TimeFormat, opt.Etime, time.Local)
  153. if !bt.IsZero() {
  154. tc.Btime = libtime.Time(bt.Unix())
  155. }
  156. if !et.IsZero() {
  157. tc.Etime = libtime.Time(et.Unix())
  158. }
  159. err = jsonhelp(confJSON, []byte(opt.ConfJSON), tc)
  160. return
  161. }
  162. func jsonhelp(jsonObject interface{}, bs []byte, tc *taskmod.Config) (err error) {
  163. if err = json.Unmarshal(bs, jsonObject); err != nil {
  164. log.Error("jsonhelp error(%v)", string(bs), err)
  165. return
  166. }
  167. if bs, err = json.Marshal(jsonObject); err != nil {
  168. log.Error("jsonhelp json.Marshal(%s) error(%v)", err)
  169. return
  170. }
  171. tc.ConfJSON = string(bs)
  172. return
  173. }
  174. func configList(c *bm.Context) {
  175. qp := &taskmod.QueryParams{}
  176. if err := c.Bind(qp); err != nil {
  177. return
  178. }
  179. configs, count, err := srv.QueryConfigs(c, qp)
  180. if err != nil {
  181. c.JSON(nil, err)
  182. return
  183. }
  184. qp.Pager.Total = int(count)
  185. c.JSONMap(map[string]interface{}{
  186. "pager": qp.Pager,
  187. "data": configs,
  188. }, nil)
  189. }
  190. func configDelete(c *bm.Context) {
  191. opt := new(struct {
  192. ID int64 `form:"id" validate:"required"`
  193. })
  194. if err := c.Bind(opt); err != nil {
  195. return
  196. }
  197. c.JSON(nil, srv.DeleteConfig(c, opt.ID))
  198. }
  199. func configSet(c *bm.Context) {
  200. opt := new(struct {
  201. ID int64 `form:"id" validate:"required"`
  202. State int8 `form:"state"`
  203. })
  204. if err := c.Bind(opt); err != nil {
  205. return
  206. }
  207. c.JSON(nil, srv.SetStateConfig(c, opt.ID, opt.State))
  208. }
  209. func maxWeight(c *bm.Context) {
  210. opt := &common.BaseOptions{}
  211. if err := c.Bind(opt); err != nil {
  212. return
  213. }
  214. c.JSON(srv.MaxWeight(c, opt))
  215. }
  216. func weightlog(c *bm.Context) {
  217. opt := new(struct {
  218. TaskID int64 `form:"task_id" validate:"required"`
  219. Pn int `form:"pn" default:"1"`
  220. Ps int `form:"ps" default:"20"`
  221. })
  222. if err := c.Bind(opt); err != nil {
  223. return
  224. }
  225. ls, count, err := srv.WeightLog(c, opt.TaskID, opt.Pn, opt.Ps)
  226. c.JSONMap(map[string]interface{}{
  227. "data": ls,
  228. "pager": common.Pager{
  229. Pn: opt.Pn,
  230. Ps: opt.Ps,
  231. Total: count,
  232. },
  233. }, err)
  234. }
  235. func role(c *bm.Context) {
  236. opt := &common.BaseOptions{}
  237. if err := c.Bind(opt); err != nil {
  238. c.JSON(nil, ecode.RequestErr)
  239. return
  240. }
  241. opt.UID = uid(c)
  242. opt.Uname = uname(c)
  243. _, role, err := srv.GetRole(c, opt)
  244. c.JSON(role, err)
  245. }
  246. func roleFlush(c *bm.Context) {
  247. opt := new(struct {
  248. Uids []int64 `form:"uids,split" validate:"required"`
  249. BizID int64 `form:"business_id" validate:"required"`
  250. FlowID int64 `form:"flow_id" validate:"required"`
  251. })
  252. if err := c.Bind(opt); err != nil {
  253. c.JSON(nil, ecode.RequestErr)
  254. return
  255. }
  256. c.JSON(nil, srv.FlushRole(c, opt.BizID, opt.FlowID, opt.Uids))
  257. }
  258. func checkTaskRole() bm.HandlerFunc {
  259. return func(ctx *bm.Context) {
  260. opt := &common.BaseOptions{}
  261. if err := ctx.Bind(opt); err != nil {
  262. ctx.Abort()
  263. return
  264. }
  265. if opt.BusinessID <= 0 || opt.FlowID <= 0 {
  266. httpCode(ctx, "缺少business_id或flow_id", ecode.RequestErr)
  267. ctx.Abort()
  268. return
  269. }
  270. if srv.Debug() == "local" {
  271. ctx.Request.Form.Set("role", strconv.Itoa(int(taskmod.TaskRoleLeader)))
  272. return
  273. }
  274. user := uid(ctx)
  275. if srv.IsAdmin(user) {
  276. ctx.Request.Form.Set("role", strconv.Itoa(int(taskmod.TaskRoleLeader)))
  277. log.Info("checkTaskRole uid(%d) is admin", user)
  278. return
  279. }
  280. _, role, err := srv.GetRole(ctx, opt)
  281. if err != nil || role == 0 {
  282. ctx.JSON(nil, ecode.AccessDenied)
  283. ctx.Abort()
  284. return
  285. }
  286. log.Info("checkTaskRole opt(%+v) role(%d)", role)
  287. ctx.Request.Form.Set("role", fmt.Sprint(role))
  288. }
  289. }
  290. //判断业务用户权限,无授权业务则报错
  291. func checkBizRole(role string, noAdmin bool, bizID bool) bm.HandlerFunc {
  292. return func(ctx *bm.Context) {
  293. if srv.Debug() == "local" {
  294. return
  295. }
  296. user := uid(ctx)
  297. if srv.IsAdmin(user) {
  298. log.Info("checkBizRole uid(%d) is admin", user)
  299. return
  300. }
  301. businessID, err := srv.GetRoleBiz(ctx, user, role, noAdmin)
  302. if err != nil || len(businessID) == 0 {
  303. ctx.JSON(nil, ecode.AccessDenied)
  304. ctx.Abort()
  305. return
  306. }
  307. ctx.Set(business.AccessBiz, businessID)
  308. //request business in accessed biz range
  309. if bizID {
  310. var biz int64
  311. if strings.Contains(ctx.Request.Header.Get("Content-Type"), "application/json") {
  312. var body []byte
  313. if body, err = ioutil.ReadAll(ctx.Request.Body); err != nil {
  314. log.Error("checkBizRole ioutil.ReadAll error(%+v)", err)
  315. return
  316. }
  317. ctx.Request.Body.Close()
  318. ctx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
  319. pm := new(struct {
  320. BusinessID int64 `json:"business_id" form:"business_id"`
  321. })
  322. if err = json.Unmarshal(body, pm); err != nil {
  323. log.Error("checkBizRole json.Unmarshal error(%+v) body(%s)", err, string(body))
  324. ctx.JSON(nil, ecode.RequestErr)
  325. ctx.Abort()
  326. return
  327. }
  328. biz = pm.BusinessID
  329. } else {
  330. if biz, err = strconv.ParseInt(ctx.Request.Form.Get("business_id"), 10, 64); err != nil {
  331. log.Error("checkBizRole strconv.ParseInt(%s) error(%v)", ctx.Request.Form.Get("business_id"), err)
  332. ctx.JSON(nil, ecode.RequestErr)
  333. ctx.Abort()
  334. return
  335. }
  336. }
  337. exist := false
  338. for _, item := range businessID {
  339. if item == biz {
  340. exist = true
  341. break
  342. }
  343. }
  344. if !exist || biz <= 0 {
  345. ctx.JSON(nil, ecode.AccessDenied)
  346. ctx.Abort()
  347. return
  348. }
  349. }
  350. }
  351. }
  352. //判断任务用户权限,无授权业务则报错
  353. func checkAccessTask() bm.HandlerFunc {
  354. return func(ctx *bm.Context) {
  355. if srv.Debug() == "local" {
  356. return
  357. }
  358. user := uid(ctx)
  359. if srv.IsAdmin(user) {
  360. log.Info("checkrole uid(%d) is admin", user)
  361. return
  362. }
  363. businessID, flowID, err := srv.GetTaskBizFlows(ctx, user)
  364. if err != nil || len(businessID) == 0 || len(flowID) == 0 {
  365. //ctx.JSON(nil, ecode.AccessDenied)
  366. ctx.JSON(nil, nil)
  367. ctx.Abort()
  368. return
  369. }
  370. log.Info("checkAccessTask uid(%d) can see business(%+v) flow(%+v)", user, businessID, flowID)
  371. ctx.Set(business.AccessBiz, businessID)
  372. ctx.Set(business.AccessFlow, flowID)
  373. }
  374. }
  375. //是否为指定业务的管理员角色
  376. func checkBizID() bm.HandlerFunc {
  377. return checkBizRole(business.BizBIDAdmin, false, true)
  378. }
  379. //为哪些业务的管理员角色
  380. func checkBizAdmin() bm.HandlerFunc {
  381. return checkBizRole(business.BizBIDAdmin, false, false)
  382. }
  383. //为哪些业务的非管理员角色
  384. func checkBizBID() bm.HandlerFunc {
  385. return checkBizRole("", true, false)
  386. }
  387. func checkBizLeader() bm.HandlerFunc {
  388. return checkBizRole("leader", true, true)
  389. }
  390. func checkBizOper() bm.HandlerFunc {
  391. return checkBizRole("oper", true, true)
  392. }
  393. //为指定业务的非管理员角色
  394. func checkBizBIDBiz() bm.HandlerFunc {
  395. return checkBizRole("", true, true)
  396. }
  397. func getAccessBiz(c *bm.Context) (biz []int64) {
  398. biz = []int64{}
  399. ids, _ := c.Get(business.AccessBiz)
  400. if ids != nil {
  401. biz = ids.([]int64)
  402. }
  403. return
  404. }
  405. func getAccessFlow(c *bm.Context) (flowID []int64) {
  406. flowID = []int64{}
  407. ids, _ := c.Get(business.AccessFlow)
  408. if ids != nil {
  409. flowID = ids.([]int64)
  410. }
  411. return
  412. }
  413. func checkon() bm.HandlerFunc {
  414. return func(ctx *bm.Context) {
  415. if srv.Debug() == "local" {
  416. return
  417. }
  418. opt := &common.BaseOptions{}
  419. if err := ctx.Bind(opt); err != nil {
  420. ctx.Abort()
  421. return
  422. }
  423. if !srv.IsOn(ctx, opt) {
  424. if err := srv.On(ctx, opt); err != nil {
  425. ctx.JSON(nil, err)
  426. ctx.Abort()
  427. }
  428. return
  429. }
  430. }
  431. }
  432. func preHandlerUser() bm.HandlerFunc {
  433. return func(ctx *bm.Context) {
  434. if srv.Debug() == "local" {
  435. return
  436. }
  437. uidS, ok := ctx.Get("uid")
  438. if !ok {
  439. ctx.JSON(nil, ecode.NoLogin)
  440. ctx.Abort()
  441. return
  442. }
  443. unamei, ok := ctx.Get("username")
  444. if !ok {
  445. ctx.JSON(nil, ecode.NoLogin)
  446. ctx.Abort()
  447. return
  448. }
  449. ctx.Request.Form.Set("uid", fmt.Sprint(uidS))
  450. ctx.Request.Form.Set("uname", unamei.(string))
  451. }
  452. }