360 lines
9.4 KiB
Go
360 lines
9.4 KiB
Go
package xianmai
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/codinl/go-logger"
|
|
"io"
|
|
"io/ioutil"
|
|
"mh-server/config"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type XianMaiClient struct {
|
|
CustomPhone string `json:"custom_phone"`
|
|
BaseURL string `json:"base_url"`
|
|
}
|
|
|
|
var xian *XianMaiClient
|
|
|
|
func GetXianClient() *XianMaiClient {
|
|
if xian == nil {
|
|
xian = &XianMaiClient{
|
|
CustomPhone: "17080320980",
|
|
BaseURL: "https://oam.xianmai.net.cn/",
|
|
//BaseURL: "https://test.oam.xianmai.net.cn/",
|
|
}
|
|
}
|
|
return xian
|
|
}
|
|
|
|
func (m *XianMaiClient) post(xmApi string, params map[string]interface{}, resp interface{}) error {
|
|
//params.Set("customPhone", m.CustomPhone)
|
|
//uri := m.BaseURL + xmApi + params.Encode()
|
|
params["customPhone"] = m.CustomPhone
|
|
|
|
uri := m.BaseURL + xmApi
|
|
data, err := json.Marshal(params)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return err
|
|
}
|
|
|
|
res, err := http.Post(uri, "application/json;charset=utf-8", strings.NewReader(string(data)))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return err
|
|
}
|
|
//headers["Content-Type"] = "application/json;charset=utf-8"
|
|
//res.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
defer res.Body.Close()
|
|
|
|
dataRsp, err := ioutil.ReadAll(res.Body)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return err
|
|
}
|
|
|
|
fmt.Println("dataRsp:", string(dataRsp))
|
|
if err = json.Unmarshal(dataRsp, resp); err != nil {
|
|
fmt.Println(err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type GameCassetteListResp struct {
|
|
Flag bool `json:"flag"`
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data1 struct {
|
|
Total int `json:"total"`
|
|
List []struct {
|
|
GoodsId int `json:"goodsId"`
|
|
GoodsName string `json:"goodsName"`
|
|
GoodsImg string `json:"goodsImg"`
|
|
Keyword string `json:"keyword"`
|
|
} `json:"list"`
|
|
PageNum int `json:"pageNum"`
|
|
PageSize int `json:"pageSize"`
|
|
Size int `json:"size"`
|
|
StartRow int `json:"startRow"`
|
|
EndRow int `json:"endRow"`
|
|
Pages int `json:"pages"`
|
|
PrePage int `json:"prePage"`
|
|
NextPage int `json:"nextPage"`
|
|
IsFirstPage bool `json:"isFirstPage"`
|
|
IsLastPage bool `json:"isLastPage"`
|
|
HasPreviousPage bool `json:"hasPreviousPage"`
|
|
HasNextPage bool `json:"hasNextPage"`
|
|
NavigatePages int `json:"navigatePages"`
|
|
NavigatepageNums []int `json:"navigatepageNums"`
|
|
NavigateFirstPage int `json:"navigateFirstPage"`
|
|
NavigateLastPage int `json:"navigateLastPage"`
|
|
} `json:"data1"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
type Cassette struct {
|
|
GoodsId int `json:"goods_id"`
|
|
GoodsName string `json:"goods_name"`
|
|
GoodsImg string `json:"goods_img"`
|
|
Keyword string `json:"keyword"`
|
|
}
|
|
|
|
type GameCheckGoodsResp struct {
|
|
Flag bool `json:"flag"`
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data1 struct {
|
|
GoodsId int `json:"goodsId"`
|
|
GoodsName string `json:"goodsName"`
|
|
GoodsImg string `json:"goodsImg"`
|
|
SkuTitleVos []struct {
|
|
ProblemId int `json:"problemId"`
|
|
ProblemName string `json:"problemName"`
|
|
Interval int `json:"interval"`
|
|
SkuDetailVoList []struct {
|
|
ProblemValueId int `json:"problemValueId"`
|
|
ProblemValueName string `json:"problemValueName"`
|
|
} `json:"skuDetailVoList"`
|
|
Toption int `json:"toption"`
|
|
Tindex int `json:"tindex"`
|
|
} `json:"skuTitleVos"`
|
|
ProblemTitleVos []struct {
|
|
ProblemId int `json:"problemId"`
|
|
ProblemName string `json:"problemName"`
|
|
Interval int `json:"interval"`
|
|
SkuDetailVoList interface{} `json:"skuDetailVoList"`
|
|
ProblemDetailVoList []struct {
|
|
ProblemValueId int `json:"problemValueId"`
|
|
ProblemValueName string `json:"problemValueName"`
|
|
} `json:"problemDetailVoList"`
|
|
Toption int `json:"toption"`
|
|
Tindex int `json:"tindex"`
|
|
} `json:"problemTitleVos"`
|
|
} `json:"data1"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
type XMCheckGoods struct {
|
|
GoodsId int `json:"goods_id"`
|
|
GoodsName string `json:"goods_name"`
|
|
GoodsImg string `json:"goods_img"`
|
|
Skus []XMSkuTitle `json:"skus"`
|
|
Problems []XMProblemTitle `json:"problems"`
|
|
}
|
|
|
|
type XMSkuTitle struct {
|
|
ProblemId int `json:"problem_id"`
|
|
ProblemName string `json:"problem_name"`
|
|
List []XMSkuDetail `json:"list"`
|
|
}
|
|
|
|
type XMSkuDetail struct {
|
|
ProblemValueId int `json:"problem_value_id"`
|
|
ProblemValueName string `json:"problem_value_name"`
|
|
}
|
|
|
|
type XMProblemTitle struct {
|
|
ProblemId int `json:"problem_id"`
|
|
ProblemName string `json:"problem_name"`
|
|
List []XMSkuDetail `json:"list"`
|
|
}
|
|
|
|
type XMProblemAttr struct {
|
|
ProblemAttrId string `json:"problemAttrId"`
|
|
ProblemAttrValueId string `json:"problemAttrValueId"`
|
|
}
|
|
|
|
type GameEvaluationResp struct {
|
|
Flag bool `json:"flag"`
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data1 interface{} `json:"data1"`
|
|
Data int `json:"data"`
|
|
}
|
|
|
|
type SanMaiClient struct {
|
|
Authentication string `json:"authentication"`
|
|
BaseURL string `json:"base_url"`
|
|
ExpireTime time.Time `json:"expire_time"`
|
|
}
|
|
|
|
const (
|
|
SmBaseURL = "https://testh5.xianmai.net.cn" //测试环境
|
|
//SmBaseURL = "https://www.xianmai.net.cn" //正式环境
|
|
)
|
|
|
|
var sanMaiClient *SanMaiClient
|
|
|
|
func GetSanMaiClient() *SanMaiClient {
|
|
nowTime := time.Now()
|
|
fmt.Println("time.Now:", nowTime)
|
|
if sanMaiClient == nil || sanMaiClient.ExpireTime.Before(nowTime) {
|
|
fmt.Println("初始化数据")
|
|
sanMaiClient = &SanMaiClient{
|
|
Authentication: SanMaiToken(),
|
|
BaseURL: config.AppConfig.XianMaiBaseUrl,
|
|
ExpireTime: nowTime.AddDate(0, 0, 1),
|
|
}
|
|
return sanMaiClient
|
|
}
|
|
return sanMaiClient
|
|
}
|
|
|
|
func SanMaiToken() string {
|
|
smExternal := SmExternal{
|
|
ParaMap: SubmitLoginApiReq{
|
|
Username: "13307551242",
|
|
Password: "dw123",
|
|
},
|
|
ApiRoute: "/loginApi",
|
|
}
|
|
sanMaiClient = &SanMaiClient{
|
|
Authentication: "",
|
|
BaseURL: config.AppConfig.XianMaiBaseUrl,
|
|
}
|
|
|
|
resp := &SubmitLoginApiResp{}
|
|
err := sanMaiClient.post(smExternal.ApiRoute, smExternal.ParaMap, resp)
|
|
if err != nil {
|
|
logger.Error("SubmitLoginApiResp err:", err)
|
|
return ""
|
|
}
|
|
return resp.Data.Token
|
|
}
|
|
|
|
func EoSanMaiToken(wechatId, wechatMobile, requestType string) (string, error) {
|
|
if wechatId == "" || wechatMobile == "" || requestType == "" {
|
|
return "", errors.New("para err")
|
|
}
|
|
smExternal := SmExternal{
|
|
ParaMap: SubmitLoginApiReq{
|
|
Username: "13307551242",
|
|
Password: "dw123",
|
|
WechatId: wechatId,
|
|
WechatMobile: wechatMobile,
|
|
RequestType: requestType,
|
|
},
|
|
ApiRoute: "/loginApi",
|
|
}
|
|
sanMaiClient = &SanMaiClient{
|
|
Authentication: "",
|
|
BaseURL: config.AppConfig.XianMaiBaseUrl,
|
|
}
|
|
|
|
resp := &SubmitLoginApiResp{}
|
|
err := sanMaiClient.post(smExternal.ApiRoute, smExternal.ParaMap, resp)
|
|
if err != nil {
|
|
logger.Error("SubmitLoginApiResp err:", err)
|
|
return "", err
|
|
}
|
|
return resp.Data.Token, nil
|
|
}
|
|
|
|
type SubmitLoginApiReq struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
WechatId string `json:"wechatId"`
|
|
WechatMobile string `json:"wechatMobile"`
|
|
RequestType string `json:"requestType"`
|
|
}
|
|
type SubmitLoginApiResp struct {
|
|
Flag bool `json:"flag"`
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data1 interface{} `json:"data1"`
|
|
Data struct {
|
|
ComponyName string `json:"componyName"`
|
|
Permissions []string `json:"permissions"`
|
|
Roles []string `json:"roles"`
|
|
ExipreTime string `json:"exipreTime"`
|
|
ComponyId string `json:"componyId"`
|
|
Token string `json:"token"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
//func (m *SanMaiClient) post(xmApi string, params map[string]interface{}, resp interface{}) error {
|
|
// uri := m.BaseURL + xmApi
|
|
// data, err := json.Marshal(params)
|
|
// if err != nil {
|
|
// logger.Error("Marshal params err:", err)
|
|
// return err
|
|
// }
|
|
//
|
|
// req, err := http.NewRequest("POST", uri, bytes.NewReader(data))
|
|
// if err != nil {
|
|
// logger.Error("NewRequest err:", err)
|
|
// return err
|
|
// }
|
|
// req.Header.Add("Content-Type", "application/json;charset=utf-8")
|
|
// req.Header.Add("Authentication", m.Authentication)
|
|
//
|
|
// res, err := http.DefaultClient.Do(req)
|
|
// if err != nil {
|
|
// logger.Error("Request err:", err)
|
|
// return err
|
|
// }
|
|
//
|
|
// defer res.Body.Close()
|
|
//
|
|
// dataRsp, err := io.ReadAll(res.Body)
|
|
// if err != nil {
|
|
// fmt.Println(err)
|
|
// return err
|
|
// }
|
|
//
|
|
// fmt.Println("dataRsp:", string(dataRsp))
|
|
// if err = json.Unmarshal(dataRsp, resp); err != nil {
|
|
// fmt.Println(err)
|
|
// return err
|
|
// }
|
|
//
|
|
// return nil
|
|
//}
|
|
|
|
func (m *SanMaiClient) post(xmApi string, params interface{}, resp interface{}) error {
|
|
uri := m.BaseURL + xmApi
|
|
data, err := json.Marshal(¶ms)
|
|
if err != nil {
|
|
logger.Error("Marshal params err:", err)
|
|
return err
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", uri, bytes.NewReader(data))
|
|
if err != nil {
|
|
logger.Error("NewRequest err:", err)
|
|
return err
|
|
}
|
|
req.Header.Add("Content-Type", "application/json;charset=utf-8")
|
|
req.Header.Add("Authentication", m.Authentication)
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
logger.Error("Request err:", err)
|
|
return err
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
dataRsp, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
logger.Error("ReadAll Body err:", err)
|
|
return err
|
|
}
|
|
|
|
fmt.Println("dataRsp:", string(dataRsp))
|
|
if err = json.Unmarshal(dataRsp, resp); err != nil {
|
|
logger.Error("Unmarshal dataRsp err:", err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|