项目编号:BS-PT-066
语言环境:Java:jdk8
数据库:Mysql:mysql7REDIS缓存数据库
应用服务器:Tomcat:tomcat31
开发工具:IDEA或eclipse
后台开发技术:springboot+mybatisplus+jdk8+mysql6
前后台开发技术:VUE+ElementsUI
系统展示
用户登陆注册
发布二手物品
查看详情
查看我发布的商品:可以进行商品上下架和删除操作
管理我发布的留言
核心代码展示
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;
}
}
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<String, Object> map = new HashMap<>();
map.put("page", page);
map.put("number", number);
map.put("key", key);
if (page != null && number != null) {
Page<Product> productPage = productService.selectListPage(map);
List<Product> 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<Product> 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<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<Product> productPage = productService.selectListPageByUser(map);
List<Product> 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<Object> list = new ArrayList<Object>();
for (int i = 0; i < pIds.length; i++) {
if (!list.contains(pIds[i])) {
list.add(pIds[i]);
}
}
Object[] pIdss = list.toArray();
Map<String, Object> map = new HashMap<>();
map.put("page", page);
map.put("number", number);
map.put("pId", pIdss);
if (page != null && number != null && pIds != null) {
Page<Product> productPage = productService.selectListsPageById(map);
List<Product> 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 = "/productListByIds", produces = "application/json")
@ApiOperation("分页查询属于某个用户的商品列表,就是我发布的商品,入参是page:第几页,number:每页几条")
public JSONObject productListByIds(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
JSONObject ret = new JSONObject();
try {
Object[] pIds = jsonObject.getJSONArray("pId").toArray();
List<Product> productList = new ArrayList<>();
for (int i = 0; i < pIds.length; i++) {
Product product = productService.selectById(pIds[i].toString());
productList.add(product);
}
ret.put("code", 0);
ret.put("data", StringTool.ListToJsonArray(productList));
ret.put("msg", "查询成功");
} catch (Exception e) {
ret.put("code", -1);
ret.put("data", null);
ret.put("msg", "查询失败");
}
return ret;
}
@PutMapping("/addProduct")
@ApiOperation("增加商品,入参是要增加的商品信息,记得带上上传图片接口返回的url")
public JSONObject addProduct(@RequestBody JSONObject par, HttpServletRequest request) {
JSONObject ret = new JSONObject();
try {
Product product = new Product();
product.setId(StringTool.getUUID());
product.setCreateTime(new Date());
product.setBprice(par.getDouble("bprice"));
product.setTitle(par.getString("title"));
product.setImgUrl(par.getString("imgUrl"));
product.setPrice(par.getDouble("price"));
product.setContent(par.getString("content"));
User user = userService.qureyByUserName(par.getString("username"));
product.setUserId(user.getId());
int i = productService.insert(product);
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", "增加失败");
}
return ret;
}
@PutMapping("/update")
@ApiOperation("修改商品信息,传入要修改的信息,主要是商品id")
public JSONObject update(Product product) {
JSONObject ret = new JSONObject();
product.setCreateTime(new Date());
try {
if (product.getId() != null && productService.update(product) > 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;
}
@PutMapping("/obtained")
@ApiOperation("上架下架接口,主要传id即可")
public JSONObject Obtained(@RequestBody JSONObject id) {
JSONObject ret = new JSONObject();
try {
String dbId = id.getString("id");
Product product = productService.selectById(dbId);
if (product != null && product.getIsShow() == 1) {
productService.updateById(dbId, true);
ret.put("code", "0");
ret.put("data", true);
ret.put("msg", "下架成功");
} else if (product != null && product.getIsShow() == 0) {
productService.updateById(dbId, false);
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;
}
@GetMapping("/selectById")
@ApiOperation("查询商品详情,传id即可,id是个字符串")
public JSONObject selectById(String id) {
JSONObject ret = new JSONObject();
try {
Product product = productService.selectById(id);
if (product != null) {
ret.put("code", "0");
ret.put("data", StringTool.ObjectToJSONObject(product));
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;
}
@PutMapping("/addCount")
@ApiOperation("增加浏览次数接口,主要传id即可,当用户打开商品页的时候发这个请求")
public JSONObject Increase(@RequestBody JSONObject id) {
JSONObject ret = new JSONObject();
try {
String dbId = id.getString("id");
Product product = productService.selectById(dbId);
if (product != null) {
productService.updateIncrease(product);
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;
}
@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 = productService.delete(user.getId(), pId);
if (i > 0) {
if(messageService.deletebyFid(pId)>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;
}
}
项目总结
文章为作者独立观点,不代表 股票程序化软件自动交易接口观点