telegram_bind.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package pub
  2. import (
  3. "app/commons/services"
  4. "github.com/gin-gonic/gin"
  5. )
  6. // TelegramBind 绑定Telegram账户到平台用户
  7. // POST /api/v1/pub/telegram/bind
  8. func (s *Server) TelegramBind(ctx *gin.Context) {
  9. c := s.FromContext(ctx)
  10. if c.UserId() <= 0 {
  11. c.Fail("请先登录平台账户")
  12. return
  13. }
  14. type request struct {
  15. Token string `json:"token" binding:"required"`
  16. }
  17. req := new(request)
  18. if err := c.ShouldBindBodyWithJSON(req); err != nil {
  19. c.Fail("参数错误:请提供绑定码")
  20. return
  21. }
  22. bindService := &services.TgBindService{}
  23. bind, err := bindService.BindTelegramUser(c.UserId(), req.Token)
  24. if err != nil {
  25. c.Fail(err.Error())
  26. return
  27. }
  28. c.Resp(gin.H{
  29. "telegramId": bind.TelegramId,
  30. "telegramUsername": bind.TelegramUsername,
  31. "bindTime": bind.BindTime,
  32. })
  33. }
  34. // TelegramBindStatus 查询Telegram绑定状态
  35. // GET /api/v1/pub/telegram/bind/status?telegramId=xxx
  36. func (s *Server) TelegramBindStatus(ctx *gin.Context) {
  37. c := s.FromContext(ctx)
  38. telegramId := c.QueryInt64("telegramId", 0)
  39. if telegramId <= 0 {
  40. c.Fail("参数错误:telegramId无效")
  41. return
  42. }
  43. bindService := &services.TgBindService{}
  44. bound, userId := bindService.IsUserBound(telegramId)
  45. c.Resp(gin.H{
  46. "bound": bound,
  47. "userId": userId,
  48. })
  49. }