182 lines
5.4 KiB
Go
182 lines
5.4 KiB
Go
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"`
|
||
}
|
||
|
||
type PoiInfo struct {
|
||
Address string `json:"address,omitempty"`
|
||
AppID int `json:"app_id,omitempt"`
|
||
AppPoiCode string `json:"app_poi_code,omitempt"`
|
||
CityID int `json:"city_id,omitempt"`
|
||
Ctime int64 `json:"ctime,omitempt"`
|
||
InvoiceDescription string `json:"invoice_description,omitempt"`
|
||
InvoiceMinPrice int `json:"invoice_min_price,omitempt"`
|
||
InvoiceSupport int `json:"invoice_support,omitempt"`
|
||
IsOnline int `json:"is_online,omitempt"`
|
||
Latitude float64 `json:"latitude,omitempt"`
|
||
LocationID int `json:"location_id,omitempt"`
|
||
Longitude float64 `json:"longitude,omitempt"`
|
||
Name string `json:"name,omitempt"`
|
||
OpenLevel int `json:"open_level,omitempt"`
|
||
Phone string `json:"phone,omitempt"`
|
||
PicURL string `json:"pic_url,omitempt"`
|
||
PicURLLarge string `json:"pic_url_large,omitempt"`
|
||
PreBook int `json:"pre_book,omitempt"`
|
||
PreBookMaxDays int `json:"pre_book_max_days,omitempt"`
|
||
PreBookMinDays int `json:"pre_book_min_days,omitempt"`
|
||
PromotionInfo string `json:"promotion_info,omitempt"`
|
||
Remark string `json:"remark,omitempt"`
|
||
ShippingFee float64 `json:"shipping_fee,omitempt"`
|
||
ShippingTime string `json:"shipping_time,omitempt"`
|
||
StandbyTel string `json:"standby_tel,omitempt"`
|
||
TagName string `json:"tag_name,omitempt"`
|
||
ThirdTagName string `json:"third_tag_name,omitempt"`
|
||
TimeSelect int `json:"time_select,omitempt"`
|
||
Utime int64 `json:"utime,omitempt"`
|
||
}
|
||
|
||
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 []*PoiInfo, err error) {
|
||
result, err := a.AccessAPI("poi/mget", true, map[string]interface{}{
|
||
KeyAppPoiCodes: strings.Join(poiCodes, ","),
|
||
})
|
||
if err == nil {
|
||
err = utils.Map2StructByJson(result, &pois, false)
|
||
}
|
||
return pois, err
|
||
}
|
||
|
||
func (a *API) PoiGet(poiCode string) (poi *PoiInfo, 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) PoiOpen(poiCode string) (err error) {
|
||
_, err = a.AccessAPI("poi/open", false, map[string]interface{}{
|
||
KeyAppPoiCode: poiCode,
|
||
})
|
||
return err
|
||
}
|
||
|
||
func (a *API) PoiOffline(poiCode string) (err error) {
|
||
_, err = a.AccessAPI("poi/offline", 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, shippingTime string) (err error) {
|
||
_, err = a.AccessAPI("poi/shippingtime/update", 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
|
||
}
|