This commit is contained in:
parent
1676d0dfdb
commit
8e77619a37
|
|
@ -29,11 +29,6 @@ func InitializeApp(configConfig *config.Config, allLogger log.AllLogger) (*serve
|
||||||
publishImpl := impl.NewPublishImpl(db)
|
publishImpl := impl.NewPublishImpl(db)
|
||||||
loginRelationImpl := impl.NewLoginRelationImpl(db)
|
loginRelationImpl := impl.NewLoginRelationImpl(db)
|
||||||
articleTypeImpl := impl.NewArticleTypeImpl(db)
|
articleTypeImpl := impl.NewArticleTypeImpl(db)
|
||||||
publishBiz := biz.NewPublishBiz(configConfig, publishImpl, userImpl, platImpl, tokenImpl, loginRelationImpl)
|
|
||||||
authBiz := biz.NewAuthBiz(configConfig, tokenImpl, userImpl)
|
|
||||||
appService := service.NewAppService(configConfig, tokenImpl, userImpl, platImpl, publishBiz, authBiz, loginRelationImpl)
|
|
||||||
loginService := service.NewLoginService(configConfig, publishBiz, authBiz)
|
|
||||||
publishService := service.NewPublishService(configConfig, publishBiz, authBiz, db)
|
|
||||||
productImpl := impl.NewProductImpl(db)
|
productImpl := impl.NewProductImpl(db)
|
||||||
productSourceImpl := impl.NewProductSourceImpl(db)
|
productSourceImpl := impl.NewProductSourceImpl(db)
|
||||||
oss, err := utils_oss.NewClient(configConfig)
|
oss, err := utils_oss.NewClient(configConfig)
|
||||||
|
|
@ -41,6 +36,12 @@ func InitializeApp(configConfig *config.Config, allLogger log.AllLogger) (*serve
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
productBiz := biz.NewProductBiz(productImpl, productSourceImpl, configConfig, oss)
|
productBiz := biz.NewProductBiz(productImpl, productSourceImpl, configConfig, oss)
|
||||||
|
publishBiz := biz.NewPublishBiz(configConfig, publishImpl, userImpl, platImpl, tokenImpl, loginRelationImpl)
|
||||||
|
authBiz := biz.NewAuthBiz(configConfig, tokenImpl, userImpl)
|
||||||
|
appService := service.NewAppService(configConfig, tokenImpl, userImpl, platImpl, publishBiz, authBiz, loginRelationImpl, productBiz)
|
||||||
|
loginService := service.NewLoginService(configConfig, publishBiz, authBiz)
|
||||||
|
publishService := service.NewPublishService(configConfig, publishBiz, authBiz, db)
|
||||||
|
|
||||||
productService := service.NewProductService(configConfig, productImpl, authBiz, productBiz)
|
productService := service.NewProductService(configConfig, productImpl, authBiz, productBiz)
|
||||||
aiBiz := biz.NewAiBiz(platImpl, articleTypeImpl)
|
aiBiz := biz.NewAiBiz(platImpl, articleTypeImpl)
|
||||||
productSourceService := service.NewProductSourceService(configConfig, productImpl, authBiz, aiBiz, productBiz, productSourceImpl, publishBiz, articleTypeImpl)
|
productSourceService := service.NewProductSourceService(configConfig, productImpl, authBiz, aiBiz, productBiz, productSourceImpl, publishBiz, articleTypeImpl)
|
||||||
|
|
|
||||||
|
|
@ -92,6 +92,15 @@ func (p *ProductBiz) AddSource(ctx context.Context, source *model.ProductSource)
|
||||||
|
|
||||||
func (p *ProductBiz) SourceUpload(ctx context.Context, file []byte, fileName string) (string, error) {
|
func (p *ProductBiz) SourceUpload(ctx context.Context, file []byte, fileName string) (string, error) {
|
||||||
url, err := p.oss.UploadBytes(p.cfg.Oss.FilePath+"source/"+fileName, file)
|
url, err := p.oss.UploadBytes(p.cfg.Oss.FilePath+"source/"+fileName, file)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("上传文件失败: %w", err)
|
||||||
|
}
|
||||||
|
return url, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ProductBiz) OssUpload(ctx context.Context, userIndex string, platIndex string, data []byte) (string, error) {
|
||||||
|
url, err := p.oss.UploadBytes(fmt.Sprintf("%scookie/%s/%s.json", p.cfg.Oss.FilePath, userIndex, platIndex), data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("上传文件失败: %w", err)
|
return "", fmt.Errorf("上传文件失败: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,9 @@ func (p *PlatImpl) GetPlatListWithLoginStatus(ctx context.Context, cond *builder
|
||||||
plat.img_url,
|
plat.img_url,
|
||||||
plat.index,
|
plat.index,
|
||||||
plat.desc,
|
plat.desc,
|
||||||
|
plat.login_url,
|
||||||
|
plat.logined_url,
|
||||||
|
plat.logined_judge_way,
|
||||||
COALESCE(login_relation.login_status, 2) as login_status
|
COALESCE(login_relation.login_status, 2) as login_status
|
||||||
FROM plat
|
FROM plat
|
||||||
LEFT JOIN login_relation ON login_relation.plat_index COLLATE utf8mb4_unicode_ci = plat.index AND login_relation.status = 1
|
LEFT JOIN login_relation ON login_relation.plat_index COLLATE utf8mb4_unicode_ci = plat.index AND login_relation.status = 1
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,13 @@ type (
|
||||||
UserIndex string `json:"user_index" validate:"required" zh:"用户索引"`
|
UserIndex string `json:"user_index" validate:"required" zh:"用户索引"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SendCookieRequest struct {
|
||||||
|
AccessToken string `json:"access_token" validate:"required" zh:"access_token"`
|
||||||
|
UserIndex string `json:"user_index" validate:"required" zh:"用户索引"`
|
||||||
|
CookieData string `json:"cookie_data" validate:"required" zh:"cookie_data"`
|
||||||
|
PlatIndex string `json:"plat_index" validate:"required" zh:"平台索引"`
|
||||||
|
}
|
||||||
|
|
||||||
PlatListRequest struct {
|
PlatListRequest struct {
|
||||||
AccessToken string `json:"access_token" validate:"required" zh:"access_token"`
|
AccessToken string `json:"access_token" validate:"required" zh:"access_token"`
|
||||||
PlatType int `json:"plat_type" zh:"平台类型"`
|
PlatType int `json:"plat_type" zh:"平台类型"`
|
||||||
|
|
|
||||||
|
|
@ -98,12 +98,12 @@ func (pm *PublishManager) getTaskLogger(publishID int, requestID string) (*log.L
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start 启动自动发布(支持并发worker数量)
|
// Start 启动自动发布(支持并发worker数量)
|
||||||
func (pm *PublishManager) Start(tokenID int, workerNum int) bool {
|
func (pm *PublishManager) Start(workerNum int) bool {
|
||||||
pm.mu.Lock()
|
pm.mu.Lock()
|
||||||
defer pm.mu.Unlock()
|
defer pm.mu.Unlock()
|
||||||
|
|
||||||
if pm.AutoStatus {
|
if pm.AutoStatus {
|
||||||
log.Printf("自动发布服务已在运行中,tokenID=%d", tokenID)
|
log.Printf("自动发布服务已在运行中")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -114,7 +114,6 @@ func (pm *PublishManager) Start(tokenID int, workerNum int) bool {
|
||||||
workerNum = MaxWorkerNum
|
workerNum = MaxWorkerNum
|
||||||
}
|
}
|
||||||
|
|
||||||
pm.TokenID = tokenID
|
|
||||||
pm.AutoStatus = true
|
pm.AutoStatus = true
|
||||||
pm.workerNum = workerNum
|
pm.workerNum = workerNum
|
||||||
pm.stopCh = make(chan struct{})
|
pm.stopCh = make(chan struct{})
|
||||||
|
|
@ -129,7 +128,7 @@ func (pm *PublishManager) Start(tokenID int, workerNum int) bool {
|
||||||
go pm.workerLoop(i)
|
go pm.workerLoop(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("自动发布服务已启动,tokenID=%d,worker数量=%d", tokenID, workerNum)
|
log.Printf("自动发布服务已启动,worker数量=%d", workerNum)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -242,8 +241,7 @@ func (pm *PublishManager) acquireTask() (*entitys.PublishTaskDetail, error) {
|
||||||
SELECT p2.request_id
|
SELECT p2.request_id
|
||||||
FROM publish p2
|
FROM publish p2
|
||||||
INNER JOIN plat pl ON p2.plat_index COLLATE utf8mb4_unicode_ci = pl.index
|
INNER JOIN plat pl ON p2.plat_index COLLATE utf8mb4_unicode_ci = pl.index
|
||||||
WHERE p2.token_id = ?
|
WHERE p2.status = ?
|
||||||
AND p2.status = ?
|
|
||||||
AND p2.publish_time <= ?
|
AND p2.publish_time <= ?
|
||||||
AND pl.status = 1
|
AND pl.status = 1
|
||||||
ORDER BY p2.publish_time ASC
|
ORDER BY p2.publish_time ASC
|
||||||
|
|
@ -253,7 +251,7 @@ func (pm *PublishManager) acquireTask() (*entitys.PublishTaskDetail, error) {
|
||||||
AND p.status = ?
|
AND p.status = ?
|
||||||
`
|
`
|
||||||
|
|
||||||
result := pm.db.Client.Exec(updateSQL, StatusProcessing, uid, pm.TokenID, StatusPending, currentTime, StatusPending)
|
result := pm.db.Client.Exec(updateSQL, StatusProcessing, uid, StatusPending, currentTime, StatusPending)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return nil, result.Error
|
return nil, result.Error
|
||||||
}
|
}
|
||||||
|
|
@ -272,6 +270,7 @@ func (pm *PublishManager) acquireTask() (*entitys.PublishTaskDetail, error) {
|
||||||
p.title,
|
p.title,
|
||||||
p.tag,
|
p.tag,
|
||||||
p.user_index,
|
p.user_index,
|
||||||
|
p.token_id,
|
||||||
p.url,
|
p.url,
|
||||||
p.img,
|
p.img,
|
||||||
p.publish_time,
|
p.publish_time,
|
||||||
|
|
@ -284,12 +283,12 @@ func (pm *PublishManager) acquireTask() (*entitys.PublishTaskDetail, error) {
|
||||||
pl.desc
|
pl.desc
|
||||||
FROM publish p
|
FROM publish p
|
||||||
INNER JOIN plat pl ON p.plat_index COLLATE utf8mb4_unicode_ci = pl.index AND pl.status = 1
|
INNER JOIN plat pl ON p.plat_index COLLATE utf8mb4_unicode_ci = pl.index AND pl.status = 1
|
||||||
WHERE uid=? AND p.token_id = ?
|
WHERE uid=?
|
||||||
ORDER BY p.publish_time DESC
|
ORDER BY p.publish_time DESC
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
`
|
`
|
||||||
|
|
||||||
err := pm.db.Client.Raw(querySQL, uid, pm.TokenID).Scan(&task).Error
|
err := pm.db.Client.Raw(querySQL, uid).Scan(&task).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ func (m *AppModule) Register(router fiber.Router) {
|
||||||
router.Post("/add_user", vali(m.appService.AddUser, &entitys.AddUserRequest{}))
|
router.Post("/add_user", vali(m.appService.AddUser, &entitys.AddUserRequest{}))
|
||||||
router.Post("/del_user", vali(m.appService.DelUser, &entitys.DelUserRequest{}))
|
router.Post("/del_user", vali(m.appService.DelUser, &entitys.DelUserRequest{}))
|
||||||
router.Post("/get_app", vali(m.appService.GetApp, &entitys.GetAppRequest{}))
|
router.Post("/get_app", vali(m.appService.GetApp, &entitys.GetAppRequest{}))
|
||||||
|
router.Post("/send_cookie", vali(m.appService.SendCookie, &entitys.SendCookieRequest{}))
|
||||||
router.Post("/publish_records", vali(m.publishService.PublishRecords, &entitys.PublishRecordsRequest{}))
|
router.Post("/publish_records", vali(m.publishService.PublishRecords, &entitys.PublishRecordsRequest{}))
|
||||||
router.Post("/publish_on", vali(m.publishService.PublishOn, &entitys.PublishOnRequest{}))
|
router.Post("/publish_on", vali(m.publishService.PublishOn, &entitys.PublishOnRequest{}))
|
||||||
router.Post("/publish_off", vali(m.publishService.PublishOff, &entitys.PublishOffRequest{}))
|
router.Post("/publish_off", vali(m.publishService.PublishOff, &entitys.PublishOffRequest{}))
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ package service
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
|
|
||||||
"geo/internal/biz"
|
"geo/internal/biz"
|
||||||
|
|
@ -23,6 +25,7 @@ type AppService struct {
|
||||||
loginRelationImpl *impl.LoginRelationImpl
|
loginRelationImpl *impl.LoginRelationImpl
|
||||||
publishBiz *biz.PublishBiz
|
publishBiz *biz.PublishBiz
|
||||||
authBiz *biz.AuthBiz
|
authBiz *biz.AuthBiz
|
||||||
|
productBiz *biz.ProductBiz
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAppService(
|
func NewAppService(
|
||||||
|
|
@ -34,6 +37,7 @@ func NewAppService(
|
||||||
publishBiz *biz.PublishBiz,
|
publishBiz *biz.PublishBiz,
|
||||||
authBiz *biz.AuthBiz,
|
authBiz *biz.AuthBiz,
|
||||||
loginRelationImpl *impl.LoginRelationImpl,
|
loginRelationImpl *impl.LoginRelationImpl,
|
||||||
|
productBiz *biz.ProductBiz,
|
||||||
) *AppService {
|
) *AppService {
|
||||||
return &AppService{
|
return &AppService{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
|
|
@ -43,6 +47,7 @@ func NewAppService(
|
||||||
platImpl: platImpl,
|
platImpl: platImpl,
|
||||||
publishBiz: publishBiz,
|
publishBiz: publishBiz,
|
||||||
authBiz: authBiz,
|
authBiz: authBiz,
|
||||||
|
productBiz: productBiz,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -191,6 +196,23 @@ func (a *AppService) GetApp(c *fiber.Ctx, req *entitys.GetAppRequest) error {
|
||||||
return pkg.HandleResponse(c, result)
|
return pkg.HandleResponse(c, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *AppService) SendCookie(c *fiber.Ctx, req *entitys.SendCookieRequest) error {
|
||||||
|
_, err := a.authBiz.ValidateAccessToken(c.UserContext(), req.AccessToken)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dir := filepath.Join(a.cfg.Sys.CookiesDir, req.UserIndex)
|
||||||
|
cookieFile := filepath.Join(dir, req.PlatIndex+".json")
|
||||||
|
os.WriteFile(cookieFile, []byte(req.CookieData), 0644)
|
||||||
|
_, _ = a.productBiz.OssUpload(c.UserContext(), req.UserIndex, req.PlatIndex, []byte(req.CookieData))
|
||||||
|
// 更新登录状态为未登录
|
||||||
|
err = a.publishBiz.UpdateLoginStatus(c.UserContext(), req.UserIndex, req.PlatIndex, 1)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (a *AppService) PlatList(c *fiber.Ctx, req *entitys.PlatListRequest) error {
|
func (a *AppService) PlatList(c *fiber.Ctx, req *entitys.PlatListRequest) error {
|
||||||
_, err := a.authBiz.ValidateAccessToken(c.UserContext(), req.AccessToken)
|
_, err := a.authBiz.ValidateAccessToken(c.UserContext(), req.AccessToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -82,7 +82,7 @@ func (s *PublishService) PublishRecords(c *fiber.Ctx, req *entitys.PublishRecord
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PublishService) PublishOn(c *fiber.Ctx, req *entitys.PublishOnRequest) error {
|
func (s *PublishService) PublishOn(c *fiber.Ctx, req *entitys.PublishOnRequest) error {
|
||||||
tokenInfo, err := s.authBiz.ValidateAccessToken(c.UserContext(), req.AccessToken)
|
_, err := s.authBiz.ValidateAccessToken(c.UserContext(), req.AccessToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -91,7 +91,7 @@ func (s *PublishService) PublishOn(c *fiber.Ctx, req *entitys.PublishOnRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if pm.Start(int(tokenInfo.ID), s.cfg.Sys.AutoPublishWorkers) {
|
if pm.Start(s.cfg.Sys.AutoPublishWorkers) {
|
||||||
return pkg.HandleResponse(c, fiber.Map{
|
return pkg.HandleResponse(c, fiber.Map{
|
||||||
"auto_status": pm.AutoStatus,
|
"auto_status": pm.AutoStatus,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue