api.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. package elec
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "time"
  8. "go-common/app/interface/main/creative/conf"
  9. "go-common/app/interface/main/creative/dao/tool"
  10. "go-common/app/interface/main/creative/model/elec"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. )
  14. const (
  15. _userInfoURI = "/internal/member/info"
  16. _userJoinURI = "/internal/member/elec/partin"
  17. _userExitURI = "/internal/member/elec/exit"
  18. _arcOpenURI = "/internal/archice/partin"
  19. _arcCloseURI = "/internal/archice/exit"
  20. _notifyURI = "/internal/notify/info"
  21. _getStatusURI = "/api/user/queryset/v2"
  22. _setStatusURI = "/api/user/modifyset/v2"
  23. _recentRankURI = "/api/query.recent.do"
  24. _currentRankURI = "/api/query.rank.do"
  25. _totalRankURI = "/api/query.total.rank.do"
  26. _dailyBillURI = "/api/query.daily.bill.do"
  27. _balanceURI = "/api/query.wallet.balance.do"
  28. _recentElecURI = "/api/recent/elec"
  29. _remarkListURI = "/api/elec/remark/list"
  30. _remarkDetailURI = "/api/elec/remake/detail"
  31. _remarkURI = "/api/remake/reply"
  32. )
  33. // UserInfo get user elec info.
  34. func (d *Dao) UserInfo(c context.Context, mid int64, ip string) (st *elec.UserInfo, err error) {
  35. params := url.Values{}
  36. params.Set("mid", strconv.FormatInt(mid, 10))
  37. var res struct {
  38. Code int `json:"code"`
  39. Data *elec.UserInfo
  40. }
  41. if err = d.client.Get(c, d.userInfoURL, ip, params, &res); err != nil {
  42. log.Error("elec url(%s) response(%v) error(%v)", d.userInfoURL+"?"+params.Encode(), res, err)
  43. err = ecode.CreativeElecErr
  44. return
  45. }
  46. if res.Code != 0 {
  47. log.Error("elec url(%s) res(%v)", d.userInfoURL, res)
  48. err = ecode.CreativeElecErr
  49. return
  50. }
  51. st = res.Data
  52. return
  53. }
  54. // UserUpdate join or exit elec.
  55. func (d *Dao) UserUpdate(c context.Context, mid int64, st int8, ip string) (u *elec.UserInfo, err error) {
  56. params := url.Values{}
  57. params.Set("appkey", conf.Conf.App.Key)
  58. params.Set("appsecret", conf.Conf.App.Secret)
  59. params.Set("mid", strconv.FormatInt(mid, 10))
  60. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  61. // url
  62. var (
  63. query, _ = tool.Sign(params)
  64. url string
  65. )
  66. if st == 1 {
  67. url = d.userJoinURL + "?" + query
  68. } else if st == 2 {
  69. url = d.userExitURL + "?" + query
  70. }
  71. // new requests
  72. req, err := http.NewRequest("POST", url, nil)
  73. if err != nil {
  74. log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
  75. err = ecode.CreativeElecErr
  76. return
  77. }
  78. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  79. var res struct {
  80. Code int `json:"code"`
  81. Data *elec.UserInfo
  82. }
  83. if err = d.client.Do(c, req, &res); err != nil {
  84. log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
  85. err = ecode.CreativeElecErr
  86. return
  87. }
  88. if res.Code != 0 {
  89. log.Error("user elec update state url(%s) res(%v); mid(%d), ip(%s), code(%d)", url, res, mid, ip, res.Code)
  90. err = ecode.CreativeElecErr
  91. }
  92. u = res.Data
  93. return
  94. }
  95. // ArcUpdate arc open or close elec.
  96. func (d *Dao) ArcUpdate(c context.Context, mid, aid int64, st int8, ip string) (err error) {
  97. params := url.Values{}
  98. params.Set("appkey", conf.Conf.App.Key)
  99. params.Set("appsecret", conf.Conf.App.Secret)
  100. params.Set("mid", strconv.FormatInt(mid, 10))
  101. params.Set("aid", strconv.FormatInt(aid, 10))
  102. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  103. // url
  104. var (
  105. query, _ = tool.Sign(params)
  106. url string
  107. )
  108. if st == 1 {
  109. url = d.arcOpenURL + "?" + query
  110. } else if st == 2 {
  111. url = d.arcCloseURL + "?" + query
  112. }
  113. // new request
  114. req, err := http.NewRequest("POST", url, nil)
  115. if err != nil {
  116. log.Error("http.NewRequest(%s) error(%v); mid(%d), aid(%d), ip(%s)", url, err, mid, aid, ip)
  117. err = ecode.CreativeElecErr
  118. return
  119. }
  120. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  121. var res struct {
  122. Code int `json:"code"`
  123. }
  124. if err = d.client.Do(c, req, &res); err != nil {
  125. log.Error("d.client.Do(%s) error(%v); mid(%d), aid(%d), ip(%s)", url, err, mid, aid, ip)
  126. err = ecode.CreativeElecErr
  127. return
  128. }
  129. if res.Code != 0 {
  130. log.Error("arc elec update state url(%s) res(%v); mid(%d), aid(%d), ip(%s)", url, res, mid, aid, ip)
  131. err = ecode.CreativeElecErr
  132. }
  133. return
  134. }
  135. // Notify get up-to-date notice.
  136. func (d *Dao) Notify(c context.Context, ip string) (nt *elec.Notify, err error) {
  137. params := url.Values{}
  138. var res struct {
  139. Code int `json:"code"`
  140. Data *elec.Notify
  141. }
  142. if err = d.client.Get(c, d.notifyURL, ip, params, &res); err != nil {
  143. log.Error("elec url(%s) response(%v) error(%v)", d.notifyURL+"?"+params.Encode(), res, err)
  144. err = ecode.CreativeElecErr
  145. return
  146. }
  147. if res.Code != 0 {
  148. log.Error("elec notify url(%s) res(%v)", d.notifyURL, res)
  149. err = ecode.CreativeElecErr
  150. return
  151. }
  152. nt = res.Data
  153. return
  154. }
  155. // Status get elec setting status.
  156. func (d *Dao) Status(c context.Context, mid int64, ip string) (st *elec.Status, err error) {
  157. params := url.Values{}
  158. params.Set("act", "appkey")
  159. params.Set("mid", strconv.FormatInt(mid, 10))
  160. var res struct {
  161. Code int `json:"code"`
  162. Data struct {
  163. Info *elec.Status `json:"info"`
  164. } `json:"data"`
  165. }
  166. if err = d.client.Get(c, d.getStatusURL, ip, params, &res); err != nil {
  167. log.Error("elec url(%s) response(%v) error(%v)", d.getStatusURL+"?"+params.Encode(), res, err)
  168. err = ecode.CreativeElecErr
  169. return
  170. }
  171. if res.Code != 0 {
  172. log.Error("elec url(%s) res(%v)", d.getStatusURL, res)
  173. err = ecode.CreativeElecErr
  174. return
  175. }
  176. st = res.Data.Info
  177. return
  178. }
  179. // UpStatus update elec setting status.
  180. func (d *Dao) UpStatus(c context.Context, mid int64, spday int, ip string) (err error) {
  181. params := url.Values{}
  182. params.Set("type", "json")
  183. params.Set("act", "appkey")
  184. params.Set("appkey", conf.Conf.App.Key)
  185. params.Set("appsecret", conf.Conf.App.Secret)
  186. params.Set("mid", strconv.FormatInt(mid, 10))
  187. params.Set("display_specialday", strconv.Itoa(spday))
  188. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  189. var (
  190. query, _ = tool.Sign(params)
  191. url string
  192. )
  193. url = d.setStatusURL + "?" + query
  194. // new request
  195. req, err := http.NewRequest("POST", url, nil)
  196. if err != nil {
  197. log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
  198. err = ecode.CreativeElecErr
  199. return
  200. }
  201. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  202. var res struct {
  203. Code int `json:"code"`
  204. Data struct {
  205. Ret int `json:"ret"`
  206. } `json:"data"`
  207. }
  208. if err = d.client.Do(c, req, &res); err != nil {
  209. log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
  210. err = ecode.CreativeElecErr
  211. return
  212. }
  213. if res.Code != 0 {
  214. log.Error("user elec update setting url(%s) res(%v); mid(%d), ip(%s)", url, res, mid, ip)
  215. err = ecode.CreativeElecErr
  216. }
  217. return
  218. }
  219. // RecentRank get recent rank.
  220. func (d *Dao) RecentRank(c context.Context, mid, size int64, ip string) (rec []*elec.Rank, err error) {
  221. params := url.Values{}
  222. params.Set("type", "json")
  223. params.Set("act", "appkey")
  224. params.Set("mid", strconv.FormatInt(mid, 10))
  225. params.Set("size", strconv.FormatInt(size, 10))
  226. var res struct {
  227. Code int `json:"code"`
  228. Data struct {
  229. List []*elec.Rank `json:"list"`
  230. } `json:"data"`
  231. }
  232. if err = d.client.Get(c, d.recentRankURL, ip, params, &res); err != nil {
  233. log.Error("elec url(%s) response(%v) error(%v)", d.recentRankURL+"?"+params.Encode(), res, err)
  234. err = ecode.CreativeElecErr
  235. return
  236. }
  237. if res.Code != 0 {
  238. log.Error("elec url(%s) res(%v)", d.recentRankURL, res)
  239. err = ecode.CreativeElecErr
  240. return
  241. }
  242. rec = res.Data.List
  243. return
  244. }
  245. // CurrentRank get current rank.
  246. func (d *Dao) CurrentRank(c context.Context, mid int64, ip string) (cur []*elec.Rank, err error) {
  247. params := url.Values{}
  248. params.Set("type", "json")
  249. params.Set("act", "appkey")
  250. params.Set("mid", strconv.FormatInt(mid, 10))
  251. var res struct {
  252. Code int `json:"code"`
  253. Data struct {
  254. List []*elec.Rank `json:"list"`
  255. } `json:"data"`
  256. }
  257. if err = d.client.Get(c, d.currentRankURL, ip, params, &res); err != nil {
  258. log.Error("elec url(%s) response(%v) error(%v)", d.currentRankURL+"?"+params.Encode(), res, err)
  259. err = ecode.CreativeElecErr
  260. return
  261. }
  262. if res.Code != 0 {
  263. log.Error("elec url(%s) res(%v)", d.currentRankURL, res)
  264. err = ecode.CreativeElecErr
  265. return
  266. }
  267. cur = res.Data.List
  268. return
  269. }
  270. // TotalRank get total rank.
  271. func (d *Dao) TotalRank(c context.Context, mid int64, ip string) (tol []*elec.Rank, err error) {
  272. params := url.Values{}
  273. params.Set("type", "json")
  274. params.Set("act", "appkey")
  275. params.Set("mid", strconv.FormatInt(mid, 10))
  276. var res struct {
  277. Code int `json:"code"`
  278. Data struct {
  279. List []*elec.Rank `json:"list"`
  280. } `json:"data"`
  281. }
  282. if err = d.client.Get(c, d.totalRankURL, ip, params, &res); err != nil {
  283. log.Error("elec url(%s) response(%v) error(%v)", d.totalRankURL+"?"+params.Encode(), res, err)
  284. err = ecode.CreativeElecErr
  285. return
  286. }
  287. if res.Code != 0 {
  288. log.Error("elec url(%s) res(%v)", d.totalRankURL, res)
  289. err = ecode.CreativeElecErr
  290. return
  291. }
  292. tol = res.Data.List
  293. return
  294. }
  295. // DailyBill daily settlement.
  296. func (d *Dao) DailyBill(c context.Context, mid int64, pn, ps int, begin, end, ip string) (bl *elec.BillList, err error) {
  297. params := url.Values{}
  298. params.Set("type", "json")
  299. params.Set("act", "appkey")
  300. params.Set("appkey", conf.Conf.App.Key)
  301. params.Set("appsecret", conf.Conf.App.Secret)
  302. params.Set("mid", strconv.FormatInt(mid, 10))
  303. params.Set("page_no", strconv.Itoa(pn))
  304. params.Set("page_size", strconv.Itoa(ps))
  305. params.Set("begin_time", begin)
  306. params.Set("end_time", end)
  307. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  308. // url
  309. var (
  310. query, _ = tool.Sign(params)
  311. url string
  312. )
  313. url = d.dailyBillURL + "?" + query
  314. // new request
  315. req, err := http.NewRequest("POST", url, nil)
  316. if err != nil {
  317. log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
  318. err = ecode.CreativeElecErr
  319. return
  320. }
  321. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  322. var res struct {
  323. Code int `json:"code"`
  324. Data *elec.BillList `json:"data"`
  325. }
  326. if err = d.client.Do(c, req, &res); err != nil {
  327. log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
  328. err = ecode.CreativeElecErr
  329. return
  330. }
  331. if res.Code != 0 {
  332. log.Error("user elec daily bill url(%s) res(%v); mid(%d), ip(%s)", url, res, mid, ip)
  333. err = ecode.CreativeElecErr
  334. }
  335. bl = res.Data
  336. return
  337. }
  338. // Balance get battery balance.
  339. func (d *Dao) Balance(c context.Context, mid int64, ip string) (bal *elec.Balance, err error) {
  340. params := url.Values{}
  341. params.Set("type", "json")
  342. params.Set("act", "appkey")
  343. params.Set("appkey", conf.Conf.App.Key)
  344. params.Set("appsecret", conf.Conf.App.Secret)
  345. params.Set("mid", strconv.FormatInt(mid, 10))
  346. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  347. var (
  348. query, _ = tool.Sign(params)
  349. url string
  350. )
  351. url = d.balanceURL + "?" + query
  352. // new request
  353. req, err := http.NewRequest("POST", url, nil)
  354. if err != nil {
  355. log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
  356. err = ecode.CreativeElecErr
  357. return
  358. }
  359. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  360. var res struct {
  361. Code int `json:"code"`
  362. Data *elec.Balance `json:"data"`
  363. }
  364. if err = d.client.Do(c, req, &res); err != nil {
  365. log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
  366. err = ecode.CreativeElecErr
  367. return
  368. }
  369. if res.Code != 0 {
  370. log.Error("user elec balance url(%s) res(%v); mid(%d), ip(%s)", url, res, mid, ip)
  371. err = ecode.CreativeElecErr
  372. }
  373. bal = res.Data
  374. return
  375. }
  376. // RecentElec get aid & elec_num.
  377. func (d *Dao) RecentElec(c context.Context, mid int64, pn, ps int, ip string) (rec *elec.RecentElecList, err error) {
  378. params := url.Values{}
  379. params.Set("type", "json")
  380. params.Set("act", "appkey")
  381. params.Set("mid", strconv.FormatInt(mid, 10))
  382. params.Set("pn", strconv.Itoa(pn))
  383. params.Set("ps", strconv.Itoa(ps))
  384. var res struct {
  385. Code int `json:"code"`
  386. Data *elec.RecentElecList `json:"data"`
  387. }
  388. if err = d.client.Get(c, d.recentElecURL, ip, params, &res); err != nil {
  389. log.Error("elec url(%s) response(%v) error(%v)", d.recentElecURL+"?"+params.Encode(), res, err)
  390. err = ecode.CreativeElecErr
  391. return
  392. }
  393. if res.Code != 0 {
  394. log.Error("elec url(%s) res(%v)", d.recentElecURL, res)
  395. err = ecode.CreativeElecErr
  396. return
  397. }
  398. rec = res.Data
  399. return
  400. }
  401. // RemarkList get remark list.
  402. func (d *Dao) RemarkList(c context.Context, mid int64, pn, ps int, begin, end, ip string) (rec *elec.RemarkList, err error) {
  403. params := url.Values{}
  404. params.Set("type", "json")
  405. params.Set("act", "appkey")
  406. params.Set("mid", strconv.FormatInt(mid, 10))
  407. params.Set("pn", strconv.Itoa(pn))
  408. params.Set("ps", strconv.Itoa(ps))
  409. params.Set("start_time", begin)
  410. params.Set("end_time", end)
  411. var res struct {
  412. Code int `json:"code"`
  413. Data *elec.RemarkList `json:"data"`
  414. }
  415. if err = d.client.Get(c, d.remarkListURL, ip, params, &res); err != nil {
  416. log.Error("elec url(%s) response(%v) error(%v)", d.remarkListURL+"?"+params.Encode(), res, err)
  417. err = ecode.CreativeElecErr
  418. return
  419. }
  420. if res.Code != 0 {
  421. log.Error("elec url(%s) res(%v)", d.remarkListURL, res)
  422. err = ecode.CreativeElecErr
  423. return
  424. }
  425. rec = res.Data
  426. return
  427. }
  428. // RemarkDetail get remark detail.
  429. func (d *Dao) RemarkDetail(c context.Context, mid, id int64, ip string) (re *elec.Remark, err error) {
  430. params := url.Values{}
  431. params.Set("type", "json")
  432. params.Set("act", "appkey")
  433. params.Set("mid", strconv.FormatInt(mid, 10))
  434. params.Set("id", strconv.FormatInt(id, 10))
  435. var res struct {
  436. Code int `json:"code"`
  437. Data struct {
  438. Info *elec.Remark `json:"info"`
  439. } `json:"data"`
  440. }
  441. if err = d.client.Get(c, d.remarkDetailURL, ip, params, &res); err != nil {
  442. log.Error("elec url(%s) response(%v) error(%v)", d.remarkDetailURL+"?"+params.Encode(), res, err)
  443. err = ecode.CreativeElecErr
  444. return
  445. }
  446. if res.Code != 0 {
  447. log.Error("elec url(%s) res(%v)", d.remarkDetailURL, res)
  448. err = ecode.CreativeElecErr
  449. return
  450. }
  451. re = res.Data.Info
  452. return
  453. }
  454. // Remark reply a msg.
  455. func (d *Dao) Remark(c context.Context, mid, id int64, msg, ak, ck, ip string) (status int, err error) {
  456. params := url.Values{}
  457. params.Set("type", "json")
  458. params.Set("act", "appkey")
  459. params.Set("access_key", ak)
  460. params.Set("appkey", conf.Conf.App.Key)
  461. params.Set("appsecret", conf.Conf.App.Secret)
  462. params.Set("mid", strconv.FormatInt(mid, 10))
  463. params.Set("id", strconv.FormatInt(id, 10))
  464. params.Set("message", msg)
  465. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  466. var (
  467. query, _ = tool.Sign(params)
  468. url string
  469. )
  470. url = d.remarkURL + "?" + query
  471. // new request
  472. req, err := http.NewRequest("POST", url, nil)
  473. if err != nil {
  474. log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
  475. err = ecode.CreativeElecErr
  476. return
  477. }
  478. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  479. req.Header.Set("Cookie", ck)
  480. var res struct {
  481. Code int `json:"code"`
  482. Data struct {
  483. Status int `json:"status"`
  484. } `json:"data"`
  485. }
  486. if err = d.client.Do(c, req, &res); err != nil {
  487. log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
  488. err = ecode.CreativeElecErr
  489. return
  490. }
  491. if res.Code != 0 {
  492. log.Error("user elec daily bill url(%s) res(%v); mid(%d), ip(%s)", url, res, mid, ip)
  493. if res.Code == 61001 || res.Code == 61002 {
  494. err = ecode.Int(res.Code)
  495. } else {
  496. err = ecode.CreativeElecErr
  497. }
  498. }
  499. status = res.Data.Status
  500. return
  501. }