qywx.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package wx
  2. import (
  3. "ColdVerify_server/logs"
  4. "ColdVerify_server/models/Account"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "time"
  11. )
  12. var Qytoken string
  13. func init() {
  14. go func() {
  15. gettoken()
  16. time.Sleep(time.Hour * 24)
  17. }()
  18. }
  19. func gettoken() {
  20. url := "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww0befb06f19910467&corpsecret=83STGNLwghXsjYRacRhUHPL-fkkO1jwsMgabjXcRLyg"
  21. method := "GET"
  22. client := &http.Client {
  23. }
  24. req, err := http.NewRequest(method, url, nil)
  25. if err != nil {
  26. fmt.Println(err)
  27. return
  28. }
  29. res, err := client.Do(req)
  30. if err != nil {
  31. fmt.Println(err)
  32. return
  33. }
  34. defer res.Body.Close()
  35. body, err := ioutil.ReadAll(res.Body)
  36. if err != nil {
  37. fmt.Println(err)
  38. return
  39. }
  40. fmt.Println(string(body))
  41. // Product _
  42. type Product struct {
  43. Access_token string `json:"access_token"`
  44. Errcode int `json:"errcode"`
  45. }
  46. p := &Product{}
  47. err = json.Unmarshal(body, p)
  48. if err != nil {
  49. time.Sleep(time.Second * 1)
  50. gettoken()
  51. }
  52. if p.Errcode != 0 {
  53. time.Sleep(time.Second * 1)
  54. gettoken()
  55. }
  56. Qytoken = p.Access_token
  57. }
  58. // Message represents the JSON structure
  59. type Message struct {
  60. Touser string `json:"touser"`
  61. Toparty string `json:"toparty"`
  62. Totag string `json:"totag"`
  63. Msgtype string `json:"msgtype"`
  64. Agentid int `json:"agentid"`
  65. Text Text `json:"text"`
  66. Safe int `json:"safe"`
  67. EnableIDTrans int `json:"enable_id_trans"`
  68. EnableDuplicateCheck int `json:"enable_duplicate_check"`
  69. }
  70. // Text represents the text component of the message
  71. type Text struct {
  72. Content string `json:"content"`
  73. }
  74. // @all LiKeHui|xxxx
  75. func WxSend(User,Content string) {
  76. if len(User) == 0 {
  77. logs.Println("Error User 为空")
  78. return
  79. }
  80. User = Account.AdminWXListToMap(Account.Read_Admin_List_ALL_1())[User]
  81. if len(User) == 0 {
  82. logs.Println("Error User 为空")
  83. return
  84. }
  85. url := "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+Qytoken
  86. method := "POST"
  87. // Example of creating and marshaling the struct to JSON
  88. message := Message{
  89. Touser: User,
  90. Toparty: "@all",
  91. Totag: "@all",
  92. Msgtype: "text",
  93. Agentid: 1000008,
  94. Text: Text{Content: Content},
  95. Safe: 0,
  96. EnableIDTrans: 0,
  97. EnableDuplicateCheck: 0,
  98. }
  99. jsonData, err := json.Marshal(message)
  100. if err != nil {
  101. logs.Println("Error marshalling to JSON:", err)
  102. return
  103. }
  104. logs.Println(string(jsonData))
  105. payload := strings.NewReader(string(jsonData))
  106. client := &http.Client {
  107. }
  108. req, err := http.NewRequest(method, url, payload)
  109. if err != nil {
  110. fmt.Println(err)
  111. return
  112. }
  113. req.Header.Add("Content-Type", "application/json")
  114. res, err := client.Do(req)
  115. if err != nil {
  116. fmt.Println(err)
  117. return
  118. }
  119. defer res.Body.Close()
  120. body, err := ioutil.ReadAll(res.Body)
  121. if err != nil {
  122. fmt.Println(err)
  123. return
  124. }
  125. logs.Println("WxSend:",string(body))
  126. if strings.Contains(string(body),"access_token expired"){
  127. gettoken()
  128. time.Sleep(time.Second * 1)
  129. WxSend(User,Content)
  130. }
  131. }