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 }