银豹商品上传图片
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
package yinbaoapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@@ -13,9 +17,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
pageUrl = "https://beta27.pospal.cn"
|
||||
|
||||
pageUrl = "https://beta27.pospal.cn"
|
||||
MainStoreVendorOrgCode = "3933189"
|
||||
letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -57,6 +61,71 @@ func (a *API) AccessStorePage(action 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) AccessStorePage2(action string, userID, productID string, 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()
|
||||
fullURL := utils.GenerateGetURL(pageUrl, action, nil)
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
request, _ := http.NewRequest(http.MethodPost, fullURL+"?userId="+userID+"&productId="+productID, 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 err == nil {
|
||||
if jsonResult1["successed"] != nil {
|
||||
if !jsonResult1["successed"].(bool) {
|
||||
errLevel = platformapi.ErrLevelGeneralFail
|
||||
err = utils.NewErrorCode(jsonResult1["msg"].(string), "-1", 0)
|
||||
baseapi.SugarLogger.Debugf("yinbao AccessStorePageAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
}
|
||||
} else {
|
||||
errLevel = platformapi.ErrLevelGeneralFail
|
||||
if strings.Contains(jsonResult1["fakeData"].(string), "登录") {
|
||||
err = utils.NewErrorCode("银豹Cookie可能失效了!", "-1", 0)
|
||||
}
|
||||
}
|
||||
retVal = jsonResult1
|
||||
}
|
||||
return errLevel, err
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func IsErrCookie(err error) (isExist bool) {
|
||||
return utils.IsErrMatch(err, "-1", []string{"银豹Cookie可能失效了"})
|
||||
}
|
||||
@@ -300,7 +369,7 @@ func (a *API) FindProduct(productId string) (findProductResult *FindProductResul
|
||||
return findProductResult, err
|
||||
}
|
||||
|
||||
//更新单个商品信息(称编码)
|
||||
//更新单个商品信息(称编码和图片)
|
||||
//Request URL: https://beta27.pospal.cn/Product/SaveProduct
|
||||
func (a *API) SaveProduct(userId, keyword string) (err error) {
|
||||
productId, err := a.LoadProductsByPage(userId, keyword)
|
||||
@@ -356,6 +425,13 @@ func (a *API) SaveProduct(userId, keyword string) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
//上传图片
|
||||
//https://beta27.pospal.cn/Product/UploadProductImage?userId=3933189&productId=8788044
|
||||
func (a *API) UploadProductImage(userID, productId string, data []byte, fileName string) (err error) {
|
||||
_, err = a.AccessStorePage2("Product/UploadProductImage", userID, productId, data, fileName)
|
||||
return err
|
||||
}
|
||||
|
||||
//登录?
|
||||
//https://beta27.pospal.cn/account/SignIn?noLog=
|
||||
//userName: 18048531223
|
||||
|
||||
Reference in New Issue
Block a user