screen-shot.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package http
  2. import (
  3. "go-common/library/ecode"
  4. "go-common/library/log"
  5. bm "go-common/library/net/http/blademaster"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. // 截图相关业务
  11. // getScreeShotByRoomID 得到一个房间某个时间段的截图
  12. func getSingleScreenShot(c *bm.Context) {
  13. params := c.Request.URL.Query()
  14. room := params.Get("room_id")
  15. // 2018-10-24 14:27:07
  16. start := params.Get("start_time")
  17. end := params.Get("end_time")
  18. channel := params.Get("channel")
  19. c.Set("input_params", params)
  20. if room == "" || start == "" || end == "" {
  21. c.Set("output_data", "some fields are empty")
  22. c.JSONMap(map[string]interface{}{"message": "some fields are empty"}, ecode.RequestErr)
  23. c.Abort()
  24. return
  25. }
  26. roomID, err := strconv.ParseInt(room, 10, 64)
  27. if err != nil || roomID <= 0 {
  28. c.Set("output_data", "room_id is not right")
  29. c.JSONMap(map[string]interface{}{"message": "room_id is not right"}, ecode.RequestErr)
  30. c.Abort()
  31. return
  32. }
  33. startTime, err := time.ParseInLocation("2006-01-02 15:04:05", start, time.Local)
  34. if err != nil {
  35. c.Set("output_data", "Start time format is incorrect")
  36. c.JSONMap(map[string]interface{}{"message": "Start time format is incorrect"}, ecode.RequestErr)
  37. c.Abort()
  38. return
  39. }
  40. endTime, err := time.ParseInLocation("2006-01-02 15:04:05", end, time.Local)
  41. if err != nil {
  42. c.Set("output_data", "End time format is incorrect")
  43. c.JSONMap(map[string]interface{}{"message": "End time format is incorrect"}, ecode.RequestErr)
  44. c.Abort()
  45. return
  46. }
  47. info, err := srv.GetSingleScreeShot(c, roomID, startTime.Unix(), endTime.Unix(), channel)
  48. if err != nil {
  49. c.Set("output_data", err.Error())
  50. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  51. c.Abort()
  52. return
  53. }
  54. c.Set("output_data", info)
  55. c.JSONMap(map[string]interface{}{"data": map[string][]string{"list": info}}, nil)
  56. }
  57. // getMultiScreenShot 得到多个房间一个时间点截图
  58. func getMultiScreenShot(c *bm.Context) {
  59. params := c.Request.URL.Query()
  60. rooms := params.Get("room_ids")
  61. ts := params.Get("ts")
  62. channel := params.Get("channel")
  63. c.Set("input_params", params)
  64. if rooms == "" || ts == "" {
  65. c.Set("output_data", "some fields are empty")
  66. c.JSONMap(map[string]interface{}{"message": "some fields are empty"}, ecode.RequestErr)
  67. c.Abort()
  68. return
  69. }
  70. // 切割room_id
  71. roomIDs := strings.Split(rooms, ",")
  72. if len(roomIDs) <= 0 {
  73. c.Set("output_data", "room_ids is not right")
  74. c.JSONMap(map[string]interface{}{"message": "room_ids is not right"}, ecode.RequestErr)
  75. c.Abort()
  76. return
  77. }
  78. tsInt, _ := strconv.ParseInt(ts, 10, 64)
  79. rids := []int64{}
  80. for _, v := range roomIDs {
  81. roomID, err := strconv.ParseInt(v, 10, 64)
  82. if err != nil {
  83. log.Warn("room id is not right")
  84. continue
  85. }
  86. rids = append(rids, roomID)
  87. }
  88. resp, err := srv.GetMultiScreenShot(c, rids, tsInt, channel)
  89. if err != nil {
  90. c.Set("output_data", err.Error())
  91. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  92. c.Abort()
  93. return
  94. }
  95. c.Set("output_data", resp)
  96. c.JSONMap(map[string]interface{}{"data": map[string]interface{}{"list": resp}}, nil)
  97. }
  98. // getOriginScreenShotPic 获取原始图片地址
  99. func getOriginScreenShotPic(c *bm.Context) {
  100. params := c.Request.URL.Query()
  101. rooms := params.Get("room_ids")
  102. ts := params.Get("ts")
  103. //tp := params.Get("type")
  104. c.Set("input_params", params)
  105. if rooms == "" {
  106. c.Set("output_data", "some fields are empty")
  107. c.JSONMap(map[string]interface{}{"message": "some fields are empty"}, ecode.RequestErr)
  108. c.Abort()
  109. return
  110. }
  111. // 切割room_id
  112. roomIDs := strings.Split(rooms, ",")
  113. if len(roomIDs) <= 0 {
  114. c.Set("output_data", "room_ids is not right")
  115. c.JSONMap(map[string]interface{}{"message": "room_ids is not right"}, ecode.RequestErr)
  116. c.Abort()
  117. return
  118. }
  119. tsInt, _ := strconv.ParseInt(ts, 10, 64)
  120. rids := []int64{}
  121. for _, v := range roomIDs {
  122. roomID, err := strconv.ParseInt(v, 10, 64)
  123. if err != nil {
  124. log.Warn("room id is not right")
  125. continue
  126. }
  127. rids = append(rids, roomID)
  128. }
  129. resp, err := srv.GetOriginScreenShotPic(c, rids, tsInt)
  130. if err != nil {
  131. c.Set("output_data", err.Error())
  132. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  133. c.Abort()
  134. return
  135. }
  136. c.Set("output_data", resp)
  137. c.JSONMap(map[string]interface{}{"data": map[string]interface{}{"list": resp}}, nil)
  138. }
  139. // getTimePeriodScreenShot 获取多个房间一个时间段内的截图
  140. func getTimePeriodScreenShot(c *bm.Context) {
  141. params := c.Request.URL.Query()
  142. room := params.Get("room_ids")
  143. // 2018-10-24 14:27:07
  144. start := params.Get("start_time")
  145. end := params.Get("end_time")
  146. channel := params.Get("channel")
  147. c.Set("input_params", params)
  148. if room == "" || start == "" || end == "" {
  149. c.Set("output_data", "some fields are empty")
  150. c.JSONMap(map[string]interface{}{"message": "some fields are empty"}, ecode.RequestErr)
  151. c.Abort()
  152. return
  153. }
  154. startTime, err := time.ParseInLocation("2006-01-02 15:04:05", start, time.Local)
  155. if err != nil {
  156. c.Set("output_data", "Start time format is incorrect")
  157. c.JSONMap(map[string]interface{}{"message": "Start time format is incorrect"}, ecode.RequestErr)
  158. c.Abort()
  159. return
  160. }
  161. endTime, err := time.ParseInLocation("2006-01-02 15:04:05", end, time.Local)
  162. if err != nil {
  163. c.Set("output_data", "End time format is incorrect")
  164. c.JSONMap(map[string]interface{}{"message": "End time format is incorrect"}, ecode.RequestErr)
  165. c.Abort()
  166. return
  167. }
  168. // 切割room_id
  169. roomIDs := strings.Split(room, ",")
  170. if len(roomIDs) <= 0 {
  171. c.Set("output_data", "room_ids is not right")
  172. c.JSONMap(map[string]interface{}{"message": "room_ids is not right"}, ecode.RequestErr)
  173. c.Abort()
  174. return
  175. }
  176. resp := map[int64][]string{}
  177. for _, v := range roomIDs {
  178. roomID, err := strconv.ParseInt(v, 10, 64)
  179. if err != nil {
  180. log.Warn("room id is not right")
  181. continue
  182. }
  183. urls, err := srv.GetSingleScreeShot(c, roomID, startTime.Unix(), endTime.Unix(), channel)
  184. if err != nil {
  185. log.Warn("%v", err)
  186. continue
  187. }
  188. resp[roomID] = urls
  189. }
  190. c.Set("output_data", resp)
  191. c.JSONMap(map[string]interface{}{"data": resp}, nil)
  192. }