123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package models
- import (
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "hongze/hongze_api/utils"
- )
- type WxToken struct {
- AccessToken string
- ExpiresIn int64
- Id int
- }
- func GetWxToken() (item *WxToken, err error) {
- sql := `SELECT * FROM wx_token `
- o := orm.NewOrm()
- err = o.Raw(sql).QueryRow(&item)
- return
- }
- func AddWxToken(token string, expiresIn int64) (err error) {
- sql := `INSERT INTO wx_token(access_token, expires_in)VALUES(?,?) `
- o := orm.NewOrm()
- _, err = o.Raw(sql, token, expiresIn).Exec()
- return
- }
- func UpdateWxToken(token string, expiresIn int64, id int) (err error) {
- sql := `UPDATE wx_token SET access_token=?, expires_in=? WHERE id=? `
- o := orm.NewOrm()
- _, err = o.Raw(sql, token, expiresIn, id).Exec()
- return
- }
- // ModifyAccessToken 修改wx_access_token
- func ModifyAccessToken(accessToken string, expiresIn int64) (err error) {
- o := orm.NewOrm()
- sql := `SELECT * FROM wx_token LIMIT 1`
- wxToken := new(WxToken)
- err = o.Raw(sql).QueryRow(&wxToken)
- if err != nil && err.Error() != utils.ErrNoRow() {
- utils.FileLog.Info("Get wxToken Err:", err.Error())
- return
- }
- if wxToken == nil {
- fmt.Println("wxToken is empty")
- addSql := "insert into wx_token (access_token,expires_in) values (?,?)"
- _, err = o.Raw(addSql, accessToken, expiresIn).Exec()
- } else {
- //判断token是否过期
- updateSql := "update wx_token set access_token = ?,expires_in = ? "
- _, err = o.Raw(updateSql, accessToken, expiresIn).Exec()
- fmt.Println("更新 TOKEN:", err)
- }
- return
- }
|