You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

66 lines
1.6 KiB

package musicobj
import "errors"
type (
SearchResultResponse struct {
Msg string `json:"msg"`
Data struct {
Musics []struct {
Id int64 `json:"id"`
Name string `json:"name"`
Image string `json:"image"`
Singer string `json:"singer"`
} `json:"musics"`
} `json:"data"`
Code int `json:"code"`
}
UrlResultResponse struct {
Msg string `json:"msg"`
Data struct {
Id int64 `json:"id"`
URL string `json:"url"`
Ts int64 `json:"ts"`
} `json:"data"`
Code int `json:"code"`
}
ISys interface {
SearchMusic(keywords string, limit, offset int) (result *SearchResultResponse, err error)
MusicUrl(id int64) (result *UrlResultResponse, err error)
}
)
var defsys ISys
// ErrNotInited 音乐对象存储未初始化。OnInit 失败时调用方可选择降级;此时包级函数返回本错误,而不是 panic。
var ErrNotInited = errors.New("musicobj 未初始化:配置缺失")
func OnInit(config map[string]interface{}, option ...Option) error {
sys, err := newSys(newOptions(config, option...))
if err != nil {
// 出错时不要给 defsys 赋值,避免「非 nil 接口 + nil 指针」让守卫失效。
return err
}
defsys = sys
return nil
}
func NewSys(option ...Option) (sys ISys, err error) {
sys, err = newSys(newOptionsByOption(option...))
return
}
func SearchMusic(keywords string, limit, offset int) (result *SearchResultResponse, err error) {
if defsys == nil {
return nil, ErrNotInited
}
return defsys.SearchMusic(keywords, limit, offset)
}
func MusicUrl(id int64) (result *UrlResultResponse, err error) {
if defsys == nil {
return nil, ErrNotInited
}
return defsys.MusicUrl(id)
}