Files
ai_academy/internal/im/wecompush/client.go
T
runny f43fbef5db
deploy-hxtesting / build-and-deploy (push) Successful in 1m14s
feat(企微签到):逻辑:员工使用企微扫码后直接跳出签到页面,在签到页面对该课程进行点击签到!
规则:1.签到要校验是否为培训当天签到,如果不是当天扫码签到则进行友好提示;2. 签到需提供员工工号及随机时间戳(超过签到二维码有效期后需重新扫码签到);3.签到二维码每5分钟自动更新一次(更新后之前扫码签到自动作废需重新扫码);
补充说明:1.签到页面需展示该签到课程相关信息;2.展示签到倒计时还有多久过期,过期后需重新扫码进行优化提示;3.签到后提示签到成功!自动登录进入平台系统并进入该课程详情页面
2026-11-25 11:02:48 +08:00

104 lines
3.6 KiB
Go

// Package wecompush resolves per-tenant WeCom proactive-push clients from
// the tenant's IM channel credentials.
//
// It exists so the application service layer never imports internal/im
// (directly or via the wecom adapter): internal/im's in-package tests
// import internal/application/service, so a service → im edge creates an
// import cycle in the im test binary. The container injects
// TenantClient into service.NewTrainingScheduler instead.
package wecompush
import (
"context"
"errors"
"fmt"
"academy/internal/im"
"academy/internal/im/wecom"
"academy/internal/types"
"gorm.io/gorm"
)
// TenantClient builds a wecom.ProactiveClient from the tenant's enabled
// webhook-mode wecom IM channel. Only webhook (self-built app) channels can
// call message/send; websocket smart-bot credentials cannot and are
// ignored.
func TenantClient(ctx context.Context, db *gorm.DB, tenantID uint64) (*wecom.ProactiveClient, error) {
var channel im.IMChannel
err := db.WithContext(ctx).
Where("tenant_id = ? AND platform = ? AND mode = ? AND enabled = ? AND deleted_at IS NULL",
tenantID, im.PlatformWeCom, "webhook", true).
Order("created_at").
First(&channel).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.New("未配置企微自建应用渠道(IM 集成中创建 webhook 模式的企业微信渠道)")
}
return nil, fmt.Errorf("load wecom channel: %w", err)
}
creds, err := im.ParseCredentials(channel.Credentials)
if err != nil {
return nil, fmt.Errorf("parse wecom credentials: %w", err)
}
corpAgentID := 0
if v, ok := creds["corp_agent_id"]; ok {
switch val := v.(type) {
case float64:
corpAgentID = int(val)
case int:
corpAgentID = val
}
}
return wecom.NewProactiveClient(
im.GetString(creds, "corp_id"),
im.GetString(creds, "agent_secret"),
corpAgentID,
im.GetString(creds, "api_base_url"),
)
}
// TenantWecomUserIDs maps platform user IDs to WeCom userids via
// org_employees.wecom_userid; users without a binding are absent from the
// result.
func TenantWecomUserIDs(ctx context.Context, db *gorm.DB, tenantID uint64, userIDs []string) (map[string]string, error) {
out := map[string]string{}
if len(userIDs) == 0 {
return out, nil
}
var employees []types.OrgEmployee
if err := db.WithContext(ctx).
Select("user_id", "wecom_userid").
Where("tenant_id = ? AND user_id IN ? AND deleted_at IS NULL", tenantID, userIDs).
Find(&employees).Error; err != nil {
return nil, fmt.Errorf("load wecom userids: %w", err)
}
for _, e := range employees {
if e.WecomUserid != "" {
out[e.UserID] = e.WecomUserid
}
}
return out, nil
}
// Backend adapts this package to the service layer's WecomPushBackend
// interface (structural typing: *wecom.ProactiveClient satisfies
// service.WecomPushSender).
type Backend struct{}
// ClientForTenant resolves the tenant's push client.
func (Backend) ClientForTenant(ctx context.Context, db *gorm.DB, tenantID uint64) (*wecom.ProactiveClient, error) {
return TenantClient(ctx, db, tenantID)
}
// OAuthClientForTenant resolves the tenant's client for web OAuth (培训扫码
// 签到免登):同一个自建应用凭据既推消息也做网页授权,复用一套配置。
func (Backend) OAuthClientForTenant(ctx context.Context, db *gorm.DB, tenantID uint64) (*wecom.ProactiveClient, error) {
return TenantClient(ctx, db, tenantID)
}
// TenantWecomUserIDs resolves the userid bindings.
func (Backend) TenantWecomUserIDs(ctx context.Context, db *gorm.DB, tenantID uint64, userIDs []string) (map[string]string, error) {
return TenantWecomUserIDs(ctx, db, tenantID, userIDs)
}