api.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package archive
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "encoding/hex"
  7. "encoding/json"
  8. "go-common/app/interface/main/creative/dao/tool"
  9. "go-common/app/interface/main/videoup/conf"
  10. "go-common/app/interface/main/videoup/model/archive"
  11. pordermdl "go-common/app/interface/main/videoup/model/porder"
  12. upapi "go-common/app/service/main/up/api/v1"
  13. "go-common/library/ecode"
  14. "go-common/library/log"
  15. "go-common/library/sync/errgroup"
  16. "net/http"
  17. "net/url"
  18. "strconv"
  19. "sync"
  20. "time"
  21. )
  22. const (
  23. _viewURL = "/videoup/view"
  24. _addURL = "/videoup/add"
  25. _editURL = "/videoup/edit"
  26. _tagUpURL = "/videoup/tag/up"
  27. _applyStaffs = "/videoup/staff/archive/applys"
  28. // StaffWhiteGroupID const
  29. StaffWhiteGroupID = int64(24)
  30. )
  31. // View get archive and videos.
  32. func (d *Dao) View(c context.Context, aid int64, ip string) (a *archive.Archive, vs []*archive.Video, err error) {
  33. params := url.Values{}
  34. params.Set("aid", strconv.FormatInt(aid, 10))
  35. var res struct {
  36. Code int `json:"code"`
  37. Message string `json:"message"`
  38. Data struct {
  39. Archive *archive.Archive `json:"archive"`
  40. Videos []*archive.Video `json:"videos"`
  41. } `json:"data"`
  42. }
  43. if err = d.httpR.Get(c, d.viewURI, ip, params, &res); err != nil {
  44. log.Error("videoup view archive error(%v) | viewUri(%s) aid(%d) ip(%s) params(%v)", err, d.viewURI+"?"+params.Encode(), aid, ip, params)
  45. err = ecode.CreativeArchiveAPIErr
  46. return
  47. }
  48. if res.Code != 0 {
  49. err = ecode.Error(ecode.Int(res.Code), res.Message)
  50. log.Error("videoup view archive res code nq zero, res.Code(%d) | viewUri(%s) aid(%d) ip(%s) params(%v) res(%v)", res.Code, d.viewURI+"?"+params.Encode(), aid, ip, params, res)
  51. return
  52. }
  53. a = res.Data.Archive
  54. vs = res.Data.Videos
  55. return
  56. }
  57. // ApplyStaffs fn
  58. func (d *Dao) ApplyStaffs(c context.Context, aid int64, ip string) (staffs []*archive.Staff, err error) {
  59. params := url.Values{}
  60. params.Set("aid", strconv.FormatInt(aid, 10))
  61. var res struct {
  62. Code int `json:"code"`
  63. Message string `json:"message"`
  64. Data []*archive.StaffView `json:"data"`
  65. }
  66. if err = d.httpR.Get(c, d.applyStaffs, ip, params, &res); err != nil {
  67. log.Error("videoup view archive error(%v) | viewUri(%s) aid(%d) ip(%s) params(%v)", err, d.viewURI+"?"+params.Encode(), aid, ip, params)
  68. err = ecode.CreativeArchiveAPIErr
  69. return
  70. }
  71. if res.Code != 0 {
  72. log.Error("videoup view archive res code nq zero, res.Code(%d) | viewUri(%s) aid(%d) ip(%s) params(%v) res(%v)", res.Code, d.viewURI+"?"+params.Encode(), aid, ip, params, res)
  73. err = ecode.CreativeArchiveAPIErr
  74. return
  75. }
  76. for _, v := range res.Data {
  77. staff := &archive.Staff{
  78. Mid: v.ApMID,
  79. Title: v.ApTitle,
  80. }
  81. staffs = append(staffs, staff)
  82. }
  83. return
  84. }
  85. // Add add archive and videos.
  86. func (d *Dao) Add(c context.Context, ap *archive.ArcParam, ip string) (aid int64, err error) {
  87. params := url.Values{}
  88. params.Set("appkey", d.c.App.Key)
  89. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  90. mh := md5.Sum([]byte(params.Encode() + d.c.App.Secret))
  91. params.Set("sign", hex.EncodeToString(mh[:]))
  92. var (
  93. uri = d.addURI + "?" + params.Encode()
  94. )
  95. bs, err := json.Marshal(ap)
  96. if err != nil {
  97. log.Error("json.Marshal error(%v) | ap(%v) ap.Mid(%d) ap.videos(%v)", err, ap, ap.Mid, ap.Videos)
  98. return
  99. }
  100. req, err := http.NewRequest("POST", uri, bytes.NewReader(bs))
  101. if err != nil {
  102. log.Error("http.NewRequest error(%v) | uri(%s) ap.Mid(%d)", err, uri, ap.Mid)
  103. return
  104. }
  105. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  106. var res struct {
  107. Code int `json:"code"`
  108. Message string `json:"message"`
  109. Data struct {
  110. Aid int64 `json:"aid"`
  111. } `json:"data"`
  112. }
  113. if err = d.httpW.Do(c, req, &res); err != nil {
  114. log.Error("d.Add error(%v) | uri(%s) ap(%+v)", err, uri, ap)
  115. err = ecode.CreativeArchiveAPIErr
  116. return
  117. }
  118. if res.Code != 0 {
  119. err = ecode.Error(ecode.Int(res.Code), res.Message)
  120. log.Error("d.Add nq zero (%v)|(%v)|(%v)|(%v)|uri(%s),ap(%+v)", res.Code, res.Message, res.Data, err, uri, ap)
  121. return
  122. }
  123. log.Info("d.Add (%s)|res.Data.Aid(%d) ip(%s) ", string(bs), res.Data.Aid, ip)
  124. aid = res.Data.Aid
  125. return
  126. }
  127. // Edit edit archive and videos.
  128. func (d *Dao) Edit(c context.Context, ap *archive.ArcParam, ip string) (err error) {
  129. params := url.Values{}
  130. params.Set("appkey", conf.Conf.App.Key)
  131. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  132. mh := md5.Sum([]byte(params.Encode() + d.c.App.Secret))
  133. params.Set("sign", hex.EncodeToString(mh[:]))
  134. // uri
  135. var (
  136. uri = d.editURI + "?" + params.Encode()
  137. )
  138. // new request
  139. bs, err := json.Marshal(ap)
  140. if err != nil {
  141. log.Error("json.Marshal ap error (%v) | ap(%v)", err, ap)
  142. return
  143. }
  144. req, err := http.NewRequest("POST", uri, bytes.NewReader(bs))
  145. if err != nil {
  146. log.Error("http.NewRequest error(%v) | uri(%s) ap(%v)", err, uri, ap)
  147. return
  148. }
  149. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  150. var res struct {
  151. Code int `json:"code"`
  152. Message string `json:"message"`
  153. }
  154. if err = d.httpW.Do(c, req, &res); err != nil {
  155. log.Error("d.Edit error(%v) | uri(%s) ap(%+v)", err, uri, ap)
  156. err = ecode.CreativeArchiveAPIErr
  157. return
  158. }
  159. if res.Code != 0 {
  160. err = ecode.Error(ecode.Int(res.Code), res.Message)
  161. log.Error("d.Add nq zero (%v)|(%v)|(%v)|uri(%s),ap(%+v)", res.Code, res.Message, err, uri, ap)
  162. return
  163. }
  164. log.Info("d.Edit(%s) | ip(%s)", string(bs), ip)
  165. return
  166. }
  167. // DescFormat fn
  168. func (d *Dao) DescFormat(c context.Context) (descFormats map[int]*archive.DescFormat, err error) {
  169. var res struct {
  170. Code int `json:"code"`
  171. Message string `json:"message"`
  172. Data []*archive.DescFormat `json:"data"`
  173. }
  174. descFormats = make(map[int]*archive.DescFormat)
  175. if err = d.httpR.Get(c, d.descFormatURI, "", nil, &res); err != nil {
  176. log.Error("videoup descFormat error(%v) | descFormatURI(%s) err(%v)", err, d.descFormatURI, err)
  177. err = ecode.CreativeArchiveAPIErr
  178. return
  179. }
  180. if res.Code != 0 {
  181. err = ecode.Error(ecode.Int(res.Code), res.Message)
  182. log.Error("videoup descFormat res.Code(%d) | descFormatURI(%s) res(%v) err(%v)", res.Code, d.descFormatURI, res, err)
  183. return
  184. }
  185. for _, v := range res.Data {
  186. descFormats[v.ID] = v
  187. }
  188. return
  189. }
  190. // TagUp fn
  191. func (d *Dao) TagUp(c context.Context, aid int64, tag, ip string) (err error) {
  192. params := url.Values{}
  193. params.Set("aid", strconv.Itoa(int(aid)))
  194. params.Set("tag", tag)
  195. var res struct {
  196. Code int `json:"code"`
  197. }
  198. if err = d.httpW.Post(c, d.tagUpURI, ip, params, &res); err != nil {
  199. log.Error("Post(%s,%s,%s) err(%v)", d.tagUpURI, ip, params.Encode(), err)
  200. err = ecode.CreativeArchiveAPIErr
  201. return
  202. }
  203. if res.Code != 0 {
  204. log.Error("Code(%s,%s,%s) err(%v), code(%d)", d.tagUpURI, ip, params.Encode(), err, res.Code)
  205. err = ecode.CreativeArchiveAPIErr
  206. return
  207. }
  208. return
  209. }
  210. // PorderCfgList fn
  211. func (d *Dao) PorderCfgList(c context.Context) (cfgs map[int64]*pordermdl.Config, err error) {
  212. cfgs = make(map[int64]*pordermdl.Config)
  213. var res struct {
  214. Code int `json:"code"`
  215. Data []*pordermdl.Config `json:"data"`
  216. }
  217. if err = d.httpR.Get(c, d.porderConfigURL, "", nil, &res); err != nil {
  218. log.Error("archive.porderConfigURL url(%s) error(%v)", d.porderConfigURL, err)
  219. err = ecode.CreativeArchiveAPIErr
  220. return
  221. }
  222. if res.Code != 0 {
  223. log.Error("archive.porderConfigURL url(%s) res(%v)", d.porderConfigURL, res)
  224. err = ecode.CreativeArchiveAPIErr
  225. return
  226. }
  227. for _, cfg := range res.Data {
  228. cfgs[cfg.ID] = cfg
  229. }
  230. return
  231. }
  232. // GameList fn
  233. func (d *Dao) GameList(c context.Context) (gameMap map[int64]*pordermdl.Game, err error) {
  234. gameMap = make(map[int64]*pordermdl.Game)
  235. params := url.Values{}
  236. params.Set("appkey", conf.Conf.Game.App.Key)
  237. params.Set("appsecret", conf.Conf.Game.App.Secret)
  238. params.Set("ts", strconv.FormatInt(time.Now().UnixNano()/1000000, 10))
  239. var res struct {
  240. Code int `json:"code"`
  241. Data []*pordermdl.Game `json:"data"`
  242. }
  243. var (
  244. query, _ = tool.Sign(params)
  245. url string
  246. )
  247. url = d.gameListURL + "?" + query
  248. req, err := http.NewRequest("GET", url, nil)
  249. if err != nil {
  250. log.Error("http.NewRequest(%s) error(%v), ", url, err)
  251. err = ecode.CreativeGameOpenAPIErr
  252. return
  253. }
  254. if err = d.httpR.Do(c, req, &res); err != nil {
  255. log.Error("d.httpR.Do(%s) error(%v);", url, err)
  256. err = ecode.CreativeGameOpenAPIErr
  257. return
  258. }
  259. log.Info("GameList url(%+v)|gameLen(%+v)", url, len(res.Data))
  260. if res.Code != 0 {
  261. log.Error("GameList api url(%s) res(%v);, code(%d)", url, res, res.Code)
  262. err = ecode.CreativeGameOpenAPIErr
  263. return
  264. }
  265. for _, data := range res.Data {
  266. gameMap[data.GameBaseID] = data
  267. }
  268. return
  269. }
  270. //StaffUps 联合投稿白名单
  271. func (d *Dao) StaffUps(c context.Context) (ups map[int64]int64, err error) {
  272. return d.UpSpecial(c, StaffWhiteGroupID)
  273. }
  274. // StaffTypeConfig 获取联合投稿分区配置
  275. func (d *Dao) StaffTypeConfig(c context.Context) (isGary bool, typeConf map[int16]*archive.StaffTypeConf, err error) {
  276. typeConf = make(map[int16]*archive.StaffTypeConf)
  277. params := url.Values{}
  278. var res struct {
  279. Code int `json:"code"`
  280. Data struct {
  281. IsGary bool `json:"is_gary"`
  282. TypeList []*archive.StaffTypeConf `json:"typelist"`
  283. } `json:"data"`
  284. }
  285. if err = d.httpR.Get(c, d.staffConfigURI, "", params, &res); err != nil {
  286. log.Error("StaffTypeConfig error(%v) | staffConfigURI(%s)", err, d.staffConfigURI)
  287. err = ecode.CreativeArchiveAPIErr
  288. return
  289. }
  290. log.Info("StaffTypeConfig url(%+v)|res(%+v)", d.staffConfigURI, res)
  291. if res.Code != 0 || res.Data.TypeList == nil {
  292. log.Error("StaffTypeConfig api url(%s) res(%+v);, code(%d)", d.staffConfigURI, res, res.Code)
  293. err = ecode.CreativeArchiveAPIErr
  294. return
  295. }
  296. isGary = res.Data.IsGary
  297. for _, v := range res.Data.TypeList {
  298. typeConf[v.TypeID] = v
  299. }
  300. return
  301. }
  302. // UpSpecial 获取UP主的特殊用户组
  303. func (d *Dao) UpSpecial(c context.Context, gpid int64) (ups map[int64]int64, err error) {
  304. var (
  305. res *upapi.UpGroupMidsReply
  306. page int
  307. g errgroup.Group
  308. l sync.RWMutex
  309. )
  310. if res, err = d.UpClient.UpGroupMids(c, &upapi.UpGroupMidsReq{
  311. GroupID: gpid,
  312. Pn: 1,
  313. Ps: 1,
  314. }); err != nil {
  315. log.Error("UpSpecial d.UpSpecial gpid(%d)|error(%v)", gpid, err)
  316. return
  317. }
  318. log.Warn("UpSpecial get total: gpid(%d)|total(%d)", gpid, res.Total)
  319. if res.Total <= 0 {
  320. return
  321. }
  322. ups = make(map[int64]int64, res.Total)
  323. ps := int(10000)
  324. pageNum := res.Total / ps
  325. if res.Total%ps != 0 {
  326. pageNum++
  327. }
  328. for page = 1; page <= pageNum; page++ {
  329. tmpPage := page
  330. g.Go(func() (err error) {
  331. resgg, err := d.UpClient.UpGroupMids(c, &upapi.UpGroupMidsReq{
  332. GroupID: gpid,
  333. Pn: tmpPage,
  334. Ps: ps,
  335. })
  336. if err != nil {
  337. log.Error("d.UpGroupMids gg (%d,%d,%d) error(%v) ", gpid, tmpPage, ps, err)
  338. err = nil
  339. return
  340. }
  341. for _, mid := range resgg.Mids {
  342. l.Lock()
  343. ups[mid] = mid
  344. l.Unlock()
  345. }
  346. return
  347. })
  348. }
  349. g.Wait()
  350. log.Warn("UpSpecial get result: gpid,total,midslen,upslens (%d)|(%d)|(%d)", gpid, res.Total, len(ups))
  351. return
  352. }