dao.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package audio
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/interface/main/app-interface/conf"
  7. "go-common/app/interface/main/app-interface/model/audio"
  8. "go-common/library/ecode"
  9. httpx "go-common/library/net/http/blademaster"
  10. "go-common/library/net/metadata"
  11. "go-common/library/xstr"
  12. "github.com/pkg/errors"
  13. )
  14. const (
  15. _audios = "/audio/music-service-c/songs/internal/upsongslist"
  16. _allAudio = "/audio/music-service-c/songs/internal/uppersongs-preload"
  17. _audioDetail = "/audio/music-service-c/songs/internal/uppersongs-batch"
  18. _favAudio = "/audio/music-service-c/collections"
  19. _upperCert = "/audio/music-service-c/internal/upper-cert"
  20. _card = "/x/internal/v1/audio/privilege/mcard"
  21. _fav = "/x/internal/v1/audio/personal/coll"
  22. )
  23. type Dao struct {
  24. client *httpx.Client
  25. audios string
  26. allAudio string
  27. audioDetail string
  28. favAudio string
  29. upperCert string
  30. card string
  31. fav string
  32. }
  33. func New(c *conf.Config) (d *Dao) {
  34. d = &Dao{
  35. client: httpx.NewClient(c.HTTPClient),
  36. audios: c.Host.APICo + _audios,
  37. allAudio: c.Host.APICo + _allAudio,
  38. audioDetail: c.Host.APICo + _audioDetail,
  39. favAudio: c.Host.APICo + _favAudio,
  40. upperCert: c.Host.APICo + _upperCert,
  41. card: c.Host.APICo + _card,
  42. fav: c.Host.APICo + _fav,
  43. }
  44. return
  45. }
  46. // Audios
  47. func (d *Dao) Audios(c context.Context, mid int64, pn, ps int) (audios []*audio.Audio, total int, err error) {
  48. ip := metadata.String(c, metadata.RemoteIP)
  49. params := url.Values{}
  50. params.Set("uid", strconv.FormatInt(mid, 10))
  51. params.Set("pageIndex", strconv.Itoa(pn))
  52. params.Set("pageSize", strconv.Itoa(ps))
  53. var res struct {
  54. Code int `json:"code"`
  55. Data *struct {
  56. Total int `json:"total"`
  57. List []*audio.Audio `json:"list"`
  58. } `json:"data"`
  59. }
  60. if err = d.client.Get(c, d.audios, ip, params, &res); err != nil {
  61. return
  62. }
  63. if res.Code != ecode.OK.Code() {
  64. err = errors.Wrap(ecode.Int(res.Code), d.audios+"?"+params.Encode())
  65. return
  66. }
  67. if res.Data != nil {
  68. total = res.Data.Total
  69. audios = res.Data.List
  70. }
  71. return
  72. }
  73. // AllAudio get 100 audio by ctime desc
  74. func (d *Dao) AllAudio(c context.Context, vmid int64) (aus []*audio.Audio, err error) {
  75. ip := metadata.String(c, metadata.RemoteIP)
  76. params := url.Values{}
  77. params.Set("uid", strconv.FormatInt(vmid, 10))
  78. var res struct {
  79. Code int `json:"code"`
  80. Data []*audio.Audio `json:"data"`
  81. }
  82. if err = d.client.Get(c, d.allAudio, ip, params, &res); err != nil {
  83. return
  84. }
  85. if res.Code != ecode.OK.Code() {
  86. err = errors.Wrap(ecode.Int(res.Code), d.allAudio+"?"+params.Encode())
  87. return
  88. }
  89. aus = res.Data
  90. return
  91. }
  92. func (d *Dao) AudioDetail(c context.Context, ids []int64) (aum map[int64]*audio.Audio, err error) {
  93. ip := metadata.String(c, metadata.RemoteIP)
  94. params := url.Values{}
  95. params.Set("ids", xstr.JoinInts(ids))
  96. var res struct {
  97. Code int `json:"code"`
  98. Data map[int64]*audio.Audio `json:"data"`
  99. }
  100. if err = d.client.Get(c, d.audioDetail, ip, params, &res); err != nil {
  101. return
  102. }
  103. if res.Code != ecode.OK.Code() {
  104. err = errors.Wrap(ecode.Int(res.Code), d.audioDetail+"?"+params.Encode())
  105. return
  106. }
  107. aum = res.Data
  108. return
  109. }
  110. func (d *Dao) FavAudio(c context.Context, accessKey string, mid int64, pn, ps int) (aus []*audio.FavAudio, err error) {
  111. ip := metadata.String(c, metadata.RemoteIP)
  112. params := url.Values{}
  113. params.Set("access_key", accessKey)
  114. params.Set("mid", strconv.FormatInt(mid, 10))
  115. params.Set("sort", "-1")
  116. params.Set("page_index", strconv.Itoa(pn))
  117. params.Set("page_size", strconv.Itoa(ps))
  118. var res struct {
  119. Code int `json:"code"`
  120. Data *struct {
  121. List []*audio.FavAudio `json:"list"`
  122. } `json:"data"`
  123. }
  124. if err = d.client.Get(c, d.favAudio, ip, params, &res); err != nil {
  125. return
  126. }
  127. if res.Code != ecode.OK.Code() {
  128. err = errors.Wrap(ecode.Int(res.Code), d.favAudio+"?"+params.Encode())
  129. return
  130. }
  131. if res.Data != nil {
  132. aus = res.Data.List
  133. }
  134. return
  135. }
  136. func (d *Dao) UpperCert(c context.Context, uid int64) (cert *audio.UpperCert, err error) {
  137. ip := metadata.String(c, metadata.RemoteIP)
  138. params := url.Values{}
  139. params.Set("uid", strconv.FormatInt(uid, 10))
  140. var res struct {
  141. Code int `json:"code"`
  142. Data *audio.UpperCert `json:"data"`
  143. }
  144. if err = d.client.Get(c, d.upperCert, ip, params, &res); err != nil {
  145. return
  146. }
  147. if res.Code != ecode.OK.Code() {
  148. err = errors.Wrap(ecode.Int(res.Code), d.upperCert+"?"+params.Encode())
  149. return
  150. }
  151. cert = res.Data
  152. return
  153. }
  154. func (d *Dao) Card(c context.Context, mid ...int64) (cardm map[int64]*audio.Card, err error) {
  155. params := url.Values{}
  156. params.Set("mid", xstr.JoinInts(mid))
  157. var res struct {
  158. Code int `json:"code"`
  159. Data map[int64]*audio.Card `json:"data"`
  160. }
  161. if err = d.client.Get(c, d.card, "", params, &res); err != nil {
  162. return
  163. }
  164. if res.Code != ecode.OK.Code() {
  165. err = errors.Wrap(ecode.Int(res.Code), d.card+"?"+params.Encode())
  166. return
  167. }
  168. cardm = res.Data
  169. return
  170. }
  171. func (d *Dao) Fav(c context.Context, mid int64) (fav *audio.Fav, err error) {
  172. ip := metadata.String(c, metadata.RemoteIP)
  173. params := url.Values{}
  174. params.Set("mid", strconv.FormatInt(mid, 10))
  175. var res struct {
  176. Code int `json:"code"`
  177. Data *audio.Fav `json:"data"`
  178. }
  179. if err = d.client.Get(c, d.fav, ip, params, &res); err != nil {
  180. return
  181. }
  182. if res.Code != ecode.OK.Code() {
  183. err = errors.Wrap(ecode.Int(res.Code), d.fav+"?"+params.Encode())
  184. return
  185. }
  186. fav = res.Data
  187. return
  188. }