123 lines
2.7 KiB
Go
123 lines
2.7 KiB
Go
package YiDun
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/md5"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"servicebase/pkg/common/HyTools"
|
|
"sort"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/anxpp/beego/logs"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
const ()
|
|
|
|
// 易盾API 一键登录等
|
|
type YiDunClient struct {
|
|
SecretId string
|
|
SecretKey string
|
|
}
|
|
|
|
func NewYiDunClient() *YiDunClient {
|
|
secretId := viper.GetString("netease.yidun.secretId")
|
|
secretKey := viper.GetString("netease.yidun.secretKey")
|
|
return &YiDunClient{SecretId: secretId, SecretKey: secretKey}
|
|
}
|
|
|
|
// 生成易盾签名
|
|
func (client *YiDunClient) GenerateSign(params map[string]string) (sign string) {
|
|
|
|
var keys []string
|
|
for key, _ := range params {
|
|
keys = append(keys, key)
|
|
}
|
|
sort.Strings(keys)
|
|
buf := bytes.NewBufferString("")
|
|
for _, key := range keys {
|
|
buf.WriteString(key + params[key])
|
|
}
|
|
buf.WriteString(client.SecretKey)
|
|
has := md5.Sum(buf.Bytes())
|
|
|
|
sign = fmt.Sprintf("%x", has)
|
|
return
|
|
}
|
|
|
|
// 获取易盾请求公共参数
|
|
func (client *YiDunClient) GetCommonParams(businessId string) (params map[string]string) {
|
|
|
|
params = make(map[string]string, 0)
|
|
params["secretId"] = client.SecretId
|
|
params["businessId"] = businessId
|
|
params["version"] = "v1"
|
|
params["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
|
|
params["nonce"] = HyTools.GetUUID()
|
|
|
|
return
|
|
}
|
|
|
|
// 一键登录API
|
|
func (client *YiDunClient) OneClickApi(params map[string]string) (mobile string, resultErr error) {
|
|
|
|
apiUrl := "https://ye.dun.163.com/v1/oneclick/check"
|
|
|
|
paramString := ""
|
|
for k, v := range params {
|
|
kv := k + "=" + v + "&"
|
|
paramString += kv
|
|
}
|
|
|
|
if len(paramString) > 0 {
|
|
paramString = HyTools.Substr(paramString, 0, len(paramString)-1)
|
|
}
|
|
|
|
logs.Info("oneClick params:" + paramString)
|
|
|
|
headerMap := make(map[string]string, 0)
|
|
headerMap["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
|
|
|
|
result, err := HyTools.HttpDo(http.MethodPost, apiUrl, headerMap, paramString)
|
|
logs.Info("oneClick result:" + result)
|
|
|
|
if err != nil {
|
|
logs.Error("一键登录失败,请求API失败:" + err.Error())
|
|
resultErr = err
|
|
}
|
|
|
|
//{"code":200,"msg":"ok","data":{"phone":"13079217909","resultCode":"0"}}
|
|
|
|
var resultDTO struct {
|
|
Code int64 `json:"code"`
|
|
Msg string `json:"msg"`
|
|
Data map[string]interface{} `json:"data"`
|
|
}
|
|
|
|
err2 := json.Unmarshal([]byte(result), &resultDTO)
|
|
|
|
if err2 != nil {
|
|
logs.Error("一键登录失败:解析API结果失败" + err2.Error())
|
|
resultErr = err2
|
|
return
|
|
}
|
|
|
|
if resultDTO.Code != 200 {
|
|
logs.Error("一键登录失败(%s) code[%v]:"+resultDTO.Msg, "", resultDTO.Code)
|
|
resultErr = errors.New(resultDTO.Msg)
|
|
return
|
|
}
|
|
|
|
// 结果
|
|
|
|
if phone, ok := resultDTO.Data["phone"]; ok {
|
|
mobile = phone.(string)
|
|
}
|
|
|
|
return
|
|
}
|