Files
baseapi/platformapi/mtwmapi/poi.go
2018-12-04 10:41:10 +08:00

140 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package mtwmapi
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
)
type PoiCategoryInfo struct {
ID int `json:"id"`
Name string `json:"name"`
}
func (a *API) PoiSave(poiCode string, poiParams map[string]interface{}) (err error) {
_, err = a.AccessAPI("poi/save", false, utils.MergeMaps(utils.Params2Map(KeyAppPoiCode, poiCode), poiParams))
return err
}
func (a *API) PoiGetIDs() (ids []string, err error) {
result, err := a.AccessAPI("poi/getids", true, nil)
if err == nil {
idsTmp := result.([]interface{})
ids = make([]string, len(idsTmp))
for k, v := range idsTmp {
ids[k] = v.(string)
}
return ids, nil
}
return nil, err
}
func (a *API) PoiMGet(poiCodes []string) (pois []map[string]interface{}, err error) {
result, err := a.AccessAPI("poi/mget", true, map[string]interface{}{
KeyAppPoiCodes: strings.Join(poiCodes, ","),
})
if err == nil {
return utils.Slice2MapSlice(result.([]interface{})), nil
}
return nil, err
}
func (a *API) PoiGet(poiCode string) (poi map[string]interface{}, err error) {
result, err := a.PoiMGet([]string{poiCode})
if err == nil {
if len(result) == 0 {
return nil, fmt.Errorf("找不到ID为%s的美团外卖门店", poiCode)
}
return result[0], nil
}
return nil, err
}
func (a *API) PoiClose(poiCode string) (err error) {
_, err = a.AccessAPI("poi/close", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
})
return err
}
func (a *API) PoiOnline(poiCode string) (err error) {
_, err = a.AccessAPI("poi/online", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
})
return err
}
func (a *API) PoiUpdatePromoteInfo(poiCode, promotionInfo string) (err error) {
_, err = a.AccessAPI("poi/updatepromoteinfo", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
"promotion_info": promotionInfo,
})
return err
}
func (a *API) PoiTagList(poiCode string) (catList []*PoiCategoryInfo, err error) {
result, err := a.AccessAPI("poiTag/list", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
})
if err == nil {
catListTmp := result.([]interface{})
catList = make([]*PoiCategoryInfo, len(catListTmp))
for k, v := range catListTmp {
cat := v.(map[string]interface{})
catList[k] = &PoiCategoryInfo{
ID: int(utils.MustInterface2Int64(cat["id"])),
Name: utils.Interface2String(cat["name"]),
}
}
return catList, nil
}
return nil, err
}
func (a *API) PoiShipTimeUpdate(poiCode string, beginTime1, endTime1, beginTime2, endTime2 string) (err error) {
shippingTime := beginTime1 + "-" + endTime1
if beginTime2 != "" {
shippingTime += "," + beginTime2 + "-" + endTime2
}
_, err = a.AccessAPI("poi/updatepromoteinfo", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
"shipping_time": shippingTime,
})
return err
}
// 美团要求必须是jpg|jpeg图片格式且imgName必须以jpg或jpeg结尾
func (a *API) ImageUpload(poiCode, imgName string, imgData []byte) (imgID string, err error) {
result, err := a.AccessAPI("image/upload", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
KeyImgName: imgName,
KeyImgData: imgData,
})
if err == nil {
return result.(string), nil
}
return "", err
}
func (a *API) ImageUploadByURL(poiCode, imgName, imgURL string) (imgID string, err error) {
response, err := http.Get(imgURL)
if err == nil {
defer func() {
response.Body.Close()
}()
if response.StatusCode == http.StatusOK {
bodyData, err2 := ioutil.ReadAll(response.Body)
if err = err2; err == nil {
return a.ImageUpload(poiCode, imgName, bodyData)
}
} else {
err = platformapi.ErrHTTPCodeIsNot200
}
}
return "", err
}