作者主页:编程千纸鹤
作者简介:Java、前端、Pythone开发多年,做过高程,项目经理,架构师
主要内容:Java项目开发、毕业设计开发、面试技术整理、最新技术分享
项目编号:BS-PT-066
本项目基于springboot+vue实现了一个前后端分离模式的校园二手交易平台。校内师生可以在此平台上注册自己的账户,然后发布自己想要处理的二手物品,平台本身不实现在线交易功能,发布的二手物品由买卖双方自行联系进行线下交易。主要实现的功能有用户注册、登陆、发布商品、收藏商品、统计浏览量和收藏量、在线留言、全文检索、管理个人发布的商品和留言等功能。系统功能完整,业务模式清晰,开发结构简单,比较符合目前后端分离模式开发的主流诉求,可以用作毕业设计或课程设计以及期未作业使用。
语言环境:Java: jdk1.8
数据库:Mysql: mysql5.7 REDIS缓存数据库
应用服务器:Tomcat: tomcat8.5.31
开发工具:IDEA或eclipse
后台开发技术:springboot+mybatisplus+jdk8+mysql5.6
前后台开发技术:VUE+ElementsUI
用户登陆注册
发布二手物品
查看详情
查看我的收藏
查看我发布的商品:可以进行商品上下架 和删除操作
管理我发布的留言
package cn.fleamarket.controller;
import cn.fleamarket.common.R;
import cn.fleamarket.domain.Favorites;
import cn.fleamarket.domain.User;
import cn.fleamarket.service.FavoritesService;
import cn.fleamarket.service.UserService;
import cn.fleamarket.utils.StringTool;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* 产品收藏表
*
* @author znz
* @date 2022-09-12 10:46:22
*/
@RestController
@RequestMapping("/favorites")
public class FavoritesController {
@Autowired
private FavoritesService favoritesService;
@Autowired
UserService userService;
@PostMapping(value = "/favoriteList", produces = "application/json")
@ApiOperation("收藏分页查询列表,入参是page:第几页,number:每页几条")
public R> favoritesList(@RequestBody Map params) {
if (Objects.isNull(params)) {
return R.error("参数错误!");
}
User nowUser = userService.qureyByUserName(params.getOrDefault("username","").toString());
if (Objects.isNull(nowUser)) {
return R.error("用户未登录!");
}
params.put("userId",nowUser.getId());
Page favoritesPage = favoritesService.selectListPage(params);
List data = favoritesPage
.getRecords().stream()
.filter(distinctByKey(Favorites::getProductId))
.collect(Collectors.toList());
return R.pageBuild(favoritesPage.getTotal(),
favoritesPage.hasNext(),
favoritesPage.hasPrevious(), data);
}
@PostMapping(value = "/addFavorites", produces = "application/json")
@ApiOperation("添加收藏,pid:商品id")
public JSONObject addFavorites(@RequestBody JSONObject jsonObject, HttpServletRequest request, HttpServletResponse response) {
JSONObject ret = new JSONObject();
User user = null;
try {
user = userService.qureyByUserName(jsonObject.getString("username"));
} catch (Exception e) {
e.printStackTrace();
}
try {
List list = favoritesService.selectByUid(user.getId());
if (list.size() != 0) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getProductId().equals(jsonObject.getString("pid"))) {
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "添加失败");
return ret;
}
}
}
if (user != null) {
String productId = jsonObject.getString("pid");
Integer state = jsonObject.getInteger("state");
Favorites favorites = new Favorites();
favorites.setId(StringTool.getUUID());
favorites.setUserId(user.getId());
favorites.setProductId(productId);
favorites.setCreateTime(new Date());
favorites.setState(state);
Integer isS = favoritesService.addFavorites(favorites);
if (isS > 0) {
ret.put("data", true);
ret.put("code", 0);
ret.put("msg", "添加成功");
} else {
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "添加失败");
}
} else {
ret.put("msg", "用户未登录");
}
} catch (Exception e) {
e.printStackTrace();
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "未知错误");
}
return ret;
}
@PostMapping(value = "/deleteFavorites", produces = "application/json")
@ApiOperation("删除收藏,fid:收藏id")
public JSONObject deleteFavorites(@RequestBody JSONObject jsonObject, HttpServletRequest request, HttpServletResponse response) {
JSONObject ret = new JSONObject();
User user = null;
try {
user = userService.qureyByUserName(jsonObject.getString("username"));
} catch (Exception e) {
e.printStackTrace();
}
try {
if (user != null) {
String fid = jsonObject.getString("fid");
Integer isS = favoritesService.deleteFavorites(fid);
if (isS > 0) {
ret.put("code", 0);
ret.put("data", true);
ret.put("msg", "删除成功");
}
} else {
ret.put("msg", "用户未登录");
}
} catch (Exception e) {
e.printStackTrace();
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "删除失败");
}
return ret;
}
private static Predicate distinctByKey(Function super T, ?> keyExtractor) {
Map
package cn.fleamarket.controller;
import cn.fleamarket.common.R;
import cn.fleamarket.config.PathConfig;
import cn.fleamarket.domain.Image;
import cn.fleamarket.service.ImageService;
import cn.fleamarket.utils.StringTool;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.Objects;
/**
* 图片表
*
* @author znz
* @date 2022-09-12 10:46:22
*/
@RestController
@RequestMapping("/image")
@Api("图片接口")
@CrossOrigin
public class ImageController {
@Autowired
ImageService imageService;
@Autowired
private PathConfig pathConfig;
/**
* 上传img
*
* @param file 文件
* @return {@link JSONObject}
*/
@SneakyThrows
@PostMapping("/uploadImg")
@ApiOperation("图片上传")
public R uploadImg(MultipartFile file) {
JSONObject ret = new JSONObject();
String fileName = file.getOriginalFilename();
String newFileName = StringTool.getUUID() + Objects.requireNonNull(fileName).substring(fileName.indexOf("."));
Image image = new Image();
File file1;
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("win")) {
file1 = new File(pathConfig.getWinPath(), newFileName);
} else {
file1 = new File(pathConfig.getWinPath(), newFileName);
}
if (!file1.exists()) {
System.out.println(file1.mkdir());
}
file.transferTo(file1);
image.setId(StringTool.getUUID());
image.setImgUrl(newFileName);
imageService.insert(image);
return R.ok(0,"图片上传成功",File.separator + "static" + File.separator + "img" + File.separator + image.getImgUrl());
}
}
package cn.fleamarket.controller;
import cn.fleamarket.domain.Message;
import cn.fleamarket.domain.User;
import cn.fleamarket.service.MessageService;
import cn.fleamarket.service.UserService;
import cn.fleamarket.utils.StringTool;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 留言接口
*/
@RestController
@RequestMapping("/message")
@Api("留言接口")
public class MessageController {
@Autowired
MessageService messageService;
@Autowired
UserService userService;
@PostMapping(value = "/messageList", produces = "application/json")
@ApiOperation("分页查询留言列表,入参是page:第几页,number:每页几条,pId:属于哪个商品的id")
public JSONObject messageList(@RequestBody JSONObject jsonObject) {
JSONObject ret = new JSONObject();
try {
Long page = jsonObject.getLong("page");
Long number = jsonObject.getLong("number");
String pId = jsonObject.getString("pId");
Map map = new HashMap<>();
map.put("page", page);
map.put("number", number);
map.put("pId", pId);
if (page != null && number != null) {
Page messagePage = messageService.selectListPage(map);
List messagesList = messagePage.getRecords();
ret.put("code", 0);
ret.put("data", StringTool.ListToJsonArray(messagesList));
ret.put("total", messagePage.getTotal());//总数
ret.put("next", messagePage.hasNext());//下一页
ret.put("previous", messagePage.hasPrevious());//上一页
ret.put("msg", "查询成功");
}
} catch (Exception e) {
e.printStackTrace();
ret.put("code", -1);
ret.put("data", null);
ret.put("msg", "查询失败");
}
return ret;
}
@PostMapping("/addMessage")
@ApiOperation("新增留言接口,text:留言内容,tid:发送人(不用填),fid:接受人(填商品id)")
public JSONObject addMessage(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
JSONObject ret = new JSONObject();
Message message = new Message();
try {
User user = userService.qureyByUserName(jsonObject.getString("username"));
message.setTid(user.getId());
message.setTime(new Date());
message.setId(StringTool.getUUID());
message.setFid(jsonObject.getString("fid"));
message.setText(jsonObject.getString("text"));
if (messageService.addMessage(message) > 0) {
ret.put("code", 0);
ret.put("data", true);
ret.put("msg", "新增留言成功");
} else {
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "新增留言失败");
}
} catch (Exception e) {
e.printStackTrace();
ret.put("code", -1);
ret.put("data", false);
ret.put("msg", "新增留言失败");
}
return ret;
}
@PostMapping(value = "/messageListByUser", produces = "application/json")
@ApiOperation("分页查询我的留言,入参是page:第几页,number:每页几条")
public JSONObject productListByUser(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
JSONObject ret = new JSONObject();
try {
Long page = jsonObject.getLong("page");
Long number = jsonObject.getLong("number");
Map map = new HashMap<>();
User user = userService.qureyByUserName(jsonObject.getString("username"));
map.put("page", page);
map.put("number", number);
map.put("userId", user.getId());
if (page != null && number != null) {
Page messagePage = messageService.selectListPageByUser(map);
List messageList = messagePage.getRecords();
ret.put("code", 0);
ret.put("data", StringTool.ListToJsonArray(messageList));
ret.put("total", messagePage.getTotal());//总数
ret.put("next", messagePage.hasNext());//下一页
ret.put("previous", messagePage.hasPrevious());//上一页
ret.put("msg", "查询成功");
}
} catch (Exception e) {
ret.put("code", -1);
ret.put("data", null);
ret.put("msg", "查询失败");
e.printStackTrace();
}
return ret;
}
@PostMapping("/delete")
@ApiOperation("删除留言接口,主要传留言id即可")
public JSONObject delete(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
JSONObject ret = new JSONObject();
String pId = jsonObject.getString("id");
User user = null;
try {
user = userService.qureyByUserName(jsonObject.getString("username"));
} catch (Exception e) {
e.printStackTrace();
}
try {
int i = messageService.delete(user.getId(), pId);
if (i > 0) {
ret.put("code", "0");
ret.put("data", true);
ret.put("msg", "删除留言成功");
} else {
ret.put("code", "-1");
ret.put("data", false) ;
ret.put("msg", "删除留言失败");
}
} catch (Exception e) {
ret.put("code", "-1");
ret.put("data", false);
ret.put("msg", "删除留言失败");
e.printStackTrace();
}
return ret;
}
}
package cn.fleamarket.controller;
import java.util.*;
import cn.fleamarket.domain.Product;
import cn.fleamarket.domain.User;
import cn.fleamarket.service.MessageService;
import cn.fleamarket.service.ProductService;
import cn.fleamarket.service.UserService;
import cn.fleamarket.utils.StringTool;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
/**
* 商品表
*
* @author znz
* @email ${email}
* @date 2022-09-12 10:46:22
*/
@RestController
@RequestMapping("/product")
@Api("商品接口")
public class ProductController {
@Autowired
ProductService productService;
@Autowired
UserService userService;
@Autowired
MessageService messageService;
@PostMapping(value = "/productList", produces = "application/json")
@ApiOperation("分页查询列表,入参是page:第几页,number:每页几条,key:查询条件(可选)")
public JSONObject productList(@RequestBody JSONObject jsonObject) {
JSONObject ret = new JSONObject();
try {
Long page = jsonObject.getLong("page");
Long number = jsonObject.getLong("number");
String key = jsonObject.getString("key");
JSONArray jsonArray = jsonObject.getJSONArray("pId");
Map map = new HashMap<>();
map.put("page", page);
map.put("number", number);
map.put("key", key);
if (page != null && number != null) {
Page productPage = productService.selectListPage(map);
List productList = productPage.getRecords();
ret.put("code", 0);
ret.put("data", StringTool.ListToJsonArray(productList));
ret.put("total", productPage.getTotal());//总数
ret.put("next", productPage.hasNext());//下一页
ret.put("previous", productPage.hasPrevious());//上一页
ret.put("msg", "查询成功");
return ret;
}
if (jsonArray.size() != 0 && jsonArray != null) {
List list = new ArrayList<>();
for (int i = 0; i < jsonArray.size(); i++) {
Product product = productService.selectById(jsonArray.get(i).toString());
list.add(product);
}
ret.put("code", 0);
ret.put("data", StringTool.ListToJsonArray(list));
ret.put("msg", "查询成功");
return ret;
}
} catch (Exception e) {
ret.put("code", -1);
ret.put("data", null);
ret.put("msg", "查询失败");
}
return ret;
}
@PostMapping(value = "/productListByUser", produces = "application/json")
@ApiOperation("分页查询属于某个用户的商品列表,就是我发布的商品,入参是page:第几页,number:每页几条")
public JSONObject productListByUser(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
JSONObject ret = new JSONObject();
try {
Long page = jsonObject.getLong("page");
Long number = jsonObject.getLong("number");
Map map = new HashMap<>();
User user = userService.qureyByUserName(jsonObject.getString("username"));
map.put("page", page);
map.put("number", number);
map.put("userId", user.getId());
if (page != null && number != null) {
Page productPage = productService.selectListPageByUser(map);
List productList = productPage.getRecords();
ret.put("code", 0);
ret.put("data", StringTool.ListToJsonArray(productList));
ret.put("total", productPage.getTotal());//总数
ret.put("next", productPage.hasNext());//下一页
ret.put("previous", productPage.hasPrevious());//上一页
ret.put("msg", "查询成功");
}
} catch (Exception e) {
ret.put("code", -1);
ret.put("data", null);
ret.put("msg", "查询失败");
}
return ret;
}
@PostMapping(value = "/productListById", produces = "application/json")
@ApiOperation("分页查询属于某个用户的商品列表,就是我发布的商品,入参是page:第几页,number:每页几条")
public JSONObject productListById(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
JSONObject ret = new JSONObject();
try {
Long page = jsonObject.getLong("page");
Long number = jsonObject.getLong("number");
Object[] pIds = jsonObject.getJSONArray("pId").toArray();
List