123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- package elec
- import (
- "context"
- "net/http"
- "net/url"
- "strconv"
- "time"
- "go-common/app/interface/main/creative/conf"
- "go-common/app/interface/main/creative/dao/tool"
- "go-common/app/interface/main/creative/model/elec"
- "go-common/library/ecode"
- "go-common/library/log"
- )
- const (
- _userInfoURI = "/internal/member/info"
- _userJoinURI = "/internal/member/elec/partin"
- _userExitURI = "/internal/member/elec/exit"
- _arcOpenURI = "/internal/archice/partin"
- _arcCloseURI = "/internal/archice/exit"
- _notifyURI = "/internal/notify/info"
- _getStatusURI = "/api/user/queryset/v2"
- _setStatusURI = "/api/user/modifyset/v2"
- _recentRankURI = "/api/query.recent.do"
- _currentRankURI = "/api/query.rank.do"
- _totalRankURI = "/api/query.total.rank.do"
- _dailyBillURI = "/api/query.daily.bill.do"
- _balanceURI = "/api/query.wallet.balance.do"
- _recentElecURI = "/api/recent/elec"
- _remarkListURI = "/api/elec/remark/list"
- _remarkDetailURI = "/api/elec/remake/detail"
- _remarkURI = "/api/remake/reply"
- )
- // UserInfo get user elec info.
- func (d *Dao) UserInfo(c context.Context, mid int64, ip string) (st *elec.UserInfo, err error) {
- params := url.Values{}
- params.Set("mid", strconv.FormatInt(mid, 10))
- var res struct {
- Code int `json:"code"`
- Data *elec.UserInfo
- }
- if err = d.client.Get(c, d.userInfoURL, ip, params, &res); err != nil {
- log.Error("elec url(%s) response(%v) error(%v)", d.userInfoURL+"?"+params.Encode(), res, err)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("elec url(%s) res(%v)", d.userInfoURL, res)
- err = ecode.CreativeElecErr
- return
- }
- st = res.Data
- return
- }
- // UserUpdate join or exit elec.
- func (d *Dao) UserUpdate(c context.Context, mid int64, st int8, ip string) (u *elec.UserInfo, err error) {
- params := url.Values{}
- params.Set("appkey", conf.Conf.App.Key)
- params.Set("appsecret", conf.Conf.App.Secret)
- params.Set("mid", strconv.FormatInt(mid, 10))
- params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
- // url
- var (
- query, _ = tool.Sign(params)
- url string
- )
- if st == 1 {
- url = d.userJoinURL + "?" + query
- } else if st == 2 {
- url = d.userExitURL + "?" + query
- }
- // new requests
- req, err := http.NewRequest("POST", url, nil)
- if err != nil {
- log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
- err = ecode.CreativeElecErr
- return
- }
- req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
- var res struct {
- Code int `json:"code"`
- Data *elec.UserInfo
- }
- if err = d.client.Do(c, req, &res); err != nil {
- log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("user elec update state url(%s) res(%v); mid(%d), ip(%s), code(%d)", url, res, mid, ip, res.Code)
- err = ecode.CreativeElecErr
- }
- u = res.Data
- return
- }
- // ArcUpdate arc open or close elec.
- func (d *Dao) ArcUpdate(c context.Context, mid, aid int64, st int8, ip string) (err error) {
- params := url.Values{}
- params.Set("appkey", conf.Conf.App.Key)
- params.Set("appsecret", conf.Conf.App.Secret)
- params.Set("mid", strconv.FormatInt(mid, 10))
- params.Set("aid", strconv.FormatInt(aid, 10))
- params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
- // url
- var (
- query, _ = tool.Sign(params)
- url string
- )
- if st == 1 {
- url = d.arcOpenURL + "?" + query
- } else if st == 2 {
- url = d.arcCloseURL + "?" + query
- }
- // new request
- req, err := http.NewRequest("POST", url, nil)
- if err != nil {
- log.Error("http.NewRequest(%s) error(%v); mid(%d), aid(%d), ip(%s)", url, err, mid, aid, ip)
- err = ecode.CreativeElecErr
- return
- }
- req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
- var res struct {
- Code int `json:"code"`
- }
- if err = d.client.Do(c, req, &res); err != nil {
- log.Error("d.client.Do(%s) error(%v); mid(%d), aid(%d), ip(%s)", url, err, mid, aid, ip)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("arc elec update state url(%s) res(%v); mid(%d), aid(%d), ip(%s)", url, res, mid, aid, ip)
- err = ecode.CreativeElecErr
- }
- return
- }
- // Notify get up-to-date notice.
- func (d *Dao) Notify(c context.Context, ip string) (nt *elec.Notify, err error) {
- params := url.Values{}
- var res struct {
- Code int `json:"code"`
- Data *elec.Notify
- }
- if err = d.client.Get(c, d.notifyURL, ip, params, &res); err != nil {
- log.Error("elec url(%s) response(%v) error(%v)", d.notifyURL+"?"+params.Encode(), res, err)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("elec notify url(%s) res(%v)", d.notifyURL, res)
- err = ecode.CreativeElecErr
- return
- }
- nt = res.Data
- return
- }
- // Status get elec setting status.
- func (d *Dao) Status(c context.Context, mid int64, ip string) (st *elec.Status, err error) {
- params := url.Values{}
- params.Set("act", "appkey")
- params.Set("mid", strconv.FormatInt(mid, 10))
- var res struct {
- Code int `json:"code"`
- Data struct {
- Info *elec.Status `json:"info"`
- } `json:"data"`
- }
- if err = d.client.Get(c, d.getStatusURL, ip, params, &res); err != nil {
- log.Error("elec url(%s) response(%v) error(%v)", d.getStatusURL+"?"+params.Encode(), res, err)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("elec url(%s) res(%v)", d.getStatusURL, res)
- err = ecode.CreativeElecErr
- return
- }
- st = res.Data.Info
- return
- }
- // UpStatus update elec setting status.
- func (d *Dao) UpStatus(c context.Context, mid int64, spday int, ip string) (err error) {
- params := url.Values{}
- params.Set("type", "json")
- params.Set("act", "appkey")
- params.Set("appkey", conf.Conf.App.Key)
- params.Set("appsecret", conf.Conf.App.Secret)
- params.Set("mid", strconv.FormatInt(mid, 10))
- params.Set("display_specialday", strconv.Itoa(spday))
- params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
- var (
- query, _ = tool.Sign(params)
- url string
- )
- url = d.setStatusURL + "?" + query
- // new request
- req, err := http.NewRequest("POST", url, nil)
- if err != nil {
- log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
- err = ecode.CreativeElecErr
- return
- }
- req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
- var res struct {
- Code int `json:"code"`
- Data struct {
- Ret int `json:"ret"`
- } `json:"data"`
- }
- if err = d.client.Do(c, req, &res); err != nil {
- log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("user elec update setting url(%s) res(%v); mid(%d), ip(%s)", url, res, mid, ip)
- err = ecode.CreativeElecErr
- }
- return
- }
- // RecentRank get recent rank.
- func (d *Dao) RecentRank(c context.Context, mid, size int64, ip string) (rec []*elec.Rank, err error) {
- params := url.Values{}
- params.Set("type", "json")
- params.Set("act", "appkey")
- params.Set("mid", strconv.FormatInt(mid, 10))
- params.Set("size", strconv.FormatInt(size, 10))
- var res struct {
- Code int `json:"code"`
- Data struct {
- List []*elec.Rank `json:"list"`
- } `json:"data"`
- }
- if err = d.client.Get(c, d.recentRankURL, ip, params, &res); err != nil {
- log.Error("elec url(%s) response(%v) error(%v)", d.recentRankURL+"?"+params.Encode(), res, err)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("elec url(%s) res(%v)", d.recentRankURL, res)
- err = ecode.CreativeElecErr
- return
- }
- rec = res.Data.List
- return
- }
- // CurrentRank get current rank.
- func (d *Dao) CurrentRank(c context.Context, mid int64, ip string) (cur []*elec.Rank, err error) {
- params := url.Values{}
- params.Set("type", "json")
- params.Set("act", "appkey")
- params.Set("mid", strconv.FormatInt(mid, 10))
- var res struct {
- Code int `json:"code"`
- Data struct {
- List []*elec.Rank `json:"list"`
- } `json:"data"`
- }
- if err = d.client.Get(c, d.currentRankURL, ip, params, &res); err != nil {
- log.Error("elec url(%s) response(%v) error(%v)", d.currentRankURL+"?"+params.Encode(), res, err)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("elec url(%s) res(%v)", d.currentRankURL, res)
- err = ecode.CreativeElecErr
- return
- }
- cur = res.Data.List
- return
- }
- // TotalRank get total rank.
- func (d *Dao) TotalRank(c context.Context, mid int64, ip string) (tol []*elec.Rank, err error) {
- params := url.Values{}
- params.Set("type", "json")
- params.Set("act", "appkey")
- params.Set("mid", strconv.FormatInt(mid, 10))
- var res struct {
- Code int `json:"code"`
- Data struct {
- List []*elec.Rank `json:"list"`
- } `json:"data"`
- }
- if err = d.client.Get(c, d.totalRankURL, ip, params, &res); err != nil {
- log.Error("elec url(%s) response(%v) error(%v)", d.totalRankURL+"?"+params.Encode(), res, err)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("elec url(%s) res(%v)", d.totalRankURL, res)
- err = ecode.CreativeElecErr
- return
- }
- tol = res.Data.List
- return
- }
- // DailyBill daily settlement.
- func (d *Dao) DailyBill(c context.Context, mid int64, pn, ps int, begin, end, ip string) (bl *elec.BillList, err error) {
- params := url.Values{}
- params.Set("type", "json")
- params.Set("act", "appkey")
- params.Set("appkey", conf.Conf.App.Key)
- params.Set("appsecret", conf.Conf.App.Secret)
- params.Set("mid", strconv.FormatInt(mid, 10))
- params.Set("page_no", strconv.Itoa(pn))
- params.Set("page_size", strconv.Itoa(ps))
- params.Set("begin_time", begin)
- params.Set("end_time", end)
- params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
- // url
- var (
- query, _ = tool.Sign(params)
- url string
- )
- url = d.dailyBillURL + "?" + query
- // new request
- req, err := http.NewRequest("POST", url, nil)
- if err != nil {
- log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
- err = ecode.CreativeElecErr
- return
- }
- req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
- var res struct {
- Code int `json:"code"`
- Data *elec.BillList `json:"data"`
- }
- if err = d.client.Do(c, req, &res); err != nil {
- log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("user elec daily bill url(%s) res(%v); mid(%d), ip(%s)", url, res, mid, ip)
- err = ecode.CreativeElecErr
- }
- bl = res.Data
- return
- }
- // Balance get battery balance.
- func (d *Dao) Balance(c context.Context, mid int64, ip string) (bal *elec.Balance, err error) {
- params := url.Values{}
- params.Set("type", "json")
- params.Set("act", "appkey")
- params.Set("appkey", conf.Conf.App.Key)
- params.Set("appsecret", conf.Conf.App.Secret)
- params.Set("mid", strconv.FormatInt(mid, 10))
- params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
- var (
- query, _ = tool.Sign(params)
- url string
- )
- url = d.balanceURL + "?" + query
- // new request
- req, err := http.NewRequest("POST", url, nil)
- if err != nil {
- log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
- err = ecode.CreativeElecErr
- return
- }
- req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
- var res struct {
- Code int `json:"code"`
- Data *elec.Balance `json:"data"`
- }
- if err = d.client.Do(c, req, &res); err != nil {
- log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("user elec balance url(%s) res(%v); mid(%d), ip(%s)", url, res, mid, ip)
- err = ecode.CreativeElecErr
- }
- bal = res.Data
- return
- }
- // RecentElec get aid & elec_num.
- func (d *Dao) RecentElec(c context.Context, mid int64, pn, ps int, ip string) (rec *elec.RecentElecList, err error) {
- params := url.Values{}
- params.Set("type", "json")
- params.Set("act", "appkey")
- params.Set("mid", strconv.FormatInt(mid, 10))
- params.Set("pn", strconv.Itoa(pn))
- params.Set("ps", strconv.Itoa(ps))
- var res struct {
- Code int `json:"code"`
- Data *elec.RecentElecList `json:"data"`
- }
- if err = d.client.Get(c, d.recentElecURL, ip, params, &res); err != nil {
- log.Error("elec url(%s) response(%v) error(%v)", d.recentElecURL+"?"+params.Encode(), res, err)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("elec url(%s) res(%v)", d.recentElecURL, res)
- err = ecode.CreativeElecErr
- return
- }
- rec = res.Data
- return
- }
- // RemarkList get remark list.
- func (d *Dao) RemarkList(c context.Context, mid int64, pn, ps int, begin, end, ip string) (rec *elec.RemarkList, err error) {
- params := url.Values{}
- params.Set("type", "json")
- params.Set("act", "appkey")
- params.Set("mid", strconv.FormatInt(mid, 10))
- params.Set("pn", strconv.Itoa(pn))
- params.Set("ps", strconv.Itoa(ps))
- params.Set("start_time", begin)
- params.Set("end_time", end)
- var res struct {
- Code int `json:"code"`
- Data *elec.RemarkList `json:"data"`
- }
- if err = d.client.Get(c, d.remarkListURL, ip, params, &res); err != nil {
- log.Error("elec url(%s) response(%v) error(%v)", d.remarkListURL+"?"+params.Encode(), res, err)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("elec url(%s) res(%v)", d.remarkListURL, res)
- err = ecode.CreativeElecErr
- return
- }
- rec = res.Data
- return
- }
- // RemarkDetail get remark detail.
- func (d *Dao) RemarkDetail(c context.Context, mid, id int64, ip string) (re *elec.Remark, err error) {
- params := url.Values{}
- params.Set("type", "json")
- params.Set("act", "appkey")
- params.Set("mid", strconv.FormatInt(mid, 10))
- params.Set("id", strconv.FormatInt(id, 10))
- var res struct {
- Code int `json:"code"`
- Data struct {
- Info *elec.Remark `json:"info"`
- } `json:"data"`
- }
- if err = d.client.Get(c, d.remarkDetailURL, ip, params, &res); err != nil {
- log.Error("elec url(%s) response(%v) error(%v)", d.remarkDetailURL+"?"+params.Encode(), res, err)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("elec url(%s) res(%v)", d.remarkDetailURL, res)
- err = ecode.CreativeElecErr
- return
- }
- re = res.Data.Info
- return
- }
- // Remark reply a msg.
- func (d *Dao) Remark(c context.Context, mid, id int64, msg, ak, ck, ip string) (status int, err error) {
- params := url.Values{}
- params.Set("type", "json")
- params.Set("act", "appkey")
- params.Set("access_key", ak)
- params.Set("appkey", conf.Conf.App.Key)
- params.Set("appsecret", conf.Conf.App.Secret)
- params.Set("mid", strconv.FormatInt(mid, 10))
- params.Set("id", strconv.FormatInt(id, 10))
- params.Set("message", msg)
- params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
- var (
- query, _ = tool.Sign(params)
- url string
- )
- url = d.remarkURL + "?" + query
- // new request
- req, err := http.NewRequest("POST", url, nil)
- if err != nil {
- log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
- err = ecode.CreativeElecErr
- return
- }
- req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
- req.Header.Set("Cookie", ck)
- var res struct {
- Code int `json:"code"`
- Data struct {
- Status int `json:"status"`
- } `json:"data"`
- }
- if err = d.client.Do(c, req, &res); err != nil {
- log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
- err = ecode.CreativeElecErr
- return
- }
- if res.Code != 0 {
- log.Error("user elec daily bill url(%s) res(%v); mid(%d), ip(%s)", url, res, mid, ip)
- if res.Code == 61001 || res.Code == 61002 {
- err = ecode.Int(res.Code)
- } else {
- err = ecode.CreativeElecErr
- }
- }
- status = res.Data.Status
- return
- }
|