|
|
|
@ -2,12 +2,14 @@ package DataExchange
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"crypto/md5"
|
|
|
|
|
"dsBaseRpc/Utils/CommonUtil"
|
|
|
|
|
"dsBaseRpc/Utils/ConfigUtil"
|
|
|
|
|
"dsBaseRpc/Utils/DbUtil"
|
|
|
|
|
"dsBaseRpc/Utils/FileUtil"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
@ -234,16 +236,51 @@ func UploadData() int {
|
|
|
|
|
return postCount
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取签名用的结构体
|
|
|
|
|
type authStruct struct {
|
|
|
|
|
AuthTime string `json:"auth_time"`
|
|
|
|
|
SystemId string `json:"system_id"`
|
|
|
|
|
SystemToken string `json:"system_token"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
功能:将数据批量上报到汇集中心
|
|
|
|
|
作者:黄海
|
|
|
|
|
时间:2020-07-17
|
|
|
|
|
*/
|
|
|
|
|
func PostToServer(t tableStruct, list []map[string]interface{}) bool {
|
|
|
|
|
//(1)计算出system_token=MD5.hash(MD5.hash(system_id+auth_time)+system_key)
|
|
|
|
|
var as authStruct
|
|
|
|
|
as.AuthTime=CommonUtil.GetCurrentTime()
|
|
|
|
|
as.SystemId=ConfigUtil.DataExchangeSystemId
|
|
|
|
|
//计算 md5
|
|
|
|
|
w := md5.New()
|
|
|
|
|
io.WriteString(w, as.SystemId+as.AuthTime)
|
|
|
|
|
//将str写入到w中
|
|
|
|
|
md5str := fmt.Sprintf("%x", w.Sum(nil))
|
|
|
|
|
|
|
|
|
|
w = md5.New()
|
|
|
|
|
io.WriteString(w, md5str+ConfigUtil.DataExchangeSystemKey)
|
|
|
|
|
//将str写入到w中
|
|
|
|
|
md5str= fmt.Sprintf("%x", w.Sum(nil))
|
|
|
|
|
//系统token
|
|
|
|
|
as.SystemToken=md5str
|
|
|
|
|
|
|
|
|
|
//(2)根据system_token换取authToken
|
|
|
|
|
jsonBytes, err := json.Marshal(as)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(CommonUtil.GetCurrentTime() +err.Error())
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
p := httpDo("POST", ConfigUtil.DataExchangeSystemAuthUrl, string(jsonBytes))
|
|
|
|
|
if !p.Success {
|
|
|
|
|
fmt.Println(CommonUtil.GetCurrentTime() +"获取认证签名失败!")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
//上报到Http Api--->Body--->Post
|
|
|
|
|
var ps postStruct
|
|
|
|
|
ps.DataSource = t.DataSource
|
|
|
|
|
ps.AuthToken = ConfigUtil.DataExchangeAuthToken
|
|
|
|
|
ps.AuthToken =p.Message //p.Message中记录了authToken
|
|
|
|
|
ps.SystemId = ConfigUtil.DataExchangeSystemId
|
|
|
|
|
var dsMap = make([]dataStruct, 0)
|
|
|
|
|
for k := range list {
|
|
|
|
@ -263,10 +300,10 @@ func PostToServer(t tableStruct, list []map[string]interface{}) bool {
|
|
|
|
|
}
|
|
|
|
|
ps.Datas = dsMap
|
|
|
|
|
//将Struct转为json
|
|
|
|
|
jsonBytes, _ := json.Marshal(ps)
|
|
|
|
|
jsonBytes, _ = json.Marshal(ps)
|
|
|
|
|
msg := string(jsonBytes)
|
|
|
|
|
//提交到汇集中心
|
|
|
|
|
p := httpDo("POST", ConfigUtil.DataExchangeUrl, msg)
|
|
|
|
|
p = httpDo("POST", ConfigUtil.DataExchangeUrl, msg)
|
|
|
|
|
if !p.Success {
|
|
|
|
|
fmt.Println(CommonUtil.GetCurrentTime() + " 同步:上报到数据汇集中心失败,将休息5秒后重试!错误原因:" + p.Message)
|
|
|
|
|
time.Sleep(5 * 1e9)
|
|
|
|
|