wechat.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package models
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/http"
  7. "hongze/hongze_task/utils"
  8. "strings"
  9. "time"
  10. )
  11. type WxToken struct {
  12. AccessToken string
  13. ExpiresIn int64
  14. Id int64
  15. }
  16. func GetWxAccessToken() (accessTokenStr string, err error) {
  17. o := orm.NewOrm()
  18. sql := `SELECT * FROM wx_token LIMIT 1`
  19. wxToken := new(WxToken)
  20. err = o.Raw(sql).QueryRow(&wxToken)
  21. if err != nil && err.Error() != utils.ErrNoRow() {
  22. utils.FileLog.Info(fmt.Sprintf("Get wxToken Err:", err.Error()))
  23. return
  24. }
  25. //Token不存在
  26. if wxToken == nil {
  27. accessToken, err := GetWxToken()
  28. if err != nil {
  29. return "", err
  30. }
  31. if accessToken.AccessToken != "" {
  32. expiresIn := time.Now().Add(time.Duration(accessToken.ExpiresIn) * time.Second).Unix()
  33. addSql := "insert into wx_token (access_token,expires_in) values (?,?)"
  34. _, err = o.Raw(addSql, accessToken.AccessToken, expiresIn).Exec()
  35. accessTokenStr = accessToken.AccessToken
  36. }
  37. return accessTokenStr, err
  38. } else {
  39. //判断token是否过期
  40. if time.Now().Unix() > wxToken.ExpiresIn {
  41. accessToken, err := GetWxToken()
  42. if err != nil {
  43. return "", err
  44. }
  45. if accessToken.AccessToken != "" {
  46. expiresIn := time.Now().Add(time.Duration(accessToken.ExpiresIn) * time.Second).Unix()
  47. updateSql := "update wx_token set access_token = ?,expires_in = ? "
  48. _, err = o.Raw(updateSql, accessToken.AccessToken, expiresIn).Exec()
  49. accessTokenStr = accessToken.AccessToken
  50. }
  51. return accessTokenStr, err
  52. } else {
  53. return wxToken.AccessToken, nil
  54. }
  55. }
  56. return
  57. }
  58. type WxAccessToken struct {
  59. AccessToken string `json:"access_token"`
  60. ExpiresIn int `json:"expires_in"`
  61. Errcode int
  62. Errmsg string
  63. }
  64. func GetWxToken() (item *WxAccessToken, err error) {
  65. getUrl := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + utils.WxAppId + "&secret=" + utils.WxAppSecret
  66. result, err := http.Get(getUrl)
  67. if err != nil {
  68. utils.FileLog.Info(fmt.Sprintf("GetWxToken Err:", err.Error()))
  69. return
  70. }
  71. if string(result) == "" {
  72. utils.FileLog.Info(fmt.Sprintf("GetWxToken: %s", string(result)))
  73. }
  74. fmt.Println("result", string(result))
  75. //必须要初始化该变量,要不然下面会报错
  76. err = json.Unmarshal(result, &item)
  77. if err != nil {
  78. utils.FileLog.Info(fmt.Sprintf("GetWxToken Unmarshal Err: %s ,%s", string(result), err.Error()))
  79. return
  80. }
  81. if item.Errmsg != "" {
  82. utils.FileLog.Info(fmt.Sprintf("GetWxToken fail result:%s", string(result)))
  83. }
  84. return
  85. }
  86. type WxUsers struct {
  87. Total int
  88. Count int
  89. Data struct {
  90. Openid []string
  91. }
  92. NextOpenid string
  93. }
  94. func WxUsersGet() (openIdStr string) {
  95. accessToken, err := GetWxAccessToken()
  96. if err != nil {
  97. utils.FileLog.Info("GetWxAccessToken Err:%s", err.Error())
  98. return
  99. }
  100. url := "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken
  101. result, err := http.Get(url)
  102. if err != nil {
  103. utils.FileLog.Info("GetUser Err:", err.Error())
  104. return
  105. }
  106. item := new(WxUsers)
  107. err = json.Unmarshal(result, &item)
  108. if err != nil {
  109. fmt.Println("Unmarshal Err:", err.Error())
  110. return
  111. }
  112. openIdStr = strings.Join(item.Data.Openid, "','")
  113. openIdStr = "'" + openIdStr + "'"
  114. utils.FileLog.Info("openIdStr:%s", openIdStr)
  115. return
  116. }