1. 数据库新增 000064_ad_tracking.sql 迁移,创建 ad_tracking_links 和 ad_tracking_clicks 表,并在 users、rental_orders 增加归因字段 2. 后端新增 adtracking 模块,挂载 GET /r/:code 302 智能重定向、点击流水异步记录、转化率/GMV聚合统计及管理接口 3. 用户认证 (auth) 与订单创建 (order) 链路贯通,实现注册用户与订单成交推广短码全链路归因 4. 前端管理后台左侧新增【推广追踪】目录,支持大盘指标总览、推广链接 CRUD、状态启停、一键复制短链及二维码下载 (PNG)
133 lines
3.5 KiB
Go
133 lines
3.5 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"hfb_sys/backend/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type UserRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewUserRepository(db *gorm.DB) *UserRepository {
|
|
return &UserRepository{db: db}
|
|
}
|
|
|
|
func (r *UserRepository) FindByID(ctx context.Context, id uint64) (*model.User, error) {
|
|
var user model.User
|
|
if err := r.db.WithContext(ctx).First(&user, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
// FindActiveForToken 校验用户仍可用且令牌版本未被撤销。
|
|
func (r *UserRepository) FindActiveForToken(ctx context.Context, id uint64, tokenVersion int64) (*model.User, error) {
|
|
if r == nil || r.db == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
var user model.User
|
|
if err := r.db.WithContext(ctx).
|
|
Select("id, phone, status, token_version").
|
|
First(&user, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if user.Status != "active" {
|
|
return nil, ErrUserDisabled
|
|
}
|
|
if user.TokenVersion != tokenVersion {
|
|
return nil, ErrTokenVersionMismatch
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *UserRepository) UpdateProfile(ctx context.Context, id uint64, nickname string, avatarURL string) (*model.User, error) {
|
|
if err := r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", id).Updates(map[string]any{
|
|
"nickname": nickname,
|
|
"avatar_url": avatarURL,
|
|
}).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return r.FindByID(ctx, id)
|
|
}
|
|
|
|
func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string, trackCode ...string) (*model.User, error) {
|
|
now := time.Now()
|
|
var code string
|
|
if len(trackCode) > 0 {
|
|
code = strings.TrimSpace(trackCode[0])
|
|
}
|
|
user := model.User{
|
|
Phone: phone,
|
|
Nickname: "用户" + phone[len(phone)-4:],
|
|
RealnameStatus: "unverified",
|
|
RiskStatus: "normal",
|
|
CreditScore: 100,
|
|
Status: "active",
|
|
RegisterTrackCode: code,
|
|
LastLoginAt: &now,
|
|
}
|
|
|
|
err := r.db.WithContext(ctx).Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "phone"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{"last_login_at", "updated_at"}),
|
|
}).Create(&user).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var found model.User
|
|
if err := r.db.WithContext(ctx).Where("phone = ?", phone).First(&found).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &found, nil
|
|
}
|
|
|
|
func (r *UserRepository) FindByPhone(ctx context.Context, phone string) (*model.User, error) {
|
|
var user model.User
|
|
if err := r.db.WithContext(ctx).Where("phone = ?", phone).First(&user).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *UserRepository) SetPassword(ctx context.Context, userID uint64, hash string) error {
|
|
return r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", userID).Update("password_hash", hash).Error
|
|
}
|
|
|
|
func (r *UserRepository) RegisterWithPassword(ctx context.Context, phone string, hash string, trackCode ...string) (*model.User, error) {
|
|
now := time.Now()
|
|
var code string
|
|
if len(trackCode) > 0 {
|
|
code = strings.TrimSpace(trackCode[0])
|
|
}
|
|
user := model.User{
|
|
Phone: phone,
|
|
PasswordHash: hash,
|
|
Nickname: "用户" + phone[len(phone)-4:],
|
|
RealnameStatus: "unverified",
|
|
RiskStatus: "normal",
|
|
CreditScore: 100,
|
|
Status: "active",
|
|
RegisterTrackCode: code,
|
|
LastLoginAt: &now,
|
|
}
|
|
|
|
err := r.db.WithContext(ctx).Create(&user).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func IsNotFound(err error) bool {
|
|
return errors.Is(err, gorm.ErrRecordNotFound)
|
|
}
|