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 2.0 kB

3 years ago
3 years ago
3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 GetUserWechatOpenId(userId int64) string {
  43. param := &User{}
  44. x.Cols("wechat_open_id").Where("ID =?", userId).Get(param)
  45. return param.WechatOpenId
  46. }
  47. func GetUserByWechatOpenId(wechatOpenId string) *User {
  48. user := &User{}
  49. x.Where("wechat_open_id = ?", wechatOpenId).Get(user)
  50. return user
  51. }
  52. func UnbindWechatOpenId(userId int64, oldWechatOpenID string) error {
  53. sess := x.NewSession()
  54. defer sess.Close()
  55. if err := sess.Begin(); err != nil {
  56. return err
  57. }
  58. n, err := x.Table(new(User)).Where("ID = ? AND wechat_open_id =?", userId, oldWechatOpenID).Update(map[string]interface{}{"wechat_open_id": ""})
  59. if err != nil {
  60. log.Error("update wechat_open_id failed,e=%v", err)
  61. return err
  62. }
  63. if n == 0 {
  64. log.Error("update wechat_open_id failed,user not exist,userId=%d", userId)
  65. return nil
  66. }
  67. logParam := &WechatBindLog{
  68. UserID: userId,
  69. WechatOpenId: oldWechatOpenID,
  70. Action: int(WECHAT_UNBIND),
  71. }
  72. sess.Insert(logParam)
  73. return sess.Commit()
  74. }