live.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package assist
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "time"
  8. "go-common/app/interface/main/creative/model/assist"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. gtime "go-common/library/time"
  12. )
  13. const (
  14. // api
  15. _liveStatus = "/user/v1/UserRoom/isAnchor"
  16. _liveAddAssist = "/live_user/v1/RoomAdmin/add"
  17. _liveDelAssist = "/live_user/v1/RoomAdmin/del"
  18. _liveRevocBanned = "/live_user/v1/RoomSilent/del"
  19. _liveAssists = "/live_user/v1/RoomAdmin/get_by_anchor"
  20. _liveCheckAssist = "/live_user/v1/RoomAdmin/is_admin"
  21. )
  22. // LiveStatus check if user opened live room. 0:yes 500401: no
  23. func (d *Dao) LiveStatus(c context.Context, mid int64, ip string) (ok int8, err error) {
  24. var req *http.Request
  25. params := url.Values{}
  26. params.Set("uid", strconv.FormatInt(mid, 10))
  27. if req, err = http.NewRequest("GET", d.liveStatusURL+"?"+params.Encode(), nil); err != nil {
  28. log.Error("LiveStatus url(%s) error(%v)", d.liveStatusURL+"?"+params.Encode(), err)
  29. err = ecode.CreativeLiveErr
  30. return
  31. }
  32. req.Header.Set("X-BiliLive-UID", strconv.FormatInt(mid, 10))
  33. var res struct {
  34. Code int `json:"code"`
  35. }
  36. if err = d.client.Do(c, req, &res); err != nil {
  37. log.Error("LiveStatus url(%s) response(%+v) error(%v)", d.liveStatusURL+"?"+params.Encode(), res, err)
  38. err = ecode.CreativeLiveErr
  39. return
  40. }
  41. if res.Code == 500401 {
  42. return
  43. }
  44. if res.Code != 0 {
  45. log.Error("LiveStatus url(%s) res(%v)", d.liveStatusURL+"?"+params.Encode(), res)
  46. err = ecode.Int(res.Code)
  47. return
  48. }
  49. ok = 1
  50. return
  51. }
  52. // LiveAddAssist add assist in live room 120013: 直播间不存在
  53. func (d *Dao) LiveAddAssist(c context.Context, mid, assistMid int64, cookie, ip string) (err error) {
  54. var req *http.Request
  55. params := url.Values{}
  56. params.Set("admin", strconv.FormatInt(assistMid, 10))
  57. params.Set("anchor_id", strconv.FormatInt(mid, 10))
  58. if req, err = http.NewRequest("POST", d.liveAddAssistURL+"?"+params.Encode(), nil); err != nil {
  59. log.Error("LiveAddAssist url(%s) error(%v)", d.liveAddAssistURL+"?"+params.Encode(), err)
  60. err = ecode.CreativeLiveErr
  61. return
  62. }
  63. req.Header.Set("X-BiliLive-UID", strconv.FormatInt(mid, 10))
  64. req.Header.Set("Cookie", cookie)
  65. var res struct {
  66. Code int `json:"code"`
  67. Message string `json:"message"`
  68. }
  69. if err = d.client.Do(c, req, &res); err != nil {
  70. log.Error("LiveAddAssist url(%s) response(%+v) error(%v)", d.liveAddAssistURL+"?"+params.Encode(), res, err)
  71. err = ecode.CreativeLiveErr
  72. return
  73. }
  74. if res.Code == 120013 {
  75. log.Error("LiveAddAssist url(%s) mid(%d) not opened(%v)", d.liveAddAssistURL+"?"+params.Encode(), mid, 120013)
  76. err = ecode.CreativeLiveNotOpenErr
  77. return
  78. }
  79. if res.Code != 0 {
  80. log.Error("LiveAddAssist url(%s) res(%v)", d.liveAddAssistURL+"?"+params.Encode(), res)
  81. err = ecode.Int(res.Code)
  82. return
  83. }
  84. return
  85. }
  86. // LiveDelAssist del assist in live room
  87. func (d *Dao) LiveDelAssist(c context.Context, mid, assistMid int64, cookie, ip string) (err error) {
  88. var req *http.Request
  89. params := url.Values{}
  90. params.Set("admin", strconv.FormatInt(assistMid, 10))
  91. params.Set("anchor_id", strconv.FormatInt(mid, 10))
  92. if req, err = http.NewRequest("POST", d.liveDelAssistURL+"?"+params.Encode(), nil); err != nil {
  93. log.Error("LiveDelAssist url(%s) error(%v)", d.liveDelAssistURL+"?"+params.Encode(), err)
  94. err = ecode.CreativeLiveErr
  95. return
  96. }
  97. req.Header.Set("X-BiliLive-UID", strconv.FormatInt(mid, 10))
  98. req.Header.Set("Cookie", cookie)
  99. var res struct {
  100. Code int `json:"code"`
  101. Message string `json:"message"`
  102. }
  103. if err = d.client.Do(c, req, &res); err != nil {
  104. log.Error("LiveDelAssist url(%s) response(%+v) error(%v)", d.liveDelAssistURL+"?"+params.Encode(), res, err)
  105. err = ecode.CreativeLiveErr
  106. return
  107. }
  108. if res.Code == 120013 {
  109. log.Error("LiveDelAssist url(%s) mid(%d) not opened(%v)", d.liveDelAssistURL+"?"+params.Encode(), mid, 120013)
  110. err = ecode.CreativeLiveNotOpenErr
  111. return
  112. }
  113. if res.Code != 0 {
  114. log.Error("LiveDelAssist url(%s) res(%v)", d.liveDelAssistURL+"?"+params.Encode(), res)
  115. err = ecode.Int(res.Code)
  116. return
  117. }
  118. return
  119. }
  120. // LiveBannedRevoc recover assist banned state
  121. func (d *Dao) LiveBannedRevoc(c context.Context, mid int64, banID, cookie, ip string) (err error) {
  122. var req *http.Request
  123. params := url.Values{}
  124. params.Set("uid", strconv.FormatInt(mid, 10))
  125. params.Set("id", banID)
  126. if req, err = http.NewRequest("POST", d.liveRevocBannedURL+"?"+params.Encode(), nil); err != nil {
  127. log.Error("liveRevocBanned url(%s) error(%v)", d.liveRevocBannedURL+"?"+params.Encode(), err)
  128. err = ecode.CreativeLiveErr
  129. return
  130. }
  131. req.Header.Set("X-BiliLive-UID", strconv.FormatInt(mid, 10))
  132. req.Header.Set("Cookie", cookie)
  133. var res struct {
  134. Code int `json:"code"`
  135. Message string `json:"message"`
  136. }
  137. if err = d.client.Do(c, req, &res); err != nil {
  138. log.Error("liveRevocBanned url(%s) response(%+v) error(%v)", d.liveRevocBannedURL+"?"+params.Encode(), res, err)
  139. err = ecode.CreativeLiveErr
  140. return
  141. }
  142. if res.Code != 0 {
  143. log.Error("liveRevocBanned url(%s) res(%v)", d.liveRevocBannedURL+"?"+params.Encode(), res)
  144. err = ecode.Int(res.Code)
  145. return
  146. }
  147. return
  148. }
  149. // LiveAssists get assists from live
  150. func (d *Dao) LiveAssists(c context.Context, mid int64, ip string) (assists []*assist.LiveAssist, err error) {
  151. var req *http.Request
  152. params := url.Values{}
  153. params.Set("anchor_id", strconv.FormatInt(mid, 10))
  154. if req, err = http.NewRequest("GET", d.liveAssistsURL+"?"+params.Encode(), nil); err != nil {
  155. log.Error("LiveAssists url(%s) error(%v)", d.liveAssistsURL+"?"+params.Encode(), err)
  156. err = ecode.CreativeLiveErr
  157. return
  158. }
  159. req.Header.Set("X-BiliLive-UID", strconv.FormatInt(mid, 10))
  160. var res struct {
  161. Code int `json:"code"`
  162. Data []*assist.LiveAssist `json:"data"`
  163. }
  164. if err = d.client.Do(c, req, &res); err != nil {
  165. log.Error("LiveAssists url(%s) response(%+v) error(%v)", d.liveAssistsURL+"?"+params.Encode(), res, err)
  166. err = ecode.CreativeLiveErr
  167. return
  168. }
  169. if res.Code == 120013 {
  170. err = nil
  171. return
  172. }
  173. if res.Code != 0 {
  174. log.Error("LiveAssists url(%s) res(%v)", d.liveAssistsURL+"?"+params.Encode(), res)
  175. err = ecode.Int(res.Code)
  176. return
  177. }
  178. assists = res.Data
  179. // set ctime
  180. for _, ass := range assists {
  181. intTime, err := time.Parse("2006-01-02 15:04:05", ass.Datetime)
  182. if err == nil {
  183. var ctime gtime.Time
  184. ctime.Scan(intTime)
  185. ass.CTime = ctime - 8*3600 // adjust timezone for fe
  186. }
  187. }
  188. return
  189. }
  190. // LiveCheckAssist check assist in live room
  191. func (d *Dao) LiveCheckAssist(c context.Context, mid, assistMid int64, ip string) (isAss int8, err error) {
  192. var req *http.Request
  193. params := url.Values{}
  194. params.Set("uid", strconv.FormatInt(assistMid, 10))
  195. params.Set("anchor_id", strconv.FormatInt(mid, 10))
  196. if req, err = http.NewRequest("POST", d.liveCheckAssURL+"?"+params.Encode(), nil); err != nil {
  197. log.Error("LiveCheckAssist url(%s) error(%v)", d.liveCheckAssURL+"?"+params.Encode(), err)
  198. err = ecode.CreativeLiveErr
  199. return
  200. }
  201. req.Header.Set("X-BiliLive-UID", strconv.FormatInt(mid, 10))
  202. var res struct {
  203. Code int `json:"code"`
  204. }
  205. if err = d.client.Do(c, req, &res); err != nil {
  206. log.Error("LiveCheckAssist url(%s) response(%+v) error(%v)", d.liveCheckAssURL+"?"+params.Encode(), res, err)
  207. err = ecode.CreativeLiveErr
  208. return
  209. }
  210. if res.Code == 120013 {
  211. log.Error("LiveCheckAssist url(%s) mid(%d) not opened(%v)", d.liveCheckAssURL+"?"+params.Encode(), mid, 120013)
  212. err = ecode.CreativeLiveNotOpenErr
  213. return
  214. }
  215. if res.Code != 0 {
  216. log.Error("LiveCheckAssist url(%s) res(%v)", d.liveCheckAssURL+"?"+params.Encode(), res)
  217. err = ecode.Int(res.Code)
  218. return
  219. }
  220. isAss = 1
  221. return
  222. }