24a6ba5c2a751c6ccd3752cbdd8c23998d7944ab.svn-base 4.5 KB

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