feat(企微签到):逻辑:员工使用企微扫码后直接跳出签到页面,在签到页面对该课程进行点击签到!
deploy-hxtesting / build-and-deploy (push) Successful in 1m14s
deploy-hxtesting / build-and-deploy (push) Successful in 1m14s
规则:1.签到要校验是否为培训当天签到,如果不是当天扫码签到则进行友好提示;2. 签到需提供员工工号及随机时间戳(超过签到二维码有效期后需重新扫码签到);3.签到二维码每5分钟自动更新一次(更新后之前扫码签到自动作废需重新扫码); 补充说明:1.签到页面需展示该签到课程相关信息;2.展示签到倒计时还有多久过期,过期后需重新扫码进行优化提示;3.签到后提示签到成功!自动登录进入平台系统并进入该课程详情页面
This commit is contained in:
1 parent
9950a28346
commit
f43fbef5db
24 files changed
+1336
-67
No files matched your search
@@ -711,6 +711,8 @@ func (s *trainingService) AttachQuestionBank(ctx context.Context, tenantID uint6
|
||||
|
||||
// CreateSignCode generates (or refreshes) the session's sign-in code with an
|
||||
// optional geo-fence; default expiry = start + 4h or now + 12h.
|
||||
// 2026-11 轮换需求:每轮 token 有效期 = min(now + rotation_sec, 场次总有效期),
|
||||
// 讲师页轮询 GetSignCode 惰性续轮。
|
||||
func (s *trainingService) CreateSignCode(ctx context.Context, tenantID uint64, sessionID string, lat, lng float64, radius int, operatorID string) (*types.TrainingSignCode, error) {
|
||||
session, err := s.repo.GetSession(ctx, tenantID, sessionID)
|
||||
if err != nil {
|
||||
@@ -719,12 +721,19 @@ func (s *trainingService) CreateSignCode(ctx context.Context, tenantID uint64, s
|
||||
if radius <= 0 {
|
||||
radius = int(s.ruleFloat(ctx, tenantID, types.RuleKeySignPolicy, "geo_radius_m", 300))
|
||||
}
|
||||
expire := time.Now().Add(12 * time.Hour)
|
||||
now := time.Now()
|
||||
expire := now.Add(12 * time.Hour)
|
||||
if session.StartAt != nil {
|
||||
if candidate := session.StartAt.Add(4 * time.Hour); candidate.After(time.Now()) {
|
||||
if candidate := session.StartAt.Add(4 * time.Hour); candidate.After(now) {
|
||||
expire = candidate
|
||||
}
|
||||
}
|
||||
rotation := time.Duration(s.ruleFloat(ctx, tenantID, types.RuleKeySignPolicy, "rotation_sec", 300)) * time.Second
|
||||
if rotation > 0 {
|
||||
if capped := now.Add(rotation); capped.Before(expire) {
|
||||
expire = capped
|
||||
}
|
||||
}
|
||||
code := &types.TrainingSignCode{
|
||||
ID: trainingNewID(),
|
||||
TenantID: tenantID,
|
||||
@@ -747,22 +756,86 @@ func trainingNewToken() string {
|
||||
return strings.ReplaceAll(trainingNewID(), "-", "")[:32]
|
||||
}
|
||||
|
||||
// GetSignCode returns the session's active sign-in code.
|
||||
// GetSignCode returns the session's active sign-in code. 超过轮换窗口
|
||||
// (rotation_sec,默认 5 分钟)时惰性轮换:旧 token 挪入 prev(宽限期内仍可
|
||||
// 签到),新码即时生效 —— 讲师页每 10s 轮询本接口即可驱动自动更新。
|
||||
func (s *trainingService) GetSignCode(ctx context.Context, tenantID uint64, sessionID string) (*types.TrainingSignCode, error) {
|
||||
code, err := s.repo.GetSignCodeBySession(ctx, tenantID, sessionID)
|
||||
if err != nil {
|
||||
return nil, errors.NewNotFoundError("尚未生成签到码")
|
||||
}
|
||||
return code, nil
|
||||
rotation := time.Duration(s.ruleFloat(ctx, tenantID, types.RuleKeySignPolicy, "rotation_sec", 300)) * time.Second
|
||||
if rotation <= 0 || time.Now().Before(code.UpdatedAt.Add(rotation)) {
|
||||
return code, nil
|
||||
}
|
||||
return s.rotateSignCode(ctx, tenantID, code)
|
||||
}
|
||||
|
||||
// rotateSignCode 轮换场次签到码:生成新 token,旧 token/有效期挪入 prev 供
|
||||
// 宽限签到(rotation_grace_sec,默认 30s,0 = 严格作废);upsert 覆盖唯一行后
|
||||
// 旧 token 即刻退出当前码位(GetSignCodeByToken 不再命中,自然作废)。
|
||||
// 新码有效期 = min(now + rotation_sec, 场次总有效期):后者沿用开课 +4h /
|
||||
// 兜底 +12h 的既有口径,超期后不再续轮,交由现有过期校验拦截。
|
||||
func (s *trainingService) rotateSignCode(ctx context.Context, tenantID uint64, code *types.TrainingSignCode) (*types.TrainingSignCode, error) {
|
||||
session, err := s.repo.GetSession(ctx, tenantID, code.SessionID)
|
||||
if err != nil {
|
||||
return nil, errors.NewNotFoundError("课程场次不存在")
|
||||
}
|
||||
now := time.Now()
|
||||
rotation := time.Duration(s.ruleFloat(ctx, tenantID, types.RuleKeySignPolicy, "rotation_sec", 300)) * time.Second
|
||||
expire := now.Add(rotation)
|
||||
overall := now.Add(12 * time.Hour)
|
||||
if session.StartAt != nil {
|
||||
if candidate := session.StartAt.Add(4 * time.Hour); candidate.After(now) {
|
||||
overall = candidate
|
||||
}
|
||||
}
|
||||
if expire.After(overall) {
|
||||
expire = overall
|
||||
}
|
||||
next := &types.TrainingSignCode{
|
||||
ID: trainingNewID(),
|
||||
TenantID: tenantID,
|
||||
SessionID: code.SessionID,
|
||||
Token: trainingNewToken(),
|
||||
ExpireAt: &expire,
|
||||
PrevToken: code.Token,
|
||||
PrevExpireAt: code.ExpireAt,
|
||||
GeoLat: code.GeoLat,
|
||||
GeoLng: code.GeoLng,
|
||||
GeoRadius: code.GeoRadius,
|
||||
CreatedBy: code.CreatedBy,
|
||||
}
|
||||
if err := s.repo.UpsertSignCode(ctx, next); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return next, nil
|
||||
}
|
||||
|
||||
// Checkin performs the scan sign-in with rule-based anti-fraud checks:
|
||||
// token/expiry, roster membership, duplicate scan, late arrival, geo fence.
|
||||
// token/expiry, same-day, roster membership, employee-no match, nonce replay,
|
||||
// duplicate scan, late arrival, geo fence.
|
||||
// 一期为确定性规则引擎,不调 LLM;规则参数来自 sign_policy 配置。
|
||||
func (s *trainingService) Checkin(ctx context.Context, tenantID uint64, userID, token string, lat, lng float64, channel string) (*types.TrainingSignRecord, error) {
|
||||
// 2026-11 企微扫码需求增强:
|
||||
// - 培训当天校验:非当天扫码签到直接友好提示(提前扫/过期补扫分文案);
|
||||
// - 工号严格校验(in.EmployeeNo 非空时):须与登录账号档案工号一致,且必须在
|
||||
// 该场次「参加人员」名单中 —— 名单外直接拒绝(不再仅标记待复核);
|
||||
// - nonce 防重放 + client_ts 时钟偏差校验;
|
||||
// - 轮换宽限:token 未命中当前码时接受 prev_token 在宽限期内签到。
|
||||
// 既有登录态签到(不带 employee_no/nonce)行为不变:名单外仍标记 not_in_roster
|
||||
// 进待复核,不硬拒。
|
||||
func (s *trainingService) Checkin(ctx context.Context, tenantID uint64, userID string, in *types.CheckinInput) (*types.TrainingSignRecord, error) {
|
||||
if in == nil || in.Token == "" {
|
||||
return nil, errors.NewBadRequestError("签到码无效")
|
||||
}
|
||||
token, lat, lng := in.Token, in.Lat, in.Lng
|
||||
code, err := s.repo.GetSignCodeByToken(ctx, tenantID, token)
|
||||
if err != nil {
|
||||
return nil, errors.NewBadRequestError("签到码无效")
|
||||
// 轮换宽限:未命中当前码时尝试前码(prev_token),宽限期内放行。
|
||||
code, err = s.resolveCheckinPrevToken(ctx, tenantID, token)
|
||||
if err != nil {
|
||||
return nil, errors.NewBadRequestError("签到码无效或已更新,请重新扫码")
|
||||
}
|
||||
}
|
||||
if code.ExpireAt != nil && time.Now().After(*code.ExpireAt) {
|
||||
return nil, errors.NewBadRequestError("签到码已过期")
|
||||
@@ -779,7 +852,7 @@ func (s *trainingService) Checkin(ctx context.Context, tenantID uint64, userID,
|
||||
return nil, errors.NewForbiddenError("未建立培训档案,无法签到")
|
||||
}
|
||||
|
||||
// 名单校验(名单外人员自动标记)。
|
||||
// 名单校验(名单外人员自动标记;严格模式下直接拒绝,见下)。
|
||||
roster, err := s.repo.ListEnrollments(ctx, tenantID, session.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -793,6 +866,47 @@ func (s *trainingService) Checkin(ctx context.Context, tenantID uint64, userID,
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
// 严格扫码链路(employee_no 非空,即 2026-11 新签到页):当天校验/工号+名单
|
||||
// 硬拒/nonce 防重放/时钟偏差仅作用于此链路。既有路径(登录态直签、代录补签)
|
||||
// 行为不变 —— 尤其代录补签常发生在非当天,当天校验不得拦截。
|
||||
strictScan := strings.TrimSpace(in.EmployeeNo) != ""
|
||||
|
||||
// 培训当天校验(2026-11 需求规则 1):非当天扫码签到,分提前/滞后友好提示。
|
||||
// 时区固定北京时间,与前端 fmtBeijingTime 口径一致。
|
||||
if strictScan && s.ruleFloat(ctx, tenantID, types.RuleKeySignPolicy, "same_day_check", 1) > 0 &&
|
||||
session.StartAt != nil && !sameBeijingDay(now, *session.StartAt) {
|
||||
if session.StartAt.After(now) {
|
||||
return nil, errors.NewBadRequestError(
|
||||
"培训尚未开始:该课程 " + fmtBeijingCST(session.StartAt) + " 开课,培训当天再来签到")
|
||||
}
|
||||
return nil, errors.NewBadRequestError("本课程签到已结束,无法补签")
|
||||
}
|
||||
|
||||
// 工号严格校验(2026-11 需求规则 2 + 补充规则):工号须与档案一致,且必须在
|
||||
// 「参加人员」名单中,名单外直接拒绝。
|
||||
if strictScan {
|
||||
if !strings.EqualFold(strings.TrimSpace(member.EmployeeNo), strings.TrimSpace(in.EmployeeNo)) {
|
||||
return nil, errors.NewForbiddenError("录入工号与当前账号的档案工号不一致,请确认后重试")
|
||||
}
|
||||
if !inRoster {
|
||||
return nil, errors.NewForbiddenError("您不在该课程的「参加人员」名单中,请联系讲师或人事确认")
|
||||
}
|
||||
}
|
||||
|
||||
// nonce 防重放 + 时钟偏差(仅扫码链路携带;同 nonce 二次提交直接拒绝)。
|
||||
if strictScan && in.Nonce != "" {
|
||||
if dup, derr := s.repo.GetSignRecordByNonce(ctx, tenantID, in.Nonce); derr == nil && dup != nil {
|
||||
return nil, errors.NewBadRequestError("签到请求无效,请刷新页面后重试")
|
||||
}
|
||||
}
|
||||
skewSec := s.ruleFloat(ctx, tenantID, types.RuleKeySignPolicy, "clock_skew_sec", 60)
|
||||
if strictScan && in.ClientTS > 0 && skewSec > 0 {
|
||||
if diff := now.UnixMilli() - in.ClientTS; diff > int64(skewSec)*1000 || diff < -int64(skewSec)*1000 {
|
||||
return nil, errors.NewBadRequestError("签到请求时间异常,请校准手机时间后重试")
|
||||
}
|
||||
}
|
||||
|
||||
flags := []string{}
|
||||
status := types.TrainingSignNormal
|
||||
|
||||
@@ -813,7 +927,7 @@ func (s *trainingService) Checkin(ctx context.Context, tenantID uint64, userID,
|
||||
}
|
||||
}
|
||||
|
||||
// 名单外人员标记。
|
||||
// 名单外人员标记(工号严格链路已在上方硬拒,此处仅既有登录态路径可达)。
|
||||
if !inRoster {
|
||||
flags = append(flags, "not_in_roster")
|
||||
status = types.TrainingSignAbnormal
|
||||
@@ -824,6 +938,7 @@ func (s *trainingService) Checkin(ctx context.Context, tenantID uint64, userID,
|
||||
return nil, errors.NewBadRequestError("您已签到,请勿重复扫码")
|
||||
}
|
||||
|
||||
channel := in.Channel
|
||||
if channel == "" {
|
||||
channel = "wecom_scan"
|
||||
}
|
||||
@@ -839,6 +954,7 @@ func (s *trainingService) Checkin(ctx context.Context, tenantID uint64, userID,
|
||||
Lng: lng,
|
||||
DistanceM: distance,
|
||||
Status: status,
|
||||
Nonce: strings.TrimSpace(in.Nonce),
|
||||
AbnormalFlags: flagsJSON,
|
||||
}
|
||||
if err := s.repo.UpsertSignRecord(ctx, record); err != nil {
|
||||
@@ -852,6 +968,28 @@ func (s *trainingService) Checkin(ctx context.Context, tenantID uint64, userID,
|
||||
return record, nil
|
||||
}
|
||||
|
||||
// resolveCheckinPrevToken 轮换宽限:当前码未命中时按前码定位,宽限期内放行
|
||||
// (rotation_grace_sec,默认 30s,0 = 严格作废)。跨租户 token 一律拒绝。
|
||||
func (s *trainingService) resolveCheckinPrevToken(ctx context.Context, tenantID uint64, token string) (*types.TrainingSignCode, error) {
|
||||
code, err := s.repo.GetSignCodeByTokenGlobal(ctx, token)
|
||||
if err != nil || code == nil || code.TenantID != tenantID || code.Token == token {
|
||||
return nil, errors.NewBadRequestError("签到码无效")
|
||||
}
|
||||
grace := time.Duration(s.ruleFloat(ctx, tenantID, types.RuleKeySignPolicy, "rotation_grace_sec", 30)) * time.Second
|
||||
if code.PrevExpireAt == nil || time.Now().After(code.PrevExpireAt.Add(grace)) {
|
||||
return nil, errors.NewBadRequestError("签到码已更新,请重新扫码")
|
||||
}
|
||||
return code, nil
|
||||
}
|
||||
|
||||
// fmtBeijingCST 时间格式化为北京时间文案(当天性提示用)。
|
||||
func fmtBeijingCST(t *time.Time) string {
|
||||
if t == nil {
|
||||
return ""
|
||||
}
|
||||
return t.In(time.FixedZone("CST", 8*3600)).Format("2006-01-02 15:04")
|
||||
}
|
||||
|
||||
// haversineMeters computes the great-circle distance in meters.
|
||||
func haversineMeters(lat1, lng1, lat2, lng2 float64) float64 {
|
||||
const earthRadius = 6371000.0
|
||||
|
||||
Reference in new issue
Block a user