cfg.go 967 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package cfg
  2. import (
  3. "encoding/json"
  4. "mtp2_if/global/app"
  5. "mtp2_if/global/e"
  6. "net/http"
  7. "os"
  8. "github.com/gin-gonic/gin"
  9. )
  10. // QueryCfg 查询终端配置
  11. func QueryCfg(c *gin.Context) {
  12. appG := app.Gin{C: c}
  13. // 获取请求参数
  14. key := appG.C.Query("key")
  15. if key == "" {
  16. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  17. return
  18. }
  19. // 读取json文件
  20. filePtr, err := os.Open("config/cfg.json")
  21. if err != nil {
  22. appG.Response(http.StatusBadRequest, e.ERROR_CFG_FILE_FAIL, nil)
  23. return
  24. }
  25. defer filePtr.Close()
  26. var datas map[string]interface{}
  27. // 创建Json编码器
  28. decoder := json.NewDecoder(filePtr)
  29. if err = decoder.Decode(&datas); err != nil {
  30. appG.Response(http.StatusBadRequest, e.ERROR_CFG_JSON_FAIL, nil)
  31. return
  32. }
  33. // 获取指定key的配置内容
  34. if _, ok := datas[key]; !ok {
  35. appG.Response(http.StatusBadRequest, e.ERROR_CFG_KEY_FAIL, nil)
  36. return
  37. }
  38. appG.Response(http.StatusOK, e.SUCCESS, datas[key])
  39. }