diff --git a/xianyu-common/src/main/java/top/biwin/xinayu/common/dto/request/GoofishAddCookieRequest.java b/xianyu-common/src/main/java/top/biwin/xinayu/common/dto/request/GoofishAddCookieRequest.java new file mode 100644 index 0000000..654d19c --- /dev/null +++ b/xianyu-common/src/main/java/top/biwin/xinayu/common/dto/request/GoofishAddCookieRequest.java @@ -0,0 +1,15 @@ +package top.biwin.xinayu.common.dto.request; + +import lombok.Data; + +/** + * TODO + * + * @author wangli + * @since 2026-01-24 21:52 + */ +@Data +public class GoofishAddCookieRequest { + private String id; + private String value; +} diff --git a/xianyu-common/src/main/java/top/biwin/xinayu/common/dto/request/GoofishPwdLoginRequest.java b/xianyu-common/src/main/java/top/biwin/xinayu/common/dto/request/GoofishPwdLoginRequest.java new file mode 100644 index 0000000..a7f8217 --- /dev/null +++ b/xianyu-common/src/main/java/top/biwin/xinayu/common/dto/request/GoofishPwdLoginRequest.java @@ -0,0 +1,22 @@ +package top.biwin.xinayu.common.dto.request; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +/** + * 闲鱼账号用户名密码登陆方式 + * + * @author wangli + * @since 2026-01-24 21:25 + */ +@Data +public class GoofishPwdLoginRequest { + + @JsonProperty("account_id") + private String accountId; + private String account; + private String password; + @JsonProperty("show_browser") + private Boolean show_browser; + +} diff --git a/xianyu-core/src/main/java/top/biwin/xianyu/core/repository/GoofishAccountRepository.java b/xianyu-core/src/main/java/top/biwin/xianyu/core/repository/GoofishAccountRepository.java index c2dfb70..151e16b 100644 --- a/xianyu-core/src/main/java/top/biwin/xianyu/core/repository/GoofishAccountRepository.java +++ b/xianyu-core/src/main/java/top/biwin/xianyu/core/repository/GoofishAccountRepository.java @@ -9,7 +9,9 @@ import java.util.Optional; @Repository public interface GoofishAccountRepository extends JpaRepository { - Optional findByUserId(Long UserId); + Optional findByUserId(Long UserId); - long countByEnabled(Boolean enabled); + long countByEnabled(Boolean enabled); + + Optional findByUsername(String username); } diff --git a/xianyu-server/src/main/java/top/biwin/xinayu/server/controller/GoofishAccountController.java b/xianyu-server/src/main/java/top/biwin/xinayu/server/controller/GoofishAccountController.java index 57e1b19..eb17214 100644 --- a/xianyu-server/src/main/java/top/biwin/xinayu/server/controller/GoofishAccountController.java +++ b/xianyu-server/src/main/java/top/biwin/xinayu/server/controller/GoofishAccountController.java @@ -3,15 +3,24 @@ package top.biwin.xinayu.server.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import top.biwin.xianyu.core.entity.GoofishAccountEntity; import top.biwin.xianyu.core.repository.GoofishAccountRepository; +import top.biwin.xianyu.goofish.qrcode.QrLoginService; +import top.biwin.xinayu.common.dto.request.GoofishAddCookieRequest; +import top.biwin.xinayu.common.dto.request.GoofishPwdLoginRequest; +import top.biwin.xinayu.common.dto.response.BaseResponse; import top.biwin.xinayu.common.dto.response.GoofishAccountResponse; +import top.biwin.xinayu.common.dto.response.QrLoginResponse; import top.biwin.xinayu.server.util.CurrentUserUtil; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; /** @@ -23,36 +32,98 @@ import java.util.stream.Collectors; @RestController @RequestMapping("/goofish") public class GoofishAccountController { - @Autowired - private GoofishAccountRepository goofishAccountRepository; + @Autowired + private QrLoginService qrLoginService; + @Autowired + private GoofishAccountRepository goofishAccountRepository; - /** - * 获取所有Cookie的详细信息(包括值和状态) - * 对应Python的 get_cookies_details 接口 - */ - @GetMapping("/details") - public ResponseEntity> getAllGoofishAccountDetails() { - List goofishAccountEntities = new ArrayList<>(); - if (CurrentUserUtil.isSuperAdmin()) { - goofishAccountEntities.addAll(goofishAccountRepository.findAll()); - } else { - goofishAccountRepository.findByUserId(CurrentUserUtil.getCurrentUserId()) - .ifPresent(goofishAccountEntities::add); + /** + * 获取所有Cookie的详细信息(包括值和状态) + * 对应Python的 get_cookies_details 接口 + */ + @GetMapping("/details") + public ResponseEntity> getAllGoofishAccountDetails() { + List goofishAccountEntities = new ArrayList<>(); + if (CurrentUserUtil.isSuperAdmin()) { + goofishAccountEntities.addAll(goofishAccountRepository.findAll()); + } else { + goofishAccountRepository.findByUserId(CurrentUserUtil.getCurrentUserId()) + .ifPresent(goofishAccountEntities::add); + } + + // 构建详细信息响应 + return ResponseEntity.ok(goofishAccountEntities.stream().map(account -> { + GoofishAccountResponse response = new GoofishAccountResponse(); + response.setId(account.getId()); + response.setCookie(account.getCookie()); + response.setEnabled(account.getEnabled()); + response.setAutoConfirm(account.getAutoConfirm()); + response.setRemark(account.getRemark() != null ? account.getRemark() : ""); + response.setPauseDuration(account.getPauseDuration() != null ? account.getPauseDuration() : 10); + response.setUsername(account.getUsername()); + response.setLoginPassword(account.getPassword()); + response.setShowBrowser(account.getShowBrowser() == 1); + return response; + }).collect(Collectors.toList())); } - // 构建详细信息响应 - return ResponseEntity.ok(goofishAccountEntities.stream().map(account -> { - GoofishAccountResponse response = new GoofishAccountResponse(); - response.setId(account.getId()); - response.setCookie(account.getCookie()); - response.setEnabled(account.getEnabled()); - response.setAutoConfirm(account.getAutoConfirm()); - response.setRemark(account.getRemark() != null ? account.getRemark() : ""); - response.setPauseDuration(account.getPauseDuration() != null ? account.getPauseDuration() : 10); - response.setUsername(account.getUsername()); - response.setLoginPassword(account.getPassword()); - response.setShowBrowser(account.getShowBrowser() == 1); - return response; - }).collect(Collectors.toList())); - } + @PostMapping("/qr-login/generate") + public ResponseEntity generateQrCode() { + return ResponseEntity.ok(qrLoginService.generateQrCode()); + } + + @GetMapping("/qr-login/check/{sessionId}") + public Map checkQrCodeStatus(@PathVariable String sessionId) { + return qrLoginService.checkQrCodeStatus(sessionId); + } + + @PostMapping("/password-login") + public ResponseEntity passwordLogin(@RequestBody GoofishPwdLoginRequest request) { + if (request.getAccountId() == null || request.getAccount() == null || request.getPassword() == null) { + return ResponseEntity.ok(BaseResponse.builder() + .success(false) + .message("账号ID、登录账号和密码不能为空") + .build()); + } + + // TODO 自动化操作浏览器完成登陆,并写 goofishAccount 表 +// browserService.startPasswordLogin( +// request.getAccount_id(), +// request.getAccount(), +// request.getPassword(), +// request.getShow_browser() != null && request.getShow_browser(), +// userId +// ); + + return ResponseEntity.ok(BaseResponse.builder() + .success(true) + .message("登录任务已启动") + .build()); + } + + + @PostMapping("/cookies") + public ResponseEntity loginByCookie(@RequestBody GoofishAddCookieRequest request) { + GoofishAccountEntity entity = goofishAccountRepository.findByUsername(request.getId()) + .map(account -> { + account.setCookie(account.getCookie()); + return account; + }) + .orElseGet(() -> { + GoofishAccountEntity newAccount = new GoofishAccountEntity(); + // TODO 该属性,应该利用 cookie 真实去获取, 这里暂时写死 + newAccount.setId(1000L); + newAccount.setCookie(request.getValue()); + newAccount.setUserId(1L); + newAccount.setAutoConfirm(1); + newAccount.setUsername(request.getId()); + return newAccount; + }); + + goofishAccountRepository.save(entity); + return ResponseEntity.ok(BaseResponse.builder() + .success(true) + .message("手动添加 cookie 成功") + .build()); + } } diff --git a/xianyu-server/src/main/java/top/biwin/xinayu/server/controller/QrLoginController.java b/xianyu-server/src/main/java/top/biwin/xinayu/server/controller/QrLoginController.java deleted file mode 100644 index a748684..0000000 --- a/xianyu-server/src/main/java/top/biwin/xinayu/server/controller/QrLoginController.java +++ /dev/null @@ -1,34 +0,0 @@ -package top.biwin.xinayu.server.controller; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RestController; -import top.biwin.xianyu.goofish.qrcode.QrLoginService; -import top.biwin.xinayu.common.dto.response.QrLoginResponse; - -import java.util.Map; - -/** - * TODO - * - * @author wangli - * @since 2026-01-23 22:37 - */ -@RestController -public class QrLoginController { - @Autowired - private QrLoginService qrLoginService; - - @PostMapping("/qr-login/generate") - public ResponseEntity generateQrCode() { - return ResponseEntity.ok(qrLoginService.generateQrCode()); - } - - @GetMapping("/qr-login/check/{sessionId}") - public Map checkQrCodeStatus(@PathVariable String sessionId) { - return qrLoginService.checkQrCodeStatus(sessionId); - } -}