项目编号: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<List<Favorites>> favoritesList(@RequestBody Map<String,Object> 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<Favorites> favoritesPage = favoritesService.selectListPage(params); List<Favorites> 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<Favorites> 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 <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { Map<Object, Boolean> seen = new ConcurrentHashMap<>(); return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; } }
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<String> 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<String, Object> map = new HashMap<>(); map.put("page", page); map.put("number", number); map.put("pId", pId); if (page != null && number != null) { Page<Message> messagePage = messageService.selectListPage(map); List<Message> 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<String, Object> 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<Message> messagePage = messageService.selectListPageByUser(map); List<Message> 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; } }