jdsh2
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
package jdshopapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -26,6 +30,7 @@ var (
|
||||
const (
|
||||
JdsOrderDeliverTypeStore = 1274
|
||||
JdsOrderDeleverTypeSelf = 332098
|
||||
letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
)
|
||||
|
||||
func (a *API) AccessStorePage(fullURL string, bizParams map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
|
||||
@@ -103,6 +108,66 @@ func (a *API) AccessStorePage2(fullURL string, bizParams map[string]interface{},
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func RandStringBytes(n int) string {
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func getBoundary() (boundary string) {
|
||||
boundary = "----WebKitFormBoundary"
|
||||
boundary += RandStringBytes(16)
|
||||
return boundary
|
||||
}
|
||||
|
||||
func (a *API) AccessStorePage3(data []byte, fileName string) (retVal map[string]interface{}, err error) {
|
||||
// 实例化multipart
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
writer.SetBoundary(getBoundary())
|
||||
// 创建multipart 文件字段
|
||||
h := make(textproto.MIMEHeader)
|
||||
h.Set("Content-Disposition",
|
||||
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
|
||||
"file", fileName))
|
||||
h.Set("Content-Type", "image/jpeg")
|
||||
part, _ := writer.CreatePart(h)
|
||||
// part, _ := writer.CreateFormFile("file", "4ff17b24fdccfc37fd0847469a8039e9.jpg")
|
||||
// 写入文件数据到multipart,和读取本地文件方法的唯一区别
|
||||
_, err = part.Write(data)
|
||||
//将额外参数也写入到multipart
|
||||
_ = writer.WriteField("name", fileName)
|
||||
err = writer.Close()
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
request, _ := http.NewRequest(http.MethodPost, "https://o2o-stores.shop.jd.com/shop/uploadImageNew", body)
|
||||
request.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
a.FillRequestCookies(request)
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
if strings.Contains(bodyStr, "登录") || strings.Contains(bodyStr, "访问的内容") {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("cookie可能过期了!")
|
||||
}
|
||||
if err == nil {
|
||||
if jsonResult1["error_response"] != nil {
|
||||
errLevel = platformapi.ErrLevelGeneralFail
|
||||
err = utils.NewErrorCode(jsonResult1["error_response"].(map[string]interface{})["zh_desc"].(string), jsonResult1["error_response"].(map[string]interface{})["code"].(string))
|
||||
baseapi.SugarLogger.Debugf("jdeclp AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
}
|
||||
retVal = jsonResult1
|
||||
}
|
||||
return errLevel, err
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
type CreateShopCategoryParam struct {
|
||||
HomeShow string `json:"homeShow"`
|
||||
ID string `json:"id"`
|
||||
@@ -678,3 +743,13 @@ func (a *API) UpdateExpand(storeID int) (err error) {
|
||||
}, true)
|
||||
return err
|
||||
}
|
||||
|
||||
//上传图片
|
||||
//https://porder.shop.jd.com/order/orderlist/allOrders
|
||||
func (a *API) UploadImageNew(data []byte, fileName string) (jdURL string, err error) {
|
||||
result, err := a.AccessStorePage3(data, fileName)
|
||||
if err == nil {
|
||||
jdURL = result["data"].(string)
|
||||
}
|
||||
return jdURL, err
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"crypto/cipher"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
@@ -181,3 +182,15 @@ func TestUpdateExpand(t *testing.T) {
|
||||
}
|
||||
// t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
|
||||
func TestUploadImageNew(t *testing.T) {
|
||||
// data, _, err := DownloadFileByURL("http://image.jxc4.com/image/b90ae8585e8cf2f3871f6e8318bde1dc.tem.png")
|
||||
|
||||
// result, err := api.UploadImageNew(data, "b90ae8585e8cf2f3871f6e8318bde1dc.tem.png")
|
||||
// if err != nil {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
// t.Log(utils.Format4Output(result, false))
|
||||
str := "http://image.jxc4.com/image/b90ae8585e8cf2f3871f6e8318bde1dc.tem.png"
|
||||
fmt.Println(str[strings.LastIndex(str, "/")+1 : len(str)])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user