Skip to content

Commit

Permalink
@dev 优化代码、更新 readme.md、使用拦截器、配置文件优化
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepybear1113 committed May 3, 2022
1 parent c46a4e0 commit bc6759c
Show file tree
Hide file tree
Showing 23 changed files with 347 additions and 308 deletions.
13 changes: 1 addition & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.xjx</groupId>
<artifactId>ddt-crawler</artifactId>
<version>0.1.1</version>
<version>0.2.0</version>
<name>ddt-crawler</name>
<description>ddt-crawler</description>
<properties>
Expand Down Expand Up @@ -47,11 +47,6 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.baomidou</groupId>
Expand All @@ -74,12 +69,6 @@
<artifactId>xstream</artifactId>
<version>1.4.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>

</dependencies>

Expand Down
40 changes: 33 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
## 这里是本工程的介绍
# 介绍
## 简介
这个是用 Java 写的弹弹堂拍卖场列表爬取、展示的工程。直接请求拍卖场接口,获取拍卖场数据,解析价格,展示于页面。
## 技术栈
后盾:基于 Spring Boot 3.0.0-M2 使用 Java 17 编写。数据库方面,使用 MySQL 并使用 MyBatis-plus 工具进行连接管理。

前端:简单的 HTML+JavaScript+CSS,没有使用其他框架;HTTP 请求使用 axios。
## 简单的静态页面
这个静态页面仅做展示用,没有后端的相关支撑,故无法返回相关请求。静态页面:[ddt.html](https://sleepybear1113.github.io/ddt-crawler/src/main/resources/static/ddt.html)
# 部署方式

静态页面:[ddt.html](https://sleepybear1113.github.io/ddt-crawler/src/main/resources/static/ddt.html)

本地部署:
1. 下载 jar 包,[releases](https://github.com/sleepybear1113/ddt-crawler/releases)
## 本地 Jar 包部署:
1. 下载 Jar 包,[releases](https://github.com/sleepybear1113/ddt-crawler/releases)
2. Java 所需版本为 Java 17。
3. 执行 `java -jar 文件名` 即可启动。
## IDEA 部署
1. 设置 Java 版本为 Java 17。
2. clone 代码并构建工程。
3. 启动工程。
## 数据库相关
`src/resources/application.yml` 文件中有定义的数据库地址,需要新建名为 `ddt` 的 schema。然后新建相关数据库,如下代码:
```sql
create table template
(
id bigint not null primary key,
name varchar(255) null,
price decimal(10, 2) null,
modify_time bigint null,
constraint template_id_unique_index unique (id)
) engine = InnoDB;

create index template_name_index on template (name);
```
### 物品信息
由于拍卖场物品获取到的仅为物品 id,没有物品名称,需要在数据库中手动插入物品 id 和名称的对应关系,即 `template` 数据库的作用。此处不提供该数据。
# 使用方法
首先需要获取拍卖场的 HTTP 请求,可以在浏览器的控制台或者抓包获取。获取到 selfid 和 key 之后,填入页面对应位置,点击保存即可,而后就能进行物品的搜索。

其他具体部署方式待补充......
其中 selfid 为用户 id,不会变,key 为每次登录随机生成,下线即失效,所以需要账号一直在线才能使用。
46 changes: 46 additions & 0 deletions src/main/java/com/xjx/ddtcrawler/config/ConfigBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.xjx.ddtcrawler.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
* There is description
*
* @author sleepybear
* @date 2022/05/04 00:18
*/
@Configuration
public class ConfigBean {
private static List<Long> adminUserIds;
private static List<Long> commonUserIds;
private static String gameUrlPrefix;

public static List<Long> getAdminUserIds() {
return adminUserIds;
}

@Value("${user-id.admin}")
public void setAdminUserIds(List<Long> adminUserIds) {
ConfigBean.adminUserIds = adminUserIds;
}

public static List<Long> getCommonUserIds() {
return commonUserIds;
}

@Value("${user-id.common}")
public void setCommonUserIds(List<Long> commonUserIds) {
ConfigBean.commonUserIds = commonUserIds;
}

public static String getGameUrlPrefix() {
return gameUrlPrefix;
}

@Value("${game-url.prefix}")
public void setGameUrlPrefix(String gameUrlPrefix) {
ConfigBean.gameUrlPrefix = gameUrlPrefix;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.xjx.ddtcrawler.controller;

import com.xjx.ddtcrawler.cookie.UserPrivilege;
import com.xjx.ddtcrawler.cookie.WebUser;
import com.xjx.ddtcrawler.domain.QueryUrl;
import com.xjx.ddtcrawler.domain.Result;
Expand All @@ -21,8 +20,6 @@
public class AuctionController {
@Resource
private AuctionLogic auctionLogic;
@Resource
private UserPrivilege userPrivilege;

@RequestMapping("/auction/getAuctionItems")
public Result getItems(@RequestParam(required = false, defaultValue = "1") Integer page,
Expand All @@ -49,13 +46,13 @@ public Result getItems(@RequestParam(required = false, defaultValue = "1") Integ
queryUrl.setSort(sort);
queryUrl.setPage(page);
queryUrl.setName(itemName.trim());
if (userPrivilege.notAdmin(webUser)) {
if (webUser.notAdmin()) {
queryUrl.setUserId(-1L);
queryUrl.setBuyId(-1L);
}

Result result = auctionLogic.getSingleResult(queryUrl);
if (userPrivilege.notAdmin(webUser)) {
if (webUser.notAdmin()) {
result.hideSensitiveInfo();
}
return result;
Expand All @@ -78,7 +75,7 @@ public Result getAuctionPriceOder(String itemName,
queryUrl.setName(itemName.trim());

Result result = auctionLogic.getResultsByBatchPages(queryUrl, QueryUrl.DEFAULT_PAGES, true, 250L, priceType, sort, null);
if (userPrivilege.notAdmin(webUser)) {
if (webUser.notAdmin()) {
result.hideSensitiveInfo();
}
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.xjx.ddtcrawler.controller;

import com.xjx.ddtcrawler.cookie.UserPrivilege;
import com.xjx.ddtcrawler.config.ConfigBean;
import com.xjx.ddtcrawler.exception.MyException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -16,31 +15,26 @@
@RestController
public class PrivilegeController {

@Resource
private UserPrivilege userPrivilege;

@RequestMapping("/privilege/addCommonUserId")
public List<Long> addCommonUserId(Long id) {
if (id == null) {
throw new MyException("错误");
}

for (Long commonUserId : userPrivilege.getCommonUserIds()) {
for (Long commonUserId : ConfigBean.getCommonUserIds()) {
if (commonUserId.equals(id)) {
throw new MyException("重复");
}
}
userPrivilege.getCommonUserIds().add(id);
return userPrivilege.getCommonUserIds();
ConfigBean.getCommonUserIds().add(id);
return ConfigBean.getCommonUserIds();
}

@RequestMapping("/privilege/deleteCommonUserId")
public Object deleteCommonUserId(Long id) {
if (id == null) {
throw new MyException("错误");
public List<Long> deleteCommonUserId(Long id) {
if (id != null) {
ConfigBean.getCommonUserIds().removeIf(u -> u.equals(id));
}

userPrivilege.getCommonUserIds().removeIf(u -> u.equals(id));
return userPrivilege.getCommonUserIds();
return ConfigBean.getCommonUserIds();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.xjx.ddtcrawler.controller;

import com.xjx.ddtcrawler.cookie.UserPrivilege;
import com.xjx.ddtcrawler.cookie.WebUser;
import com.xjx.ddtcrawler.domain.Template;
import com.xjx.ddtcrawler.dto.TemplateDto;
import com.xjx.ddtcrawler.exception.MyException;
import com.xjx.ddtcrawler.logic.TemplateLogic;
import jakarta.annotation.Resource;
Expand All @@ -19,13 +18,11 @@
public class TemplateController {
@Resource
private TemplateLogic templateLogic;
@Resource
private UserPrivilege userPrivilege;

@RequestMapping("/template/getTemplateById")
public Template getTemplateById(Long id) throws MyException {
public TemplateDto getTemplateById(Long id) throws MyException {
WebUser webUser = WebUser.getSafeWebUser();
if (userPrivilege.notAdmin(webUser)) {
if (webUser.notAdmin()) {
throw new MyException("无权操作该接口");
}
return templateLogic.getTemplateById(id);
Expand All @@ -34,19 +31,19 @@ public Template getTemplateById(Long id) throws MyException {
@RequestMapping("/template/saveTemplate")
public Boolean saveTemplate(Long templateId, String templateName) throws MyException {
WebUser webUser = WebUser.getSafeWebUser();
if (userPrivilege.notAdmin(webUser)) {
if (webUser.notAdmin()) {
throw new MyException("无权操作该接口");
}
return templateLogic.saveTemplate(templateId, templateName);
}

@RequestMapping("/template/listAllTemplates")
public List<Template> listAllTemplates() {
public List<TemplateDto> listAllTemplates() {
return templateLogic.listAllTemplates();
}

@RequestMapping("/template/listCommonSlv4")
public List<Template> listCommonSlv4() {
public List<TemplateDto> listCommonSlv4() {
return templateLogic.listCommonSlv4();
}
}
54 changes: 0 additions & 54 deletions src/main/java/com/xjx/ddtcrawler/cookie/UserPrivilege.java

This file was deleted.

Loading

0 comments on commit bc6759c

Please sign in to comment.