|
- package models
-
- import (
- "code.gitea.io/gitea/modules/log"
- "time"
- )
-
- type WechatBindAction int
-
- const (
- WECHAT_BIND WechatBindAction = iota + 1
- WECHAT_UNBIND
- )
-
- type WechatBindLog struct {
- ID int64 `xorm:"pk autoincr"`
- UserID int64 `xorm:"INDEX"`
- WechatOpenId string `xorm:"INDEX"`
- Action int
- CreateTime time.Time `xorm:"INDEX created"`
- }
-
- func BindWechatOpenId(userId int64, wechatOpenId string) error {
- sess := x.NewSession()
- defer sess.Close()
- if err := sess.Begin(); err != nil {
- return err
- }
-
- param := &User{WechatOpenId: wechatOpenId}
- n, err := sess.Where("ID = ?", userId).Update(param)
- if err != nil {
- log.Error("update wechat_open_id failed,e=%v", err)
- return err
- }
- if n == 0 {
- log.Error("update wechat_open_id failed,user not exist,userId=%d", userId)
- return nil
- }
-
- logParam := &WechatBindLog{
- UserID: userId,
- WechatOpenId: wechatOpenId,
- Action: int(WECHAT_BIND),
- }
- sess.Insert(logParam)
- return sess.Commit()
- }
-
- func UnbindWechatOpenId(userId int64) error {
- sess := x.NewSession()
- defer sess.Close()
- if err := sess.Begin(); err != nil {
- return err
- }
-
- param := &User{WechatOpenId: ""}
- n, err := x.Where("ID = ?", userId).Update(param)
- if err != nil {
- log.Error("update wechat_open_id failed,e=%v", err)
- return err
- }
- if n == 0 {
- log.Error("update wechat_open_id failed,user not exist,userId=%d", userId)
- return nil
- }
- //todo 是否记录原有微信openId
- logParam := &WechatBindLog{
- UserID: userId,
- Action: int(WECHAT_UNBIND),
- }
- sess.Insert(logParam)
- return sess.Commit()
- }
|