package util import ( "bytes" "fmt" "github.com/Anderson-Lu/gofasion/gofasion" "github.com/kptyun/KPTCOMM/routers/restful" "github.com/xormplus/xorm" "io/ioutil" "net/http" "strings" ) func Httppostdata(url,RequestMethod,parammaps string,tx *xorm.Session)string{ fsion := gofasion.NewFasion(parammaps) sqlnamestr := fsion.Get("name").ValueStr() sqlnamestr1 := fsion.Get("name1").ValueStr() offset := fsion.Get("offset").ValueDefaultInt(0) pagecount := fsion.Get("pagecount").ValueDefaultInt(0) returntype := fsion.Get("returntype").ValueDefaultStr("map") sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx) sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx) body := []byte("") if sql != "" { s_params := make([]interface{}, 0) if fsion.HasKey("parammaps") { parammaps := fsion.Get("parammaps") paramslist := strings.Split(p, ",") if len(paramslist) > 0 && p != "" { for _, value := range paramslist { s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr()) } } } queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx) if err != nil { } else { paramslist1 := strings.Split(p1, ",") for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) { s_params1 := make([]interface{}, 0) if len(paramslist1) > 0 && p1 != "" { for _, v := range paramslist1 { s_params1 = append(s_params1, value[strings.ToLower(strings.Trim(v, " "))]) } } arr, _ := tx.SQL(sql1, s_params1...).QueryString()//获取 queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["arrList"] = arr body,_ = json.Marshal(queryData) } } } bs := bytes.NewBuffer(body) client := &http.Client{} //body1 := bytes.NewBuffer([]byte("{\"username\":\"00028\",\"password\":\"123456\"}")) req, err := http.NewRequest(RequestMethod, url, bs, ) if err != nil { } req.Header.Set("Content-Type", "application/json;charset=utf-8") req.Header.Set("token","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IjAwMDI4IiwicGFzc3dvcmQiOiJlMTBhZGMzOTQ5YmE1OWFiYmU1NmUwNTdmMjBmODgzZSIsImV4cCI6MTYxOTM4NjU0MiwiaXNzIjoiaHR0cHM6Ly9naXRodWIuY29tL2twdHl1bi9nby1hZG1pbi8ifQ.sJwnE9HTOVnDNOaeHqOQzi7cSydYkWS4uQHxtjY7UMY") if err != nil { // handle error } resp, err := client.Do(req) if err != nil { fmt.Println(err) return "" } defer resp.Body.Close() result_body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println(err) return "" } fmt.Println(string(result_body)) return "" } func getDataBySqlT(sqlstr string, offset, pagecount int, returntype string, params []interface{}, tx *xorm.Session) (interface{}, error) { if offset > 0 { offset = offset - 1 } //if pagecount == 0 { // pagecount = setting.AppSetting.PageSize //}else if pagecount>setting.AppSetting.PageSize{ // pagecount = setting.AppSetting.PageSize //} if returntype == "list" { queryData, err := restful.QueryByListT(sqlstr, offset, pagecount, params, tx) return queryData, err } else if returntype == "listlist" { queryData, err := restful.QueryByListListT(sqlstr, offset, pagecount, params, tx) return queryData, err } else { queryData, err := restful.QueryByMapT(sqlstr, offset, pagecount, params, tx) return queryData, err } }