wechat.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package models
  2. import (
  3. "encoding/json"
  4. "eta/eta_pub/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/rdlucklib/rdluck_tools/http"
  8. )
  9. type WxToken struct {
  10. AccessToken string
  11. ExpiresIn int64
  12. Id int64
  13. }
  14. type WxAccessToken struct {
  15. AccessToken string `json:"access_token"`
  16. ExpiresIn int `json:"expires_in"`
  17. Errcode int `json:"errcode"`
  18. Errmsg string `json:"errmsg"`
  19. }
  20. func GetWxToken(wxAppId, wxAppSecret string) (item *WxAccessToken, err error) {
  21. getUrl := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxAppId + "&secret=" + wxAppSecret
  22. result, err := http.Get(getUrl)
  23. if err != nil {
  24. utils.FileLog.Info("GetWxToken Err:", err.Error())
  25. return
  26. }
  27. utils.FileLog.Info(fmt.Sprintf("GetWxAccessToken :%s", string(result)))
  28. err = json.Unmarshal(result, &item)
  29. if err != nil {
  30. fmt.Println(fmt.Sprintf("GetWxToken Unmarshal Err:%s", err.Error()))
  31. return
  32. }
  33. if item.Errmsg != "" {
  34. utils.FileLog.Info(fmt.Sprintf("GetWxToken fail result:%s", string(result)))
  35. }
  36. return
  37. }
  38. func GetWxTokenCygx() (item *WxAccessToken, err error) {
  39. var getUrl string
  40. if utils.RunMode == "debug" {
  41. getUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + utils.WxAppId + "&secret=" + utils.WxAppSecret
  42. } else {
  43. getUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + utils.WxAppIdCygx + "&secret=" + utils.WxAppSecretCygx
  44. }
  45. result, err := http.Get(getUrl)
  46. if err != nil {
  47. utils.FileLog.Info("GetWxToken Err:", err.Error())
  48. return
  49. }
  50. utils.FileLog.Info(fmt.Sprintf("GetWxAccessToken :%s", string(result)))
  51. err = json.Unmarshal(result, &item)
  52. if err != nil {
  53. fmt.Println(fmt.Sprintf("GetWxToken Unmarshal Err:%s", err.Error()))
  54. return
  55. }
  56. if item.Errmsg != "" {
  57. utils.FileLog.Info(fmt.Sprintf("GetWxToken fail result:%s", string(result)))
  58. }
  59. return
  60. }
  61. // ModifyAccessToken 修改wx_access_token
  62. func ModifyAccessToken(accessToken string, expiresIn int64) (err error) {
  63. o := orm.NewOrm()
  64. sql := `SELECT * FROM wx_token LIMIT 1`
  65. wxToken := new(WxToken)
  66. err = o.Raw(sql).QueryRow(&wxToken)
  67. if err != nil && err.Error() != utils.ErrNoRow() {
  68. utils.FileLog.Info("Get wxToken Err:", err.Error())
  69. return
  70. }
  71. if wxToken == nil {
  72. fmt.Println("wxToken is empty")
  73. addSql := "insert into wx_token (access_token,expires_in) values (?,?)"
  74. _, err = o.Raw(addSql, accessToken, expiresIn).Exec()
  75. } else {
  76. //判断token是否过期
  77. updateSql := "update wx_token set access_token = ?,expires_in = ? "
  78. _, err = o.Raw(updateSql, accessToken, expiresIn).Exec()
  79. fmt.Println("更新 TOKEN:", err)
  80. }
  81. return
  82. }
  83. func GetWxTokenMfyx() (item *WxAccessToken, err error) {
  84. var getUrl string
  85. if utils.RunMode == "debug"{
  86. getUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + utils.WxAppId + "&secret=" + utils.WxAppSecret
  87. }else{
  88. getUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + utils.WxAppIdMfyx + "&secret=" + utils.WxAppSecretMfyx
  89. }
  90. result, err := http.Get(getUrl)
  91. if err != nil {
  92. utils.FileLog.Info("GetWxToken Err:", err.Error())
  93. return
  94. }
  95. utils.FileLog.Info(fmt.Sprintf("GetWxAccessToken :%s", string(result)))
  96. err = json.Unmarshal(result, &item)
  97. if err != nil {
  98. fmt.Println(fmt.Sprintf("GetWxToken Unmarshal Err:%s", err.Error()))
  99. return
  100. }
  101. if item.Errmsg != "" {
  102. utils.FileLog.Info(fmt.Sprintf("GetWxToken fail result:%s", string(result)))
  103. }
  104. return
  105. }