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.go 2.4 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package authentication
  2. import (
  3. "code.gitea.io/gitea/modules/auth/wechat"
  4. "code.gitea.io/gitea/modules/context"
  5. "code.gitea.io/gitea/modules/log"
  6. "code.gitea.io/gitea/modules/redis/redis_client"
  7. "code.gitea.io/gitea/modules/redis/redis_key"
  8. "code.gitea.io/gitea/modules/setting"
  9. "encoding/json"
  10. "errors"
  11. gouuid "github.com/satori/go.uuid"
  12. "time"
  13. )
  14. type QRCodeResponse struct {
  15. Url string
  16. Ticket string
  17. SceneStr string
  18. ExpireSeconds int
  19. }
  20. // GetQRCode4Bind get QR code for wechat binding
  21. func GetQRCode4Bind(ctx *context.Context) {
  22. userId := ctx.User.ID
  23. r, err := createQRCode4Bind(userId)
  24. if err != nil {
  25. ctx.JSON(200, map[string]interface{}{
  26. "code": "9999",
  27. "msg": "Get QR code failed",
  28. })
  29. return
  30. }
  31. ctx.JSON(200, map[string]interface{}{
  32. "code": "00",
  33. "msg": "success",
  34. "data": r,
  35. })
  36. }
  37. // GetQRCode4Bind get QR code for wechat binding
  38. func GetBindStatus(ctx *context.Context) {
  39. sceneStr := ctx.Query("sceneStr")
  40. val, _ := redis_client.Get(redis_key.WechatBindingUserIdKey(sceneStr))
  41. if val == "" {
  42. ctx.JSON(200, map[string]interface{}{
  43. "code": "00",
  44. "msg": "QR code expired",
  45. "data": map[string]interface{}{
  46. "status": wechat.BIND_STATUS_EXPIRED,
  47. },
  48. })
  49. return
  50. }
  51. qrCache := new(wechat.QRCode4BindCache)
  52. json.Unmarshal([]byte(val), qrCache)
  53. ctx.JSON(200, map[string]interface{}{
  54. "code": "00",
  55. "msg": "success",
  56. "data": map[string]interface{}{
  57. "status": qrCache.Status,
  58. },
  59. })
  60. }
  61. func createQRCode4Bind(userId int64) (*QRCodeResponse, error) {
  62. sceneStr := gouuid.NewV4().String()
  63. r := wechat.GetWechatQRCode4Bind(sceneStr)
  64. if r == nil {
  65. return nil, errors.New("createQRCode4Bind failed")
  66. }
  67. jsonStr, _ := json.Marshal(&wechat.QRCode4BindCache{
  68. UserId: userId,
  69. Status: wechat.BIND_STATUS_UNBIND,
  70. })
  71. isOk, err := redis_client.Setex(redis_key.WechatBindingUserIdKey(sceneStr), string(jsonStr), time.Duration(setting.WechatQRCodeExpireSeconds)*time.Second)
  72. if err != nil {
  73. log.Error("createQRCode4Bind failed.e=%v", err)
  74. return nil, err
  75. }
  76. if !isOk {
  77. log.Error("createQRCode4Bind failed.redis reply is not ok")
  78. return nil, errors.New("reply is not ok when set WechatBindingUserIdKey")
  79. }
  80. result := &QRCodeResponse{
  81. Url: r.Url,
  82. Ticket: r.Ticket,
  83. SceneStr: sceneStr,
  84. ExpireSeconds: setting.WechatQRCodeExpireSeconds,
  85. }
  86. return result, nil
  87. }