parent
eec374f6f7
commit
764513a257
@ -1,3 +1,3 @@
|
||||
## v1.0.19 / 2021-07-27
|
||||
## v1.0.20 / 2021-08-16
|
||||
|
||||
- 测试版本
|
||||
- 增加电影票服务
|
@ -0,0 +1,5 @@
|
||||
package go_library
|
||||
|
||||
func Version() string {
|
||||
return "v1.0.20"
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package library
|
||||
|
||||
func Version() string {
|
||||
return "v1.0.19"
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
AppKey string
|
||||
AppSecret string
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
type ErrResp struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
Code int `json:"code"`
|
||||
}
|
||||
|
||||
func (app *App) request(url string, params map[string]interface{}) ([]byte, error) {
|
||||
|
||||
// common params
|
||||
params["time"] = time.Now().Unix()
|
||||
params["appKey"] = app.AppKey
|
||||
|
||||
// sign params
|
||||
params["sign"] = app.getSign(app.AppSecret, params)
|
||||
|
||||
var req *http.Request
|
||||
req, err := http.NewRequest("POST", fmt.Sprintf("https://movieapi2.pintoto.cn/%s", url), strings.NewReader(app.getRequestData(params)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
httpClient := &http.Client{}
|
||||
httpClient.Timeout = app.Timeout
|
||||
var response *http.Response
|
||||
response, err = httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// 请求错误
|
||||
if response.StatusCode != 200 {
|
||||
return nil, errors.New(fmt.Sprintf("请求错误:%d", response.StatusCode))
|
||||
}
|
||||
|
||||
defer response.Body.Close()
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
|
||||
// 检查错误
|
||||
apiErr := ErrResp{}
|
||||
if err := json.Unmarshal(body, &apiErr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 接口状态错误
|
||||
if apiErr.Code != 200 {
|
||||
return nil, errors.New(apiErr.Message)
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func (app *App) GradeToFloat64(i interface{}) float64 {
|
||||
switch v := i.(type) {
|
||||
case string:
|
||||
float, _ := strconv.ParseFloat(v, 64)
|
||||
return float
|
||||
case float64:
|
||||
return v
|
||||
case int64:
|
||||
return float64(v) / math.Pow10(0)
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type GetCityArea struct {
|
||||
CityId int `json:"cityId"` // 城市id
|
||||
}
|
||||
|
||||
type GetCityAreaResult struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data struct {
|
||||
List []struct {
|
||||
AreaId int `json:"areaId"` // 区域id
|
||||
AreaName string `json:"areaName"` // 区域名
|
||||
} `json:"list"`
|
||||
} `json:"data"`
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
// GetCityArea 城市下区域 https://www.showdoc.com.cn/1154868044931571/6243539682553126
|
||||
func (app *App) GetCityArea(param GetCityArea) (result GetCityAreaResult, err error) {
|
||||
// api params
|
||||
params := map[string]interface{}{}
|
||||
b, _ := json.Marshal(¶m)
|
||||
var m map[string]interface{}
|
||||
_ = json.Unmarshal(b, &m)
|
||||
for k, v := range m {
|
||||
params[k] = v
|
||||
}
|
||||
body, err := app.request("movieapi/movie-info/get-city-area", params)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err = json.Unmarshal(body, &result); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type GetCityListResult struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data struct {
|
||||
List []struct {
|
||||
PinYin string `json:"pinYin"` // 城市首字母
|
||||
RegionName string `json:"regionName"` // 城市名
|
||||
CityId int `json:"cityId"` // 城市id
|
||||
} `json:"list"`
|
||||
} `json:"data"`
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
// GetCityList 城市列表 https://www.showdoc.com.cn/1154868044931571/5865562425538244
|
||||
func (app *App) GetCityList() (result GetCityListResult, err error) {
|
||||
body, err := app.request("movieapi/movie-info/get-city-list", map[string]interface{}{})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err = json.Unmarshal(body, &result); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package v2
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type GetVersionResult struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data string `json:"data"`
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
// GetVersion 获取同步版本号 https://www.showdoc.com.cn/1154868044931571/6566701084841699
|
||||
func (app *App) GetVersion() (result GetVersionResult, err error) {
|
||||
body, err := app.request("movieapi/movie-info/get-version", map[string]interface{}{})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err = json.Unmarshal(body, &result); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (app *App) getSign(appSecret string, p map[string]interface{}) string {
|
||||
var keys []string
|
||||
for k := range p {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
signStr := ""
|
||||
for _, key := range keys {
|
||||
signStr += fmt.Sprintf("%s=%s&", key, app.getString(p[key]))
|
||||
}
|
||||
signStr += fmt.Sprintf("appSecret=%s", appSecret)
|
||||
// md5加密
|
||||
data := []byte(signStr)
|
||||
has := md5.Sum(data)
|
||||
return fmt.Sprintf("%x", has)
|
||||
}
|
||||
|
||||
func (app *App) getString(i interface{}) string {
|
||||
switch v := i.(type) {
|
||||
case string:
|
||||
return v
|
||||
case int:
|
||||
return strconv.Itoa(v)
|
||||
case bool:
|
||||
return strconv.FormatBool(v)
|
||||
default:
|
||||
bytes, _ := json.Marshal(v)
|
||||
return string(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取请求数据
|
||||
func (app *App) getRequestData(params map[string]interface{}) string {
|
||||
// 公共参数
|
||||
args := url.Values{}
|
||||
// 请求参数
|
||||
for key, val := range params {
|
||||
args.Set(key, app.getString(val))
|
||||
}
|
||||
return args.Encode()
|
||||
}
|
Loading…
Reference in new issue