From 2543c93ecfde2ebd932b02ea90940e63b47eb305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=B8=85?= <6581896+oncream@user.noreply.gitee.com> Date: Mon, 7 Apr 2025 12:05:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(1.0.2):=E4=BF=AE=E5=A4=8D=E4=B8=93?= =?UTF-8?q?=E6=A0=8F=E9=97=AE=E9=A2=98;=E4=BF=AE=E5=A4=8D=E5=A4=9A?= =?UTF-8?q?=E7=AB=AF=E7=99=BB=E5=BD=95=E9=97=AE=E9=A2=98=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/application/article/service.go | 40 +++++++++++++--- internal/application/crypto/password_test.go | 11 +++++ internal/application/crypto/phone_test.go | 13 ++++++ internal/application/purchase/service.go | 35 ++++++-------- internal/application/user/service.go | 16 +------ internal/domain/purchase/repository.go | 8 +--- .../persistence/purchase/purchase_repo.go | 46 ++++++++----------- .../interfaces/article/article_handler.go | 4 +- ui/src/app/app.module.ts | 6 +++ .../article-content.component.ts | 15 ++++-- 10 files changed, 110 insertions(+), 84 deletions(-) create mode 100644 internal/application/crypto/password_test.go create mode 100644 internal/application/crypto/phone_test.go diff --git a/internal/application/article/service.go b/internal/application/article/service.go index 6d71535..ad9d942 100644 --- a/internal/application/article/service.go +++ b/internal/application/article/service.go @@ -2,6 +2,7 @@ package article import ( "cls/internal/domain/article" + "cls/internal/domain/column" "cls/internal/domain/free_trial" "cls/internal/domain/purchase" domainUser "cls/internal/domain/user" @@ -17,6 +18,7 @@ import ( type ArticleService struct { repo article.ArticleRepository userRepo domainUser.UserRepository + columnRepo column.ColumnRepository purchaseRepo purchase.Repository userAggregateRepo domainUser.UserAggregateRepository FreeRepo free_trial.FreeTrialRepository @@ -26,11 +28,13 @@ type ArticleService struct { func NewArticleService(repo article.ArticleRepository, userRepo domainUser.UserRepository, purchaseRepo purchase.Repository, + columnRepo column.ColumnRepository, userAggregateRepo domainUser.UserAggregateRepository, FreeRepo free_trial.FreeTrialRepository, log logger.New) *ArticleService { return &ArticleService{repo, userRepo, + columnRepo, purchaseRepo, userAggregateRepo, FreeRepo, @@ -61,10 +65,28 @@ var class_type_reverse = map[string]uint64{ "金牌纪要库": 20087, } +var column_class = map[uint64]uint64{ + 81201: 20015, + 81202: 20014, + 81204: 20021, + 81205: 20013, + 81206: 20022, + 81208: 20023, + 81209: 20025, + 81210: 20026, + 81232: 20087, +} + func (a *ArticleService) Find(ePhone string, page *page.Page, searchParams map[string]string) error { conds := make([]builder.Cond, 0) - class := searchParams["search_like_class"] + columnInfo := &column.Column{} + var err error + class := searchParams["search_eq_class"] if class != "" { + columnInfo, err = a.columnRepo.FindByName(class) + if err != nil { + a.log.Error(err) + } classCode := class_type_reverse[class] if classCode != 0 { conds = append(conds, builder.Eq{"type": classCode}) @@ -73,7 +95,7 @@ func (a *ArticleService) Find(ePhone string, page *page.Page, searchParams map[s conds = append(conds, builder.And(builder.Gt{"ctime": time.Now().AddDate(0, 0, -2).Unix()}).And(builder.Eq{"is_free": 0})) articles := make([]*article.LianV1Article, 0) page.Content = &articles - err := a.repo.Find(page, conds) + err = a.repo.Find(page, conds) if err != nil { a.log.Error(err) return err @@ -85,11 +107,17 @@ func (a *ArticleService) Find(ePhone string, page *page.Page, searchParams map[s a.log.Error(err) } if user != nil { - columnData, err := a.purchaseRepo.FindColumnsByUserId(user.Id) + var columnData []*purchase.Purchase + if columnInfo != nil && columnInfo.ID > 0 { + columnData, err = a.purchaseRepo.FindColumnById(user.Id, columnInfo.ID) + } else { + columnData, err = a.purchaseRepo.FindColumnsByUserId(user.Id) + } + columnMap := make(map[uint64]struct{}) if len(columnData) != 0 { for _, v := range columnData { - columnMap[v.ContentId] = struct{}{} + columnMap[column_class[v.ContentId]] = struct{}{} } } @@ -211,7 +239,7 @@ func (a *ArticleService) FindUnLock(ePhone string, page *page.Page, searchParams func (a *ArticleService) FindFree(page *page.Page, searchParams map[string]string) error { conds := make([]builder.Cond, 0) - class := searchParams["search_like_class"] + class := searchParams["search_eq_class"] if class != "" { classCode := class_type_reverse[class] if classCode != 0 { @@ -260,7 +288,7 @@ func (a *ArticleService) Detail(userPhone string, id uint64) (*ArticleDto, error return nil, errors.New("未找到用户") } - p, err := a.purchaseRepo.FindByUserIdAndContent(user.Id, id, purchase.ContentTypeArticle) + p, err := a.purchaseRepo.FindArticleWithId(user.Id, id) if err != nil { a.log.Error(err) return nil, err diff --git a/internal/application/crypto/password_test.go b/internal/application/crypto/password_test.go new file mode 100644 index 0000000..fd16246 --- /dev/null +++ b/internal/application/crypto/password_test.go @@ -0,0 +1,11 @@ +package crypto + +import "testing" + +func TestPasswordEncode(t *testing.T) { + +} + +func TestPasswordDecode(t *testing.T) { + +} diff --git a/internal/application/crypto/phone_test.go b/internal/application/crypto/phone_test.go new file mode 100644 index 0000000..f5fda79 --- /dev/null +++ b/internal/application/crypto/phone_test.go @@ -0,0 +1,13 @@ +package crypto + +import "testing" + +func TestPhoneEncode(t *testing.T) { + ser := NewPhoneEncryptionService() + v, _ := ser.Encrypt("17782351006") + t.Log(v) +} + +func TestPhoneDecode(t *testing.T) { + +} diff --git a/internal/application/purchase/service.go b/internal/application/purchase/service.go index 783c9dc..6312235 100644 --- a/internal/application/purchase/service.go +++ b/internal/application/purchase/service.go @@ -17,37 +17,28 @@ func NewService(repo purchase.Repository) *Service { } } -// HasPermission 检查用户是否有权限访问内容 -func (s *Service) HasPermission(userId, contentId uint64, contentType purchase.ContentType, price float64) (bool, error) { - // 如果内容是免费的,直接返回true - if price == 0 { - return true, nil - } +// CreatePurchase 创建购买记录 +func (s *Service) CreatePurchase(userId, contentId uint64, contentType purchase.ContentType, price float64) error { + p := &purchase.Purchase{} + var err error - // 检查用户是否购买了内容 - purchase, err := s.repo.FindByUserIdAndContent(userId, contentId, contentType) - if err != nil { - return false, err - } - if purchase == nil { - return false, nil - } + if contentType == purchase.ContentTypeColumn { + p, err = s.repo.FindColumnWithId(userId, contentId) - return purchase.IsValid(), nil -} + } else { + p, err = s.repo.FindArticleWithId(userId, contentId) + + } -// CreatePurchase 创建购买记录 -func (s *Service) CreatePurchase(userId, contentId uint64, contentType purchase.ContentType, price float64) error { // 检查是否已购买 - exist, err := s.repo.FindByUserIdAndContent(userId, contentId, contentType) if err != nil { return err } - if exist != nil { + if p != nil { return errors.New("已经购买过该内容") } // 创建购买记录 - purchase := purchase.NewPurchase(userId, contentId, contentType, price, 0) - return s.repo.Save(purchase) + newP := purchase.NewPurchase(userId, contentId, contentType, price, 0) + return s.repo.Save(newP) } diff --git a/internal/application/user/service.go b/internal/application/user/service.go index 685e01a..eeceea6 100644 --- a/internal/application/user/service.go +++ b/internal/application/user/service.go @@ -63,21 +63,6 @@ func (u *UserService) SetPassword(phone, pwd string) error { return err } -// GetUserInfo 获取用户信息 -func (u *UserService) GetUserInfo(username string) (*UserDto, error) { - existUser, err := u.repo.FindByUsername(username) - if err != nil { - return nil, err - } - - return &UserDto{ - Uid: existUser.Id, - Username: existUser.Username, - Phone: existUser.Phone, - Status: existUser.Status, - }, nil -} - func (u *UserService) GetUserGiftCount(ePhone string) (int, error) { c, err := u.repo.GetUserGiftCount(ePhone) if err != nil { @@ -109,6 +94,7 @@ func (u *UserService) GetUserProfileByePhone(ePhone string) (*UserDto, error) { return nil, err } if user == nil { + u.log.Error("未找到用户") return nil, errors.New("未找到用户") } p, err := u.phoneEncryption.StringPhone(user.Phone) diff --git a/internal/domain/purchase/repository.go b/internal/domain/purchase/repository.go index 99fef13..2a465ed 100644 --- a/internal/domain/purchase/repository.go +++ b/internal/domain/purchase/repository.go @@ -4,16 +4,10 @@ package purchase type Repository interface { // Save 保存购买记录 Save(purchase *Purchase) error - // FindByUserIdAndContent 根据用户ID和内容ID查找购买记录 - FindByUserIdAndContent(userId, contentId uint64, contentType ContentType) (*Purchase, error) - // FindByUserId 查找用户的所有购买记录 - FindByUserId(userId uint64) ([]*Purchase, error) + FindArticleWithId(uid uint64, contentId uint64) (*Purchase, error) FindColumnById(uid uint64, ids ...uint64) ([]*Purchase, error) FindArticleById(uid uint64, ids ...uint64) ([]*Purchase, error) FindArticlesByUserId(userId uint64) ([]*Purchase, error) FindColumnsByUserId(userId uint64) ([]*Purchase, error) - - // FindByContent 查找内容的所有购买记录 - FindByContent(contentId uint64, contentType ContentType) ([]*Purchase, error) FindColumnWithId(uid uint64, contentId uint64) (*Purchase, error) } diff --git a/internal/infrastructure/persistence/purchase/purchase_repo.go b/internal/infrastructure/persistence/purchase/purchase_repo.go index 149d687..3e3d5b9 100644 --- a/internal/infrastructure/persistence/purchase/purchase_repo.go +++ b/internal/infrastructure/persistence/purchase/purchase_repo.go @@ -4,6 +4,7 @@ import ( "cls/internal/domain/purchase" "cls/pkg/logger" "cls/pkg/xorm_engine" + "time" "xorm.io/builder" ) @@ -30,31 +31,6 @@ func (r *PurchaseRepositoryORM) Save(purchase *purchase.Purchase) error { return err } -func (r *PurchaseRepositoryORM) FindByUserIdAndContent(userId, contentId uint64, contentType purchase.ContentType) (*purchase.Purchase, error) { - purchase := &purchase.Purchase{} - exist, err := r.engine.Where("user_id = ? AND content_id = ? AND content_type = ?", - userId, contentId, contentType).Get(purchase) - if err != nil { - return nil, err - } - if !exist { - return nil, nil - } - return purchase, nil -} - -func (r *PurchaseRepositoryORM) FindByUserId(userId uint64) ([]*purchase.Purchase, error) { - purchases := make([]*purchase.Purchase, 0) - err := r.engine.Where("user_id = ?", userId).Find(&purchases) - return purchases, err -} - -func (r *PurchaseRepositoryORM) FindByContent(contentId uint64, contentType purchase.ContentType) ([]*purchase.Purchase, error) { - purchases := make([]*purchase.Purchase, 0) - err := r.engine.Where("content_id = ? AND content_type = ?", contentId, contentType).Find(&purchases) - return purchases, err -} - func (r *PurchaseRepositoryORM) FindArticlesByUserId(userId uint64) ([]*purchase.Purchase, error) { purchases := make([]*purchase.Purchase, 0) err := r.engine.Where(builder.Eq{"user_id": userId}.And(builder.Eq{"content_type": purchase.ContentTypeArticle})).Find(&purchases) @@ -63,7 +39,9 @@ func (r *PurchaseRepositoryORM) FindArticlesByUserId(userId uint64) ([]*purchase func (r *PurchaseRepositoryORM) FindColumnsByUserId(userId uint64) ([]*purchase.Purchase, error) { purchases := make([]*purchase.Purchase, 0) - err := r.engine.Where(builder.Eq{"user_id": userId}.And(builder.Eq{"content_type": purchase.ContentTypeColumn})).Find(&purchases) + err := r.engine.Where(builder.Eq{"user_id": userId}. + And(builder.Eq{"content_type": purchase.ContentTypeColumn})). + And(builder.Gte{"expired_at": time.Now()}).Find(&purchases) return purchases, err } @@ -75,13 +53,25 @@ func (r *PurchaseRepositoryORM) FindArticleById(uid uint64, ids ...uint64) ([]*p func (r *PurchaseRepositoryORM) FindColumnById(uid uint64, ids ...uint64) ([]*purchase.Purchase, error) { purchases := make([]*purchase.Purchase, 0) - err := r.engine.Where(builder.Eq{"user_id": uid}.And(builder.In("content_id", ids)).And(builder.Eq{"content_type": purchase.ContentTypeColumn})).Find(&purchases) + err := r.engine.Where(builder.Eq{"user_id": uid}.And(builder.In("content_id", ids)). + And(builder.Eq{"content_type": purchase.ContentTypeColumn})). + And(builder.Gte{"expired_at": time.Now()}).Find(&purchases) return purchases, err } func (r *PurchaseRepositoryORM) FindColumnWithId(uid uint64, contentId uint64) (*purchase.Purchase, error) { data := &purchase.Purchase{} - _, err := r.engine.Where("user_id = ? AND content_id = ? AND content_type = ?", uid, contentId, purchase.ContentTypeColumn).Get(data) + _, err := r.engine.Where("user_id = ? AND content_id = ? AND content_type = ?", uid, contentId, purchase.ContentTypeColumn). + And(builder.Gte{"expired_at": time.Now()}).Get(data) + if err != nil { + return nil, err + } + return data, nil +} + +func (r *PurchaseRepositoryORM) FindArticleWithId(uid uint64, contentId uint64) (*purchase.Purchase, error) { + data := &purchase.Purchase{} + _, err := r.engine.Where("user_id = ? AND content_id = ? AND content_type = ?", uid, contentId, purchase.ContentTypeArticle).Get(data) if err != nil { return nil, err } diff --git a/internal/interfaces/article/article_handler.go b/internal/interfaces/article/article_handler.go index 1fba482..7cc3ef2 100644 --- a/internal/interfaces/article/article_handler.go +++ b/internal/interfaces/article/article_handler.go @@ -49,7 +49,7 @@ func (a *ArticleHandler) findAll(c *gin.Context) { } err = a.service.Find(ePhone, p, map[string]string{ - "search_like_class": c.Query("search_like_class")}) + "search_eq_class": c.Query("search_eq_class")}) if err != nil { c.AbortWithStatus(http.StatusInternalServerError) } else { @@ -91,7 +91,7 @@ func (a *ArticleHandler) free(c *gin.Context) { return } err = a.service.FindFree(p, map[string]string{ - "search_like_class": c.Query("search_like_class")}) + "search_eq_class": c.Query("search_eq_class")}) if err != nil { c.AbortWithStatus(http.StatusInternalServerError) } else { diff --git a/ui/src/app/app.module.ts b/ui/src/app/app.module.ts index 16fc4b3..939e12c 100644 --- a/ui/src/app/app.module.ts +++ b/ui/src/app/app.module.ts @@ -8,6 +8,7 @@ import { GoCaptchaModule } from 'go-captcha-angular'; import { AppComponent } from './app.component'; import { AppRoutingModule } from './app-routing.module'; import {AuthInterceptor} from "./core/interceptors/auth.interceptor"; +import {CheckInterceptor} from "./core/interceptors/check.interceptor"; @NgModule({ declarations: [ @@ -28,6 +29,11 @@ import {AuthInterceptor} from "./core/interceptors/auth.interceptor"; useClass: AuthInterceptor, multi: true, }, + { + provide: HTTP_INTERCEPTORS, + useClass: CheckInterceptor, + multi: true, + }, ], bootstrap: [AppComponent], schemas: [CUSTOM_ELEMENTS_SCHEMA] diff --git a/ui/src/app/home/component/article-content/article-content.component.ts b/ui/src/app/home/component/article-content/article-content.component.ts index 706e7a5..095f858 100644 --- a/ui/src/app/home/component/article-content/article-content.component.ts +++ b/ui/src/app/home/component/article-content/article-content.component.ts @@ -49,7 +49,11 @@ export class ArticleContentComponent implements OnInit { } getData() { - + if(this.className != "") { + this.searchParams['search_eq_class'] = this.className; + } else { + this.searchParams['search_eq_class'] = "" + } switch (this.currentSelect){ case "new": return this.getNewData() @@ -78,6 +82,8 @@ export class ArticleContentComponent implements OnInit { this.searchParams['page'] = this.searchParams['page']+1; this.hasMore = res.items.length === this.searchParams['size']; this.cdr.detectChanges(); + } else { + this.hasMore = false; } } }) @@ -85,9 +91,6 @@ export class ArticleContentComponent implements OnInit { getUnLockData() { this.getUsername() if(this.username!= ""){ - if(this.className != "") { - this.searchParams['search_eq_class'] = this.className - } this.homeService.unlockList(this.searchParams).pipe(takeUntil(this.destroy$)).subscribe(res=>{ if(res.items) { if(res.items.length > 0) { @@ -95,6 +98,8 @@ export class ArticleContentComponent implements OnInit { this.searchParams['page'] = this.searchParams['page']+1; this.hasMore = res.items.length === this.searchParams['size']; this.cdr.detectChanges(); + } else { + this.hasMore = false; } } }) @@ -109,6 +114,8 @@ export class ArticleContentComponent implements OnInit { this.searchParams['page'] = this.searchParams['page']+1; this.hasMore = res.items.length === this.searchParams['size']; this.cdr.detectChanges(); + } else { + this.hasMore = false; } } })