Java项目:前台后台玩具商城系统(java+JSP+SSM+Springboot+Jsp+maven+Mysql)
一、项目简述
本系统主要实现的功能有:
网上商城系统,前台+后台管理,用户注册,登录,商品展示,分组展示,搜索,收货地址管理,购物车管理,添加,购买,个人信息修改。订单查询等等,后台商品管理,分类管理,库存管理,订单管理,用户管理,信息修改等等。
二、项目运行
环境配置:
Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)
项目技术:
Springboot+ SpringMVC + MyBatis + Jsp + Html+ JavaScript + JQuery + Ajax + maven等等
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
@ResponseBody
@PutMapping("/user/updatephone/{mobilephone}/{vercode}")
public ResultVo updatephone(@PathVariable("mobilephone")String mobilephone,@PathVariable("vercode")String vercode,HttpSession session) {
String userid = (String) session.getAttribute("userid");
String rel = phonecodemap.get(mobilephone);
if (StringUtils.isEmpty(rel)) {//验证码到期 或者 没发送短信验证码
return new ResultVo(false,StatusCode.ERROR,"请重新获取验证码");
}
if (rel.equalsIgnoreCase(vercode)) {//验证码正确
Login login = new Login().setUserid(userid).setMobilephone(mobilephone);
UserInfo userInfo = new UserInfo().setUserid(userid).setMobilephone(mobilephone);
Integer integer = loginService.updateLogin(login);
Integer integer1 = userInfoService.UpdateUserInfo(userInfo);
if (integer == 1 && integer1 == 1) {
return new ResultVo(true, StatusCode.OK, "更换手机号成功");
}
return new ResultVo(false, StatusCode.SERVERERROR, "系统错误,更换失败");
}
return new ResultVo(false,StatusCode.ERROR,"注册失败");
}
return new ResultVo(false,StatusCode.ERROR,"验证码错误");
}
/**登录
* 1.判断输入账号的类型
* 2.登录
* */
@ResponseBody
@PostMapping("/user/login")
public ResultVo userLogin(@RequestBody Login login, HttpSession session){
String password = userInfo.getPassword();
String mobilephone = userInfo.getMobilephone();
String vercode = userInfo.getVercode();
Login login = new Login().setMobilephone(mobilephone);
//查询账号是否已经注册
Login userIsExist = loginService.userLogin(login);
if (!StringUtils.isEmpty(userIsExist)){//用户账号已经存在
return new ResultVo(false, StatusCode.ERROR,"该用户已经注册过了");
}
login.setUsername(username).setMobilephone(null);
Login userNameIsExist = loginService.userLogin(login);
if (!StringUtils.isEmpty(userNameIsExist)){//用户名已经存在
return new ResultVo(false, StatusCode.ERROR,"用户名已存在,请换一个吧");
}
String rel = phonecodemap1.get(mobilephone);
if (StringUtils.isEmpty(rel)) {//验证码到期 或者 没发送短信验证码
return new ResultVo(false,StatusCode.ERROR,"请重新获取验证码");
}
//if (rel.equalsIgnoreCase(vercode)) {
if (vercode.equals("123456")) {//验证码正确
//盐加密
String passwords = new Md5Hash(password, "Game-shops").toString();
if (rel.equalsIgnoreCase(vercode)) {//验证码正确
Login login = new Login().setUserid(userid).setMobilephone(mobilephone);
UserInfo userInfo = new UserInfo().setUserid(userid).setMobilephone(mobilephone);
Integer integer = loginService.updateLogin(login);
Integer integer1 = userInfoService.UpdateUserInfo(userInfo);
if (integer == 1 && integer1 == 1) {
return new ResultVo(true, StatusCode.OK, "更换手机号成功");
}
return new ResultVo(false, StatusCode.SERVERERROR, "系统错误,更换失败");
}
return new ResultVo(false,StatusCode.ERROR,"验证码错误");
}
}
登录注册控制器:
if (!JustPhone.justPhone(mobilephone)) {//判断输入的手机号格式是否正确
return new ResultVo(false,StatusCode.ERROR,"请输入正确格式的手机号");
}
//查询手机号是否存在
login.setMobilephone(mobilephone);
Login userIsExist = loginService.userLogin(login);
if (StringUtils.isEmpty(userIsExist)){//用户账号不存在
return new ResultVo(false, StatusCode.LOGINERROR,"该用户不存在");
}
String code = GetCode.phonecode();
Integer result = new SmsUtil().SendMsg(mobilephone, code, type);//发送验证码
if(result == 1) {//发送成功
phonecodemap2.put(mobilephone, code);//放入map集合进行对比
//执行定时任务
ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
phonecodemap2.remove(mobilephone);
((ScheduledThreadPoolExecutor) executorService).remove(this::run);
}
}, 5 * 60 * 1000);
*/
//执行定时任务
ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
phonecodemap1.remove(mobilephone);
((ScheduledThreadPoolExecutor) executorService).remove(this::run);
}
},1 * 10 * 1000,1 * 10 * 1000, TimeUnit.HOURS);
return new ResultVo(true,StatusCode.SMS,"验证码发送成功");
}else if(result == 2){
return new ResultVo(false,StatusCode.ERROR,"请输入正确格式的手机号");
}
return new ResultVo(true,StatusCode.LOGINERROR,"用户名不存在");
}catch (IncorrectCredentialsException e){
return new ResultVo(true,StatusCode.LOGINERROR,"密码错误");
}
}
/**重置密码时发送短信验证码
* 1.判断是否为重置密码类型验证码
* 2.判断手机号格式是否正确
* 3.查询账号是否存在
* 4.发送验证码
* */
@ResponseBody
@PostMapping("/user/sendresetpwd")
public ResultVo sendresetpwd(HttpServletRequest request) throws IOException {
JSONObject json = JsonReader.receivePost(request);
final String mobilephone = json.getString("mobilephone");
Integer type = json.getInt("type");
Login login = new Login();
if(type!=1){
return new ResultVo(false,StatusCode.ACCESSERROR,"违规操作");
}
}
/**
* 联系我们
* */
@GetMapping("/contacts")
public String contacts(){
return "/common/contacts";
}
/**
* 关于我们
* */
@GetMapping("/about")
public String about(){
return "/common/about";
}
/**
* 后台管理首页
* */
}else if(result == 2){
return new ResultVo(false,StatusCode.ERROR,"请输入正确格式的手机号");
}
return new ResultVo(false,StatusCode.REMOTEERROR,"验证码发送失败");
}
/**
* 修改绑定手机号
* 1.获取session中userid
* 2.修改login和userInfo中对应的手机号
*/
@ResponseBody
@PutMapping("/user/updatephone/{mobilephone}/{vercode}")
public ResultVo updatephone(@PathVariable("mobilephone")String mobilephone,@PathVariable("vercode")String vercode,HttpSession session) {
String userid = (String) session.getAttribute("userid");
String rel = phonecodemap.get(mobilephone);
//输入的是用户名
String username = account;
//盐加密
token = new UsernamePasswordToken(username, new Md5Hash(password,"Game-shops").toString());
// System.out.println("1password : "+new Md5Hash(password,"Game-shops").toString());
}else {
//输入的是手机号
String mobilephone = account;
login.setMobilephone(mobilephone);
//将封装的login中username变为null
login.setUsername(null);
//盐加密
token=new UsernamePasswordToken(mobilephone, new Md5Hash(password,"Game-shops").toString());
userInfo.setUserid(userid).setPassword(passwords).setUimage("/pic/d1d66c3ea71044a9b938b00859ca94df.jpg").
setSign("如此清秋何吝酒,这般明月不须钱").setStatus("offline");
Integer integer1 = userInfoService.userReg(userInfo);
if (integer==1 && integer1==1){
/**注册成功后存入session*/
session.setAttribute("userid",userid);
session.setAttribute("username",username);
/**存入用户角色信息*/
userRoleService.InsertUserRole(new UserRole().setUserid(userid).setRoleid(1).setIdentity("网站用户"));
UsernamePasswordToken token=new UsernamePasswordToken(mobilephone, new Md5Hash(password,"Game-shops").toString());
Subject subject= SecurityUtils.getSubject();
subject.login(token);
return new ResultVo(true,StatusCode.OK,"注册成功");
}
return new ResultVo(false,StatusCode.ERROR,"注册失败");
}
return new ResultVo(false,StatusCode.ERROR,"验证码错误");
}
/**登录
* 1.判断输入账号的类型
* 2.登录
* */
@ResponseBody
@PostMapping("/user/login")
ValidateCode vCode = new ValidateCode(820, 200, 5, 80);
vCode.write(response.getOutputStream());
}
/**注册时发送短信验证码
* 1.判断是否为注册类型验证码
* 2.判断手机号格式是否正确
* 3.判断手机号是否已经注册过
* 4.发送注册验证码并存入map集合
* */
@ResponseBody
@PostMapping("/user/sendregcode")
public ResultVo sendregcode(HttpServletRequest request) throws IOException{
JSONObject jsonObject = JsonReader.receivePost(request);
final String mobilephone = jsonObject.getString("mobilephone");
public String contacts(){
return "/common/contacts";
}
/**
* 关于我们
* */
@GetMapping("/about")
public String about(){
return "/common/about";
}
/**
* 后台管理首页
* */
@GetMapping("/admin/index")
public String adminindex(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws IOException {
String admin = (String) session.getAttribute("admin");
/**拦截器:如果不是管理员,则进行重定向*/
if (StringUtils.isEmpty(admin)){
subject.login(token);
//盐加密
String passwords = new Md5Hash(password, "Game-shops").toString();
System.out.println("3password :"+ passwords);
login.setPassword(passwords);
Login login1 = loginService.userLogin(login);
session.setAttribute("userid",login1.getUserid());
session.setAttribute("username",login1.getUsername());
return new ResultVo(true,StatusCode.OK,"登录成功");
}catch (UnknownAccountException e){
return new ResultVo(true,StatusCode.LOGINERROR,"用户名不存在");
}catch (IncorrectCredentialsException e){
return new ResultVo(true,StatusCode.LOGINERROR,"密码错误");
}
}
/**重置密码时发送短信验证码
*前端传入页码、分页数量
*前端传入商品信息状态码(commstatus)-->全部:100,违规:0,已审核:1,待审核:3 已完成:4
* 因为是管理员查询,将userid设置为空
*/
@GetMapping("/admin/commodity/{commstatus}")
@ResponseBody
public LayuiPageVo userCommodity(@PathVariable("commstatus") Integer commstatus, int limit, int page) {
if(commstatus==100){
List<Commodity> commodityList = commodityService.queryAllCommodity((page - 1) * limit, limit, null, null);
Integer dataNumber = commodityService.queryCommodityCount(null, null);
return new LayuiPageVo("",0,dataNumber,commodityList);
}else{
List<Commodity> commodityList = commodityService.queryAllCommodity((page - 1) * limit, limit, null, commstatus);
Integer dataNumber = commodityService.queryCommodityCount(null, commstatus);
return new LayuiPageVo("",0,dataNumber,commodityList);
}
}
/**
* 管理员对商品的操作
* 前端传入商品id(commid)
* 前端传入操作的商品状态(commstatus)-->违规:0 通过审核:1
return "redirect:/";
}
}
管理员控制器:
/**
* @Description: 管理员控制器
*/
@Controller
public class AdminController {
@Autowired
private UserRoleService userRoleService;
@Autowired
private LoginService loginService;
@Autowired
private UserInfoService userInfoService;
@Autowired
private CommodityService commodityService;
@Autowired
private NoticesService noticesService;
/**
* 管理员跳转登录
@PostMapping("/user/avatar")
public ResultVo userAvatar( HttpSession session) {
String userid = (String) session.getAttribute("userid");
UserInfo userInfo = userInfoService.queryPartInfo(userid);
return new ResultVo(true, StatusCode.OK, "查询头像成功",userInfo);
}
/**
* 修改头像
* */
@PostMapping(value = "/user/updateuimg")
@ResponseBody
public JSONObject updateuimg(@RequestParam(value = "file", required = false) MultipartFile file, HttpSession session) throws IOException {
JSONObject res = new JSONObject();
JSONObject resUrl = new JSONObject();
String filename = UUID.randomUUID().toString().replaceAll("-", "");
String ext = FilenameUtils.getExtension(file.getOriginalFilename());//获得文件扩展名
String filenames = filename + "." + ext;//文件全名
String pathname = "D://file/" + filenames;
file.transferTo(new File(pathname));
resUrl.put("src", "/pic/"+filenames);
res.put("msg", "");
res.put("code", 0);
*图片验证码
* */
@RequestMapping(value = "/images", method = {RequestMethod.GET, RequestMethod.POST})
public void images(HttpServletResponse response) throws IOException {
response.setContentType("image/jpeg");
//禁止图像缓存。
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
ValidateCode vCode = new ValidateCode(820, 200, 5, 80);
vCode.write(response.getOutputStream());
}
/**注册时发送短信验证码
return new ResultVo(true, StatusCode.ERROR, "封号失败");
}else if (userstatus == 1){
Integer i = loginService.updateLogin(new Login().setUserid(userid).setUserstatus(userstatus));
Integer j = userInfoService.UpdateUserInfo(new UserInfo().setUserid(userid).setUserstatus(userstatus));
if (i ==1 && j == 1){
/**发出解封的系统通知*/
Notices notices = new Notices().setId(KeyUtil.genUniqueKey()).setUserid(userid).setTpname("系统通知")
.setWhys("您在该网站的账号已被解封,希望您保持良好的行为。");
noticesService.insertNotices(notices);
return new ResultVo(true, StatusCode.OK, "解封成功");
}
return new ResultVo(true, StatusCode.ERROR, "解封失败");
}
return new ResultVo(false,StatusCode.ACCESSERROR,"违规操作");
List<Commodity> commodityList = commodityService.queryAllCommodity((page - 1) * limit, limit, null, commstatus);
Integer dataNumber = commodityService.queryCommodityCount(null, commstatus);
return new LayuiPageVo("",0,dataNumber,commodityList);
}
}
/**
* 管理员对商品的操作
* 前端传入商品id(commid)
* 前端传入操作的商品状态(commstatus)-->违规:0 通过审核:1
* */
@ResponseBody
@PutMapping("/admin/changecommstatus/{commid}/{commstatus}")
public ResultVo ChangeCommstatus(@PathVariable("commid") String commid, @PathVariable("commstatus") Integer commstatus) {
Integer i = commodityService.ChangeCommstatus(commid, commstatus);
if (i == 1){
/**发出商品审核结果的系统通知*/
Commodity commodity = commodityService.LookCommodity(new Commodity().setCommid(commid));
if (commstatus == 0){
Notices notices = new Notices().setId(KeyUtil.genUniqueKey()).setUserid(commodity.getUserid()).setTpname("商品审核")
.setWhys("您的商品 <a href=/product-detail/"+commodity.getCommid()+" style=\"color:#08bf91\" target=\"_blank\" >"+commodity.getCommname()+"</a> 未通过审核,目前不支持公开发布。");
noticesService.insertNotices(notices);
}else if (commstatus == 1){
if (StringUtils.isEmpty(rel)) {//验证码到期 或者 没发送短信验证码
return new ResultVo(false,StatusCode.ERROR,"请重新获取验证码");
}
if (vercode.equals("123456")) {//验证码正确
//盐加密
String passwords = new Md5Hash(password, "Game-shops").toString();
login1.setPassword(passwords).setId(userIsExist.getId()).setMobilephone(null);
userInfo.setMobilephone(mobilephone).setPassword(passwords).setUserid(userIsExist.getUserid());
Integer integer = loginService.updateLogin(login1);
Integer integer1 = userInfoService.UpdateUserInfo(userInfo);
if (integer==1 && integer1==1){
return new ResultVo(true,StatusCode.OK,"重置密码成功");
}
return new ResultVo(false,StatusCode.ERROR,"重置密码失败");
}
return new ResultVo(false,StatusCode.ERROR,"验证码错误");
}
/**退出登陆*/
@Autowired
private NoticesService noticesService;
/**
* 管理员跳转登录
*/
@GetMapping("/admin")
public String admintologin() {
return "admin/login/login";
}
/**
* 管理员登录
* 1.判断输入账号的类型
* 2.判断是否为管理员或者超级管理员
* 3.登录
* */
/**
* 联系我们
* */
@GetMapping("/contacts")
public String contacts(){
return "/common/contacts";
}
/**
* 关于我们
* */
* 前端传入商品id(commid)
* 前端传入操作的商品状态(commstatus)-->违规:0 通过审核:1
* */
@ResponseBody
@PutMapping("/admin/changecommstatus/{commid}/{commstatus}")
public ResultVo ChangeCommstatus(@PathVariable("commid") String commid, @PathVariable("commstatus") Integer commstatus) {
Integer i = commodityService.ChangeCommstatus(commid, commstatus);
if (i == 1){
/**发出商品审核结果的系统通知*/
Commodity commodity = commodityService.LookCommodity(new Commodity().setCommid(commid));
if (commstatus == 0){
Notices notices = new Notices().setId(KeyUtil.genUniqueKey()).setUserid(commodity.getUserid()).setTpname("商品审核")
.setWhys("您的商品 <a href=/product-detail/"+commodity.getCommid()+" style=\"color:#08bf91\" target=\"_blank\" >"+commodity.getCommname()+"</a> 未通过审核,目前不支持公开发布。");
noticesService.insertNotices(notices);
}else if (commstatus == 1){
Notices notices = new Notices().setId(KeyUtil.genUniqueKey()).setUserid(commodity.getUserid()).setTpname("商品审核")
.setWhys("您的商品 <a href=/product-detail/"+commodity.getCommid()+" style=\"color:#08bf91\" target=\"_blank\" >"+commodity.getCommname()+"</a> 已通过审核,快去看看吧。");
noticesService.insertNotices(notices);
@GetMapping("/admin")
public String admintologin() {
return "admin/login/login";
}
/**
* 管理员登录
* 1.判断输入账号的类型
* 2.判断是否为管理员或者超级管理员
* 3.登录
* */
@ResponseBody
@PostMapping("/admin/login")
public ResultVo adminlogin(@RequestBody Login login, HttpSession session){
System.out.println("测试是否进入!!!");
String account=login.getUsername();
String password=login.getPassword();
String vercode=login.getVercode();
UsernamePasswordToken token;
if(!ValidateCode.code.equalsIgnoreCase(vercode)){
return new ResultVo(false,StatusCode.ERROR,"请输入正确的验证码");
}
//判断输入的账号是否手机号
if (!JustPhone.justPhone(account)) {
//输入的是用户名
}
Subject subject= SecurityUtils.getSubject();
try {
subject.login(token);
//盐加密
String passwords = new Md5Hash(password, "Game-shops").toString();
System.out.println("3password :"+ passwords);
login.setPassword(passwords);
Login login1 = loginService.userLogin(login);
session.setAttribute("userid",login1.getUserid());
session.setAttribute("username",login1.getUsername());
return new ResultVo(true,StatusCode.OK,"登录成功");
}catch (UnknownAccountException e){
return new ResultVo(true,StatusCode.LOGINERROR,"用户名不存在");
}catch (IncorrectCredentialsException e){
return new ResultVo(true,StatusCode.LOGINERROR,"密码错误");
}
}
/**重置密码时发送短信验证码
* 1.判断是否为重置密码类型验证码
适用
课程设计,大作业,毕业设计,项目练习,学习演示等