grfdApiUtil.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package util
  2. import (
  3. "../../pkg/logging"
  4. "bytes"
  5. "fmt"
  6. "github.com/pkg/errors"
  7. "github.com/xormplus/xorm"
  8. "io/ioutil"
  9. "mime/multipart"
  10. "net/http"
  11. )
  12. type JSON1 struct {
  13. Access_token string `json:"access_token"`
  14. }
  15. //格润富德api调用(可咨询刘欢)
  16. func Get_AccessToken1(corpid,corpsecret string) string {
  17. gettoken_url := "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret
  18. //print(gettoken_url)
  19. client := &http.Client{}
  20. req, _ := client.Get(gettoken_url)
  21. defer req.Body.Close()
  22. body, _ := ioutil.ReadAll(req.Body)
  23. //fmt.Printf("\n%q",string(body))
  24. var json_str JSON
  25. json.Unmarshal([]byte(body), &json_str)
  26. //fmt.Printf("\n%q",json_str.Access_token)
  27. return json_str.Access_token
  28. }
  29. func Send_Message1(msg string,url string)[]byte {
  30. //print(send_url)
  31. client := &http.Client{}
  32. body := bytes.NewBuffer([]byte(msg))
  33. req, err := http.NewRequest("POST",url,body)
  34. req.Header.Set("Content-Type", "application/json")
  35. req.Header.Set("charset","UTF-8")
  36. if err != nil {
  37. // handle error
  38. }
  39. resp, err := client.Do(req)
  40. if err != nil {
  41. fmt.Println(err)
  42. return nil
  43. }
  44. defer resp.Body.Close()
  45. result_body, err := ioutil.ReadAll(resp.Body)
  46. if err != nil {
  47. fmt.Println(err)
  48. return nil
  49. }
  50. return result_body
  51. }
  52. /*func main() {
  53. postData := make(map[string]string)
  54. postData["anchorId"] = "361155076095561728"
  55. postData["searchBegin"] = "2019-03-01 00:00:00"
  56. postData["searchEnd"] = "2020-03-10 00:00:00"
  57. url := "https:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  58. PostWithFormData("POST",url ,&postData)
  59. }*/
  60. func PostWithFormData(method, url string, postData *map[string]string)error{
  61. body := new(bytes.Buffer)
  62. w := multipart.NewWriter(body)
  63. for k,v := range *postData{
  64. w.WriteField(k, v)
  65. }
  66. w.Close()
  67. req, _ := http.NewRequest(method, url, body)
  68. logging.Info("setdata: ",body.String())
  69. req.Header.Set("Content-Type", w.FormDataContentType())
  70. resp, _ := http.DefaultClient.Do(req)
  71. data, _ := ioutil.ReadAll(resp.Body)
  72. resp.Body.Close()
  73. m := make(map[string]interface{},0)
  74. json.Unmarshal(data,&m)
  75. rescode := 0
  76. switch m["status"].(type) {
  77. case float64:
  78. rescode = int(m["status"].(float64) )
  79. case int:
  80. rescode = m["status"].(int)
  81. }
  82. if rescode == 1{
  83. return errors.New( xorm.String(m["message"]))
  84. }
  85. return nil
  86. }
  87. // 栏舍
  88. type Feedp struct{
  89. Status int `json:"status"`
  90. ReturnCode string `json:"returnCode"`
  91. MessageType int `json:"messageType"`
  92. Message string `json:"message"`
  93. Data []FeedpData `json:"data"`
  94. }
  95. type FeedpData struct{
  96. PenId string `json:"penId"`
  97. PenName string `json:"penName"`
  98. CowCount int `json:"cowCount"`
  99. CreatedAt string `json:"createdAt"`
  100. ModifiedAt string `json:"modifiedAt"`
  101. CcountRatio string `json:"ccountRatio"`
  102. }
  103. // 牛群类别分类
  104. type Feed struct{
  105. Status int `json:"status"`
  106. Message string `json:"message"`
  107. Data []FeedData `json:"data"`
  108. }
  109. type FeedData struct{
  110. FId string `json:"fId"`
  111. FName string `json:"fName"`
  112. FeedclassName string `json:"feedclassName"`
  113. DryMatter string `json:"dryMatter"`
  114. Price string `json:"price"`
  115. AutoChange string `json:"autoChange"`
  116. AllowDev string `json:"allowDev"`
  117. CreatedAt string `json:"createdAt"`
  118. ModifiedAt string `json:"modifiedAt"`
  119. }
  120. // 饲料分类
  121. type FeedClass struct{
  122. Status int `json:"status"`
  123. Message string `json:"message"`
  124. Data []FeedClassData `json:"data"`
  125. }
  126. type FeedClassData struct{
  127. FeedclassName string `json:"feedclassName"`
  128. FeedclassId string `json:"feedclassId"`
  129. CreatedAt string `json:"createdAt"`
  130. ModifiedAt string `json:"modifiedAt"`
  131. }
  132. // 牛群类别分类
  133. type CowClass struct{
  134. Status int `json:"status"`
  135. Message string `json:"message"`
  136. Data []CowClassData `json:"data"`
  137. }
  138. type CowClassData struct{
  139. CowClassCode string `json:"cowClassCode"`
  140. CowClassName string `json:"cowClassName"`
  141. CreatedAt string `json:"createdAt"`
  142. ModifiedAt string `json:"modifiedAt"`
  143. }
  144. // 配方
  145. type FeedTemplet struct{
  146. Status int `json:"status"`
  147. Message string `json:"message"`
  148. Data []FeedTempletData `json:"data"`
  149. }
  150. type FeedTempletData struct{
  151. FtId string `json:"ftId"`
  152. FtName string `json:"ftName"`
  153. CreatedAt string `json:"createdAt"`
  154. ModifiedAt string `json:"modifiedAt"`
  155. FeedList []FeedListData `json:"feedList"`
  156. }
  157. type FeedListData struct{
  158. FId string `json:"fId"`
  159. FName string `json:"fName"`
  160. DryMatter string `json:"dryMatter"`
  161. FodderWeight string `json:"fodderWeight"`
  162. MixNo string `json:"mixNo"`
  163. RecipeCost string `json:"recipeCost"`
  164. FeedclassName string `json:"feedclassName"`
  165. AllowableError string `json:"allowableError"`
  166. Autosecond string `json:"autosecond"`
  167. }