(c *gin.Context)
| 15 | ) |
| 16 | |
| 17 | func TelegramBind(c *gin.Context) { |
| 18 | if !common.TelegramOAuthEnabled { |
| 19 | c.JSON(200, gin.H{ |
| 20 | "message": "管理员未开启通过 Telegram 登录以及注册", |
| 21 | "success": false, |
| 22 | }) |
| 23 | return |
| 24 | } |
| 25 | params := c.Request.URL.Query() |
| 26 | if !checkTelegramAuthorization(params, common.TelegramBotToken) { |
| 27 | c.JSON(200, gin.H{ |
| 28 | "message": "无效的请求", |
| 29 | "success": false, |
| 30 | }) |
| 31 | return |
| 32 | } |
| 33 | telegramId := params["id"][0] |
| 34 | if model.IsTelegramIdAlreadyTaken(telegramId) { |
| 35 | c.JSON(200, gin.H{ |
| 36 | "message": "该 Telegram 账户已被绑定", |
| 37 | "success": false, |
| 38 | }) |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | session := sessions.Default(c) |
| 43 | id := session.Get("id") |
| 44 | user := model.User{Id: id.(int)} |
| 45 | if err := user.FillUserById(); err != nil { |
| 46 | c.JSON(200, gin.H{ |
| 47 | "message": err.Error(), |
| 48 | "success": false, |
| 49 | }) |
| 50 | return |
| 51 | } |
| 52 | if user.Id == 0 { |
| 53 | c.JSON(http.StatusOK, gin.H{ |
| 54 | "success": false, |
| 55 | "message": "用户已注销", |
| 56 | }) |
| 57 | return |
| 58 | } |
| 59 | user.TelegramId = telegramId |
| 60 | if err := user.Update(false); err != nil { |
| 61 | c.JSON(200, gin.H{ |
| 62 | "message": err.Error(), |
| 63 | "success": false, |
| 64 | }) |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | c.Redirect(302, "/setting") |
| 69 | } |
| 70 | |
| 71 | func TelegramLogin(c *gin.Context) { |
| 72 | if !common.TelegramOAuthEnabled { |
nothing calls this directly
no test coverage detected