基于javaweb+mysql的ssm+maven校园二手交易平台(java+ssm+thymeleaf+html+jquery+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SSM+Maven校园二手交易平台(java+ssm+thymeleaf+html+jquery+mysql)
项目介绍
本次设计的是一个校园二手交易平台(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版本;
技术栈
- 后端:Spring SpringMVC MyBatis 2. 前端:Thymeleaf+Html+jQuery
使用说明
- 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件; 2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven; 若为maven项目,导入成功后请执行maven clean;maven install命令,配置tomcat,然后运行; 3. 将项目中jdbc.properties配置文件中的数据库配置改为自己的配置; 4. 运行项目,输入localhost:8080/xxx 登录
/**
*/
@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());
@RequestMapping(value = "/publish_product.do", method = RequestMethod.GET)
public String publish(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);
}
//如果登录了,判断该用户有没有经过认证
try {
String realName = userInformation.getRealname();
String sno = userInformation.getSno();
String dormitory = userInformation.getDormitory();
if (StringUtils.getInstance().isNullOrEmpty(realName) || StringUtils.getInstance().isNullOrEmpty(sno) || StringUtils.getInstance().isNullOrEmpty(dormitory)) {
//没有
model.addAttribute("message", "请先认证真实信息");
return "redirect:personal_info.do";
}
} catch (Exception e) {
e.printStackTrace();
return "redirect:/login.do";
}
String goodsToken = TokenProccessor.getInstance().makeToken();
request.getSession().setAttribute("goodsToken", goodsToken);
model.addAttribute("shopInformation", new ShopInformation());
model.addAttribute("action", 1);
model.addAttribute("token", goodsToken);
return "page/publish_product";
}
//模糊查询商品
@RequestMapping(value = "/findShopByName.do")
public String findByName(HttpServletRequest request, Model model,
@RequestParam String name) {
try {
List<ShopInformation> shopInformations = shopInformationService.selectByName(name);
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
userInformation = new UserInformation();
model.addAttribute("userInformation", userInformation);
} else {
model.addAttribute("userInformation", userInformation);
}
List<ShopInformationBean> shopInformationBeans = new ArrayList<>();
String sortName;
for (ShopInformation shopInformation : shopInformations) {
int sort = shopInformation.getSort();
sortName = getSort(sort);
ShopInformationBean shopInformationBean = new ShopInformationBean();
}
}
// get the random phone`s code
private void getRandomForCodePhone(HttpServletRequest req) {
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
sb.append(random.nextInt(10));
}
System.out.println(sb.toString());
req.getSession().setAttribute("codePhone", sb.toString());
}
// //检验验证码
// private boolean checkPhoto(String photo, HttpServletRequest request) {
// photo = photo.toLowerCase();
// String true_photo = (String) request.getSession().getAttribute("rand");
// return true_photo.equals(photo);
// }
//To determine whether the user's mobile phone number exists
private boolean isUserPhoneExists(String phone) {
boolean result = false;
try {
int id = userInformationService.selectIdByPhone(phone);
if (id == 0) {
return result;
}
UserInformation userInformation = userInformationService.selectByPrimaryKey(id);
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
return false;
}
String userPhone = userInformation.getPhone();
result = !userPhone.equals("");
} catch (Exception e) {
e.printStackTrace();
return result;
}
return result;
}
//订单列表 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());
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) {
Map<String, Integer> map = new HashMap<>();
map.put("start", (counts - 1) * 12);
map.put("end", 12);
return shopInformationService.selectTen(map);
}
// //通过id查看商品详情
// @RequestMapping(value = "/showShop")
// public String showShop(@RequestParam int id, HttpServletRequest request, Model model) {
// ShopInformation shopInformation =
// }
//获取最详细的分类,第三层
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 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);
String allName = allKinds.getName();
sb.append(allName);
sb.append("-");
sb.append(classification.getName());
sb.append("-");
sb.append(specific.getName());
return sb.toString();
}
}
@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";
}
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"))) {
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());
//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=图片不能含有敏感文字";
//根据第一层的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);
}
}
package com.wsk.controller;
/**
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) {
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";
}
//send the Email to the phone
@RequestMapping(value = "sendCode.do", method = {RequestMethod.POST, RequestMethod.GET})
@ResponseBody
public BaseResponse sendEmail(HttpServletRequest req, HttpServletResponse res,
@RequestParam String phone, @RequestParam String action,
@RequestParam String token) {
res.setContentType("text/html;charset=UTF-8");
//token,防止重复提交
String sendCodeToken = (String) req.getSession().getAttribute("token");
if (StringUtils.getInstance().isNullOrEmpty(sendCodeToken) || !sendCodeToken.equals(token)) {
return BaseResponse.fail();
}
//判断手机号码是否为正确
if (!StringUtils.getInstance().isPhone(phone)) {
return BaseResponse.fail();
}
//如果是忘记密码提交的发送短信
if ("forget".equals(action)) {
if (!isUserPhoneExists(phone)) {
//失败
return BaseResponse.fail();
}
} else if ("register".equals(action)) {
//失败
if (isUserPhoneExists(phone)) {
return BaseResponse.fail();
}
}
//get the random num to phone which should check the phone to judge the phone is belong user
getRandomForCodePhone(req);
String ra = (String) req.getSession().getAttribute("codePhone");
String text1 = "【WSK的验证码】您的验证码是:";
String text2 = ",请保护好自己的验证码。";
String text = text1 + ra + text2;
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.139.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
prop.setProperty("mail.smtp.port", "25");
try {
String realPhone = phone;
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);
/**
*/
@Controller
public class SendEmailController {
@Resource
private UserInformationService userInformationService;
private static final Logger log = LoggerFactory.getLogger(SendEmailController.class);
//send the Email to the phone
@RequestMapping(value = "sendCode.do", method = {RequestMethod.POST, RequestMethod.GET})
@ResponseBody
public BaseResponse sendEmail(HttpServletRequest req, HttpServletResponse res,
@RequestParam String phone, @RequestParam String action,
@RequestParam String token) {
res.setContentType("text/html;charset=UTF-8");
//token,防止重复提交
String sendCodeToken = (String) req.getSession().getAttribute("token");
if (StringUtils.getInstance().isNullOrEmpty(sendCodeToken) || !sendCodeToken.equals(token)) {
return BaseResponse.fail();
}
//判断手机号码是否为正确
if (!StringUtils.getInstance().isPhone(phone)) {
return BaseResponse.fail();
}
//如果是忘记密码提交的发送短信
if ("forget".equals(action)) {
if (!isUserPhoneExists(phone)) {
//失败
return BaseResponse.fail();
}
} else if ("register".equals(action)) {
//失败
if (isUserPhoneExists(phone)) {
return BaseResponse.fail();
}
}
//get the random num to phone which should check the phone to judge the phone is belong user
getRandomForCodePhone(req);
String ra = (String) req.getSession().getAttribute("codePhone");
String text1 = "【WSK的验证码】您的验证码是:";
String text2 = ",请保护好自己的验证码。";
String text = text1 + ra + text2;
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.139.com");
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;
}
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);
//通过分类的第三层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);
}
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";
}
}
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);
}
private SpecificeService specificeService;
@Resource
private ClassificationService classificationService;
@Resource
private AllKindsService allKindsService;
@Resource
private UserWantService userWantService;
//进入到发布商品页面
@RequestMapping(value = "/publish_product.do", method = RequestMethod.GET)
public String publish(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);
}
//如果登录了,判断该用户有没有经过认证
try {
String realName = userInformation.getRealname();
String sno = userInformation.getSno();
String dormitory = userInformation.getDormitory();
if (StringUtils.getInstance().isNullOrEmpty(realName) || StringUtils.getInstance().isNullOrEmpty(sno) || StringUtils.getInstance().isNullOrEmpty(dormitory)) {
//没有
model.addAttribute("message", "请先认证真实信息");
return "redirect:personal_info.do";
}
} catch (Exception e) {
e.printStackTrace();
return "redirect:/login.do";
}
String goodsToken = TokenProccessor.getInstance().makeToken();
request.getSession().setAttribute("goodsToken", goodsToken);
model.addAttribute("shopInformation", new ShopInformation());
model.addAttribute("action", 1);
model.addAttribute("token", goodsToken);
return "page/publish_product";
}
//模糊查询商品
@RequestMapping(value = "/findShopByName.do")
public String findByName(HttpServletRequest request, Model model,
@RequestParam String name) {
//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";
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";
}
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();
}
}
} else if ("register".equals(action)) {
//失败
if (isUserPhoneExists(phone)) {
return BaseResponse.fail();
}
}
//get the random num to phone which should check the phone to judge the phone is belong user
getRandomForCodePhone(req);
String ra = (String) req.getSession().getAttribute("codePhone");
String text1 = "【WSK的验证码】您的验证码是:";
String text2 = ",请保护好自己的验证码。";
String text = text1 + ra + text2;
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.139.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
prop.setProperty("mail.smtp.port", "25");
try {
String realPhone = phone;
phone += "@139.com";
// message.setRecipient(Message.RecipientType.TO, new InternetAddress(phone));
// message.setSubject("来自WSK的验证码");
// message.setContent(text, "text/html;charset=UTF-8");
// //这里先不发生信息,以后要开启的
// ts.sendMessage(message, message.getAllRecipients());
// ts.close();
req.getSession().setAttribute("phone", realPhone);
return BaseResponse.success();
} catch (Exception me) {
me.printStackTrace();
return BaseResponse.fail();
}
}
// get the random phone`s code
private void getRandomForCodePhone(HttpServletRequest req) {
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
sb.append(random.nextInt(10));
}
System.out.println(sb.toString());
req.getSession().setAttribute("codePhone", sb.toString());
}
if (StringUtils.getInstance().isNullOrEmpty(realName) || StringUtils.getInstance().isNullOrEmpty(sno) || StringUtils.getInstance().isNullOrEmpty(dormitory)) {
//没有
model.addAttribute("message", "请先认证真实信息");
return "redirect:personal_info.do";
}
} catch (Exception e) {
e.printStackTrace();
return "redirect:/login.do";
}
String goodsToken = TokenProccessor.getInstance().makeToken();
request.getSession().setAttribute("goodsToken", goodsToken);
model.addAttribute("shopInformation", new ShopInformation());
model.addAttribute("action", 1);
model.addAttribute("token", goodsToken);
return "page/publish_product";
}
//模糊查询商品
@RequestMapping(value = "/findShopByName.do")
public String findByName(HttpServletRequest request, Model model,
@RequestParam String name) {
try {
List<ShopInformation> shopInformations = shopInformationService.selectByName(name);
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
userInformation = new UserInformation();
model.addAttribute("userInformation", userInformation);
} else {
model.addAttribute("userInformation", userInformation);
}
List<ShopInformationBean> shopInformationBeans = new ArrayList<>();
String sortName;
for (ShopInformation shopInformation : shopInformations) {
int sort = shopInformation.getSort();
sortName = getSort(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.setQuantity(shopInformation.getQuantity());
shopInformationBean.setTransaction(shopInformation.getTransaction());
shopInformationBean.setSort(sortName);
shopInformationBean.setUid(shopInformation.getUid());
shopInformationBean.setImage(shopInformation.getImage());
shopInformationBeans.add(shopInformationBean);
}
model.addAttribute("shopInformationBean", shopInformationBeans);
} catch (Exception e) {