193 lines
7.4 KiB
Go
193 lines
7.4 KiB
Go
package yinbaoapi
|
||
|
||
import (
|
||
"fmt"
|
||
"net/http"
|
||
"strings"
|
||
|
||
"git.rosy.net.cn/baseapi"
|
||
"git.rosy.net.cn/baseapi/platformapi"
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
)
|
||
|
||
const (
|
||
pageUrl = "https://beta27.pospal.cn"
|
||
|
||
MainStoreVendorOrgCode = "3933189"
|
||
)
|
||
|
||
func (a *API) AccessStorePage(action string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||
fullURL := utils.GenerateGetURL(pageUrl, action, nil)
|
||
a.addPageCount()
|
||
// result, _ := json.MarshalIndent(bizParams, "", " ")
|
||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||
func() *http.Request {
|
||
request, _ := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(bizParams).Encode()))
|
||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
||
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
|
||
}
|
||
|
||
type LoadSubStoresByUserIdDDLJsonResult struct {
|
||
Balance float64 `json:"balance"`
|
||
Storewebsite interface{} `json:"storewebsite"`
|
||
Version interface{} `json:"version"`
|
||
IsParent int `json:"isParent"`
|
||
SortNo int `json:"sortNo"`
|
||
UserLocation interface{} `json:"userLocation"`
|
||
StoreDomain interface{} `json:"storeDomain"`
|
||
ParentAccount interface{} `json:"parentAccount"`
|
||
ParentUserID interface{} `json:"parentUserId"`
|
||
AreaID string `json:"areaId"`
|
||
SubUsers interface{} `json:"subUsers"`
|
||
CamerasSettings interface{} `json:"camerasSettings"`
|
||
ImageRecognitionRemainingTimes string `json:"imageRecognitionRemainingTimes"`
|
||
CrmVipNum interface{} `json:"crmVipNum"`
|
||
ID int `json:"id"`
|
||
Account string `json:"account"`
|
||
Email interface{} `json:"email"`
|
||
Address string `json:"address"`
|
||
Tel string `json:"tel"`
|
||
Company string `json:"company"`
|
||
Industry string `json:"industry"`
|
||
IP interface{} `json:"ip"`
|
||
Vip interface{} `json:"vip"`
|
||
CreatedDatetime interface{} `json:"createdDatetime"`
|
||
Enable interface{} `json:"enable"`
|
||
IsOldRev interface{} `json:"isOldRev"`
|
||
EnterpriseID interface{} `json:"enterpriseId"`
|
||
Source interface{} `json:"source"`
|
||
AppID interface{} `json:"appId"`
|
||
AppKey interface{} `json:"appKey"`
|
||
UserType int `json:"userType"`
|
||
IsFranchise int `json:"isFranchise"`
|
||
SecondIndustry interface{} `json:"secondIndustry"`
|
||
Number interface{} `json:"number"`
|
||
TimeZoneID interface{} `json:"timeZoneId"`
|
||
}
|
||
|
||
//获取所有门店的userID
|
||
//https://beta27.pospal.cn/Account/LoadSubStoresByUserIdDDLJson
|
||
func (a *API) LoadSubStoresByUserIdDDLJson() (results []*LoadSubStoresByUserIdDDLJsonResult, err error) {
|
||
result, err := a.AccessStorePage("Account/LoadSubStoresByUserIdDDLJson", map[string]interface{}{
|
||
"userId": MainStoreVendorOrgCode,
|
||
"withSelf": false,
|
||
})
|
||
if err == nil {
|
||
utils.Map2StructByJson(result["stores"], &results, false)
|
||
}
|
||
return results, err
|
||
}
|
||
|
||
//获得分类ID
|
||
//https://beta27.pospal.cn/Category/CreateCategoryUid
|
||
func (a *API) CreateCategoryUid() (categoryID string, err error) {
|
||
result, err := a.AccessStorePage("Category/CreateCategoryUid", nil)
|
||
if err == nil {
|
||
categoryID = result["uid"].(string)
|
||
}
|
||
return categoryID, err
|
||
}
|
||
|
||
//添加分类
|
||
//经测试,parentCategoryName为空或者平台上不存在都是默认建成1级分类
|
||
//https://beta27.pospal.cn/Category/AddNewCategory
|
||
func (a *API) AddNewCategory(userId, categoryName, parentCategoryName string) (catID string, err error) {
|
||
uid, err := a.CreateCategoryUid()
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
_, err = a.AccessStorePage("Category/AddNewCategory", map[string]interface{}{
|
||
"userId": userId,
|
||
"uid": uid,
|
||
"parentCategoryName": parentCategoryName,
|
||
"categoryName": categoryName,
|
||
"categoryType": 0,
|
||
})
|
||
return uid, err
|
||
}
|
||
|
||
func IsErrCategoryExist(err error) (isExist bool) {
|
||
return utils.IsErrMatch(err, "1", []string{"您输入的商品分类名称已存在"})
|
||
}
|
||
|
||
//修改分类
|
||
//经测试,如果parentCategoryName为空或在平台上不存在,则此分类会单独从之前的分类中移出重建成一级分类,0_0||
|
||
//https://beta27.pospal.cn/Category/Update
|
||
func (a *API) UpdateCategory(userId, categoryUid, newCategoryName, parentCategoryName string) (err error) {
|
||
_, err = a.AccessStorePage("Category/Update", map[string]interface{}{
|
||
"userId": userId,
|
||
"categoryUid": categoryUid,
|
||
"parentCategoryName": parentCategoryName,
|
||
"newCategoryName": newCategoryName,
|
||
})
|
||
return err
|
||
}
|
||
|
||
//删除分类
|
||
// https://beta27.pospal.cn/Category/Delete
|
||
func (a *API) DeleteCategory(userId string, categoryUidsJson []string) (err error) {
|
||
_, err = a.AccessStorePage("Category/Delete", map[string]interface{}{
|
||
"userId": userId,
|
||
"categoryUidsJson": categoryUidsJson,
|
||
})
|
||
return err
|
||
}
|
||
|
||
type LoadCategorysWithOptionResult struct {
|
||
ID int `json:"id"`
|
||
UserID int `json:"userId"`
|
||
UID int64 `json:"uid"`
|
||
ParentUID int `json:"parentUid"`
|
||
Name string `json:"name"`
|
||
Enable int `json:"enable"`
|
||
CreatedDatetime string `json:"createdDatetime"`
|
||
UpdatedDatetime string `json:"updatedDatetime"`
|
||
CategoryType int `json:"categoryType"`
|
||
Categoryoption struct {
|
||
ID int `json:"id"`
|
||
UserID int `json:"userId"`
|
||
CategoryOrder int `json:"categoryOrder"`
|
||
CategoryUID int64 `json:"categoryUid"`
|
||
HideFromClient int `json:"hideFromClient"`
|
||
} `json:"categoryoption"`
|
||
TxtUID string `json:"txtUid"`
|
||
TxtParentUID string `json:"txtParentUid"`
|
||
}
|
||
|
||
//获取所有分类
|
||
//https://beta27.pospal.cn/Category/LoadCategorysWithOption
|
||
func (a *API) LoadCategorysWithOption(userId string) (loadCategorysWithOptionResult []*LoadCategorysWithOptionResult, err error) {
|
||
result, err := a.AccessStorePage("Category/LoadCategorysWithOption", map[string]interface{}{
|
||
"userId": userId,
|
||
})
|
||
if err == nil {
|
||
utils.Map2StructByJson(result["categorys"], &loadCategorysWithOptionResult, false)
|
||
}
|
||
return loadCategorysWithOptionResult, err
|
||
}
|