feat:完善信息
This commit is contained in:
211
app/common/platform/gaode.go
Normal file
211
app/common/platform/gaode.go
Normal file
@ -0,0 +1,211 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"SciencesServer/utils"
|
||||
)
|
||||
|
||||
// 高德API
|
||||
|
||||
type Gaode struct{}
|
||||
|
||||
type (
|
||||
Location struct {
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
AdCode string `json:"adcode"`
|
||||
Rectangle string `json:"rectangle"`
|
||||
}
|
||||
// GaoDeLocationResponse 第三方位置响应参数
|
||||
GaoDeLocationResponse struct {
|
||||
Status string `json:"status"`
|
||||
Count string `json:"count"`
|
||||
Info string `json:"info"`
|
||||
InfoCode string `json:"infocode"`
|
||||
GeoCodes []struct {
|
||||
FormattedAddress string `json:"formatted_address"`
|
||||
Country string `json:"country"`
|
||||
Province string `json:"province"`
|
||||
CityCode string `json:"citycode"`
|
||||
City string `json:"city"`
|
||||
District string `json:"district"`
|
||||
Township []string `json:"township"`
|
||||
Neighborhood []struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
} `json:"neighborhood"`
|
||||
Building []struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
} `json:"building"`
|
||||
AdCode string `json:"adcode"`
|
||||
Street string `json:"street"`
|
||||
Number string `json:"number"`
|
||||
Location string `json:"location"`
|
||||
Level string `json:"level"`
|
||||
} `json:"geocodes"`
|
||||
}
|
||||
|
||||
// GaoDeLocationReverseResponse 第三方反向位置响应参数
|
||||
GaoDeLocationReverseResponse struct {
|
||||
Status string `json:"status"`
|
||||
Count string `json:"count"`
|
||||
Info string `json:"info"`
|
||||
InfoCode string `json:"infocode"`
|
||||
ReGeoCode struct {
|
||||
FormattedAddress string `json:"formatted_address"`
|
||||
AddressComponent struct {
|
||||
Country string `json:"country"`
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
CityCode string `json:"citycode"`
|
||||
District string `json:"district"`
|
||||
AdCode string `json:"adcode"`
|
||||
Township string `json:"township"`
|
||||
TownCode string `json:"town_code"`
|
||||
Neighborhood struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
} `json:"neighborhood"`
|
||||
Building struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
} `json:"building"`
|
||||
StreetNumber struct {
|
||||
Street string `json:"street"`
|
||||
Number string `json:"number"`
|
||||
Location string `json:"location"`
|
||||
Direction string `json:"direction"`
|
||||
Distance string `json:"distance"`
|
||||
} `json:"streetNumber"`
|
||||
BusinessAreas []struct {
|
||||
Location string `json:"location"`
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"`
|
||||
} `json:"businessAreas"`
|
||||
} `json:"addressComponent"`
|
||||
} `json:"regeocode"`
|
||||
}
|
||||
|
||||
// GaoDeLocationIPResponse IP定位查询
|
||||
GaoDeLocationIPResponse struct {
|
||||
Status string `json:"status"`
|
||||
Count string `json:"count"`
|
||||
Info string `json:"info"`
|
||||
InfoCode string `json:"infocode"`
|
||||
Province interface{} `json:"province"`
|
||||
City interface{} `json:"city"`
|
||||
AdCode interface{} `json:"adcode"`
|
||||
Rectangle interface{} `json:"rectangle"`
|
||||
}
|
||||
|
||||
// GaoDeWeatherResponse 第三方天气响应参数
|
||||
GaoDeWeatherResponse struct {
|
||||
Status string `json:"status"`
|
||||
Count string `json:"count"`
|
||||
Info string `json:"info"`
|
||||
InfoCode string `json:"infocode"`
|
||||
Lives []struct {
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
AdCode string `json:"adcode"`
|
||||
Weather string `json:"weather"`
|
||||
Temperature string `json:"temperature"`
|
||||
WindDirection string `json:"winddirection"`
|
||||
WindPower string `json:"windpower"`
|
||||
Humidity string `json:"humidity"`
|
||||
ReportTime string `json:"reporttime"`
|
||||
} `json:"lives"`
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
GDKey = "11b1e9db2756e3482b10754bf59d0d94"
|
||||
|
||||
gaoDeRequestURLForLocation string = "https://restapi.amap.com/v3/geocode/geo"
|
||||
gaoDeRequestURLForLocationReverse string = "https://restapi.amap.com/v3/geocode/regeo"
|
||||
gaoDeRequestURLForLocationIP string = "https://restapi.amap.com/v3/ip"
|
||||
gaoDeRequestURLForLocationWeather string = "https://restapi.amap.com/v3/weather/weatherInfo"
|
||||
)
|
||||
|
||||
// Location 位置信息
|
||||
func (this *Gaode) Location(address, city string) (*GaoDeLocationResponse, error) {
|
||||
params := map[string]interface{}{
|
||||
"key": GDKey, "address": "address", "city": city,
|
||||
}
|
||||
client := utils.NewClient(gaoDeRequestURLForLocation, utils.MethodForGet, params)
|
||||
|
||||
resp, err := client.Request(utils.RequestBodyFormatForFormData)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response := new(GaoDeLocationResponse)
|
||||
|
||||
_ = utils.FromJSONBytes(resp, response)
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// LocationReverse 反向位置
|
||||
func (this *Gaode) LocationReverse(location string) (*GaoDeLocationReverseResponse, error) {
|
||||
params := map[string]interface{}{
|
||||
"key": GDKey, "location": location,
|
||||
}
|
||||
client := utils.NewClient(gaoDeRequestURLForLocationReverse, utils.MethodForGet, params)
|
||||
|
||||
resp, err := client.Request(utils.RequestBodyFormatForFormData)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response := new(GaoDeLocationReverseResponse)
|
||||
|
||||
_ = utils.FromJSONBytes(resp, response)
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// LocationIP IP查询
|
||||
func (this *Gaode) LocationIP(ip string) (*GaoDeLocationIPResponse, error) {
|
||||
params := map[string]interface{}{
|
||||
"key": GDKey,
|
||||
}
|
||||
if ip != "" {
|
||||
params["ip"] = ip
|
||||
}
|
||||
client := utils.NewClient(gaoDeRequestURLForLocationIP, utils.MethodForGet, params)
|
||||
|
||||
resp, err := client.Request(utils.RequestBodyFormatForFormData)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response := new(GaoDeLocationIPResponse)
|
||||
|
||||
_ = utils.FromJSONBytes(resp, response)
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// Weather 天气信息
|
||||
func (this *Gaode) Weather(city string) (*GaoDeWeatherResponse, error) {
|
||||
params := map[string]interface{}{
|
||||
"key": GDKey, "city": city,
|
||||
}
|
||||
client := utils.NewClient(gaoDeRequestURLForLocationWeather, utils.MethodForGet, params)
|
||||
|
||||
resp, err := client.Request(utils.RequestBodyFormatForFormData)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response := new(GaoDeWeatherResponse)
|
||||
|
||||
_ = utils.FromJSONBytes(resp, response)
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func NewGaode() *Gaode {
|
||||
return &Gaode{}
|
||||
}
|
92
app/common/platform/sms.go
Normal file
92
app/common/platform/sms.go
Normal file
@ -0,0 +1,92 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"SciencesServer/utils"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 短信API
|
||||
|
||||
type Sms struct{}
|
||||
|
||||
type SmsParam struct {
|
||||
Mobile []string `json:"mobile"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type SmsSendMode int
|
||||
|
||||
const (
|
||||
// SmsSendModeForAlone 常规单独发送
|
||||
SmsSendModeForAlone SmsSendMode = iota + 1
|
||||
// SmsSendModeForGroup 常规群发
|
||||
SmsSendModeForGroup
|
||||
// SmsSendModeForVariable 变量发送
|
||||
SmsSendModeForVariable
|
||||
)
|
||||
|
||||
const (
|
||||
smsSendURLForAlone string = "http://122.144.203.27:8001/mt.ashx"
|
||||
smsSendURLForGroup string = "http://122.144.203.27:8001/mts.ashx"
|
||||
smsSendURLForVariable string = "http://122.144.203.27:8001/mts_var_json.ashx"
|
||||
)
|
||||
|
||||
const (
|
||||
keyForAccount string = "113934"
|
||||
keyForPassword string = "zytDuehC"
|
||||
)
|
||||
|
||||
type SmsSend func(mode SmsSendMode, params *SmsParam) error
|
||||
|
||||
var smsSendModeHandle = map[SmsSendMode]func(params *SmsParam) (string, utils.Method, map[string]interface{}){
|
||||
SmsSendModeForAlone: alone,
|
||||
SmsSendModeForGroup: group,
|
||||
}
|
||||
|
||||
func alone(params *SmsParam) (string, utils.Method, map[string]interface{}) {
|
||||
return smsSendURLForAlone, utils.MethodForGet, map[string]interface{}{
|
||||
"msg": params.Content, "pn": params.Mobile[0],
|
||||
}
|
||||
}
|
||||
|
||||
func group(params *SmsParam) (string, utils.Method, map[string]interface{}) {
|
||||
return smsSendURLForGroup, utils.MethodForPost, map[string]interface{}{
|
||||
"msg": params.Content, "pn": strings.Join(params.Mobile, ","),
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Sms) Send() SmsSend {
|
||||
return func(mode SmsSendMode, params *SmsParam) error {
|
||||
now := time.Now().Format("20060102150405")
|
||||
|
||||
_params := map[string]interface{}{
|
||||
"account": keyForAccount,
|
||||
"pswd": strings.ToUpper(utils.Md5String(keyForAccount + keyForPassword + now)),
|
||||
"ts": now,
|
||||
}
|
||||
handle, has := smsSendModeHandle[mode]
|
||||
|
||||
if !has {
|
||||
return errors.New("未知的短信模式")
|
||||
}
|
||||
url, method, parameter := handle(params)
|
||||
|
||||
if len(parameter) > 0 {
|
||||
for k, v := range parameter {
|
||||
_params[k] = v
|
||||
}
|
||||
}
|
||||
client := utils.NewClient(url, method, _params)
|
||||
|
||||
_, err := client.Request(utils.RequestBodyFormatForFormData, utils.Headers{
|
||||
ContentType: utils.RequestContentTypeForXWWWFormUrlencoded,
|
||||
})
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func NewSms() *Sms {
|
||||
return &Sms{}
|
||||
}
|
10
app/common/platform/sms_test.go
Normal file
10
app/common/platform/sms_test.go
Normal file
@ -0,0 +1,10 @@
|
||||
package platform
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNewSms(t *testing.T) {
|
||||
err := NewSms().Send()(SmsSendModeForGroup, &SmsParam{
|
||||
Mobile: []string{"17718184079"}, Content: "【商挈科技】您的验证码是789789",
|
||||
})
|
||||
t.Log(err)
|
||||
}
|
76
app/common/platform/wechat.go
Normal file
76
app/common/platform/wechat.go
Normal file
@ -0,0 +1,76 @@
|
||||
package platform
|
||||
|
||||
import "SciencesServer/utils"
|
||||
|
||||
type Wechat struct{}
|
||||
|
||||
type WechatScan struct{}
|
||||
|
||||
type WechatScanHandle func() *WechatScan
|
||||
|
||||
type (
|
||||
WechatBasicRequest struct {
|
||||
AppID, Secret string
|
||||
}
|
||||
WechatErrorResponse struct {
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg int `json:"errmsg"`
|
||||
}
|
||||
// WechatAccessTokenRequest 请求AccessToken参数
|
||||
WechatAccessTokenRequest struct {
|
||||
WechatBasicRequest
|
||||
Code string
|
||||
}
|
||||
// WechatAccessTokenResponse 获取AccessToken响应参数
|
||||
WechatAccessTokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
OpenID string `json:"openid"`
|
||||
Scope string `json:"scope"`
|
||||
UnionID string `json:"unionid"`
|
||||
*WechatErrorResponse
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
// WechatAccessTokenUrl AccessToken访问地址
|
||||
WechatAccessTokenUrl string = "https://api.weixin.qq.com/sns/oauth2/access_token"
|
||||
)
|
||||
|
||||
func (this *WechatScan) Login() {
|
||||
|
||||
}
|
||||
|
||||
func (this *WechatScan) Pay() {
|
||||
|
||||
}
|
||||
|
||||
// AccessToken AccessToken操作
|
||||
func (this *Wechat) AccessToken(req *WechatAccessTokenRequest) (*WechatAccessTokenResponse, error) {
|
||||
params := map[string]interface{}{
|
||||
"appid": req.AppID, "secret": req.Secret, "code": req.Code, "grant_type": "authorization_code",
|
||||
}
|
||||
resp, err := utils.NewClient(WechatAccessTokenUrl, utils.MethodForGet, params).Request(utils.RequestBodyFormatForXWWWFormUrlencoded)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := new(WechatAccessTokenResponse)
|
||||
|
||||
if err = utils.FromJSONBytes(resp, out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Scan 扫码操作
|
||||
func (this *Wechat) Scan() WechatScanHandle {
|
||||
return func() *WechatScan {
|
||||
return &WechatScan{}
|
||||
}
|
||||
}
|
||||
|
||||
func NewWechat() *Wechat {
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user