engine.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package http
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "time"
  8. "go-common/app/admin/main/aegis/model"
  9. "go-common/app/admin/main/aegis/model/business"
  10. "go-common/app/admin/main/aegis/model/common"
  11. "go-common/app/admin/main/aegis/model/middleware"
  12. "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. "go-common/library/net/http/blademaster/binding"
  17. "go-common/library/net/http/blademaster/render"
  18. "go-common/library/xstr"
  19. )
  20. func listBizFlow(c *bm.Context) {
  21. bizID := getAccessBiz(c)
  22. flowID := getAccessFlow(c)
  23. opt := &business.OptList{}
  24. if err := c.Bind(opt); err != nil {
  25. return
  26. }
  27. c.JSON(srv.ListBizFlow(c, opt.TP, bizID, flowID))
  28. }
  29. func getBizFlow(c *bm.Context) {
  30. opt := new(struct {
  31. BusinessID int64 `form:"business_id" validate:"required"`
  32. })
  33. if err := c.Bind(opt); err != nil {
  34. return
  35. }
  36. c.JSON(srv.ListBizFlow(c, 0, []int64{opt.BusinessID}, nil))
  37. }
  38. func next(c *bm.Context) {
  39. opt := &task.NextOptions{}
  40. if err := c.Bind(opt); err != nil {
  41. return
  42. }
  43. if opt.BusinessID == 0 || opt.FlowID == 0 {
  44. httpCode(c, "缺少business_id或flow_id", ecode.RequestErr)
  45. return
  46. }
  47. opt.BisLeader = opt.Role == task.TaskRoleLeader
  48. c.JSON(srv.Next(c, opt))
  49. }
  50. func infoByTask(c *bm.Context) {
  51. opt := new(struct {
  52. TaskID int64 `form:"task_id" validate:"required"`
  53. common.BaseOptions
  54. })
  55. if err := c.Bind(opt); err != nil {
  56. return
  57. }
  58. if opt.BusinessID == 0 || opt.FlowID == 0 {
  59. httpCode(c, "缺少business_id或flow_id", ecode.RequestErr)
  60. return
  61. }
  62. c.JSON(srv.InfoTask(c, &opt.BaseOptions, opt.TaskID))
  63. }
  64. func listByTask(c *bm.Context) {
  65. opt := &task.ListOptions{}
  66. if err := c.Bind(opt); err != nil {
  67. return
  68. }
  69. if opt.BusinessID == 0 || opt.FlowID == 0 {
  70. httpCode(c, "缺少business_id或flow_id", ecode.RequestErr)
  71. return
  72. }
  73. opt.BisLeader = opt.Role == task.TaskRoleLeader
  74. infos, err := srv.ListByTask(c, opt)
  75. c.JSONMap(map[string]interface{}{
  76. "data": infos,
  77. "pager": &opt.Pager,
  78. }, err)
  79. }
  80. func infoByResource(c *bm.Context) {
  81. opt := new(common.BaseOptions)
  82. if err := c.Bind(opt); err != nil {
  83. return
  84. }
  85. if opt.BusinessID == 0 || opt.OID == "" {
  86. httpCode(c, "缺少business_id或oid", ecode.RequestErr)
  87. return
  88. }
  89. info, err := srv.InfoResource(c, opt)
  90. middleware.Response(
  91. info,
  92. err,
  93. c.JSON,
  94. &middleware.MiddleAggregate{
  95. Cfg: srv.GetMiddlewareCache(opt.BusinessID),
  96. Encode: true,
  97. })
  98. }
  99. func listByResource(c *bm.Context) {
  100. opt := new(model.SearchParams)
  101. if err := c.Bind(opt); err != nil {
  102. return
  103. }
  104. middleware.Request(opt, &middleware.MiddleAggregate{
  105. Cfg: srv.GetMiddlewareCache(opt.BusinessID),
  106. Encode: false,
  107. })
  108. columns, resources, operation, err := srv.ListByResource(c, opt)
  109. c.JSONMap(map[string]interface{}{
  110. "columns": columns,
  111. "data": resources,
  112. "operations": operation,
  113. "pager": &opt.Pager,
  114. }, err)
  115. }
  116. func submit(c *bm.Context) {
  117. opt, err := parseOptions(c)
  118. if err != nil {
  119. if err == ecode.AegisBusinessCfgErr {
  120. c.JSON(nil, err)
  121. }
  122. return
  123. }
  124. if opt.BusinessID == 0 || opt.FlowID == 0 || opt.OID == "" || opt.Result == nil || opt.Binds == nil || len(opt.Binds) == 0 {
  125. httpCode(c, "business_id,flow_id,oid,result,binds不能为空", ecode.RequestErr)
  126. return
  127. }
  128. if err = srv.Submit(c, opt); err == ecode.AegisNotRunInFlow {
  129. c.JSONMap(map[string]interface{}{
  130. "tips": "资源已被流传,本次提交无效",
  131. }, nil)
  132. return
  133. }
  134. c.JSON(nil, err)
  135. }
  136. func listforjump(c *bm.Context) {
  137. opt := &common.BaseOptions{}
  138. if err := c.Bind(opt); err != nil {
  139. c.JSON(nil, ecode.RequestErr)
  140. return
  141. }
  142. c.JSON(srv.FetchJumpFlowInfo(c, opt.FlowID))
  143. }
  144. func jump(c *bm.Context) {
  145. opt, err := parseOptions(c)
  146. if err != nil {
  147. if err == ecode.AegisBusinessCfgErr {
  148. c.JSON(nil, err)
  149. }
  150. return
  151. }
  152. if opt.BusinessID == 0 || opt.FlowID == 0 || opt.OID == "" || ((opt.Binds == nil || len(opt.Binds) == 0) && opt.NewFlowID == 0) {
  153. c.JSON(nil, ecode.RequestErr)
  154. return
  155. }
  156. c.JSON(nil, srv.JumpFlow(c, opt))
  157. }
  158. func batchSubmit(c *bm.Context) {
  159. opt := &model.BatchOption{}
  160. if err := c.Bind(opt); err != nil {
  161. return
  162. }
  163. tips, err := srv.BatchSubmit(c, opt)
  164. if err != nil {
  165. c.JSON(tips, err)
  166. return
  167. }
  168. if tips != nil && len(tips.Fail) > 0 {
  169. log.Error("批量操作有错误 %+v", tips.Fail)
  170. msg, _ := json.Marshal(tips.Fail)
  171. httpCode(c, string(msg), ecode.ServerErr)
  172. return
  173. }
  174. c.JSON(tips, err)
  175. }
  176. func add(c *bm.Context) {
  177. opt := &model.AddOption{}
  178. if err := c.Bind(opt); err != nil {
  179. return
  180. }
  181. if opt.BusinessID == 0 || opt.NetID == 0 || opt.OID == "" {
  182. c.JSON(nil, ecode.RequestErr)
  183. return
  184. }
  185. c.JSON(nil, srv.Add(c, opt))
  186. }
  187. func cancel(c *bm.Context) {
  188. opt := new(model.CancelOption)
  189. if err := c.Bind(opt); err != nil {
  190. return
  191. }
  192. c.JSON(nil, srv.Cancel(c, opt.BusinessID, opt.Oids, 399, "业务方"))
  193. }
  194. func cancelByOper(c *bm.Context) {
  195. opt := new(struct {
  196. BissinessID int64 `form:"business_id" validate:"required"`
  197. Oids []string `form:"oids,split" validate:"required"`
  198. })
  199. if err := c.Bind(opt); err != nil {
  200. return
  201. }
  202. uid := uid(c)
  203. username := uname(c)
  204. c.JSON(nil, srv.CancelByOper(c, opt.BissinessID, opt.Oids, uid, username))
  205. }
  206. func update(c *bm.Context) {
  207. opt := new(model.UpdateOption)
  208. if err := c.BindWith(opt, binding.Form); err != nil {
  209. return
  210. }
  211. upParams := c.Request.Form.Get("update")
  212. if err := json.Unmarshal([]byte(upParams), &opt.Update); err != nil {
  213. c.JSON(nil, ecode.RequestErr)
  214. return
  215. }
  216. c.JSON(nil, srv.Update(c, opt))
  217. }
  218. func upload(c *bm.Context) {
  219. file, _, err := c.Request.FormFile("file")
  220. if err != nil {
  221. log.Error("FormFile err(%v)", err)
  222. httpCode(c, fmt.Sprintf("File Upload FormFile Error:(%v)", err), ecode.RequestErr)
  223. return
  224. }
  225. defer file.Close()
  226. content, err := ioutil.ReadAll(file)
  227. if err != nil {
  228. log.Error(" uploadFile.ReadAll error(%v)", err)
  229. httpCode(c, fmt.Sprintf("File ioutil.ReadAll Error:(%v)", err), ecode.RequestErr)
  230. return
  231. }
  232. filetype := http.DetectContentType(content)
  233. switch filetype {
  234. case "image/jpeg", "image/jpg":
  235. case "image/gif":
  236. case "image/png":
  237. default:
  238. httpCode(c, fmt.Sprintf("not allow filetype(%s)", filetype), ecode.RequestErr)
  239. log.Warn("not allow filetype(%s) ", filetype)
  240. return
  241. }
  242. local, err := srv.Upload(c, "", filetype, time.Now().Unix(), content)
  243. if err != nil {
  244. log.Error("svc.Upload error(%v)", err)
  245. httpCode(c, fmt.Sprintf("svc.Upload error:(%v)", err), ecode.RequestErr)
  246. return
  247. }
  248. c.JSON(local, nil)
  249. }
  250. func parseOptions(c *bm.Context) (opt *model.SubmitOptions, err error) {
  251. opt = &model.SubmitOptions{}
  252. if err = c.BindWith(opt, binding.JSON); err != nil {
  253. log.Error("parseOptions err(%v)", err)
  254. return
  255. }
  256. if opt.Binds, err = xstr.SplitInts(opt.BindStr); err != nil {
  257. log.Error("parseOptions binds err(%v)", err)
  258. err = ecode.RequestErr
  259. return
  260. }
  261. if uidi, ok := c.Get("uid"); ok {
  262. opt.UID, _ = uidi.(int64)
  263. }
  264. if unamei, ok := c.Get("username"); ok {
  265. opt.Uname, _ = unamei.(string)
  266. }
  267. if opt.Result != nil {
  268. var cfg map[string]uint
  269. if cfg, err = srv.AttributeCFG(c, opt.BusinessID); err != nil {
  270. err = ecode.AegisBusinessCfgErr
  271. return
  272. } else if len(cfg) > 0 {
  273. opt.Result.AttrSet(cfg)
  274. }
  275. if opt.ExtraData != nil {
  276. if re, ok := opt.ExtraData["reason_extend"]; ok && len(fmt.Sprint(re)) > 0 {
  277. opt.Result.RejectReason = fmt.Sprintf("[%v]%s", re, opt.Result.RejectReason)
  278. }
  279. }
  280. }
  281. return
  282. }
  283. func httpCode(c *bm.Context, msg string, err error) {
  284. if c.IsAborted() {
  285. return
  286. }
  287. c.Error = err
  288. bcode := ecode.Cause(err)
  289. if msg == "" {
  290. msg = err.Error()
  291. }
  292. c.Render(http.StatusOK, render.JSON{
  293. Code: bcode.Code(),
  294. Message: msg,
  295. Data: nil,
  296. })
  297. }
  298. func track(c *bm.Context) {
  299. pm := new(model.TrackParam)
  300. if err := c.Bind(pm); err != nil {
  301. return
  302. }
  303. if pm.Pn > 1 && pm.LastPageTime == "" {
  304. c.JSON(nil, ecode.RequestErr)
  305. return
  306. }
  307. data, pager, err := srv.TrackResource(c, pm)
  308. c.JSONMap(map[string]interface{}{
  309. "data": data,
  310. "pager": pager,
  311. }, err)
  312. }
  313. func auditLog(c *bm.Context) {
  314. pm := new(model.SearchAuditLogParam)
  315. if err := c.Bind(pm); err != nil {
  316. return
  317. }
  318. data, pger, err := srv.SearchAuditLog(c, pm)
  319. c.JSONMap(map[string]interface{}{
  320. "data": data,
  321. "pager": pger,
  322. }, err)
  323. }
  324. func auditLogCSV(c *bm.Context) {
  325. pm := new(model.SearchAuditLogParam)
  326. csv, err := srv.SearchAuditLogCSV(c, pm)
  327. if err != nil {
  328. log.Error("auditLogCSV error(%v) pm(%v)", err, pm)
  329. c.JSON(nil, err)
  330. return
  331. }
  332. c.Render(http.StatusOK, CSV{
  333. Title: "操作日志",
  334. Content: FormatCSV(csv),
  335. })
  336. }
  337. func auth(c *bm.Context) {
  338. uid := uid(c)
  339. auth, err := srv.Auth(c, uid)
  340. c.JSONMap(map[string]interface{}{
  341. "data": auth,
  342. }, err)
  343. }