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.

bind.go 2.3 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package wechat
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/log"
  5. "fmt"
  6. )
  7. type QRCode4BindCache struct {
  8. UserId int64
  9. Status int
  10. }
  11. const (
  12. BIND_STATUS_UNBIND = 0
  13. BIND_STATUS_SCANNED = 1
  14. BIND_STATUS_BOUND = 2
  15. BIND_STATUS_EXPIRED = 9
  16. )
  17. const (
  18. BIND_REPLY_SUCCESS = "扫码成功,您可以使用OpenI启智社区算力环境。"
  19. BIND_REPLY_WECHAT_ACCOUNT_USED = "认证失败,您的微信号已绑定其他启智账号"
  20. BIND_REPLY_OPENI_ACCOUNT_USED = "认证失败,您待认证的启智账号已绑定其他微信号"
  21. BIND_REPLY_FAILED_DEFAULT = "微信认证失败"
  22. )
  23. type WechatBindError struct {
  24. Reply string
  25. }
  26. func NewWechatBindError(reply string) WechatBindError {
  27. return WechatBindError{Reply: reply}
  28. }
  29. func (err WechatBindError) Error() string {
  30. return fmt.Sprint("wechat bind error,reply=%s", err.Reply)
  31. }
  32. func BindWechat(userId int64, wechatOpenId string) error {
  33. if !IsWechatAccountUsed(userId, wechatOpenId) {
  34. log.Error("bind wechat failed, because user use wrong wechat account to bind,userId=%d wechatOpenId=%s", userId, wechatOpenId)
  35. return NewWechatBindError(BIND_REPLY_WECHAT_ACCOUNT_USED)
  36. }
  37. if !IsUserAvailableForWechatBind(userId, wechatOpenId) {
  38. log.Error("openI account has been used,userId=%d wechatOpenId=%s", userId, wechatOpenId)
  39. return NewWechatBindError(BIND_REPLY_OPENI_ACCOUNT_USED)
  40. }
  41. return models.BindWechatOpenId(userId, wechatOpenId)
  42. }
  43. func UnbindWechat(userId int64, oldWechatOpenId string) error {
  44. return models.UnbindWechatOpenId(userId, oldWechatOpenId)
  45. }
  46. //IsUserAvailableForWechatBind if user has bound wechat and the bound openId is not the given wechatOpenId,return false
  47. //otherwise,return true
  48. func IsUserAvailableForWechatBind(userId int64, wechatOpenId string) bool {
  49. currentOpenId := models.GetUserWechatOpenId(userId)
  50. return currentOpenId == "" || currentOpenId == wechatOpenId
  51. }
  52. //IsWechatAccountUsed if wechat account used by another account,return false
  53. //if wechat account not used or used by the given user,return true
  54. func IsWechatAccountUsed(userId int64, wechatOpenId string) bool {
  55. user := models.GetUserByWechatOpenId(wechatOpenId)
  56. if user != nil && user.WechatOpenId != "" && user.ID != userId {
  57. return false
  58. }
  59. return true
  60. }