httppostdata.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package util
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "tmr-watch/http/handle/restful"
  9. "github.com/Anderson-Lu/gofasion/gofasion"
  10. "github.com/xormplus/xorm"
  11. )
  12. func Httppostdata(url, RequestMethod, parammaps string, tx *xorm.Session) string {
  13. fsion := gofasion.NewFasion(parammaps)
  14. sqlnamestr := fsion.Get("name").ValueStr()
  15. sqlnamestr1 := fsion.Get("name1").ValueStr()
  16. offset := fsion.Get("offset").ValueDefaultInt(0)
  17. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  18. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  19. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  20. sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
  21. body := []byte("")
  22. if sql != "" {
  23. s_params := make([]interface{}, 0)
  24. if fsion.HasKey("parammaps") {
  25. parammaps := fsion.Get("parammaps")
  26. paramslist := strings.Split(p, ",")
  27. if len(paramslist) > 0 && p != "" {
  28. for _, value := range paramslist {
  29. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  30. }
  31. }
  32. }
  33. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  34. if err != nil {
  35. } else {
  36. paramslist1 := strings.Split(p1, ",")
  37. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  38. s_params1 := make([]interface{}, 0)
  39. if len(paramslist1) > 0 && p1 != "" {
  40. for _, v := range paramslist1 {
  41. s_params1 = append(s_params1, value[strings.ToLower(strings.Trim(v, " "))])
  42. }
  43. }
  44. arr, _ := tx.SQL(sql1, s_params1...).QueryString() //获取
  45. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["arrList"] = arr
  46. body, _ = json.Marshal(queryData)
  47. }
  48. }
  49. }
  50. bs := bytes.NewBuffer(body)
  51. client := &http.Client{}
  52. //body1 := bytes.NewBuffer([]byte("{\"username\":\"00028\",\"password\":\"123456\"}"))
  53. req, err := http.NewRequest(RequestMethod,
  54. url,
  55. bs,
  56. )
  57. if err != nil {
  58. }
  59. req.Header.Set("Content-Type", "application/json;charset=utf-8")
  60. req.Header.Set("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IjAwMDI4IiwicGFzc3dvcmQiOiJlMTBhZGMzOTQ5YmE1OWFiYmU1NmUwNTdmMjBmODgzZSIsImV4cCI6MTYxOTM4NjU0MiwiaXNzIjoiaHR0cHM6Ly9naXRodWIuY29tL2twdHl1bi9nby1hZG1pbi8ifQ.sJwnE9HTOVnDNOaeHqOQzi7cSydYkWS4uQHxtjY7UMY")
  61. if err != nil {
  62. // handle error
  63. }
  64. resp, err := client.Do(req)
  65. if err != nil {
  66. fmt.Println(err)
  67. return ""
  68. }
  69. defer resp.Body.Close()
  70. result_body, err := ioutil.ReadAll(resp.Body)
  71. if err != nil {
  72. fmt.Println(err)
  73. return ""
  74. }
  75. fmt.Println(string(result_body))
  76. return ""
  77. }
  78. func getDataBySqlT(sqlstr string, offset, pagecount int, returntype string, params []interface{}, tx *xorm.Session) (interface{}, error) {
  79. if offset > 0 {
  80. offset = offset - 1
  81. }
  82. //if pagecount == 0 {
  83. // pagecount = setting.AppSetting.PageSize
  84. //}else if pagecount>setting.AppSetting.PageSize{
  85. // pagecount = setting.AppSetting.PageSize
  86. //}
  87. if returntype == "list" {
  88. queryData, err := restful.QueryByListT(sqlstr, offset, pagecount, params, tx)
  89. return queryData, err
  90. } else if returntype == "listlist" {
  91. queryData, err := restful.QueryByListListT(sqlstr, offset, pagecount, params, tx)
  92. return queryData, err
  93. } else {
  94. queryData, err := restful.QueryByMapT(sqlstr, offset, pagecount, params, tx)
  95. return queryData, err
  96. }
  97. }