|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- package zhimeng
-
- import (
- "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/md"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/shopspring/decimal"
- "github.com/tidwall/gjson"
- "strings"
- )
-
- // SDK is zhimeng sdk
- type SDK struct {
- Action string
- operation string
- response []byte
- SmsKey string
- SmsSecret string
- data interface{}
- err error
- }
-
- // Init is init action
- // In some condition, such as send Sms, need pass sms key and secret after 'action'
- func (sdk *SDK) Init(action string, keys ...string) {
- sdk.Action = action
- //if keys[0] == "" || keys[1] == "" {
- // sdk.err = errors.New("智盟短信未配置")
- //}
- if len(keys) > 1 {
- sdk.SmsKey = keys[0]
- sdk.SmsSecret = keys[1]
- }
-
- }
-
- // SelectFunction is select api with operation
- func (sdk *SDK) SelectFunction(operation string) *SDK {
- sdk.operation = operation
- return sdk
- }
-
- // WithSMSArgs is SMS
- func (sdk *SDK) WithSMSArgs(args map[string]interface{}) *SDK {
- res, err := SMSend(sdk.Action, sdk.operation, sdk.SmsKey, sdk.SmsSecret, args)
- if err != nil {
-
- }
- sdk.response = res
- return sdk
- }
-
- // WithArgs is post data to api
- func (sdk *SDK) WithArgs(args map[string]interface{}) *SDK {
- // args["appkey"] = svc.SysCfgGet(c, md.KEY_CFG_ZM_AK)
- // args["secret_key"] = svc.SysCfgGet(c, md.KEY_CFG_ZM_SK)
-
- res, err := Send(sdk.Action, sdk.operation, args)
- if err != nil {
- }
- // for k, v := range args {
- // fmt.Printf("%s:%v \n", k, v)
- // }
- fmt.Println("唯品会请求", args, string(res))
- sdk.response = res
- return sdk
- }
-
- // Result is response data from api , return interface{}
- func (sdk *SDK) Result() (*SDK, error) {
- if sdk.err != nil {
- return nil, sdk.err
- }
- tmp := struct {
- Msg string `json:"msg"`
- Success int `json:"success"`
- Data interface{} `json:"data"`
- }{}
- if err := json.Unmarshal(sdk.response, &tmp); err != nil {
- return nil, err
- }
- if tmp.Success != StatusSuc {
- return nil, errors.New("没有商品")
- }
-
- if gjson.GetBytes(sdk.response, "data").String() == "[]" {
- return nil, errors.New("no result")
- }
-
- sdk.data = tmp.Data
- return sdk, nil
- }
-
- // WithJDDetailImageList is insert jd detail image list when api response
- func (sdk *SDK) WithJDDetailImageList(gid string) {
- if len(sdk.response) == 0 || sdk.response == nil {
- return
- }
- fmt.Println(gjson.GetBytes(sdk.response, "data").String())
- if gjson.GetBytes(sdk.response, "data").String() == "[]" {
- return
- }
- fmt.Println("debug", sdk.response)
- data, ok := sdk.data.(map[string]interface{})
- if !ok {
- }
- // debug
- sdk.data = data
- }
-
- // ToObject is the interface is Slice and get the first item
- func (sdk *SDK) ToObject() *SDK {
- s, ok := sdk.ToInterface().([]interface{})
- if !ok {
- return sdk
- }
- sdk.data = s[len(s)-len(s)]
- return sdk
- }
-
- // ToInterface is data to Interface
- func (sdk *SDK) ToInterface() interface{} {
- return sdk.data
- }
-
- // ToMapStringInterface is data to map[string]string
- func (sdk *SDK) ToMapStringInterface(item interface{}) map[string]interface{} {
- data, err := json.Marshal(item)
- if err != nil {
-
- }
- m := make(map[string]interface{})
- if err = json.Unmarshal(data, &m); err != nil {
-
- }
- for key, v := range m {
- switch v.(type) {
- case int:
- t, ok := v.(string)
- if !ok {
- }
- m[key] = t
- case int32:
- t, ok := v.(string)
- if !ok {
- }
- m[key] = t
- case int64:
- t, ok := v.(string)
- if !ok {
- }
- m[key] = t
- case float64:
- vstr := fmt.Sprintf("%v", v)
- if strings.Contains(vstr, "e+") {
- decimalNum, err := decimal.NewFromString(vstr)
- if err != nil {
-
- }
- vstr = decimalNum.String()
- }
- m[key] = vstr
- case nil:
- m[key] = v
- default:
- m[key] = v
- }
- }
- return m
- }
-
- // ToStruct is struct return
- func (sdk *SDK) ToStruct(r *md.MoreDetailResponse) error {
- // debug
- data, err := json.Marshal(sdk.ToMapStringInterface(sdk.data))
- if err != nil {
- }
- if err = json.Unmarshal(data, r); err != nil {
- return err
- }
- return nil
- }
-
- // PddToMapStringInterface is data to map[string]string
- func (sdk *SDK) PddToMapStringInterface(item interface{}) []map[string]interface{} {
- data, err := json.Marshal(item)
- if err != nil {
- }
- m1 := make([]map[string]interface{}, 10, 10)
- if err = json.Unmarshal(data, &m1); err != nil {
-
- }
- for _, m := range m1 {
- for key, v := range m {
- switch v.(type) {
- case int:
- t, ok := v.(string)
- if !ok {
- }
- m[key] = t
- case int32:
- t, ok := v.(string)
- if !ok {
- }
- m[key] = t
- case int64:
- t, ok := v.(string)
- if !ok {
- }
- m[key] = t
- case float64:
- vstr := fmt.Sprintf("%v", v)
- if strings.Contains(vstr, "e+") {
- decimalNum, err := decimal.NewFromString(vstr)
- if err != nil {
- }
- vstr = decimalNum.String()
- }
- m[key] = vstr
- case nil:
- m[key] = v
- default:
- m[key] = v
- }
- }
- }
- return m1
- }
-
- // PddToStruct is struct return for Pdd data is list by one
- func (sdk *SDK) PddToStruct(r *md.MoreDetailResponse) error {
- // debug
- data, err := json.Marshal(sdk.PddToMapStringInterface(sdk.data)[0])
- if err != nil {
- }
- if err = json.Unmarshal(data, r); err != nil {
- return err
- }
- return nil
- }
-
- // VipToMapStringInterface is data to map[string]string
- func (sdk *SDK) VipToMapStringInterface() map[string]interface{} {
- data, err := json.Marshal(sdk.data)
- if err != nil {
-
- }
- m := make(map[string]interface{})
- if err = json.Unmarshal(data, &m); err != nil {
- }
- for key, v := range m {
- switch v.(type) {
- case int:
- t, ok := v.(string)
- if !ok {
- }
- m[key] = t
- case int32:
- t, ok := v.(string)
- if !ok {
- }
- m[key] = t
- case int64:
- t, ok := v.(string)
- if !ok {
- }
- m[key] = t
- case float64:
- vstr := fmt.Sprintf("%v", v)
- if strings.Contains(vstr, "e+") {
- decimalNum, err := decimal.NewFromString(vstr)
- if err != nil {
- }
- vstr = decimalNum.String()
- }
- m[key] = vstr
- case nil:
- m[key] = v
- default:
- m[key] = v
- }
- }
- return m
- }
|