站点RPC方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
3.4 KiB

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
}