站点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

  1. package siterpc
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "log"
  6. "reflect"
  7. "git.tetele.net/tgo/helper"
  8. )
  9. /**
  10. * 取所有配置
  11. * 2021/09/04
  12. * GZ
  13. */
  14. func GetAllConfig(site_id, dbname string, url ...string) (map[string]string, error) {
  15. if dbname == "" {
  16. return nil, errors.New("参数错误")
  17. }
  18. conn, err := rpc_server_conn(url...)
  19. if err != nil {
  20. return nil, err
  21. }
  22. defer conn.Close()
  23. arg := SiteConfigItemReqArg{site_id, dbname, ""}
  24. req, err := SetReqData(arg)
  25. if err != nil {
  26. return nil, err
  27. }
  28. res := &Response{}
  29. err = conn.GetAllConfig(req, res)
  30. if err != nil {
  31. return nil, err
  32. }
  33. res_data_de, err := GetResData(res)
  34. if err != nil {
  35. return nil, err
  36. }
  37. if res_data_de == "" {
  38. return nil, nil
  39. }
  40. var res_arr []map[string]string
  41. err = json.Unmarshal([]byte(res_data_de), &res_arr)
  42. if err != nil {
  43. return nil, err
  44. }
  45. var res_map map[string]string = make(map[string]string)
  46. if len(res_arr) > 0 {
  47. var value string
  48. for _, info := range res_arr {
  49. value = ""
  50. switch info["Type"] {
  51. case "select":
  52. var content interface{}
  53. err = json.Unmarshal([]byte(info["Content"]), &content)
  54. if err != nil {
  55. log.Println(err)
  56. }
  57. content_type := reflect.TypeOf(content).String() //数据字典的类型
  58. switch content_type {
  59. case "[]interface {}": //数组形式,value字段对应数组的key
  60. list := content.([]interface{})
  61. val_key := helper.ToInt(info["Value"])
  62. value = helper.ToString(list[val_key])
  63. case "map[string]interface {}": //map形式,value字段对应key
  64. list := content.(map[string]interface{})
  65. value = helper.ToString(list[info["Value"]])
  66. }
  67. default:
  68. value = info["Value"]
  69. }
  70. res_map[info["Name"]] = value
  71. }
  72. }
  73. return res_map, nil
  74. }
  75. /**
  76. * 分组下所有配置
  77. * 2021/09/04
  78. * GZ
  79. */
  80. func GetGroupConfig(site_id, dbname string, groupname string, url ...string) (map[string]string, error) {
  81. if dbname == "" {
  82. return nil, errors.New("参数错误")
  83. }
  84. conn, err := rpc_server_conn(url...)
  85. if err != nil {
  86. return nil, err
  87. }
  88. defer conn.Close()
  89. arg := SiteConfigItemReqArg{site_id, dbname, groupname}
  90. req, err := SetReqData(arg)
  91. if err != nil {
  92. return nil, err
  93. }
  94. res := &Response{}
  95. err = conn.GetGroupConfig(req, res)
  96. if err != nil {
  97. return nil, err
  98. }
  99. res_data_de, err := GetResData(res)
  100. if err != nil {
  101. return nil, err
  102. }
  103. if res_data_de == "" {
  104. return nil, nil
  105. }
  106. var res_arr []map[string]string
  107. err = json.Unmarshal([]byte(res_data_de), &res_arr)
  108. if err != nil {
  109. return nil, err
  110. }
  111. var res_map map[string]string = make(map[string]string)
  112. if len(res_arr) > 0 {
  113. var value string
  114. for _, info := range res_arr {
  115. value = ""
  116. switch info["Type"] {
  117. case "select":
  118. var content interface{}
  119. err = json.Unmarshal([]byte(info["Content"]), &content)
  120. if err != nil {
  121. log.Println(err)
  122. }
  123. content_type := reflect.TypeOf(content).String() //数据字典的类型
  124. switch content_type {
  125. case "[]interface {}": //数组形式,value字段对应数组的key
  126. list := content.([]interface{})
  127. val_key := helper.ToInt(info["Value"])
  128. value = helper.ToString(list[val_key])
  129. case "map[string]interface {}": //map形式,value字段对应key
  130. list := content.(map[string]interface{})
  131. value = helper.ToString(list[info["Value"]])
  132. }
  133. default:
  134. value = info["Value"]
  135. }
  136. res_map[info["Name"]] = value
  137. }
  138. }
  139. return res_map, nil
  140. }