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