+ jdapi.SaveQualify
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
package jdapi
|
package jdapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -136,14 +140,56 @@ type PageShopInfo struct {
|
|||||||
StoreShareURL string `json:"storeShareUrl"`
|
StoreShareURL string `json:"storeShareUrl"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
const (
|
||||||
monthSaleNumReg = regexp.MustCompile(`(\d+)([千|万])`)
|
LincenceEconKindPerson = "个体工商户"
|
||||||
|
|
||||||
|
QualifyTypePerson = "25" // 营业执照
|
||||||
|
QualifyTypeCompany = "22" // 身份证,个体工商户要求填
|
||||||
|
QualifyTypeAddInfo = "31" // 附加信息,如果身份证是长期有效,要求身份证背面信息
|
||||||
|
|
||||||
|
SaveQualifyActionTypeCommit = 0 // 提交
|
||||||
|
SaveQualifyActionTypeSave = 1 // 暂时保存
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *API) AccessStorePage(fullURL string, params map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
|
type QualifyItem struct {
|
||||||
|
QualifyURL string `json:"qualifyUrl"`
|
||||||
|
QualifyType string `json:"qualifyType"`
|
||||||
|
QualifyExpireForever int `json:"qualifyExpireForever"` // 0:永久有性,1:非永久有效(需要填QualifyExpireEnd)
|
||||||
|
QualifyExpireStart string `json:"qualifyExpireStart"`
|
||||||
|
QualifyExpireEnd string `json:"qualifyExpireEnd,omitempty"`
|
||||||
|
QualifyName string `json:"qualifyName,omitempty"`
|
||||||
|
QualifyOwner string `json:"qualifyOwner,omitempty"`
|
||||||
|
LicenceType string `json:"licenceType,omitempty"` // -1
|
||||||
|
QualifyNumber string `json:"qualifyNumber,omitempty"`
|
||||||
|
QualifyAddress string `json:"qualifyAddress,omitempty"`
|
||||||
|
LicenceName string `json:"licenceName,omitempty"`
|
||||||
|
EconKind string `json:"econKind,omitempty"`
|
||||||
|
Scope string `json:"scope,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
monthSaleNumReg = regexp.MustCompile(`(\d+)([千|万])`)
|
||||||
|
pageExceedLimitCodes = map[string]int{
|
||||||
|
"403": 1,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
KeyImgData = "imgData"
|
||||||
|
KeyImgName = "imgName"
|
||||||
|
|
||||||
|
ResultKeyData = "data"
|
||||||
|
ResultKeyResult = "result"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (a *API) AccessStorePage2(fullURL string, params map[string]interface{}, isPost bool, resultKey string) (retVal interface{}, err error) {
|
||||||
if a.GetCookieCount() == 0 {
|
if a.GetCookieCount() == 0 {
|
||||||
return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法")
|
return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法")
|
||||||
}
|
}
|
||||||
|
imgData := params[KeyImgData]
|
||||||
|
if imgData != nil {
|
||||||
|
delete(params, KeyImgData)
|
||||||
|
}
|
||||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||||
func() *http.Request {
|
func() *http.Request {
|
||||||
var request *http.Request
|
var request *http.Request
|
||||||
@@ -151,8 +197,26 @@ func (a *API) AccessStorePage(fullURL string, params map[string]interface{}, isP
|
|||||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(fullURL, "", params), nil)
|
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(fullURL, "", params), nil)
|
||||||
} else {
|
} else {
|
||||||
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
|
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
|
||||||
|
if params[KeyImgName] == nil {
|
||||||
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
} else {
|
||||||
|
var b bytes.Buffer
|
||||||
|
w := multipart.NewWriter(&b)
|
||||||
|
if fw, err := w.CreateFormFile("uploadFile", params[KeyImgName].(string)); err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
} else {
|
||||||
|
fw.Write(imgData.([]byte))
|
||||||
|
}
|
||||||
|
for k, v := range params {
|
||||||
|
fmt.Println(k, " ", v)
|
||||||
|
w.WriteField(k, url.QueryEscape(fmt.Sprint(v)))
|
||||||
|
}
|
||||||
|
w.Close()
|
||||||
|
// b.WriteString(utils.Map2URLValues(params).Encode())
|
||||||
|
request, _ = http.NewRequest(http.MethodPost, fullURL, &b)
|
||||||
|
request.Header.Set("Content-Type", w.FormDataContentType())
|
||||||
|
}
|
||||||
request.Header.Set("charset", "UTF-8")
|
request.Header.Set("charset", "UTF-8")
|
||||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -166,13 +230,18 @@ func (a *API) AccessStorePage(fullURL string, params map[string]interface{}, isP
|
|||||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||||
}
|
}
|
||||||
retVal = jsonResult1
|
retVal = jsonResult1
|
||||||
code := jsonResult1["code"].(string)
|
code, ok := jsonResult1["code"].(string)
|
||||||
|
if !ok {
|
||||||
|
return platformapi.ErrLevelGeneralFail, utils.NewErrorCode(utils.Format4Output(jsonResult1, true), "999")
|
||||||
|
}
|
||||||
if code == ResponseCodeSuccess {
|
if code == ResponseCodeSuccess {
|
||||||
retVal, _ = jsonResult1["result"].(map[string]interface{})
|
if resultKey != "" {
|
||||||
|
retVal = jsonResult1[resultKey]
|
||||||
|
}
|
||||||
return platformapi.ErrLevelSuccess, nil
|
return platformapi.ErrLevelSuccess, nil
|
||||||
}
|
}
|
||||||
newErr := utils.NewErrorCode(jsonResult1["msg"].(string), code)
|
newErr := utils.NewErrorCode(jsonResult1["msg"].(string), code)
|
||||||
if _, ok := exceedLimitCodes[code]; ok {
|
if _, ok := pageExceedLimitCodes[code]; ok {
|
||||||
return platformapi.ErrLevelExceedLimit, newErr
|
return platformapi.ErrLevelExceedLimit, newErr
|
||||||
} else if _, ok := canRetryCodes[code]; ok {
|
} else if _, ok := canRetryCodes[code]; ok {
|
||||||
return platformapi.ErrLevelRecoverableErr, newErr
|
return platformapi.ErrLevelRecoverableErr, newErr
|
||||||
@@ -184,6 +253,14 @@ func (a *API) AccessStorePage(fullURL string, params map[string]interface{}, isP
|
|||||||
return retVal, err
|
return retVal, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *API) AccessStorePage(fullURL string, params map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
|
||||||
|
result, err := a.AccessStorePage2(fullURL, params, isPost, ResultKeyResult)
|
||||||
|
if err == nil {
|
||||||
|
retVal = result.(map[string]interface{})
|
||||||
|
}
|
||||||
|
return retVal, err
|
||||||
|
}
|
||||||
|
|
||||||
func (a *API) GetRealMobile4Order(orderId, stationNo string) (mobile string, err error) {
|
func (a *API) GetRealMobile4Order(orderId, stationNo string) (mobile string, err error) {
|
||||||
retVal, err := a.GetStoreOrderInfo(orderId, stationNo)
|
retVal, err := a.GetStoreOrderInfo(orderId, stationNo)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -346,3 +423,47 @@ func MonthSaleNum2Int(monthSaleNumStr string) (monthSaleNum int) {
|
|||||||
}
|
}
|
||||||
return monthSaleNum
|
return monthSaleNum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *API) StoreUploadImg(imgFileName string, imgBin []byte) (imgURL string, err error) {
|
||||||
|
result, err := a.AccessStorePage2("https://sta-store.jddj.com/store/uploadImg.json", map[string]interface{}{
|
||||||
|
KeyImgData: imgBin,
|
||||||
|
KeyImgName: imgFileName,
|
||||||
|
}, true, ResultKeyData)
|
||||||
|
if err == nil {
|
||||||
|
imgURL = result.(string)
|
||||||
|
}
|
||||||
|
return imgURL, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *API) StoreUploadImgByURL(inImgURL string) (imgURL string, err error) {
|
||||||
|
response, err := http.Get(inImgURL)
|
||||||
|
if err == nil {
|
||||||
|
defer func() {
|
||||||
|
response.Body.Close()
|
||||||
|
}()
|
||||||
|
if response.StatusCode == http.StatusOK {
|
||||||
|
bodyData, err2 := ioutil.ReadAll(response.Body)
|
||||||
|
if err = err2; err == nil {
|
||||||
|
imgName := utils.GetUUID()
|
||||||
|
if lastSlashIndex := strings.LastIndex(inImgURL, "/"); lastSlashIndex >= 0 {
|
||||||
|
imgName = inImgURL[lastSlashIndex+1:]
|
||||||
|
}
|
||||||
|
return a.StoreUploadImg(imgName, bodyData)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = platformapi.ErrHTTPCodeIsNot200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *API) SaveQualify(stationNo string, actionType int, qualifyList []*QualifyItem) (err error) {
|
||||||
|
_, err = a.AccessStorePage2("https://sta-store.jddj.com/store/saveQualify.o2o", map[string]interface{}{
|
||||||
|
"stationNo": stationNo,
|
||||||
|
"actionType": actionType,
|
||||||
|
"qualifyList": qualifyList,
|
||||||
|
"type": 1,
|
||||||
|
"degrade": "no",
|
||||||
|
}, true, "")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ func TestGetSkuPageImageInfo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetCorporationInfo(t *testing.T) {
|
func TestGetCorporationInfo(t *testing.T) {
|
||||||
imgList, err := api.GetCorporationInfo("", "915101003431062533")
|
imgList, err := api.GetCorporationInfo("", "92330104MA28XPXH5G")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -101,3 +101,91 @@ func TestMonthSaleNum2Int(t *testing.T) {
|
|||||||
t.Fatalf("num3:%d", num3)
|
t.Fatalf("num3:%d", num3)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStoreUploadImgByURL(t *testing.T) {
|
||||||
|
outImgURL, err := api.StoreUploadImgByURL("http://image.jxc4.com/940c151db7e396f2ceaec0304597836f.jpg")
|
||||||
|
t.Log(outImgURL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSaveQualify(t *testing.T) {
|
||||||
|
jsonStr := `
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"qualifyUrl":"http://img30.360buyimg.com/vendersettle/jfs/t1/69834/24/6602/68812/5d4d35fdEaaf373c6/5c1c50e7bb6330e4.jpg",
|
||||||
|
"qualifyType":"25",
|
||||||
|
"qualifyExpireForever":0,
|
||||||
|
"qualifyExpireStart":"2017-09-07 00:00:00",
|
||||||
|
"qualifyName":"刘男",
|
||||||
|
"licenceType":"-1",
|
||||||
|
"qualifyNumber":"92330104MA28XPXH5G",
|
||||||
|
"qualifyAddress":"浙江省杭州市江干区八堡家园5排10号一楼102",
|
||||||
|
"licenceName":"杭州市江干区刘男便利店",
|
||||||
|
"econKind":"个体工商户",
|
||||||
|
"scope":"食品经营(凭有效许可证经营);零售:卷烟、雪茄烟(凭有效许可证经营);批发、零售:日用百货,五金。(依法须经批准的项目,经相关部门批准后方可开展经营活动)"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"qualifyUrl":"http://img30.360buyimg.com/vendersettle/jfs/t1/58554/26/7134/19343/5d4d3639E57b14138/bcce25e1eac11be8.jpg",
|
||||||
|
"qualifyType":"22",
|
||||||
|
"qualifyExpireForever":1,
|
||||||
|
"qualifyExpireStart":"2013-07-22 16:59:38",
|
||||||
|
"qualifyExpireEnd":"2033-07-22 16:59:50",
|
||||||
|
"qualifyOwner":"刘男",
|
||||||
|
"qualifyNumber":"420621198110303336"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"qualifyUrl":"",
|
||||||
|
"qualifyType":"33",
|
||||||
|
"qualifyExpireForever":1,
|
||||||
|
"qualifyExpireStart":"",
|
||||||
|
"qualifyExpireEnd":""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"qualifyUrl":"",
|
||||||
|
"qualifyType":"8",
|
||||||
|
"qualifyExpireForever":1,
|
||||||
|
"qualifyExpireStart":"",
|
||||||
|
"qualifyExpireEnd":""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"qualifyUrl":"",
|
||||||
|
"qualifyType":"9",
|
||||||
|
"qualifyExpireForever":1,
|
||||||
|
"qualifyExpireStart":"",
|
||||||
|
"qualifyExpireEnd":""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"qualifyUrl":"",
|
||||||
|
"qualifyType":"10",
|
||||||
|
"qualifyExpireForever":1,
|
||||||
|
"qualifyExpireStart":"",
|
||||||
|
"qualifyExpireEnd":""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"qualifyUrl":"",
|
||||||
|
"qualifyType":"29",
|
||||||
|
"qualifyExpireForever":1,
|
||||||
|
"qualifyExpireStart":"",
|
||||||
|
"qualifyExpireEnd":""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"qualifyUrl":"",
|
||||||
|
"qualifyType":"31",
|
||||||
|
"qualifyExpireForever":1,
|
||||||
|
"qualifyExpireStart":"",
|
||||||
|
"qualifyExpireEnd":""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
`
|
||||||
|
var qualityList []*QualifyItem
|
||||||
|
err := utils.UnmarshalUseNumber([]byte(jsonStr), &qualityList)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = api.SaveQualify("11902261", SaveQualifyActionTypeSave, qualityList)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user