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 3.7 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
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package authentication
  2. import (
  3. "code.gitea.io/gitea/modules/auth/wechat"
  4. "code.gitea.io/gitea/modules/base"
  5. "code.gitea.io/gitea/modules/context"
  6. "code.gitea.io/gitea/modules/log"
  7. "code.gitea.io/gitea/modules/redis/redis_client"
  8. "code.gitea.io/gitea/modules/redis/redis_key"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/routers/response"
  11. "encoding/json"
  12. "errors"
  13. gouuid "github.com/satori/go.uuid"
  14. "strconv"
  15. "time"
  16. )
  17. const tplBindPage base.TplName = "repo/wx_autorize"
  18. type QRCodeResponse struct {
  19. Url string
  20. Ticket string
  21. SceneStr string
  22. ExpireSeconds int
  23. }
  24. // GetQRCode4Bind get QR code for wechat binding
  25. func GetQRCode4Bind(ctx *context.Context) {
  26. userId := ctx.User.ID
  27. r, err := createQRCode4Bind(userId)
  28. if err != nil {
  29. log.Error("GetQRCode4Bind failed,error=%v", err)
  30. ctx.JSON(200, map[string]interface{}{
  31. "code": "9999",
  32. "msg": "Get QR code failed",
  33. })
  34. return
  35. }
  36. ctx.JSON(200, map[string]interface{}{
  37. "code": "00",
  38. "msg": "success",
  39. "data": r,
  40. })
  41. }
  42. // GetBindStatus the web page will poll the service to get bind status
  43. func GetBindStatus(ctx *context.Context) {
  44. sceneStr := ctx.Query("sceneStr")
  45. val, _ := redis_client.Get(redis_key.WechatBindingUserIdKey(sceneStr))
  46. if val == "" {
  47. ctx.JSON(200, map[string]interface{}{
  48. "code": "00",
  49. "msg": "QR code expired",
  50. "data": map[string]interface{}{
  51. "status": wechat.BIND_STATUS_EXPIRED,
  52. },
  53. })
  54. return
  55. }
  56. qrCache := new(wechat.QRCode4BindCache)
  57. json.Unmarshal([]byte(val), qrCache)
  58. ctx.JSON(200, map[string]interface{}{
  59. "code": "00",
  60. "msg": "success",
  61. "data": map[string]interface{}{
  62. "status": qrCache.Status,
  63. },
  64. })
  65. }
  66. // UnbindWechat
  67. func UnbindWechat(ctx *context.Context) {
  68. if ctx.User.WechatOpenId != "" {
  69. wechat.UnbindWechat(ctx.User.ID, ctx.User.WechatOpenId)
  70. }
  71. ctx.JSON(200, map[string]interface{}{
  72. "code": "00",
  73. "msg": "success",
  74. })
  75. }
  76. // GetBindPage
  77. func GetBindPage(ctx *context.Context) {
  78. userId := ctx.User.ID
  79. r, _ := createQRCode4Bind(userId)
  80. if r != nil {
  81. ctx.Data["qrcode"] = r
  82. }
  83. redirectUrl := ctx.Query("redirect_to")
  84. if redirectUrl != "" {
  85. ctx.SetCookie("redirect_to", setting.AppSubURL+redirectUrl, 0, setting.AppSubURL)
  86. }
  87. ctx.HTML(200, tplBindPage)
  88. }
  89. func createQRCode4Bind(userId int64) (*QRCodeResponse, error) {
  90. log.Info("start to create qr-code for binding.userId=%d", userId)
  91. sceneStr := gouuid.NewV4().String()
  92. r := wechat.GetWechatQRCode4Bind(sceneStr)
  93. if r == nil {
  94. return nil, errors.New("createQRCode4Bind failed")
  95. }
  96. jsonStr, _ := json.Marshal(&wechat.QRCode4BindCache{
  97. UserId: userId,
  98. Status: wechat.BIND_STATUS_UNBIND,
  99. })
  100. isOk, err := redis_client.Setex(redis_key.WechatBindingUserIdKey(sceneStr), string(jsonStr), time.Duration(setting.WechatQRCodeExpireSeconds)*time.Second)
  101. if err != nil {
  102. log.Error("createQRCode4Bind failed.e=%+v", err)
  103. return nil, err
  104. }
  105. if !isOk {
  106. log.Error("createQRCode4Bind failed.redis reply is not ok")
  107. return nil, errors.New("reply is not ok when set WechatBindingUserIdKey")
  108. }
  109. result := &QRCodeResponse{
  110. Url: r.Url,
  111. Ticket: r.Ticket,
  112. SceneStr: sceneStr,
  113. ExpireSeconds: setting.WechatQRCodeExpireSeconds,
  114. }
  115. return result, nil
  116. }
  117. // GetMaterial
  118. func GetMaterial(ctx *context.Context) {
  119. mType := ctx.Query("type")
  120. offsetStr := ctx.Query("offset")
  121. countStr := ctx.Query("count")
  122. var offset, count int
  123. if offsetStr == "" {
  124. offset = 0
  125. } else {
  126. offset, _ = strconv.Atoi(offsetStr)
  127. }
  128. if countStr == "" {
  129. count = 20
  130. } else {
  131. count, _ = strconv.Atoi(countStr)
  132. }
  133. r := wechat.GetWechatMaterial(mType, offset, count)
  134. ctx.JSON(200, response.SuccessWithData(r))
  135. }