第三方api接口
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

114 行
2.0 KiB

  1. package jdSdk
  2. import (
  3. "encoding/json"
  4. )
  5. //SDK is sdk for jdSdk
  6. type SDK struct {
  7. response []byte
  8. fmap map[string]func(*RequestJD)
  9. fname string
  10. err error
  11. }
  12. //Init is init sdk
  13. func (s *SDK) Init() {
  14. s.fmap = make(map[string]func(*RequestJD))
  15. }
  16. //SelectFunction is select func
  17. func (s *SDK) SelectFunction(fname string) *SDK {
  18. s.fname = fname
  19. return s
  20. }
  21. //WithArgs is request args
  22. func (s *SDK) WithArgs(args *RequestJD) *SDK {
  23. f := s.fmap[s.fname]
  24. f(args)
  25. return s
  26. }
  27. // Result is func result
  28. func (s *SDK) Result() (*SDK, error) {
  29. return s, s.err
  30. }
  31. // Register is register func
  32. func (s *SDK) Register(name string, f func(*RequestJD)) {
  33. s.fmap[name] = f
  34. }
  35. //ToBytes is return []byte
  36. func (s *SDK) ToBytes() []byte {
  37. return s.response
  38. }
  39. // ToString is return string
  40. func (s *SDK) ToString() string {
  41. return string(s.response)
  42. }
  43. //ToMapInterface is return map[string]interface{}
  44. func (s *SDK) ToMapInterface() map[string]interface{} {
  45. m := make(map[string]interface{})
  46. json.Unmarshal(s.response, &m)
  47. return m
  48. }
  49. // Featured is jdSdk 精选列表
  50. func (s *SDK) Featured(r *RequestJD) {
  51. d, err := Featured(r)
  52. if err != nil {
  53. s.err = err
  54. }
  55. s.response = d
  56. }
  57. // Guess is 猜你喜欢列表
  58. func (s *SDK) Guess(r *RequestJD) {
  59. d, err := Guess(r)
  60. if err != nil {
  61. s.err = err
  62. }
  63. s.response = d
  64. }
  65. // Recommend is 推荐列表
  66. func (s *SDK) Recommend(r *RequestJD) {
  67. d, err := Recommend(r)
  68. if err != nil {
  69. s.err = err
  70. }
  71. s.response = d
  72. }
  73. //转链
  74. func (s *SDK) ConventUrl(r *RequestJD) {
  75. d, err := ConventUrl(r)
  76. if err != nil {
  77. s.err = err
  78. }
  79. s.response = d
  80. }
  81. func (s *SDK) OrderFollow(r *RequestJD) {
  82. d, err := OrderFollow(r)
  83. if err != nil {
  84. s.err = err
  85. }
  86. s.response = d
  87. }
  88. //NewJDSDK is jingdong sdk
  89. func NewJDSDK() *SDK {
  90. sdk := new(SDK)
  91. sdk.Init()
  92. sdk.Register("featured", sdk.Featured)
  93. sdk.Register("guess", sdk.Guess)
  94. sdk.Register("recommend", sdk.Recommend)
  95. sdk.Register("convent_url", sdk.ConventUrl)
  96. sdk.Register("order_follow", sdk.OrderFollow)
  97. return sdk
  98. }