wx_token.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_api/utils"
  6. )
  7. type WxToken struct {
  8. AccessToken string
  9. ExpiresIn int64
  10. Id int
  11. }
  12. func GetWxToken() (item *WxToken, err error) {
  13. sql := `SELECT * FROM wx_token `
  14. o := orm.NewOrm()
  15. err = o.Raw(sql).QueryRow(&item)
  16. return
  17. }
  18. func AddWxToken(token string, expiresIn int64) (err error) {
  19. sql := `INSERT INTO wx_token(access_token, expires_in)VALUES(?,?) `
  20. o := orm.NewOrm()
  21. _, err = o.Raw(sql, token, expiresIn).Exec()
  22. return
  23. }
  24. func UpdateWxToken(token string, expiresIn int64, id int) (err error) {
  25. sql := `UPDATE wx_token SET access_token=?, expires_in=? WHERE id=? `
  26. o := orm.NewOrm()
  27. _, err = o.Raw(sql, token, expiresIn, id).Exec()
  28. return
  29. }
  30. // ModifyAccessToken 修改wx_access_token
  31. func ModifyAccessToken(accessToken string, expiresIn int64) (err error) {
  32. o := orm.NewOrm()
  33. sql := `SELECT * FROM wx_token LIMIT 1`
  34. wxToken := new(WxToken)
  35. err = o.Raw(sql).QueryRow(&wxToken)
  36. if err != nil && err.Error() != utils.ErrNoRow() {
  37. utils.FileLog.Info("Get wxToken Err:", err.Error())
  38. return
  39. }
  40. if wxToken == nil {
  41. fmt.Println("wxToken is empty")
  42. addSql := "insert into wx_token (access_token,expires_in) values (?,?)"
  43. _, err = o.Raw(addSql, accessToken, expiresIn).Exec()
  44. } else {
  45. //判断token是否过期
  46. updateSql := "update wx_token set access_token = ?,expires_in = ? "
  47. _, err = o.Raw(updateSql, accessToken, expiresIn).Exec()
  48. fmt.Println("更新 TOKEN:", err)
  49. }
  50. return
  51. }