Compare commits

...

2 Commits

Author SHA1 Message Date
zwt 16d5552e05 注释
10 months ago
zwt cab1fe2a31 注释
10 months ago

@ -0,0 +1,25 @@
package com.wsk.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* ErrorController
* Spring MVCWeb
* wsk11032017/5/23
*/
@Controller
// @Controller注解标记此类为Spring MVC的控制器类会被Spring框架扫描并管理用于处理Web请求
public class ErrorController {
/**
* "/error"
* "/error"
* @return "error"SpringHTML
*/
@RequestMapping(value = "/error")
// @RequestMapping注解将error方法与"/error"这个请求路径进行映射关联,意味着该方法会处理对应的请求
public String error() {
return "error";
}
}

@ -18,51 +18,86 @@ import java.util.Map;
/**
* Created by wsk1103 on 2017/5/9.
* ForgetController
* Spring MVC
*/
@RestController
// @RestController注解结合了@Controller和@ResponseBody的功能
// 意味着该类中的方法返回值会直接作为响应体返回给客户端通常用于返回JSON等数据格式而不是跳转视图。
public class ForgetController {
@Resource
// @Resource注解用于依赖注入这里会将Spring容器中管理的UserPasswordService类型的Bean注入到当前类中
// 以便后续调用该服务层提供的与用户密码相关的业务方法。
private UserPasswordService userPasswordService;
@Resource
// 同理注入UserInformationService类型的Bean用于调用和用户信息相关的业务方法。
private UserInformationService userInformationService;
/**
*
* POSTGETtoken
*
* @param request HttpServletRequest
* @param model Spring MVCModel@RestController
* @param code
* @param token token
* @return Map"result"10
*/
@RequestMapping(value = "checkCode.do", method = {RequestMethod.POST, RequestMethod.GET})
public Map checkPhone(HttpServletRequest request, Model model,
@RequestParam String code, @RequestParam String token) {
Map<String, Integer> map = new HashMap<>();
// 从请求中获取名为"name"的参数值
String name = request.getParameter("name");
// 判断name是否不为空利用StringUtils工具类的方法来判断如果不为空则将其存入会话中。
if (!StringUtils.getInstance().isNullOrEmpty(name)) {
request.getSession().setAttribute("name", name);
}
// 从会话中获取名为"token"的属性值,用于后续验证。
String checkCodeToken = (String) request.getSession().getAttribute("token");
if (StringUtils.getInstance().isNullOrEmpty(checkCodeToken) || !checkCodeToken.equals(token)) {
// 如果获取到的会话中的token为空或者与传入的token不一致则验证失败将结果设置为0并返回。
if (StringUtils.getInstance().isNullOrEmpty(checkCodeToken) ||!checkCodeToken.equals(token)) {
map.put("result", 0);
return map;
}
//验证码错误
// 调用checkCodePhone方法验证验证码是否正确如果不正确验证失败将结果设置为0并返回。
if (!checkCodePhone(code, request)) {
map.put("result", 0);
return map;
}
// 验证通过将结果设置为1并返回。
map.put("result", 1);
return map;
}
//更新密码
/**
*
* tokenBaseResponse
*
* @param request HttpServletRequest
* @param model Spring MVCModel
* @param password
* @param token token
* @return BaseResponsesuccessfail
*/
@RequestMapping("updatePassword.do")
public BaseResponse updatePassword(HttpServletRequest request, Model model,
@RequestParam String password, @RequestParam String token) {
//防止重复提交
// 从会话中获取名为"token"的属性值用于验证是否重复提交和传入的token对比
String updatePasswordToken = (String) request.getSession().getAttribute("token");
if (StringUtils.getInstance().isNullOrEmpty(updatePasswordToken) || !updatePasswordToken.equals(token)) {
// 如果获取到的会话中的token为空或者与传入的token不一致则认为是重复提交或非法请求直接返回失败响应。
if (StringUtils.getInstance().isNullOrEmpty(updatePasswordToken) ||!updatePasswordToken.equals(token)) {
return BaseResponse.fail();
}
// 从会话中获取存储的真实手机号(假设之前已存入会话中)。
String realPhone = (String) request.getSession().getAttribute("phone");
UserPassword userPassword = new UserPassword();
// 使用StringUtils工具类的方法对传入的密码进行MD5加密处理得到加密后的新密码。
String newPassword = StringUtils.getInstance().getMD5(password);
int uid;
try {
// 通过用户信息服务根据手机号查询对应的用户ID如果查询到的ID为0表示未找到对应用户返回失败响应。
uid = userInformationService.selectIdByPhone(realPhone);
if (uid == 0) {
return BaseResponse.fail();
@ -71,29 +106,41 @@ public class ForgetController {
e.printStackTrace();
return BaseResponse.fail();
}
// 通过用户密码服务根据用户ID查询对应的用户密码记录并获取其ID。
int id = userPasswordService.selectByUid(uid).getId();
userPassword.setId(id);
userPassword.setUid(uid);
// 设置密码修改时间为当前时间。
userPassword.setModified(new Date());
userPassword.setPassword(newPassword);
int result;
try {
// 调用用户密码服务的方法,根据主键有选择地更新用户密码记录(只更新传入的非空字段对应的属性)。
result = userPasswordService.updateByPrimaryKeySelective(userPassword);
} catch (Exception e) {
return BaseResponse.fail();
}
//更新失败
if (result != 1) {
// 如果更新操作影响的行数不等于1更新失败),返回失败响应。
if (result!= 1) {
return BaseResponse.fail();
}
// 更新成功后根据用户ID查询用户信息并将其存入会话中方便后续使用。
UserInformation userInformation = userInformationService.selectByPrimaryKey(uid);
request.getSession().setAttribute("userInformation", userInformation);
return BaseResponse.success();
}
//check the phone`s code
/**
*
* "12251103"
*
* @param codePhone
* @param request HttpServletRequest
* @return truefalse
*/
// check the phone`s code
private boolean checkCodePhone(String codePhone, HttpServletRequest request) {
String trueCodePhone = "12251103";
return codePhone.equals(trueCodePhone);
}
}
}

@ -0,0 +1,326 @@
package com.wsk.controller;
import com.wsk.bean.ShopContextBean;
import com.wsk.bean.ShopInformationBean;
import com.wsk.bean.UserWantBean;
import com.wsk.pojo.*;
import com.wsk.service.*;
import com.wsk.token.TokenProccessor;
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/14.
* GoodsControllerSpring MVC
*/
@Controller
// @Controller注解表明这个类是Spring MVC的控制器类会被Spring框架扫描并用于处理Web请求可将请求映射到对应的方法上。
public class GoodsController {
@Resource
// @Resource注解用于依赖注入将Spring容器中管理的ShopInformationService类型的Bean注入到当前类中方便后续调用其提供的与店铺信息相关的业务方法。
private ShopInformationService shopInformationService;
@Resource
private ShopContextService shopContextService;
@Resource
private UserInformationService userInformationService;
@Resource
private SpecificeService specificeService;
@Resource
private ClassificationService classificationService;
@Resource
private AllKindsService allKindsService;
@Resource
private UserWantService userWantService;
/**
*
* GET
*
* @param request HttpServletRequest
* @param model Spring MVCModel
* @return "page/publish_product"
*/
@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中以便视图可以获取并展示相关信息。
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中并重定向到个人信息页面让用户去认证。
model.addAttribute("message", "请先认证真实信息");
return "redirect:personal_info.do";
}
} catch (Exception e) {
e.printStackTrace();
return "redirect:/login.do";
}
// 使用TokenProccessor生成一个token用于可能的表单防重复提交等安全验证场景此处假设
String goodsToken = TokenProccessor.getInstance().makeToken();
request.getSession().setAttribute("goodsToken", goodsToken);
// 向Model中添加一个新的ShopInformation对象可能用于页面上的表单绑定等操作具体取决于页面需求
model.addAttribute("shopInformation", new ShopInformation());
model.addAttribute("action", 1);
model.addAttribute("token", goodsToken);
// 最终跳转到发布商品的页面(具体页面路径配置可能在视图解析器中定义)。
return "page/publish_product";
}
/**
*
* Model
*
* @param request HttpServletRequest
* @param model Spring MVCModel
* @param name
* @return "mall_page.do""page/mall_page"
*/
@RequestMapping(value = "/findShopByName.do")
public String findByName(HttpServletRequest request, Model model,
@RequestParam String name) {
try {
// 调用ShopInformationService的方法根据商品名称进行模糊查询获取符合条件的ShopInformation列表。
List<ShopInformation> shopInformations = shopInformationService.selectByName(name);
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
userInformation = new UserInformation();
// 如果用户未登录用户信息为空创建一个空的UserInformation对象添加到Model中可能用于页面展示等情况。
model.addAttribute("userInformation", userInformation);
} else {
// 如果用户已登录将用户信息添加到Model中。
model.addAttribute("userInformation", userInformation);
}
List<ShopInformationBean> shopInformationBeans = new ArrayList<>();
String sortName;
// 遍历查询到的每个ShopInformation对象进行相关信息的提取和封装到ShopInformationBean对象中。
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中以便视图展示。
model.addAttribute("shopInformationBean", shopInformationBeans);
} catch (Exception e) {
e.printStackTrace();
return "redirect:mall_page.do";
}
return "page/mall_page";
}
/**
*
* ID
*
* @param id ID
* @param request HttpServletRequest
* @param model Spring MVCModel
* @return "/""page/product_info"
*/
@RequestMapping(value = "/selectById.do")
public String selectById(@RequestParam int id,
HttpServletRequest request, Model model) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
userInformation = new UserInformation();
model.addAttribute("userInformation", userInformation);
}
try {
// 调用ShopInformationService的方法根据商品ID获取商品的详细信息并添加到Model中。
ShopInformation shopInformation = shopInformationService.selectByPrimaryKey(id);
model.addAttribute("shopInformation", shopInformation);
// 调用ShopContextService的方法根据商品ID获取该商品的相关评论等上下文信息列表。
List<ShopContext> shopContexts = shopContextService.selectById(id);
List<ShopContextBean> shopContextBeans = new ArrayList<>();
// 遍历每个评论上下文信息进行相关信息的提取和封装到ShopContextBean对象中。
for (ShopContext s : shopContexts) {
ShopContextBean shopContextBean = new ShopContextBean();
UserInformation u = userInformationService.selectByPrimaryKey(s.getUid());
shopContextBean.setContext(s.getContext());
shopContextBean.setId(s.getId());
shopContextBean.setModified(s.getModified());
shopContextBean.setUid(u.getId());
shopContextBean.setUsername(u.getUsername());
shopContextBeans.add(shopContextBean);
}
String sort = getSort(shopInformation.getSort());
String goodsToken = TokenProccessor.getInstance().makeToken();
request.getSession().setAttribute("goodsToken", goodsToken);
model.addAttribute("token", goodsToken);
model.addAttribute("sort", sort);
model.addAttribute("userInformation", userInformation);
model.addAttribute("shopContextBeans", shopContextBeans);
return "page/product_info";
} catch (Exception e) {
e.printStackTrace();
return "redirect:/";
}
}
/**
*
* Model
*
* @param request HttpServletRequest
* @param model Spring MVCModel
* @return "page/require_mall"
*/
@RequestMapping(value = "/require_mall.do")
public String requireMall(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);
}
// 调用UserWantService的方法获取所有的求购商品信息列表。
List<UserWant> userWants = userWantService.selectAll();
List<UserWantBean> list = new ArrayList<>();
// 遍历每个求购商品信息进行相关信息的提取和封装到UserWantBean对象中。
for (UserWant userWant : userWants) {
UserWantBean u = new UserWantBean();
u.setSort(getSort(userWant.getSort()));
u.setRemark(userWant.getRemark());
u.setQuantity(userWant.getQuantity());
u.setPrice(userWant.getPrice().doubleValue());
u.setUid(userWant.getUid());
u.setId(userWant.getId());
u.setModified(userWant.getModified());
u.setName(userWant.getName());
list.add(u);
}
model.addAttribute("list", list);
return "page/require_mall";
}
/**
* ID使@ResponseBodyJSON
*
* @param id ID
* @return ShopInformation
*/
@RequestMapping(value = "/findShopById.do")
@ResponseBody
public ShopInformation findShopById(@RequestParam int id) {
return shopInformationService.selectByPrimaryKey(id);
}
/**
* 使@ResponseBody
*
* @param sort ID
* @return List<ShopInformation>
*/
@RequestMapping(value = "/selectBySort.do")
@ResponseBody
public List<ShopInformation> selectBySort(@RequestParam int sort) {
return shopInformationService.selectBySort(sort);
}
/**
* 使@ResponseBody
*
* @param counts
* @return List<ShopInformation>
*/
@RequestMapping(value = "/selectByCounts.do")
@ResponseBody
public List<ShopInformation> selectByCounts(@RequestParam int counts) {
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 =
// }
/**
* ID便
*
* @param sort ID
* @return SpecificID
*/
private Specific selectSpecificBySort(int sort) {
return specificeService.selectByPrimaryKey(sort);
}
/**
* ID使
*
* @param cid ID
* @return ClassificationID
*/
private Classification selectClassificationByCid(int cid) {
return classificationService.selectByPrimaryKey(cid);
}
/**
* ID便
*
* @param aid ID
* @return AllKindsID
*/
private AllKinds selectAllKindsByAid(int aid) {
return allKindsService.selectByPrimaryKey(aid);
}
/**
* ID
*
* @param sort ID
* @return "一级分类名-二级分类名-三级分类名"
*/
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();
}
}

@ -0,0 +1,336 @@
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.
* HomeControllerSpring MVC
*/
@Controller
// @Controller注解表明该类是Spring MVC的控制器会被Spring框架扫描并用于处理Web请求可将请求映射到对应的方法上。
public class HomeController {
@Resource
// @Resource注解用于依赖注入将Spring容器中管理的ShopInformationService类型的Bean注入到当前类中方便后续调用其提供的与店铺信息相关的业务方法。
private ShopInformationService shopInformationService;
@Resource
private SpecificeService specificeService;
@Resource
private ClassificationService classificationService;
@Resource
private AllKindsService allKindsService;
@Resource
private ShopContextService shopContextService;
/**
* "/""/home.do"
* "userInformation"ModelModel
*
* @param request HttpServletRequest
* @param model Spring MVCModel
* @return "page/login_page""index"
*/
@RequestMapping(value = {"/", "/home.do"})
public String home(HttpServletRequest request, Model model) {
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
// 如果用户登录了,会话中会有"userInformation"属性,这里判断其是否为空来确定用户是否登录。
if (!StringUtils.getInstance().isNullOrEmpty(userInformation)) {
// 如果用户已登录将用户信息添加到Model中以便视图可以获取并展示相关信息。
model.addAttribute("userInformation", userInformation);
} else {
userInformation = new UserInformation();
model.addAttribute("userInformation", userInformation);
}
// 一般形式进入首页,尝试获取部分商品信息进行展示。
try {
// 调用selectTen方法获取指定数量的商品信息列表此处获取第1页每页显示5条商品信息参数含义可查看selectTen方法注释
List<ShopInformation> shopInformations = selectTen(1, 5);
List<ShopInformationBean> list = new ArrayList<>();
// 获取商品的总数量,用于可能的页面展示等用途(比如分页相关功能)。
int counts = getShopCounts();
model.addAttribute("shopInformationCounts", counts);
String stringBuffer;
// 遍历获取到的每个商品信息进行相关信息的提取和封装到ShopInformationBean对象中并添加到列表中。
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";
}
/**
*
* ModelModel
*
* @param request HttpServletRequest
* @param model Spring MVCModel
* @return "page/login_page""page/mall_page"
*/
@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 {
// 调用selectTen方法获取指定数量的商品信息列表此处获取第1页每页显示12条商品信息参数含义可查看selectTen方法注释
List<ShopInformation> shopInformations = selectTen(1, 12);
List<ShopInformationBean> list = new ArrayList<>();
// 获取商品的总数量,用于可能的页面展示等用途(比如分页相关功能)。
int counts = getShopCounts();
model.addAttribute("shopInformationCounts", counts);
String sortName;
// 遍历获取到的每个商品信息进行相关信息的提取和封装到ShopInformationBean对象中并添加到列表中。
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
*
*
* @param sort ID
* @return "一级分类名-二级分类名-三级分类名"
*/
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();
}
/**
* 使@ResponseBodyJSON
*
* @return List<AllKinds>
*/
@RequestMapping(value = "/getAllKinds.do")
@ResponseBody
public List<AllKinds> getAllKind() {
return getAllKinds();
}
/**
* ID使@ResponseBodyID
*
* @param id ID
* @return List<Classification>ID
*/
@RequestMapping(value = "/getClassification.do", method = RequestMethod.POST)
@ResponseBody
public List<Classification> getClassificationByAid(@RequestParam int id) {
return selectAllClassification(id);
}
/**
* ID使@ResponseBodyID
*
* @param id ID
* @return List<Specific>ID
*/
@RequestMapping(value = "/getSpecific.do")
@ResponseBody
public List<Specific> getSpecificByCid(@RequestParam int id) {
return selectAllSpecific(id);
}
/**
* 使@ResponseBodyMap
*
* @return Map<String, Integer>"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;
}
/**
* 使@ResponseBody
*
* @param start
* @return ListShopInformation
*/
@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;
}
/**
* MapShopInformationService
*
* @param start 1
* @param end
* @return List<ShopInformation>
*/
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;
}
/**
* ID便
*
* @param sort ID
* @return SpecificID
*/
private Specific selectSpecificBySort(int sort) {
return specificeService.selectByPrimaryKey(sort);
}
/**
* ID使
*
* @param cid ID
* @return ClassificationID
*/
private Classification selectClassificationByCid(int cid) {
return classificationService.selectByPrimaryKey(cid);
}
/**
* ID便
*
* @param aid ID
* @return AllKindsID
*/
private AllKinds selectAllKindsByAid(int aid) {
return allKindsService.selectByPrimaryKey(aid);
}
/**
*
*
* @return List<AllKinds>
*/
private List<AllKinds> getAllKinds() {
return allKindsService.selectAll();
}
/**
* IDID
*
* @param aid ID
* @return List<Classification>ID
*/
private List<Classification> selectAllClassification(int aid) {
return classificationService.selectByAid(aid);
}
/**
* IDID
*
* @param cid ID
* @return List<Specific>ID
*/
private List<Specific> selectAllSpecific(int cid) {
return specificeService.selectByCid(cid);
}
/**
* ShopInformationService
*
* @return
*/
private int getShopCounts() {
return shopInformationService.getCounts();
}
/**
* ShopContextServiceID使
*
* @param sid ID
* @return
*/
private int getShopContextCounts(int sid) {
return shopContextService.getCounts(sid);
}
/**
* IDShopContextService

@ -0,0 +1,127 @@
package com.wsk.controller;
import com.wsk.pojo.UserInformation;
import com.wsk.pojo.UserPassword;
import com.wsk.response.BaseResponse;
import com.wsk.service.UserInformationService;
import com.wsk.service.UserPasswordService;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* Created by wsk1103 on 2017/5/9.
*
* Spring MVC
*/
@Controller
// @Controller注解表明这个类是Spring MVC的控制器类会被Spring框架扫描并用于处理Web请求将请求映射到对应的方法上。
public class RegisterController {
@Resource
// @Resource注解用于依赖注入将Spring容器中管理的UserPasswordService类型的Bean注入到当前类中方便后续调用其提供的与用户密码相关的业务方法。
private UserPasswordService userPasswordService;
@Resource
// 同样注入UserInformationService类型的Bean用于调用和用户信息相关的业务方法比如插入、查询、删除用户信息等操作。
private UserInformationService userInformationService;
/**
* token
* 使@ResponseBodyJSON
*
* @param request HttpServletRequestsessiontoken
* @param password
* @param token tokentoken
* @return BaseResponsesuccessfail
*/
@RequestMapping("/insertUser.do")
@ResponseBody
public BaseResponse insertUser(HttpServletRequest request,
@RequestParam String password, @RequestParam String token) {
// 从会话session中获取之前存储的手机号码该手机号码可能是在注册流程的前序步骤中由用户输入并存储到会话里的虽然代码中未展示具体存储过程但可推测是这样的流程
String realPhone = (String) request.getSession().getAttribute("phone");
// 从会话中获取名为"token"的属性值该token作为唯一标识用于验证本次请求是否是重复提交等情况例如在表单提交场景中防止用户多次点击提交按钮造成重复注册
String insertUserToken = (String) request.getSession().getAttribute("token");
// 防止重复提交验证如果获取到的会话中的token为空或者与传入的token不一致则认为是重复提交或非法请求直接返回注册失败的响应。
if (StringUtils.getInstance().isNullOrEmpty(insertUserToken) ||!insertUserToken.equals(token)) {
return BaseResponse.fail();
}
// 调用UserInformationService的方法根据手机号查询对应的用户ID判断该手机号码是否已经存在于数据库中如果查询到的ID不为0表示该手机号已被注册直接返回注册失败的响应。
int uid = userInformationService.selectIdByPhone(realPhone);
if (uid!= 0) {
return BaseResponse.fail();
}
// 创建一个新的UserInformation对象用于封装要插入数据库的用户基本信息后续会将该对象通过服务层方法插入到数据库中。
UserInformation userInformation = new UserInformation();
// 将从会话中获取到的手机号码设置到用户信息对象中,作为用户的手机号属性值。
userInformation.setPhone(realPhone);
// 设置用户信息的创建时间为当前时间通过创建一个Date对象获取当前时间并赋值给createtime属性。
userInformation.setCreatetime(new Date());
// 从会话中获取用户名同样这里的用户名可能是之前在注册流程中用户输入并存储到会话里的代码未完整展示该部分并设置到用户信息对象的username属性中。
String username = (String) request.getSession().getAttribute("name");
userInformation.setUsername(username);
// 设置用户信息的修改时间为当前时间,这里可能是考虑到新用户注册也算一次信息修改操作,所以初始创建时修改时间和创建时间都设置为当前时间。
userInformation.setModified(new Date());
// 调用UserInformationService的insertSelective方法将封装好的用户信息对象插入到数据库中该方法可能只会插入对象中不为空的属性对应的字段值具体取决于服务层实现并获取插入操作影响的行数存储到result变量中。
int result;
result = userInformationService.insertSelective(userInformation);
// 如果用户基本信息插入数据库成功即影响行数为1表示成功插入了一条记录则继续进行密码相关信息的插入操作。
if (result == 1) {
// 再次通过手机号查询用户ID确保获取到刚插入用户信息对应的正确ID以便后续关联用户密码信息。
uid = userInformationService.selectIdByPhone(realPhone);
// 使用StringUtils工具类的方法对传入的密码进行MD5加密处理得到加密后的密码字符串用于安全地存储到数据库中。
String newPassword = StringUtils.getInstance().getMD5(password);
// 创建一个新的UserPassword对象用于封装用户密码相关信息后续将其插入到数据库中与对应的用户关联起来。
UserPassword userPassword = new UserPassword();
// 设置密码的修改时间为当前时间,与用户信息的修改时间逻辑类似,记录密码信息的最后修改时间。
userPassword.setModified(new Date());
// 将前面获取到的用户ID设置到用户密码对象的uid属性中建立用户信息和密码信息的关联关系。
userPassword.setUid(uid);
// 将加密后的密码设置到用户密码对象的password属性中准备插入到数据库。
userPassword.setPassword(newPassword);
// 调用UserPasswordService的insertSelective方法将用户密码信息插入到数据库中并获取插入操作影响的行数存储到result变量中。
result = userPasswordService.insertSelective(userPassword);
// 如果密码信息插入失败即影响行数不为1表示插入操作出现问题则需要删除之前插入的用户基本信息保持数据的一致性避免产生孤立的用户信息记录然后返回注册失败的响应。
if (result!= 1) {
userInformationService.deleteByPrimaryKey(uid);
return BaseResponse.fail();
} else {
// 如果密码信息插入成功说明整个注册流程成功完成通过用户ID查询完整的用户信息对象可能包含更多详细信息比如后续添加的其他用户属性等并将该用户信息存储到会话中方便后续用户登录后的操作中可以直接获取用户信息。
userInformation = userInformationService.selectByPrimaryKey(uid);
request.getSession().setAttribute("userInformation", userInformation);
return BaseResponse.success();
}
}
// 如果用户基本信息插入数据库失败前面result不为1的情况直接返回注册失败的响应。
return BaseResponse.fail();
}
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,29 @@
package com.wsk.controller.webSocket;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
/**
* Created by wsk1103 on 2017/5/22.
*/
@Configuration
@EnableWebMvc
@EnableWebSocket
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
//WebIM WebSocket通道
registry.addHandler(chatWebSocketHandler(),"/webSocketIMServer");
registry.addHandler(chatWebSocketHandler(),"/sockjs/webSocketIMServer");
registry.addHandler(chatWebSocketHandler(), "/sockjs/webSocketIMServer").withSockJS();
}
@Bean
public ChatWebSocketHandler chatWebSocketHandler() {
return new ChatWebSocketHandler();
}
}
Loading…
Cancel
Save