forked from assimon/captcha-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
298 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package model | ||
|
||
import ( | ||
"github.com/golang-module/carbon/v2" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type BaseModel struct { | ||
ID int64 `gorm:"column:id;primary_key" json:"id"` | ||
CreatedAt carbon.Timestamp `gorm:"column:created_at" json:"created_at"` | ||
UpdatedAt carbon.Timestamp `gorm:"column:updated_at" json:"updated_at"` | ||
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package model | ||
|
||
import "github.com/golang-module/carbon/v2" | ||
|
||
const ( | ||
CaptchaStatusPending = -1 | ||
CaptchaStatusSuccess = 1 | ||
CaptchaStatusTimeout = 2 | ||
) | ||
|
||
type UserCaptchaRecord struct { | ||
BaseModel | ||
CaptchaId string `gorm:"column:captcha_id;unique_index:captcha_id_index" json:"captcha_id"` //验证id | ||
TelegramChatName string `gorm:"column:telegram_chat_name" json:"telegram_chat_name"` | ||
TelegramUserLastName string `gorm:"column:telegram_user_last_name" json:"telegram_user_last_name"` | ||
TelegramUserFirstName string `gorm:"column:telegram_user_first_name" json:"telegram_user_first_name"` | ||
TelegramUserId int64 `gorm:"column:telegram_user_id" json:"telegram_user_id"` | ||
TelegramChatId int64 `gorm:"column:telegram_chat_id" json:"telegram_chat_id"` | ||
CaptchaMessageId int `gorm:"column:captcha_message_id" json:"captcha_message_id"` // 群-待验证消息id | ||
CaptchaStatus int `gorm:"column:captcha_status" json:"captcha_status"` // 验证状态 -1待验证 1已验证 2已超时 | ||
CaptchaTimeoutMessageId int `gorm:"column:captcha_timeout_message_id" json:"captcha_timeout_message_id"` // 群-验证超时消息id | ||
CaptchaTimeoutTime carbon.Timestamp `gorm:"column:captcha_timeout_time" json:"captcha_timeout_time"` // 验证超时时间 | ||
CaptchaSuccessMessageId int `gorm:"column:captcha_success_message_id" json:"captcha_success_message_id"` // 群-验证成功的消息id | ||
CaptchaSuccessTime carbon.Timestamp `gorm:"column:captcha_success_time" json:"captcha_success_time"` // 验证成功时间 | ||
|
||
CaptchaCodeMessageId int `gorm:"column:captcha_code_message_id" json:"captcha_code_message_id"` // 验证码消息id | ||
CaptchaCode string `gorm:"column:captcha_code" json:"captcha_code"` // 验证码内容 | ||
|
||
} | ||
|
||
func (UserCaptchaRecord) TableName() string { | ||
return "user_captcha_record" | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/assimon/captcha-bot/model" | ||
"github.com/assimon/captcha-bot/util/config" | ||
"github.com/assimon/captcha-bot/util/orm" | ||
"github.com/golang-module/carbon/v2" | ||
) | ||
|
||
// CreateCaptchaRecord 创建验证记录 | ||
func CreateCaptchaRecord(record *model.UserCaptchaRecord) error { | ||
err := orm.Gdb.Model(record).Create(record).Error | ||
return err | ||
} | ||
|
||
// GetRecordByCaptchaId 通过载荷唯一标识获取记录 | ||
func GetRecordByCaptchaId(cId string) (record *model.UserCaptchaRecord, err error) { | ||
err = orm.Gdb.Model(record).Where("captcha_id = ?", cId).Find(record).Error | ||
return | ||
} | ||
|
||
// TimeoutRecordByCaptchaId 设置某条验证消息已经超时 | ||
func TimeoutRecordByCaptchaId(cId string) error { | ||
err := orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_id = ?", cId). | ||
UpdateColumns(map[string]interface{}{ | ||
"captcha_status": model.CaptchaStatusTimeout, | ||
"captcha_timeout_time": carbon.Now().Timestamp(), | ||
}).Error | ||
return err | ||
} | ||
|
||
// SuccessRecordByCaptchaId 设置某条消息已经验证成功 | ||
func SuccessRecordByCaptchaId(cId string) error { | ||
err := orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_id = ?", cId). | ||
UpdateColumns(map[string]interface{}{ | ||
"captcha_status": model.CaptchaStatusSuccess, | ||
"captcha_success_time": carbon.Now().Timestamp(), | ||
}).Error | ||
return err | ||
} | ||
|
||
// SetCaptchaCodeByCaptchaId 设置或刷新一个验证消息的code | ||
func SetCaptchaCodeByCaptchaId(cId, code string) error { | ||
err := orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_id = ?", cId). | ||
Update("captcha_code", code).Error | ||
return err | ||
} | ||
|
||
// SetCaptchaCodeMessageIdByCaptchaId 设置私聊验证消息id | ||
func SetCaptchaCodeMessageIdByCaptchaId(cId string, msgId int) error { | ||
err := orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_id = ?", cId). | ||
Update("captcha_code_message_id", msgId).Error | ||
return err | ||
} | ||
|
||
// GetTimeoutCaptchaRecords 获取已经超时的待验证记录 | ||
func GetTimeoutCaptchaRecords() (records []model.UserCaptchaRecord, err error) { | ||
now := carbon.Now().SubSeconds(config.SystemC.CaptchaTimeout) | ||
err = orm.Gdb.Model(&model.UserCaptchaRecord{}).Where("captcha_status = ?", model.CaptchaStatusPending). | ||
Where("created_at > ?", now).Find(&records).Error | ||
return | ||
} |
Oops, something went wrong.