96 lines
2.2 KiB
Go
96 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"geo/internal/biz"
|
|
"geo/internal/config"
|
|
"geo/internal/entitys"
|
|
"geo/internal/publisher"
|
|
"geo/pkg"
|
|
"geo/tmpl/errcode"
|
|
)
|
|
|
|
type LoginService struct {
|
|
cfg *config.Config
|
|
publishBiz *biz.PublishBiz
|
|
}
|
|
|
|
func NewLoginService(
|
|
cfg *config.Config,
|
|
publishBiz *biz.PublishBiz,
|
|
|
|
) *LoginService {
|
|
return &LoginService{
|
|
cfg: cfg,
|
|
publishBiz: publishBiz,
|
|
}
|
|
}
|
|
|
|
func (s *LoginService) LoginPlatform(c *fiber.Ctx, req *entitys.LoginPlatformRequest) error {
|
|
_, err := s.publishBiz.ValidateAccessToken(c.UserContext(), req.AccessToken)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 获取平台信息
|
|
platInfo, err := s.publishBiz.GetPlatInfo(c.UserContext(), req.PlatIndex)
|
|
if err != nil {
|
|
return errcode.NotFound("平台不存在")
|
|
}
|
|
|
|
// 创建发布器
|
|
platMap := map[string]interface{}{
|
|
"login_url": platInfo.LoginURL,
|
|
"edit_url": platInfo.EditURL,
|
|
"logined_url": platInfo.LoginedURL,
|
|
}
|
|
|
|
var pub interface{ WaitLogin() (bool, string) }
|
|
switch req.PlatIndex {
|
|
case "xhs":
|
|
pub = publisher.NewXiaohongshuPublisher(false, "", "", nil, req.UserIndex, req.PlatIndex, "", "", "", platMap, s.cfg)
|
|
default:
|
|
pub = publisher.NewXiaohongshuPublisher(false, "", "", nil, req.UserIndex, req.PlatIndex, "", "", "", platMap, s.cfg)
|
|
}
|
|
|
|
success, msg := pub.WaitLogin()
|
|
if !success {
|
|
return errcode.SysErr(msg)
|
|
}
|
|
|
|
// 更新登录状态
|
|
err = s.publishBiz.UpdateLoginStatus(c.UserContext(), req.UserIndex, req.PlatIndex, 1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return pkg.HandleResponse(c, fiber.Map{})
|
|
}
|
|
|
|
func (s *LoginService) LogoutPlatform(c *fiber.Ctx, req *entitys.LogoutPlatformRequest) error {
|
|
_, err := s.publishBiz.ValidateAccessToken(c.UserContext(), req.AccessToken)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 更新登录状态为未登录
|
|
err = s.publishBiz.UpdateLoginStatus(c.UserContext(), req.UserIndex, req.PlatIndex, 2)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return pkg.HandleResponse(c, fiber.Map{})
|
|
}
|
|
|
|
func (s *LoginService) ServeQrcode(c *fiber.Ctx, filename string) error {
|
|
filepath := filepath.Join(s.cfg.Sys.QrcodesDir, filename)
|
|
if _, err := os.Stat(filepath); os.IsNotExist(err) {
|
|
return errcode.NotFound("二维码不存在")
|
|
}
|
|
return c.SendFile(filepath)
|
|
}
|