feedback.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. package http
  2. import (
  3. "io/ioutil"
  4. "mime/multipart"
  5. "path"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "time"
  10. "unicode/utf8"
  11. "go-common/app/interface/main/feedback/conf"
  12. "go-common/app/interface/main/feedback/model"
  13. "go-common/library/ecode"
  14. "go-common/library/log"
  15. bm "go-common/library/net/http/blademaster"
  16. "go-common/library/sync/errgroup"
  17. )
  18. const (
  19. _timeFromat = "2006-01-02 15:04:05"
  20. )
  21. // addReply
  22. func addReply(c *bm.Context) {
  23. header := c.Request.Header
  24. params := c.Request.Form
  25. // params
  26. buvid := header.Get("Buvid")
  27. system := params.Get("system")
  28. version := params.Get("version")
  29. midStr := params.Get("mid")
  30. content := params.Get("content")
  31. imgURL := params.Get("img_url")
  32. logURL := params.Get("log_url")
  33. device := params.Get("device")
  34. channel := params.Get("channel")
  35. entrance := params.Get("entrance")
  36. netState := params.Get("net_state")
  37. netOperator := params.Get("net_operator")
  38. agencyArea := params.Get("agency_area")
  39. platform := params.Get("platform")
  40. browser := params.Get("browser")
  41. qq := params.Get("qq")
  42. mobiApp := params.Get("mobi_app")
  43. email := params.Get("email")
  44. tagStr := params.Get("tag_id")
  45. // check params
  46. if buvid == "" {
  47. c.JSON(nil, ecode.RequestErr)
  48. return
  49. }
  50. if system == "" {
  51. c.JSON(nil, ecode.RequestErr)
  52. return
  53. }
  54. if version == "" {
  55. c.JSON(nil, ecode.RequestErr)
  56. return
  57. }
  58. var (
  59. mid int64
  60. err error
  61. )
  62. if midStr != "" {
  63. if mid, err = strconv.ParseInt(midStr, 10, 64); err != nil {
  64. c.JSON(nil, ecode.RequestErr)
  65. return
  66. }
  67. }
  68. if content == "" && imgURL == "" && logURL == "" {
  69. c.JSON(nil, ecode.RequestErr)
  70. return
  71. }
  72. tagID, _ := strconv.ParseInt(tagStr, 10, 64)
  73. c.JSON(feedbackSvr.AddReply(c, mid, tagID, buvid, system, version, mobiApp, filtered(content), imgURL, logURL, device, channel, entrance, netState, netOperator, agencyArea, platform, browser, qq, email, time.Now()))
  74. }
  75. func addWebReply(c *bm.Context) {
  76. params := c.Request.Form
  77. // system := params.Get("system")
  78. version := params.Get("version")
  79. midStr := params.Get("mid")
  80. sidStr := params.Get("session_id")
  81. content := params.Get("content")
  82. imgURL := params.Get("img_url")
  83. logURL := params.Get("log_url")
  84. // device := params.Get("device")
  85. buvid := params.Get("buvid")
  86. // channel := params.Get("channel")
  87. netState := params.Get("net_state")
  88. netOperator := params.Get("net_operator")
  89. agencyArea := params.Get("agency_area")
  90. platform := params.Get("platform")
  91. browser := params.Get("browser")
  92. qq := params.Get("qq")
  93. email := params.Get("email")
  94. tagStr := params.Get("tag_id")
  95. aidStr := params.Get("aid")
  96. var (
  97. mid int64
  98. sid int64
  99. err error
  100. )
  101. if mid, err = strconv.ParseInt(midStr, 10, 64); err != nil {
  102. c.JSON(nil, ecode.RequestErr)
  103. return
  104. }
  105. if content == "" && imgURL == "" && logURL == "" {
  106. c.JSON(nil, ecode.RequestErr)
  107. return
  108. }
  109. sid, _ = strconv.ParseInt(sidStr, 10, 64)
  110. tagID, _ := strconv.ParseInt(tagStr, 10, 64)
  111. if platform == "" {
  112. platform = "ugc"
  113. }
  114. c.JSON(feedbackSvr.AddWebReply(c, mid, sid, tagID, aidStr, filtered(content), imgURL, netState, netOperator, agencyArea, platform, version, buvid, browser, qq, email, time.Now()))
  115. }
  116. //replys
  117. func replys(c *bm.Context) {
  118. header := c.Request.Header
  119. params := c.Request.Form
  120. buvid := header.Get("Buvid")
  121. system := params.Get("system")
  122. version := params.Get("version")
  123. midStr := params.Get("mid")
  124. platform := params.Get("platform")
  125. mobiApp := params.Get("mobi_app")
  126. device := params.Get("device")
  127. pnStr := params.Get("pn")
  128. psStr := params.Get("ps")
  129. entrance := params.Get("entrance")
  130. // check params
  131. if system == "" {
  132. c.JSON(nil, ecode.RequestErr)
  133. return
  134. }
  135. if version == "" {
  136. c.JSON(nil, ecode.RequestErr)
  137. return
  138. }
  139. var (
  140. mid int64
  141. err error
  142. )
  143. if midStr != "" {
  144. if mid, err = strconv.ParseInt(midStr, 10, 64); err != nil {
  145. c.JSON(nil, ecode.RequestErr)
  146. return
  147. }
  148. }
  149. if mid == 0 && buvid == "" {
  150. c.JSON(nil, ecode.RequestErr)
  151. return
  152. }
  153. pn, err := strconv.Atoi(pnStr)
  154. if err != nil || pn < 1 {
  155. pn = 1
  156. }
  157. ps, err := strconv.Atoi(psStr)
  158. if err != nil || ps < 1 || ps > conf.Conf.Feedback.ReplysNum {
  159. ps = conf.Conf.Feedback.ReplysNum
  160. }
  161. rs, isEndReply, err := feedbackSvr.Replys(c, buvid, platform, mobiApp, device, system, version, entrance, mid, pn, ps)
  162. if err != nil {
  163. c.JSON(nil, err)
  164. return
  165. }
  166. res := map[string]interface{}{
  167. "data": rs,
  168. "is_end_reply": isEndReply,
  169. }
  170. c.JSONMap(res, err)
  171. }
  172. func sessions(c *bm.Context) {
  173. params := c.Request.Form
  174. tagid := params.Get("tag_id")
  175. platform := params.Get("platform")
  176. stateStr := params.Get("state")
  177. midStr := params.Get("mid")
  178. start := params.Get("start")
  179. end := params.Get("end")
  180. pnStr := params.Get("pn")
  181. psStr := params.Get("ps")
  182. mid, err := strconv.ParseInt(midStr, 10, 64)
  183. if err != nil {
  184. c.JSON(nil, ecode.RequestErr)
  185. return
  186. }
  187. var (
  188. etime time.Time
  189. )
  190. if start == "" {
  191. start = "00-00-00 00:00:00"
  192. }
  193. stime, _ := time.Parse(_timeFromat, start)
  194. etime, _ = time.Parse(_timeFromat, end)
  195. if end == "" {
  196. etime = time.Now()
  197. }
  198. pn, err := strconv.Atoi(pnStr)
  199. if err != nil || pn < 1 {
  200. pn = 1
  201. }
  202. ps, err := strconv.Atoi(psStr)
  203. if err != nil || ps < 0 || ps > 10 {
  204. ps = 10
  205. }
  206. total, sessions, err := feedbackSvr.Sessions(c, mid, stateStr, tagid, platform, stime, etime, ps, pn)
  207. if err != nil {
  208. c.JSON(nil, err)
  209. return
  210. }
  211. res := map[string]interface{}{
  212. "data": sessions,
  213. "total": total,
  214. }
  215. c.JSONMap(res, err)
  216. }
  217. func sessionsClose(c *bm.Context) {
  218. params := c.Request.Form
  219. sidStr := params.Get("session_id")
  220. sid, err := strconv.ParseInt(sidStr, 10, 64)
  221. if err != nil {
  222. c.JSON(nil, ecode.RequestErr)
  223. return
  224. }
  225. c.JSON(nil, feedbackSvr.UpdateSessionState(c, 3, sid))
  226. }
  227. func replyTag(c *bm.Context) {
  228. params := c.Request.Form
  229. tp := params.Get("type") // NOTE: player
  230. platform := params.Get("platform")
  231. c.JSON(model.Tags[tp][platform], nil)
  232. }
  233. func ugcTag(c *bm.Context) {
  234. params := c.Request.Form
  235. tp := params.Get("type")
  236. platform := params.Get("platform")
  237. mold, err := strconv.Atoi(tp)
  238. if err != nil {
  239. c.JSON(nil, ecode.RequestErr)
  240. return
  241. }
  242. midStr := params.Get("mid")
  243. mid, err := strconv.ParseInt(midStr, 10, 64)
  244. if err != nil {
  245. c.JSON(nil, ecode.RequestErr)
  246. return
  247. }
  248. c.JSON(feedbackSvr.Tags(c, mid, mold, platform))
  249. }
  250. func webReply(c *bm.Context) {
  251. params := c.Request.Form
  252. sidStr := params.Get("session_id")
  253. sid, err := strconv.ParseInt(sidStr, 10, 64)
  254. if err != nil {
  255. c.JSON(nil, ecode.RequestErr)
  256. return
  257. }
  258. midStr := params.Get("mid")
  259. mid, err := strconv.ParseInt(midStr, 10, 64)
  260. if err != nil {
  261. c.JSON(nil, ecode.RequestErr)
  262. return
  263. }
  264. c.JSON(feedbackSvr.WebReplys(c, sid, mid))
  265. }
  266. func filtered(content string) string {
  267. v := make([]rune, 0, len(content))
  268. for _, c := range content {
  269. if c != utf8.RuneError {
  270. v = append(v, c)
  271. }
  272. }
  273. return string(v)
  274. }
  275. func playerCheck(c *bm.Context) {
  276. var (
  277. params = c.Request.Form
  278. header = c.Request.Header
  279. platform, ipChangeTimes int
  280. mid, checkTime, aid, connectSpeed, ioSpeed int64
  281. region, school string
  282. err error
  283. )
  284. platformStr := params.Get("platform")
  285. if platform = model.FormPlatForm(platformStr); platform == 0 {
  286. c.JSON(nil, ecode.RequestErr)
  287. return
  288. }
  289. if region = params.Get("region"); region == "" {
  290. c.JSON(nil, ecode.RequestErr)
  291. return
  292. }
  293. if school = params.Get("school"); school == "" {
  294. c.JSON(nil, ecode.RequestErr)
  295. return
  296. }
  297. midStr := params.Get("mid")
  298. if mid, err = strconv.ParseInt(midStr, 10, 64); err != nil {
  299. c.JSON(nil, ecode.RequestErr)
  300. return
  301. }
  302. checkTimeStr := params.Get("check_time")
  303. if checkTime, err = strconv.ParseInt(checkTimeStr, 10, 64); err != nil {
  304. c.JSON(nil, ecode.RequestErr)
  305. return
  306. }
  307. aid, _ = strconv.ParseInt(params.Get("aid"), 10, 64)
  308. ipChangeTimesStr := params.Get("ip_change_times")
  309. if ipChangeTimes, err = strconv.Atoi(ipChangeTimesStr); err != nil {
  310. c.JSON(nil, ecode.RequestErr)
  311. return
  312. }
  313. connectSpeedStr := params.Get("connect_speed")
  314. if connectSpeed, err = strconv.ParseInt(connectSpeedStr, 10, 64); err != nil {
  315. c.JSON(nil, ecode.RequestErr)
  316. return
  317. }
  318. ioSpeedStr := params.Get("io_speed")
  319. if ioSpeed, err = strconv.ParseInt(ioSpeedStr, 10, 64); err != nil {
  320. c.JSON(nil, ecode.RequestErr)
  321. return
  322. }
  323. c.JSON(nil, feedbackSvr.PlayerCheck(c, platform, ipChangeTimes, mid, checkTime, aid, connectSpeed, ioSpeed, region, school, header.Get("X-Cache-Server-Addr")))
  324. }
  325. // addReplyH5 add from H5
  326. func addReplyH5(c *bm.Context) {
  327. c.Request.ParseMultipartForm(model.MaxUploadSize)
  328. header := c.Request.Header
  329. params := c.Request.MultipartForm
  330. buvid := header.Get("Buvid")
  331. var system, version, midStr, content, logURL, device, channel, entrance, netState, netOperator, agencyArea, platform, browser, qq, mobiApp, email, tagStr string
  332. // params
  333. if len(params.Value["system"]) > 0 {
  334. system = params.Value["system"][0]
  335. }
  336. if len(params.Value["version"]) > 0 {
  337. version = params.Value["version"][0]
  338. }
  339. if len(params.Value["mid"]) > 0 {
  340. midStr = params.Value["mid"][0]
  341. }
  342. if len(params.Value["content"]) > 0 {
  343. content = params.Value["content"][0]
  344. }
  345. if len(params.Value["log_url"]) > 0 {
  346. logURL = params.Value["log_url"][0]
  347. }
  348. if len(params.Value["device"]) > 0 {
  349. device = params.Value["device"][0]
  350. }
  351. if len(params.Value["channel"]) > 0 {
  352. channel = params.Value["channel"][0]
  353. }
  354. if len(params.Value["entrance"]) > 0 {
  355. entrance = params.Value["entrance"][0]
  356. }
  357. if len(params.Value["net_state"]) > 0 {
  358. netState = params.Value["net_state"][0]
  359. }
  360. if len(params.Value["net_operator"]) > 0 {
  361. netOperator = params.Value["net_operator"][0]
  362. }
  363. if len(params.Value["agency_area"]) > 0 {
  364. agencyArea = params.Value["agency_area"][0]
  365. }
  366. if len(params.Value["platform"]) > 0 {
  367. platform = params.Value["platform"][0]
  368. }
  369. if len(params.Value["browser"]) > 0 {
  370. browser = params.Value["browser"][0]
  371. }
  372. if len(params.Value["qq"]) > 0 {
  373. qq = params.Value["qq"][0]
  374. }
  375. if len(params.Value["mobi_app"]) > 0 {
  376. mobiApp = params.Value["mobi_app"][0]
  377. }
  378. if len(params.Value["email"]) > 0 {
  379. email = params.Value["email"][0]
  380. }
  381. if len(params.Value["buvid"]) > 0 && buvid == "" {
  382. buvid = params.Value["buvid"][0]
  383. }
  384. if len(params.Value["tag_id"]) > 0 {
  385. tagStr = params.Value["tag_id"][0]
  386. }
  387. // check params
  388. if buvid == "" {
  389. c.JSON(nil, ecode.RequestErr)
  390. return
  391. }
  392. if system == "" {
  393. c.JSON(nil, ecode.RequestErr)
  394. return
  395. }
  396. if version == "" {
  397. c.JSON(nil, ecode.RequestErr)
  398. return
  399. }
  400. var (
  401. mid int64
  402. err error
  403. )
  404. if midStr != "" {
  405. if mid, err = strconv.ParseInt(midStr, 10, 64); err != nil {
  406. c.JSON(nil, ecode.RequestErr)
  407. return
  408. }
  409. }
  410. imgs := c.Request.MultipartForm.File["imgs"]
  411. var (
  412. imgURL string
  413. urls []string
  414. mutex = sync.Mutex{}
  415. )
  416. g, ctx := errgroup.WithContext(c)
  417. for k, fh := range imgs {
  418. if k == conf.Conf.Feedback.ImgLimit {
  419. break
  420. }
  421. var (
  422. img multipart.File
  423. url string
  424. fileName string
  425. fileTpye string
  426. body []byte
  427. )
  428. if img, err = fh.Open(); err != nil {
  429. log.Error("H5 addReply Open %s failed", fh.Filename)
  430. err = nil
  431. continue
  432. }
  433. defer img.Close()
  434. fileName = fh.Filename
  435. fileTpye = strings.TrimPrefix(path.Ext(fileName), ".")
  436. if body, err = ioutil.ReadAll(img); err != nil {
  437. log.Error("H5 addReply ioutil.ReadAll %s failed", fh.Filename)
  438. err = nil
  439. continue
  440. }
  441. g.Go(func() (err error) {
  442. if url, err = feedbackSvr.Upload(ctx, "", fileTpye, time.Now(), body); err != nil {
  443. log.Error("H5 addReply Upload %s failed", fh.Filename)
  444. err = nil
  445. return
  446. }
  447. mutex.Lock()
  448. urls = append(urls, url)
  449. mutex.Unlock()
  450. return
  451. })
  452. }
  453. if err = g.Wait(); err != nil {
  454. log.Error("%+v", err)
  455. return
  456. }
  457. if len(urls) > 0 {
  458. imgURL = strings.Join(urls, ";")
  459. }
  460. if content == "" && imgURL == "" && logURL == "" {
  461. c.JSON(nil, ecode.RequestErr)
  462. return
  463. }
  464. tagID, _ := strconv.ParseInt(tagStr, 10, 64)
  465. c.JSON(feedbackSvr.AddReply(c, mid, tagID, buvid, system, version, mobiApp, filtered(content), imgURL, logURL, device, channel, entrance, netState, netOperator, agencyArea, platform, browser, qq, email, time.Now()))
  466. }