Author | SHA1 | Message | Date |
---|---|---|---|
listen | 459668c150 | 去除日志 | 2 years ago |
listen | 609515783b | 修复bug | 3 years ago |
listen | 31ade9a301 | 加入获取code_bank一条可用记录的方法 | 3 years ago |
guzeng | 3afb9d3d28 | 生成pb文件 | 3 years ago |
listen | dc894e81dc | 新增获取一条code_bank记录接口 | 3 years ago |
guzeng | 0df76c79ee | 修改取所有配置方法 | 3 years ago |
guzeng | 8d6f89b962 | 增加获取所有配置方法 | 3 years ago |
guzeng | 17a0566887 | 增加公共方法 | 3 years ago |
guzeng | ad012e9e04 | 增加获取分级配置方法 | 3 years ago |
guzeng | 99b11fc452 | 增加获取所有配置方法 | 3 years ago |
guzeng | bcafeaa898 | 修改取siteinfo方法 | 3 years ago |
guzeng | 6b7051ea95 | 连接服务时增加配置判断 | 3 years ago |
guzeng | e8c45f9045 | 更新获取站点信息方法 | 3 years ago |
guzeng | 961bae3566 | 修改获取站点信息方法 | 3 years ago |
@ -0,0 +1,182 @@ | |||
package siterpc | |||
import ( | |||
"encoding/json" | |||
"errors" | |||
"log" | |||
"reflect" | |||
"git.tetele.net/tgo/helper" | |||
) | |||
/** | |||
* 取所有配置 | |||
* 2021/09/04 | |||
* GZ | |||
*/ | |||
func GetAllConfig(site_id, dbname string, url ...string) (map[string]string, error) { | |||
if dbname == "" { | |||
return nil, errors.New("参数错误") | |||
} | |||
conn, err := rpc_server_conn(url...) | |||
if err != nil { | |||
return nil, err | |||
} | |||
defer conn.Close() | |||
arg := SiteConfigItemReqArg{site_id, dbname, ""} | |||
req, err := SetReqData(arg) | |||
if err != nil { | |||
return nil, err | |||
} | |||
res := &Response{} | |||
err = conn.GetAllConfig(req, res) | |||
if err != nil { | |||
return nil, err | |||
} | |||
res_data_de, err := GetResData(res) | |||
if err != nil { | |||
return nil, err | |||
} | |||
if res_data_de == "" { | |||
return nil, nil | |||
} | |||
var res_arr []map[string]string | |||
err = json.Unmarshal([]byte(res_data_de), &res_arr) | |||
if err != nil { | |||
return nil, err | |||
} | |||
var res_map map[string]string = make(map[string]string) | |||
if len(res_arr) > 0 { | |||
var value string | |||
for _, info := range res_arr { | |||
value = "" | |||
switch info["Type"] { | |||
case "select": | |||
var content interface{} | |||
err = json.Unmarshal([]byte(info["Content"]), &content) | |||
if err != nil { | |||
log.Println(err) | |||
} | |||
content_type := reflect.TypeOf(content).String() //数据字典的类型 | |||
switch content_type { | |||
case "[]interface {}": //数组形式,value字段对应数组的key | |||
list := content.([]interface{}) | |||
val_key := helper.ToInt(info["Value"]) | |||
value = helper.ToString(list[val_key]) | |||
case "map[string]interface {}": //map形式,value字段对应key | |||
list := content.(map[string]interface{}) | |||
value = helper.ToString(list[info["Value"]]) | |||
} | |||
default: | |||
value = info["Value"] | |||
} | |||
res_map[info["Name"]] = value | |||
} | |||
} | |||
return res_map, nil | |||
} | |||
/** | |||
* 分组下所有配置 | |||
* 2021/09/04 | |||
* GZ | |||
*/ | |||
func GetGroupConfig(site_id, dbname string, groupname string, url ...string) (map[string]string, error) { | |||
if dbname == "" { | |||
return nil, errors.New("参数错误") | |||
} | |||
conn, err := rpc_server_conn(url...) | |||
if err != nil { | |||
return nil, err | |||
} | |||
defer conn.Close() | |||
arg := SiteConfigItemReqArg{site_id, dbname, groupname} | |||
req, err := SetReqData(arg) | |||
if err != nil { | |||
return nil, err | |||
} | |||
res := &Response{} | |||
err = conn.GetGroupConfig(req, res) | |||
if err != nil { | |||
return nil, err | |||
} | |||
res_data_de, err := GetResData(res) | |||
if err != nil { | |||
return nil, err | |||
} | |||
if res_data_de == "" { | |||
return nil, nil | |||
} | |||
var res_arr []map[string]string | |||
err = json.Unmarshal([]byte(res_data_de), &res_arr) | |||
if err != nil { | |||
return nil, err | |||
} | |||
var res_map map[string]string = make(map[string]string) | |||
if len(res_arr) > 0 { | |||
var value string | |||
for _, info := range res_arr { | |||
value = "" | |||
switch info["Type"] { | |||
case "select": | |||
var content interface{} | |||
err = json.Unmarshal([]byte(info["Content"]), &content) | |||
if err != nil { | |||
log.Println(err) | |||
} | |||
content_type := reflect.TypeOf(content).String() //数据字典的类型 | |||
switch content_type { | |||
case "[]interface {}": //数组形式,value字段对应数组的key | |||
list := content.([]interface{}) | |||
val_key := helper.ToInt(info["Value"]) | |||
value = helper.ToString(list[val_key]) | |||
case "map[string]interface {}": //map形式,value字段对应key | |||
list := content.(map[string]interface{}) | |||
value = helper.ToString(list[info["Value"]]) | |||
} | |||
default: | |||
value = info["Value"] | |||
} | |||
res_map[info["Name"]] = value | |||
} | |||
} | |||
return res_map, nil | |||
} |
@ -0,0 +1,13 @@ | |||
package siterpc | |||
import ( | |||
"testing" | |||
) | |||
func Test_GetAllConfig(t *testing.T) { | |||
list, err := GetGroupConfig("1056475", "shop_v2", "basic") | |||
t.Log(list) | |||
t.Log(err) | |||
} |
@ -0,0 +1,15 @@ | |||
package siterpc | |||
import ( | |||
"testing" | |||
"git.tetele.net/tgo/conf" | |||
) | |||
func Test_GetSiteInfoByHost(t *testing.T) { | |||
conf.IS_PRIVATE = true | |||
res, err := GetSiteInfoByHost("dev.tetele.net") | |||
t.Log(res) | |||
t.Log(err) | |||
} |
@ -0,0 +1,57 @@ | |||
package siterpc | |||
import ( | |||
"encoding/json" | |||
"errors" | |||
) | |||
/** | |||
* 获取code_bank一条可用记录 | |||
* 2021/09/09 | |||
* linsen | |||
*/ | |||
func GetOneCode(site_id, dbname string, url ...string) (string, error) { | |||
if dbname == "" { | |||
return "", errors.New("参数错误") | |||
} | |||
conn, err := rpc_server_conn(url...) | |||
if err != nil { | |||
return "", err | |||
} | |||
defer conn.Close() | |||
arg := SiteConfigItemReqArg{site_id, dbname, ""} | |||
req, err := SetReqData(arg) | |||
if err != nil { | |||
return "", err | |||
} | |||
res := &Response{} | |||
err = conn.GetOneCode(req, res) | |||
if err != nil { | |||
return "", err | |||
} | |||
res_data_de, err := GetResData(res) | |||
if err != nil { | |||
return "", err | |||
} | |||
var bankCode string | |||
err = json.Unmarshal([]byte(res_data_de),&bankCode) | |||
if err != nil { | |||
return "", err | |||
} | |||
if res_data_de == "" { | |||
return "", nil | |||
} | |||
return bankCode, nil | |||
} |
@ -0,0 +1,9 @@ | |||
package siterpc | |||
import "testing" | |||
func Test_getcode(t *testing.T){ | |||
res,err := GetOneCode("1058278","shop_v2") | |||
t.Log(res) | |||
t.Log(err) | |||
} |
@ -0,0 +1,106 @@ | |||
package siterpc | |||
import ( | |||
"encoding/json" | |||
"errors" | |||
"strconv" | |||
"time" | |||
"git.tetele.net/tgo/crypter" | |||
"github.com/golang/protobuf/proto" | |||
) | |||
func SetResData(data interface{}, res *Response) { | |||
res_data_json, err := json.Marshal(data) | |||
if err == nil { | |||
encryData := crypter.DesEn(string(res_data_json), DES_KEY) | |||
now_str := strconv.FormatInt(time.Now().Unix(), 10) | |||
res_sign := Sign(encryData, now_str) | |||
res.Data = proto.String(encryData) | |||
res.Time = proto.String(now_str) | |||
res.Sign = proto.String(res_sign) | |||
} | |||
} | |||
func SetReqData(arg interface{}) (*Request, error) { | |||
data_json, err := json.Marshal(arg) | |||
if err != nil { | |||
return nil, err | |||
} | |||
now_int64 := time.Now().Unix() | |||
encryData := crypter.DesEn(string(data_json), DES_KEY) | |||
now := strconv.FormatInt(now_int64, 10) | |||
sign := Sign(encryData, now) | |||
return &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}, nil | |||
} | |||
func GetReqData(req *Request) (string, error) { | |||
res_data := req.GetData() | |||
if res_data != "" { | |||
time_int64, err := strconv.ParseInt(req.GetTime(), 10, 64) | |||
if err != nil { | |||
return "", err | |||
} | |||
now_int64 := time.Now().Unix() | |||
if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 { | |||
//时间误差前后10秒,返回 | |||
return "", errors.New("返回时间错误") | |||
} | |||
check_sign := CheckSign(req.GetSign(), res_data, req.GetTime()) | |||
if !check_sign { | |||
return "", errors.New("返回数据签名错误") | |||
} | |||
//解密 | |||
return crypter.DesDe(res_data, DES_KEY), nil | |||
} | |||
return "", nil | |||
} | |||
func GetResData(res *Response) (string, error) { | |||
res_data := res.GetData() | |||
if res_data != "" { | |||
time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64) | |||
if err != nil { | |||
return "", err | |||
} | |||
now_int64 := time.Now().Unix() | |||
if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 { | |||
//时间误差前后10秒,返回 | |||
return "", errors.New("返回时间错误") | |||
} | |||
check_sign := CheckSign(res.GetSign(), res_data, res.GetTime()) | |||
if !check_sign { | |||
return "", errors.New("返回数据签名错误") | |||
} | |||
//解密 | |||
return crypter.DesDe(res_data, DES_KEY), nil | |||
} | |||
return "", nil | |||
} |
@ -0,0 +1,17 @@ | |||
package siterpc | |||
type SiteConfigItemReqArg struct { | |||
SiteId string `json:"site_id"` | |||
Dbname string `json:"dbname"` | |||
Key string `json:"key"` | |||
} | |||
type SiteConfigItemRes struct { | |||
Type string | |||
Value string | |||
Exist bool | |||
} | |||
type ConfigListRes struct { | |||
list string | |||
} |