作者主页:夜未央5788
简介:Java领域优质创作者、Java项目、学习资料、技术互助
文末获取源码
项目介绍
本次设计的是一个校园二手交易平台(C2C),C2C指个人与个人之间的电子商务,买家可以查看所有卖家发布的商品,并且根据分类进行商品过滤,也可以根据站内搜索引擎进行商品的查询,并且与卖家联系,达成交易的意向,也可以发布求购的信息,让卖家查看之后,与之联系,进行交易。而此次主要是针对校园用户所设计的网站,对于数据的分类应该更多的考虑校园用户的需求,例如二手书籍、二手数码等的分类应该更加细致。由于是C2C的项目,因此本项目无需后台管理。
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
5.是否Maven项目: 是;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目
6.数据库:MySql 5.7版本;
技术栈
1. 后端:Spring SpringMVC MyBatis
2. 前端:Thymeleaf+Html+jQuery
使用说明
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,配置tomcat,然后运行;
3. 将项目中jdbc.properties配置文件中的数据库配置改为自己的配置;
4. 运行项目,输入localhost:8080/xxx 登录
运行截图
相关代码
HomeController
package com.wsk.controller;
import com.wsk.bean.ShopInformationBean;
import com.wsk.pojo.*;
import com.wsk.service.*;
import com.wsk.tool.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by wsk1103 on 2017/5/11.
*/
@Controller
public class HomeController {
@Resource
private ShopInformationService shopInformationService;
@Resource
private SpecificeService specificeService;
@Resource
private ClassificationService classificationService;
@Resource
private AllKindsService allKindsService;
@Resource
private ShopContextService shopContextService;
@RequestMapping(value = {"/", "/home.do"})
public String home(HttpServletRequest request, Model model) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
// if user login,the session will have the "userInformation"
if (!StringUtils.getInstance().isNullOrEmpty(userInformation)) {
model.addAttribute("userInformation", userInformation);
} else {
userInformation = new UserInformation();
model.addAttribute("userInformation", userInformation);
}
//一般形式进入首页
try {
List<ShopInformation> shopInformations = selectTen(1, 5);
List<ShopInformationBean> list = new ArrayList<>();
int counts = getShopCounts();
model.addAttribute("shopInformationCounts", counts);
String stringBuffer;
for (ShopInformation shopInformation : shopInformations) {
stringBuffer = getSortName(shopInformation.getSort());
ShopInformationBean shopInformationBean = new ShopInformationBean();
shopInformationBean.setId(shopInformation.getId());
shopInformationBean.setName(shopInformation.getName());
shopInformationBean.setLevel(shopInformation.getLevel());
shopInformationBean.setPrice(shopInformation.getPrice().doubleValue());
shopInformationBean.setRemark(shopInformation.getRemark());
shopInformationBean.setSort(stringBuffer);
shopInformationBean.setQuantity(shopInformation.getQuantity());
shopInformationBean.setUid(shopInformation.getUid());
shopInformationBean.setTransaction(shopInformation.getTransaction());
shopInformationBean.setImage(shopInformation.getImage());
list.add(shopInformationBean);
}
model.addAttribute("shopInformationBean", list);
} catch (Exception e) {
e.printStackTrace();
return "page/login_page";
}
return "index";
}
//进入商城
@RequestMapping(value = "/mall_page.do")
public String mallPage(HttpServletRequest request, Model model) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
userInformation = new UserInformation();
model.addAttribute("userInformation", userInformation);
} else {
model.addAttribute("userInformation", userInformation);
}
try {
List<ShopInformation> shopInformations = selectTen(1, 12);
List<ShopInformationBean> list = new ArrayList<>();
int counts = getShopCounts();
model.addAttribute("shopInformationCounts", counts);
String sortName;
for (ShopInformation shopInformation : shopInformations) {
int sort = shopInformation.getSort();
sortName = getSortName(sort);
ShopInformationBean shopInformationBean = new ShopInformationBean();
shopInformationBean.setId(shopInformation.getId());
shopInformationBean.setName(shopInformation.getName());
shopInformationBean.setLevel(shopInformation.getLevel());
shopInformationBean.setRemark(shopInformation.getRemark());
shopInformationBean.setPrice(shopInformation.getPrice().doubleValue());
shopInformationBean.setSort(sortName);
shopInformationBean.setQuantity(shopInformation.getQuantity());
shopInformationBean.setTransaction(shopInformation.getTransaction());
shopInformationBean.setUid(shopInformation.getUid());
shopInformationBean.setImage(shopInformation.getImage());
list.add(shopInformationBean);
}
model.addAttribute("shopInformationBean", list);
} catch (Exception e) {
e.printStackTrace();
return "page/login_page";
}
return "page/mall_page";
}
//通过分类的第三层id获取全名
private String getSortName(int sort) {
StringBuilder stringBuffer = new StringBuilder();
Specific specific = selectSpecificBySort(sort);
int cid = specific.getCid();
Classification classification = selectClassificationByCid(cid);
int aid = classification.getAid();
AllKinds allKinds = selectAllKindsByAid(aid);
stringBuffer.append(allKinds.getName());
stringBuffer.append("-");
stringBuffer.append(classification.getName());
stringBuffer.append("-");
stringBuffer.append(specific.getName());
// System.out.println(sort);
return stringBuffer.toString();
}
//获得分类中的第一层
@RequestMapping(value = "/getAllKinds.do")
@ResponseBody
public List<AllKinds> getAllKind() {
return getAllKinds();
}
//获得分类中的第二层,通过第一层的id
@RequestMapping(value = "/getClassification.do", method = RequestMethod.POST)
@ResponseBody
public List<Classification> getClassificationByAid(@RequestParam int id) {
return selectAllClassification(id);
}
//通过第二层的id获取对应的第三层
@RequestMapping(value = "/getSpecific.do")
@ResponseBody
public List<Specific> getSpecificByCid(@RequestParam int id) {
return selectAllSpecific(id);
}
//get the shops counts
@RequestMapping(value = "/getShopsCounts.do")
@ResponseBody
public Map getShopsCounts() {
Map<String, Integer> map = new HashMap<>();
int counts = 0;
try {
counts = shopInformationService.getCounts();
} catch (Exception e) {
e.printStackTrace();
map.put("counts", counts);
return map;
}
map.put("counts", counts);
return map;
}
@RequestMapping(value = "/getShops.do")
@ResponseBody
public List getShops(@RequestParam int start) {
List<ShopInformation> list = new ArrayList<>();
try {
int end = 12;
list = selectTen(start, end);
} catch (Exception e) {
e.printStackTrace();
return list;
}
return list;
}
//获取商品,分页,一次性获取end个
private List<ShopInformation> selectTen(int start, int end) {
Map map = new HashMap();
map.put("start", (start - 1) * end);
map.put("end", end);
List<ShopInformation> list = shopInformationService.selectTen(map);
return list;
}
//获取最详细的分类,第三层
private Specific selectSpecificBySort(int sort) {
return specificeService.selectByPrimaryKey(sort);
}
//获得第二层分类
private Classification selectClassificationByCid(int cid) {
return classificationService.selectByPrimaryKey(cid);
}
//获得第一层分类
private AllKinds selectAllKindsByAid(int aid) {
return allKindsService.selectByPrimaryKey(aid);
}
//获得第一层所有
private List<AllKinds> getAllKinds() {
return allKindsService.selectAll();
}
//根据第一层的id获取该层下的第二层
private List<Classification> selectAllClassification(int aid) {
return classificationService.selectByAid(aid);
}
//根据第二层的id获取其对应的第三层所有id
private List<Specific> selectAllSpecific(int cid) {
return specificeService.selectByCid(cid);
}
//获得商品总页数
private int getShopCounts() {
return shopInformationService.getCounts();
}
//获得商品留言总页数
private int getShopContextCounts(int sid) {
return shopContextService.getCounts(sid);
}
//获得商品留言,10条
private List<ShopContext> selectShopContextBySid(int sid, int start) {
return shopContextService.findById(sid, (start - 1) * 10);
}
}
UserController
package com.wsk.controller;
import com.wsk.bean.GoodsCarBean;
import com.wsk.bean.ShopInformationBean;
import com.wsk.bean.UserWantBean;
import com.wsk.pojo.*;
import com.wsk.response.BaseResponse;
import com.wsk.service.*;
import com.wsk.token.TokenProccessor;
import com.wsk.tool.SaveSession;
import com.wsk.tool.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.math.BigDecimal;
import java.util.*;
/*import com.wsk.tool.OCR;
import com.wsk.tool.Pornographic;*/
/**
* Created by wsk1103 on 2017/5/9.
*/
@Controller
@Slf4j
public class UserController {
@Resource
private UserInformationService userInformationService;
@Resource
private UserPasswordService userPasswordService;
@Resource
private UserCollectionService userCollectionService;
@Resource
private UserReleaseService userReleaseService;
@Resource
private BoughtShopService boughtShopService;
@Resource
private UserWantService userWantService;
@Resource
private ShopCarService shopCarService;
@Resource
private OrderFormService orderFormService;
@Resource
private GoodsOfOrderFormService goodsOfOrderFormService;
@Resource
private UserStateService userStateService;
@Resource
private ShopInformationService shopInformationService;
@Resource
private GoodsCarService goodsCarService;
@Resource
private SpecificeService specificeService;
@Resource
private ClassificationService classificationService;
@Resource
private AllKindsService allKindsService;
@Resource
private ShopContextService shopContextService;
//进入登录界面
@RequestMapping(value = "/login.do", method = RequestMethod.GET)
public String login(HttpServletRequest request, Model model) {
String token = TokenProccessor.getInstance().makeToken();
log.info("进入登录界面,token为:" + token);
request.getSession().setAttribute("token", token);
model.addAttribute("token", token);
return "page/login_page";
}
//退出
@RequestMapping(value = "/logout.do")
public String logout(HttpServletRequest request) {
try {
request.getSession().removeAttribute("userInformation");
request.getSession().removeAttribute("uid");
System.out.println("logout");
} catch (Exception e) {
e.printStackTrace();
return "redirect:/home.do";
}
return "redirect:/";
}
//用户注册,拥有插入数据而已,没什么用的
@RequestMapping(value = "/registered.do", method = RequestMethod.POST)
public String registered(Model model,
@RequestParam String name, @RequestParam String phone, @RequestParam String password) {
UserInformation userInformation = new UserInformation();
userInformation.setUsername(name);
userInformation.setPhone(phone);
userInformation.setModified(new Date());
userInformation.setCreatetime(new Date());
if (userInformationService.insertSelective(userInformation) == 1) {
int uid = userInformationService.selectIdByPhone(phone);
UserPassword userPassword = new UserPassword();
userPassword.setModified(new Date());
password = StringUtils.getInstance().getMD5(password);
userPassword.setPassword(password);
userPassword.setUid(uid);
int result = userPasswordService.insertSelective(userPassword);
if (result != 1) {
model.addAttribute("result", "fail");
return "success";
}
model.addAttribute("result", "success");
return "success";
}
model.addAttribute("result", "fail");
return "success";
}
//用户注册
// @RequestMapping(value = "/registered", method = RequestMethod.GET)
// public String registered() {
// return "registered";
// }
//验证登录
@RequestMapping(value = "/login.do", method = RequestMethod.POST)
public String login(HttpServletRequest request,
@RequestParam String phone, @RequestParam String password, @RequestParam String token) {
String loginToken = (String) request.getSession().getAttribute("token");
if (StringUtils.getInstance().isNullOrEmpty(phone) || StringUtils.getInstance().isNullOrEmpty(password)) {
return "redirect:/login.do";
}
//防止重复提交
if (StringUtils.getInstance().isNullOrEmpty(token) || !token.equals(loginToken)) {
return "redirect:/login.do";
}
boolean b = getId(phone, password, request);
//失败,不存在该手机号码
if (!b) {
return "redirect:/login.do?msg=不存在该手机号码";
}
return "redirect:/";
}
//查看用户基本信息
@RequestMapping(value = "/personal_info.do")
public String personalInfo(HttpServletRequest request, Model model) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
return "redirect:/login.do";
}
String personalInfoToken = TokenProccessor.getInstance().makeToken();
request.getSession().setAttribute("personalInfoToken", personalInfoToken);
model.addAttribute("token", personalInfoToken);
model.addAttribute("userInformation", userInformation);
return "page/personal/personal_info";
}
//完善用户基本信息,认证
@RequestMapping(value = "/certification.do", method = RequestMethod.POST)
@ResponseBody
public Map certification(HttpServletRequest request,
@RequestParam(required = false) String userName,
@RequestParam(required = false) String realName,
@RequestParam(required = false) String clazz, @RequestParam String token,
@RequestParam(required = false) String sno, @RequestParam(required = false) String dormitory,
@RequestParam(required = false) String gender) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
Map<String, Integer> map = new HashMap<>();
map.put("result", 0);
//该用户还没有登录
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
return map;
}
String certificationToken = (String) request.getSession().getAttribute("personalInfoToken");
//防止重复提交
// boolean b = token.equals(certificationToken);
if (StringUtils.getInstance().isNullOrEmpty(certificationToken)) {
return map;
} else {
request.getSession().removeAttribute("certificationToken");
}
if (userName != null && userName.length() < 25) {
userName = StringUtils.getInstance().replaceBlank(userName);
userInformation.setUsername(userName);
} else if (userName != null && userName.length() >= 25) {
return map;
}
if (realName != null && realName.length() < 25) {
realName = StringUtils.getInstance().replaceBlank(realName);
userInformation.setRealname(realName);
} else if (realName != null && realName.length() >= 25) {
return map;
}
if (clazz != null && clazz.length() < 25) {
clazz = StringUtils.getInstance().replaceBlank(clazz);
userInformation.setClazz(clazz);
} else if (clazz != null && clazz.length() >= 25) {
return map;
}
if (sno != null && sno.length() < 25) {
sno = StringUtils.getInstance().replaceBlank(sno);
userInformation.setSno(sno);
} else if (sno != null && sno.length() >= 25) {
return map;
}
if (dormitory != null && dormitory.length() < 25) {
dormitory = StringUtils.getInstance().replaceBlank(dormitory);
userInformation.setDormitory(dormitory);
} else if (dormitory != null && dormitory.length() >= 25) {
return map;
}
if (gender != null && gender.length() <= 2) {
gender = StringUtils.getInstance().replaceBlank(gender);
userInformation.setGender(gender);
} else if (gender != null && gender.length() > 2) {
return map;
}
int result = userInformationService.updateByPrimaryKeySelective(userInformation);
if (result != 1) {
//更新失败,认证失败
return map;
}
//认证成功
request.getSession().setAttribute("userInformation", userInformation);
map.put("result", 1);
return map;
}
//enter the publishUserWant.do.html,进入求购页面
@RequestMapping(value = "/require_product.do")
public String enterPublishUserWant(HttpServletRequest request, Model model) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
return "redirect:/login.do";
}
String error = request.getParameter("error");
if (!StringUtils.getInstance().isNullOrEmpty(error)) {
model.addAttribute("error", "error");
}
String publishUserWantToken = TokenProccessor.getInstance().makeToken();
request.getSession().setAttribute("publishUserWantToken", publishUserWantToken);
model.addAttribute("token", publishUserWantToken);
model.addAttribute("userInformation", userInformation);
return "page/require_product";
}
//修改求购商品
@RequestMapping(value = "/modified_require_product.do")
public String modifiedRequireProduct(HttpServletRequest request, Model model,
@RequestParam int id) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
return "redirect:/login.do";
}
String publishUserWantToken = TokenProccessor.getInstance().makeToken();
request.getSession().setAttribute("publishUserWantToken", publishUserWantToken);
model.addAttribute("token", publishUserWantToken);
model.addAttribute("userInformation", userInformation);
UserWant userWant = userWantService.selectByPrimaryKey(id);
model.addAttribute("userWant", userWant);
String sort = getSort(userWant.getSort());
model.addAttribute("sort", sort);
return "page/modified_require_product";
}
//publish userWant,发布求购
@RequestMapping(value = "/publishUserWant.do")
// @ResponseBody
public String publishUserWant(HttpServletRequest request, Model model,
@RequestParam String name,
@RequestParam int sort, @RequestParam int quantity,
@RequestParam double price, @RequestParam String remark,
@RequestParam String token) {
// Map<String, Integer> map = new HashMap<>();
//determine whether the user exits
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
//if the user no exits in the session,
// map.put("result", 2);
return "redirect:/login.do";
}
String publishUserWantToke = (String) request.getSession().getAttribute("publishUserWantToken");
if (StringUtils.getInstance().isNullOrEmpty(publishUserWantToke) || !publishUserWantToke.equals(token)) {
// map.put("result", 2);
return "redirect:require_product.do?error=3";
} else {
request.getSession().removeAttribute("publishUserWantToken");
}
// name = StringUtils.replaceBlank(name);
// remark = StringUtils.replaceBlank(remark);
// name = StringUtils.getInstance().txtReplace(name);
// remark = StringUtils.getInstance().txtReplace(remark);
try {
if (name.length() < 1 || remark.length() < 1 || name.length() > 25 || remark.length() > 25) {
return "redirect:require_product.do";
}
} catch (Exception e) {
e.printStackTrace();
return "redirect:require_product.do?error=1";
}
UserWant userWant = new UserWant();
userWant.setModified(new Date());
userWant.setName(name);
userWant.setPrice(new BigDecimal(price));
userWant.setQuantity(quantity);
userWant.setRemark(remark);
userWant.setUid((Integer) request.getSession().getAttribute("uid"));
userWant.setSort(sort);
int result;
try {
result = userWantService.insertSelective(userWant);
if (result != 1) {
// map.put("result", result);
return "redirect:/require_product.do?error=2";
}
} catch (Exception e) {
e.printStackTrace();
// map.put("result", result);
return "redirect:/require_product.do?error=2";
}
// map.put("result", result);
return "redirect:/my_require_product.do";
}
//getUserWant,查看我的求购
@RequestMapping(value = {"/my_require_product.do", "/my_require_product_page.do"})
public String getUserWant(HttpServletRequest request, Model model) {
List<UserWant> list;
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
return "redirect:/login.do";
}
try {
int uid = (int) request.getSession().getAttribute("uid");
// list = selectUserWantByUid(4);
list = selectUserWantByUid(uid);
List<UserWantBean> userWantBeans = new ArrayList<>();
for (UserWant userWant : list) {
UserWantBean userWantBean = new UserWantBean();
userWantBean.setId(userWant.getId());
userWantBean.setModified(userWant.getModified());
userWantBean.setName(userWant.getName());
userWantBean.setPrice(userWant.getPrice().doubleValue());
userWantBean.setUid(uid);
userWantBean.setQuantity(userWant.getQuantity());
userWantBean.setRemark(userWant.getRemark());
userWantBean.setSort(getSort(userWant.getSort()));
userWantBeans.add(userWantBean);
}
model.addAttribute("userWant", userWantBeans);
} catch (Exception e) {
e.printStackTrace();
return "redirect:/";
}
model.addAttribute("userInformation", userInformation);
return "page/personal/my_require_product_page";
}
//getUserWantCounts.do,查看求购总数
@RequestMapping(value = "/getUserWantCounts.do")
@ResponseBody
public Map getUserWantCounts(HttpServletRequest request, Model model) {
Map<String, Integer> map = new HashMap<>();
if (StringUtils.getInstance().isNullOrEmpty(request.getSession().getAttribute("userInformation"))) {
map.put("counts", -1);
return map;
}
try {
int counts = getUserWantCounts((Integer) request.getSession().getAttribute("uid"));
map.put("counts", counts);
} catch (Exception e) {
e.printStackTrace();
map.put("counts", -1);
}
return map;
}
//删除求购
@RequestMapping(value = "/deleteUserWant.do")
public String deleteUserWant(HttpServletRequest request, @RequestParam int id) {
// Map<String, Integer> map = new HashMap<>();
if (StringUtils.getInstance().isNullOrEmpty(request.getSession().getAttribute("userInformation"))) {
return "redirect:/login.do";
}
UserWant userWant = new UserWant();
userWant.setId(id);
userWant.setDisplay(0);
try {
int result = userWantService.updateByPrimaryKeySelective(userWant);
if (result != 1) {
return "redirect:my_require_product.do";
}
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:my_require_product.do";
}
//收藏
//add the userCollection
@RequestMapping(value = "/addUserCollection.do")
@ResponseBody
public BaseResponse addUserCollection(HttpServletRequest request, @RequestParam int sid) {
//determine whether the user exits
if (StringUtils.getInstance().isNullOrEmpty(request.getSession().getAttribute("userInformation"))) {
//if the user no exits in the session,
return BaseResponse.fail();
}
UserCollection userCollection = new UserCollection();
userCollection.setModified(new Date());
userCollection.setSid(sid);
userCollection.setUid((Integer) request.getSession().getAttribute("uid"));
//begin insert the userCollection
int result = userCollectionService.insertSelective(userCollection);
if (result != 1) {
return BaseResponse.fail();
}
return BaseResponse.success();
}
// delete the userCollection
@RequestMapping(value = "/deleteUserCollection.do")
@ResponseBody
public BaseResponse deleteUserCollection(HttpServletRequest request, @RequestParam int ucid) {
if (StringUtils.getInstance().isNullOrEmpty(request.getSession().getAttribute("userInformation"))) {
return BaseResponse.fail();
}
UserCollection userCollection = new UserCollection();
// userCollection.setUid((Integer) request.getSession().getAttribute("uid"));
// userCollection.setSid(sid);
userCollection.setId(ucid);
userCollection.setModified(new Date());
userCollection.setDisplay(0);
int result;
result = userCollectionService.updateByPrimaryKeySelective(userCollection);
if (result != 1) {
return BaseResponse.fail();
}
return BaseResponse.success();
}
//购物车开始。。。。。。。。。。。
//getShopCarCounts.do
@RequestMapping(value = "/getShopCarCounts.do")
@ResponseBody
public BaseResponse getShopCarCounts(HttpServletRequest request) {
if (StringUtils.getInstance().isNullOrEmpty(request.getSession().getAttribute("userInformation"))) {
return BaseResponse.fail();
}
int uid = (int) request.getSession().getAttribute("uid");
int counts = getShopCarCounts(uid);
return BaseResponse.success();
}
//check the shopping cart,查看购物车
@RequestMapping(value = "/shopping_cart.do")
public String selectShopCar(HttpServletRequest request, Model model) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
userInformation = new UserInformation();
model.addAttribute("userInformation", userInformation);
// list.add(shopCar);
return "redirect:/login.do";
} else {
model.addAttribute("userInformation", userInformation);
}
int uid = userInformation.getId();
List<GoodsCar> goodsCars = goodsCarService.selectByUid(uid);
List<GoodsCarBean> goodsCarBeans = new ArrayList<>();
for (GoodsCar goodsCar : goodsCars) {
GoodsCarBean goodsCarBean = new GoodsCarBean();
goodsCarBean.setUid(goodsCar.getUid());
goodsCarBean.setSid(goodsCar.getSid());
goodsCarBean.setModified(goodsCar.getModified());
goodsCarBean.setId(goodsCar.getId());
goodsCarBean.setQuantity(goodsCar.getQuantity());
ShopInformation shopInformation = shopInformationService.selectByPrimaryKey(goodsCar.getSid());
goodsCarBean.setName(shopInformation.getName());
goodsCarBean.setRemark(shopInformation.getRemark());
goodsCarBean.setImage(shopInformation.getImage());
goodsCarBean.setPrice(shopInformation.getPrice().doubleValue());
goodsCarBean.setSort(getSort(shopInformation.getSort()));
goodsCarBeans.add(goodsCarBean);
}
model.addAttribute("list", goodsCarBeans);
return "page/shopping_cart";
}
// //通过购物车的id获取购物车里面的商品
// @RequestMapping(value = "/selectGoodsOfShopCar")
// @ResponseBody
// public List<GoodsCar> selectGoodsCar(HttpServletRequest request) {
// List<GoodsCar> list = new ArrayList<>();
// GoodsCar goodsCar = new GoodsCar();
// if (Empty.isNullOrEmpty(request.getSession().getAttribute("userInformation"))) {
// list.add(goodsCar);
// return list;
// }
// try {
// int scid = shopCarService.selectByUid((Integer) request.getSession().getAttribute("uid")).getId();
// list = goodsCarService.selectByUid(scid);
// return list;
// } catch (Exception e) {
// e.printStackTrace();
// return list;
// }
// }
//添加到购物车
@RequestMapping(value = "/insertGoodsCar.do")
@ResponseBody
public BaseResponse insertGoodsCar(HttpServletRequest request, @RequestParam int id) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
return BaseResponse.fail();
}
int uid = userInformation.getId();
GoodsCar goodsCar = new GoodsCar();
goodsCar.setDisplay(1);
goodsCar.setModified(new Date());
goodsCar.setQuantity(1);
goodsCar.setUid(uid);
goodsCar.setSid(id);
int result = goodsCarService.insertSelective(goodsCar);
return BaseResponse.success();
}
//删除购物车的商品
@RequestMapping(value = "/deleteShopCar.do")
@ResponseBody
public BaseResponse deleteShopCar(HttpServletRequest request, @RequestParam int id, @RequestParam int sid) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
return BaseResponse.fail();
}
int uid = userInformation.getId();
GoodsCar goodsCar = new GoodsCar();
goodsCar.setDisplay(0);
goodsCar.setId(id);
goodsCar.setSid(sid);
goodsCar.setUid(uid);
int result = goodsCarService.updateByPrimaryKeySelective(goodsCar);
if (result != 1) {
return BaseResponse.fail();
}
return BaseResponse.success();
}
//发布商品
@RequestMapping(value = "/insertGoods.do", method = RequestMethod.POST)
public String insertGoods(@RequestParam String name, @RequestParam int level,
@RequestParam String remark, @RequestParam double price,
@RequestParam int sort, @RequestParam int quantity,
@RequestParam String token, @RequestParam(required = false) MultipartFile image,
@RequestParam int action, @RequestParam(required = false) int id,
HttpServletRequest request, Model model) {
String goodsToken = (String) request.getSession().getAttribute("goodsToken");
// String publishProductToken = TokenProccessor.getInstance().makeToken();
// request.getSession().setAttribute("token",publishProductToken);
//防止重复提交
if (StringUtils.getInstance().isNullOrEmpty(goodsToken) || !goodsToken.equals(token)) {
return "redirect:publish_product.do?error=1";
} else {
request.getSession().removeAttribute("goodsToken");
}
// //从session中获得用户的基本信息
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
model.addAttribute("userInformation", userInformation);
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
//如果用户不存在,
return "redirect:/login.do";
}
name = StringUtils.getInstance().replaceBlank(name);
remark = StringUtils.getInstance().replaceBlank(remark);
//judge the data`s format
if (StringUtils.getInstance().isNullOrEmpty(name) || StringUtils.getInstance().isNullOrEmpty(level) || StringUtils.getInstance().isNullOrEmpty(remark) || StringUtils.getInstance().isNullOrEmpty(price)
|| StringUtils.getInstance().isNullOrEmpty(sort) || StringUtils.getInstance().isNullOrEmpty(quantity) || name.length() > 25 || remark.length() > 122) {
model.addAttribute("message", "请输入正确的格式!!!!!");
model.addAttribute("token", goodsToken);
request.getSession().setAttribute("goodsToken", goodsToken);
return "page/publish_product";
}
//插入
if (action == 1) {
if (StringUtils.getInstance().isNullOrEmpty(image)) {
model.addAttribute("message", "请选择图片!!!");
model.addAttribute("token", goodsToken);
request.getSession().setAttribute("goodsToken", goodsToken);
return "redirect:publish_product.do?error=请插入图片";
}
String random;
String path = "D:\\", save = "";
random = "image\\" + StringUtils.getInstance().getRandomChar() + System.currentTimeMillis() + ".jpg";
StringBuilder thumbnails = new StringBuilder();
thumbnails.append(path);
thumbnails.append("image/thumbnails/");
StringBuilder wsk = new StringBuilder();
wsk.append(StringUtils.getInstance().getRandomChar()).append(System.currentTimeMillis()).append(".jpg");
thumbnails.append(wsk);
// String fileName = "\\" + random + ".jpg";
File file = new File(path, random);
if (!file.exists()) {
file.mkdir();
}
try {
image.transferTo(file);
} catch (Exception e) {
e.printStackTrace();
}
/* String pornograp = Pornographic.CheckPornograp("D:\\" + random);
if (pornograp.equals("色情图片")) {
return "redirect:publish_product?error=不能使用色情图片";
}
if (!OCR.isOk2(pornograp)) {
return "redirect:publish_product?error=图片不能含有敏感文字";
}*/
//创建缩略图文件夹
File thumbnailsFile = new File(thumbnails.toString());
if (!thumbnailsFile.exists()) {
thumbnailsFile.mkdir();
}
if (StringUtils.getInstance().thumbnails(path + random, thumbnails.toString())) {
save = "/images/thumbnails/" + wsk.toString();
} else {
return "redirect:publish_product.do?error=生成缩略图失败";
}
//begin insert the shopInformation to the MySQL
ShopInformation shopInformation = new ShopInformation();
shopInformation.setName(name);
shopInformation.setLevel(level);
shopInformation.setRemark(remark);
shopInformation.setPrice(new BigDecimal(price));
shopInformation.setSort(sort);
shopInformation.setQuantity(quantity);
shopInformation.setModified(new Date());
shopInformation.setImage(random);//This is the other uniquely identifies
shopInformation.setThumbnails(save);
// shopInformation.setUid(4);
int uid = (int) request.getSession().getAttribute("uid");
shopInformation.setUid(uid);
try {
int result = shopInformationService.insertSelective(shopInformation);
//插入失败???
if (result != 1) {
model.addAttribute("message", "请输入正确的格式!!!!!");
model.addAttribute("token", goodsToken);
request.getSession().setAttribute("goodsToken", goodsToken);
return "page/publish_product";
}
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("token", goodsToken);
model.addAttribute("message", "请输入正确的格式!!!!!");
request.getSession().setAttribute("goodsToken", goodsToken);
return "page/publish_product";
}
int sid = shopInformationService.selectIdByImage(random);// get the id which is belongs shopInformation
//将发布的商品的编号插入到用户的发布中
UserRelease userRelease = new UserRelease();
userRelease.setModified(new Date());
userRelease.setSid(sid);
userRelease.setUid(uid);
try {
int result = userReleaseService.insertSelective(userRelease);
//如果关联失败,删除对应的商品和商品图片
if (result != 1) {
//if insert failure,transaction rollback.
shopInformationService.deleteByPrimaryKey(sid);
// shopPictureService.deleteByPrimaryKey(spid);
model.addAttribute("token", goodsToken);
model.addAttribute("message", "请输入正确的格式!!!!!");
request.getSession().setAttribute("goodsToken", goodsToken);
return "page/publish_product";
}
} catch (Exception e) {
//if insert failure,transaction rollback.
shopInformationService.deleteByPrimaryKey(sid);
e.printStackTrace();
model.addAttribute("token", goodsToken);
model.addAttribute("message", "请输入正确的格式!!!!!");
request.getSession().setAttribute("goodsToken", goodsToken);
return "page/publish_product";
}
shopInformation.setId(sid);
goodsToken = TokenProccessor.getInstance().makeToken();
request.getSession().setAttribute("goodsToken", goodsToken);
model.addAttribute("token", goodsToken);
model.addAttribute("shopInformation", shopInformation);
model.addAttribute("userInformation", userInformation);
String sb = getSort(sort);
model.addAttribute("sort", sb);
model.addAttribute("action", 2);
return "redirect:/my_publish_product_page.do";
} else if (action == 2) {//更新商品
ShopInformation shopInformation = new ShopInformation();
shopInformation.setModified(new Date());
shopInformation.setQuantity(quantity);
shopInformation.setSort(sort);
shopInformation.setPrice(new BigDecimal(price));
shopInformation.setRemark(remark);
shopInformation.setLevel(level);
shopInformation.setName(name);
shopInformation.setId(id);
try {
int result = shopInformationService.updateByPrimaryKeySelective(shopInformation);
if (result != 1) {
return "redirect:publish_product.do";
}
} catch (Exception e) {
e.printStackTrace();
return "redirect:publish_product.do";
}
goodsToken = TokenProccessor.getInstance().makeToken();
request.getSession().setAttribute("goodsToken", goodsToken);
model.addAttribute("token", goodsToken);
shopInformation = shopInformationService.selectByPrimaryKey(id);
model.addAttribute("userInformation", userInformation);
model.addAttribute("shopInformation", shopInformation);
model.addAttribute("action", 2);
model.addAttribute("sort", getSort(sort));
}
return "redirect:/my_publish_product_page.do";
}
//从发布的商品直接跳转到修改商品
@RequestMapping(value = "/modifiedMyPublishProduct.do")
public String modifiedMyPublishProduct(HttpServletRequest request, Model model,
@RequestParam int id) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
return "redirect:/login.do";
}
String goodsToken = TokenProccessor.getInstance().makeToken();
request.getSession().setAttribute("goodsToken", goodsToken);
model.addAttribute("token", goodsToken);
ShopInformation shopInformation = shopInformationService.selectByPrimaryKey(id);
model.addAttribute("userInformation", userInformation);
model.addAttribute("shopInformation", shopInformation);
model.addAttribute("action", 2);
model.addAttribute("sort", getSort(shopInformation.getSort()));
return "page/publish_product";
}
//发表留言
@RequestMapping(value = "/insertShopContext.do")
@ResponseBody
public Map insertShopContext(@RequestParam int id, @RequestParam String context, @RequestParam String token,
HttpServletRequest request) {
String goodsToken = (String) request.getSession().getAttribute("goodsToken");
Map<String, String> map = new HashMap<>();
map.put("result", "1");
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
map.put("result", "2");
return map;
}
if (StringUtils.getInstance().isNullOrEmpty(goodsToken) || !token.equals(goodsToken)) {
return map;
}
ShopContext shopContext = new ShopContext();
shopContext.setContext(context);
Date date = new Date();
shopContext.setModified(date);
shopContext.setSid(id);
int uid = (int) request.getSession().getAttribute("uid");
shopContext.setUid(uid);
try {
int result = shopContextService.insertSelective(shopContext);
if (result != 1) {
return map;
}
} catch (Exception e) {
e.printStackTrace();
return map;
}
map.put("result", "1");
map.put("username", userInformation.getUsername());
map.put("context", context);
map.put("time", StringUtils.getInstance().DateToString(date));
return map;
}
//下架商品
@RequestMapping(value = "/deleteShop.do")
public String deleteShop(HttpServletRequest request, Model model, @RequestParam int id) {
// Map<String, Integer> map = new HashMap<>();
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
return "redirect:/login.do";
} else {
model.addAttribute("userInformation", userInformation);
}
ShopInformation shopInformation = new ShopInformation();
shopInformation.setModified(new Date());
shopInformation.setDisplay(0);
shopInformation.setId(id);
try {
int result = shopInformationService.updateByPrimaryKeySelective(shopInformation);
if (result != 1) {
return "redirect:my_publish_product_page.do";
}
return "redirect:my_publish_product_page.do";
} catch (Exception e) {
e.printStackTrace();
return "redirect:my_publish_product_page.do";
}
}
//查看发布的所有商品总数
@RequestMapping(value = "/getReleaseShopCounts.do")
@ResponseBody
public Map getReleaseShopCounts(HttpServletRequest request) {
Map<String, Integer> map = new HashMap<>();
if (StringUtils.getInstance().isNullOrEmpty(request.getSession().getAttribute("userInformation"))) {
map.put("counts", -1);
return map;
}
int counts = getReleaseCounts((Integer) request.getSession().getAttribute("uid"));
map.put("counts", counts);
return map;
}
//查看我的发布的商品
@RequestMapping(value = "/my_publish_product_page.do")
public String getReleaseShop(HttpServletRequest request, Model model) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
return "redirect:/login.do";
} else {
model.addAttribute("userInformation", userInformation);
}
int uid = (int) request.getSession().getAttribute("uid");
List<ShopInformation> shopInformations = shopInformationService.selectUserReleaseByUid(uid);
List<ShopInformationBean> list = new ArrayList<>();
String stringBuffer;
// int i=0;
for (ShopInformation shopInformation : shopInformations) {
// if (i>=5){
// break;
// }
// i++;
stringBuffer = getSort(shopInformation.getSort());
ShopInformationBean shopInformationBean = new ShopInformationBean();
shopInformationBean.setId(shopInformation.getId());
shopInformationBean.setName(shopInformation.getName());
shopInformationBean.setLevel(shopInformation.getLevel());
shopInformationBean.setPrice(shopInformation.getPrice().doubleValue());
shopInformationBean.setRemark(shopInformation.getRemark());
shopInformationBean.setSort(stringBuffer);
shopInformationBean.setQuantity(shopInformation.getQuantity());
shopInformationBean.setTransaction(shopInformation.getTransaction());
shopInformationBean.setUid(shopInformation.getUid());
shopInformationBean.setImage(shopInformation.getImage());
list.add(shopInformationBean);
}
model.addAttribute("shopInformationBean", list);
return "page/personal/my_publish_product_page";
}
//更新商品信息
private String getSort(int sort) {
StringBuilder sb = new StringBuilder();
Specific specific = selectSpecificBySort(sort);
int cid = specific.getCid();
Classification classification = selectClassificationByCid(cid);
int aid = classification.getAid();
AllKinds allKinds = selectAllKindsByAid(aid);
sb.append(allKinds.getName());
sb.append("-");
sb.append(classification.getName());
sb.append("-");
sb.append(specific.getName());
return sb.toString();
}
//查看用户收藏的货物的总数
private int getCollectionCounts(int uid) {
int counts;
try {
counts = userCollectionService.getCounts(uid);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
return counts;
}
//查看收藏,一次10个
private List<UserCollection> selectContectionByUid(int uid, int start) {
try {
return userCollectionService.selectByUid(uid, (start - 1) * 10);
} catch (Exception e) {
e.printStackTrace();
List<UserCollection> list = new ArrayList<>();
list.add(new UserCollection());
return list;
}
}
//查看用户发布的货物的总数
private int getReleaseCounts(int uid) {
try {
return userReleaseService.getCounts(uid);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
//查看发布的货物,一次10个
private List<UserRelease> selectReleaseByUid(int uid, int start) {
try {
return userReleaseService.selectByUid(uid, (start - 1) * 10);
} catch (Exception e) {
e.printStackTrace();
List<UserRelease> list = new ArrayList<>();
list.add(new UserRelease());
return list;
}
}
//查看用户购买到的物品的总数
private int getBoughtShopCounts(int uid) {
try {
return boughtShopService.getCounts(uid);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
//查看用户的购买,10个
private List<BoughtShop> selectBoughtShopByUid(int uid, int start) {
try {
return boughtShopService.selectByUid(uid, (start - 1) * 10);
} catch (Exception e) {
e.printStackTrace();
List<BoughtShop> list = new ArrayList<>();
list.add(new BoughtShop());
return list;
}
}
//查看用户的求购总个数
private int getUserWantCounts(int uid) {
try {
return userWantService.getCounts(uid);
} catch (Exception e) {
return -1;
}
}
//求购列表10
private List<UserWant> selectUserWantByUid(int uid) {
try {
return userWantService.selectMineByUid(uid);
} catch (Exception e) {
e.printStackTrace();
List<UserWant> list = new ArrayList<>();
list.add(new UserWant());
return list;
}
}
//我的购物车总数
private int getShopCarCounts(int uid) {
try {
return shopCarService.getCounts(uid);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
//购物车列表 10
private ShopCar selectShopCarByUid(int uid) {
try {
return shopCarService.selectByUid(uid);
} catch (Exception e) {
e.printStackTrace();
// List<ShopCar> list
return new ShopCar();
}
}
//查看订单总数
private int getOrderFormCounts(int uid) {
try {
return orderFormService.getCounts(uid);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
//订单列表 10个
private List<OrderForm> selectOrderFormByUid(int uid, int start) {
try {
return orderFormService.selectByUid(uid, (start - 1) * 10);
} catch (Exception e) {
e.printStackTrace();
List<OrderForm> list = new ArrayList<>();
list.add(new OrderForm());
return list;
}
}
//订单中的商品
private List<GoodsOfOrderForm> selectGoodsOfOrderFormByOFid(int ofid) {
try {
return goodsOfOrderFormService.selectByOFid(ofid);
} catch (Exception e) {
e.printStackTrace();
List<GoodsOfOrderForm> list = new ArrayList<>();
list.add(new GoodsOfOrderForm());
return list;
}
}
//查看用户的状态
private UserState selectUserStateByUid(int uid) {
try {
return userStateService.selectByUid(uid);
} catch (Exception e) {
e.printStackTrace();
return new UserState();
}
}
//判断该手机号码及其密码是否一一对应
private boolean getId(String phone, String password, HttpServletRequest request) {
int uid = userInformationService.selectIdByPhone(phone);
if (uid == 0 || StringUtils.getInstance().isNullOrEmpty(uid)) {
return false;
}
UserInformation userInformation = userInformationService.selectByPrimaryKey(uid);
if (null == userInformation) {
return false;
}
password = StringUtils.getInstance().getMD5(password);
String password2 = userPasswordService.selectByUid(userInformation.getId()).getPassword();
if (!password.equals(password2)) {
return false;
}
//如果密码账号对应正确,将userInformation存储到session中
request.getSession().setAttribute("userInformation", userInformation);
request.getSession().setAttribute("uid", uid);
SaveSession.getInstance().save(phone, System.currentTimeMillis());
return true;
}
//获取最详细的分类,第三层
private Specific selectSpecificBySort(int sort) {
return specificeService.selectByPrimaryKey(sort);
}
//获得第二层分类
private Classification selectClassificationByCid(int cid) {
return classificationService.selectByPrimaryKey(cid);
}
//获得第一层分类
private AllKinds selectAllKindsByAid(int aid) {
return allKindsService.selectByPrimaryKey(aid);
}
public void save(ShopInformation shopInformation, UserRelease userRelease) {
shopInformationService.insertSelective(shopInformation);
userReleaseService.insertSelective(userRelease);
}
//循环插入商品
//发布商品
@RequestMapping(value = "/test")
public String insertGoods() {
//begin insert the shopInformation to the MySQL
// ShopInformation shopInformation = new ShopInformation();
// shopInformation.setName(name);
// shopInformation.setLevel(level);
// shopInformation.setRemark(remark);
// shopInformation.setPrice(new BigDecimal(price));
// shopInformation.setSort(sort);
// shopInformation.setQuantity(quantity);
// shopInformation.setModified(new Date());
// shopInformation.setImage(image);//This is the other uniquely identifies
// shopInformation.setUid(uid);
// //将发布的商品的编号插入到用户的发布中
// UserRelease userRelease = new UserRelease();
// userRelease.setModified(new Date());
// userRelease.setSid(sid);
// userRelease.setUid(uid);
// shopInformation.setId(sid);
Random random = new Random();
ShopInformation shopInformation;
UserRelease userRelease;
int level, uid, quantity;
double price;
for (int i = 1, k = 1, j = 189; i < 1000; i++, j++, k++) {
if (k > 94) {
k = 1;
}
level = random.nextInt(10) + 1;
price = Math.random() * 1000 + 1;
quantity = random.nextInt(10) + 1;
uid = random.nextInt(100) + 1;
shopInformation = new ShopInformation();
shopInformation.setId(j);
shopInformation.setName("百年孤独");
shopInformation.setModified(new Date());
shopInformation.setLevel(level);
shopInformation.setRemark("看上的请联系我,QQ:test,微信:test");
// double price = Math.random()*1000.00+1;
shopInformation.setPrice(new BigDecimal(price));
shopInformation.setSort(k);
shopInformation.setQuantity(quantity);
shopInformation.setImage("/image/QyBHYiMfYQ4XZFCqxEv0.jpg");
// int uid = random.nextInt(100)+1;
shopInformation.setUid(uid);
// userRelease = new UserRelease();
// userRelease.setUid(uid);
// userRelease.setSid(j);
// userRelease.setModified(new Date());
// userRelease.setDisplay(1);
shopInformationService.updateByPrimaryKeySelective(shopInformation);
// userReleaseService.insertSelective(userRelease);
}
System.out.println("success");
return "page/publish_product";
}
}
如果也想学习本系统,下面领取。关注并回复:063ssm