You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

wechat_bind.go 1.7 kB

4 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package models
  2. import (
  3. "code.gitea.io/gitea/modules/log"
  4. "time"
  5. )
  6. type WechatBindAction int
  7. const (
  8. WECHAT_BIND WechatBindAction = iota + 1
  9. WECHAT_UNBIND
  10. )
  11. type WechatBindLog struct {
  12. ID int64 `xorm:"pk autoincr"`
  13. UserID int64 `xorm:"INDEX"`
  14. WechatOpenId string `xorm:"INDEX"`
  15. Action int
  16. CreateTime time.Time `xorm:"INDEX created"`
  17. }
  18. func BindWechatOpenId(userId int64, wechatOpenId string) error {
  19. sess := x.NewSession()
  20. defer sess.Close()
  21. if err := sess.Begin(); err != nil {
  22. return err
  23. }
  24. param := &User{WechatOpenId: wechatOpenId}
  25. n, err := sess.Where("ID = ?", userId).Update(param)
  26. if err != nil {
  27. log.Error("update wechat_open_id failed,e=%v", err)
  28. return err
  29. }
  30. if n == 0 {
  31. log.Error("update wechat_open_id failed,user not exist,userId=%d", userId)
  32. return nil
  33. }
  34. logParam := &WechatBindLog{
  35. UserID: userId,
  36. WechatOpenId: wechatOpenId,
  37. Action: int(WECHAT_BIND),
  38. }
  39. sess.Insert(logParam)
  40. return sess.Commit()
  41. }
  42. func UnbindWechatOpenId(userId int64, oldWechatOpenID string) error {
  43. sess := x.NewSession()
  44. defer sess.Close()
  45. if err := sess.Begin(); err != nil {
  46. return err
  47. }
  48. n, err := x.Table(new(User)).Where("ID = ? AND wechat_open_id =?", userId, oldWechatOpenID).Update(map[string]interface{}{"wechat_open_id": ""})
  49. if err != nil {
  50. log.Error("update wechat_open_id failed,e=%v", err)
  51. return err
  52. }
  53. if n == 0 {
  54. log.Error("update wechat_open_id failed,user not exist,userId=%d", userId)
  55. return nil
  56. }
  57. logParam := &WechatBindLog{
  58. UserID: userId,
  59. WechatOpenId: oldWechatOpenID,
  60. Action: int(WECHAT_UNBIND),
  61. }
  62. sess.Insert(logParam)
  63. return sess.Commit()
  64. }