源码获取:俺的博客首页 "资源" 里下载!
管理员角色包含以下功能:
管理员登录,类目管理,用户管理,菜品管理,订单管理,公告管理,留言管理,查看日志等功能。
用户管理包含以下功能:
查看首页,用户登录,查看商品详情,查看我的收藏,查看购物车,查看我的订单,提交订单,提交留言,修改个人信息,修改密码等功能。
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项目: 是;
6.数据库:MySql 5.7等版本均可;
1. 后端:Spring+SpringMVC+Mybatis
2. 前端:JSP+CSS+JavaScript+jquery+bootstrap+echarts
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中db.properties配置文件中的数据库配置改为自己的配置;
4. 运行项目,在浏览器中输入http://localhost:8080/shop 登录
用户账号/密码: user/123456
管理员账号/密码:admin/admin
- /**
- * 后台登陆
- */
- @Controller
- @RequestMapping("")
- public class LoginController {
-
- @Autowired
- UserService userService;
-
- @RequestMapping(value="/login",method=RequestMethod.POST)
- public String login(Model model, String name, String password){//throws ParseException
- Subject subject = SecurityUtils.getSubject();
- UsernamePasswordToken token = new UsernamePasswordToken(name,password);
- try {
- subject.login(token);
- User us = userService.getByName(name);
- String lastLoginTime = "";
- if(us!=null){
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- //上次时间
- Date time = us.getLasttime();
- lastLoginTime = sdf.format(time);
- //新时间
- String format = sdf.format(new Date());
- //string转date 不处理时间格式会不理想
- ParsePosition pos = new ParsePosition(0);
- Date strtodate = sdf.parse(format, pos);
- us.setLasttime(strtodate);
- userService.update(us);
- }
- if (us.getStatus()==1){
- Session session=subject.getSession();
- session.setAttribute("subject", subject);
- session.setAttribute("lastLoginTime",lastLoginTime);
- return "redirect:index";
- }else {
- model.addAttribute("error", "账号已被停用!");
- return "/login";
- }
-
- } catch (AuthenticationException e) {
- model.addAttribute("error", "验证失败!");
- return "/login";
- }
- }
-
-
- }
- /**
- * 订单模块controller
- */
- @Controller
- @RequestMapping("/order")
- public class OrderController {
-
- @Autowired
- OrderService orderService;
- @Autowired
- OrderItemService orderItemService;
-
- /**
- * 所有订单
- * @param model
- * @param page
- * @return
- */
- @RequestMapping("/list")
- public String list(Model model, Page page){
- PageHelper.offsetPage(page.getStart(),page.getCount());
-
- List<Order> os= orderService.list();
-
- int total = (int) new PageInfo<>(os).getTotal();
- page.setTotal(total);
- //为订单添加订单项数据
- orderItemService.fill(os);
-
- model.addAttribute("os", os);
- model.addAttribute("page", page);
- model.addAttribute("totals", total);
-
- return "ordermodule/order-list";
- }
-
- /**
- * 订单发货
- * @param o
- * @return
- */
- @RequestMapping("/orderDelivery")
- public String delivery(Order o){
- o.setStatus(2);
- orderService.update(o);
- return "redirect:list";
- }
-
- /**
- * 查看当前订单的订单项
- * @param oid
- * @param model
- * @return
- */
- @RequestMapping("/seeOrderItem")
- public String seeOrderItem(int oid,Model model){
- Order o = orderService.get(oid);
- orderItemService.fill(o);
- model.addAttribute("orderItems",o.getOrderItems());
- model.addAttribute("total",o.getOrderItems().size());
- model.addAttribute("totalPrice",o.getTotal());
- return "ordermodule/orderItem-list";
- }
-
- }
- /**
- * 管理员controller
- */
- @Controller
- @RequestMapping("/config")
- public class UserController {
- @Autowired
- UserRoleService userRoleService;
- @Autowired
- UserService userService;
- @Autowired
- RoleService roleService;
-
-
- @RequestMapping("/enableStatus")
- @ResponseBody
- public String enableStatus(@RequestParam(value = "name") String name){
- return userService.enableStatus(name);
- }
-
- @RequestMapping("/stopStatus")
- @ResponseBody
- public String stopStatus(@RequestParam(value = "name") String name){
- return userService.stopStatus(name);
- }
-
- @RequestMapping("/adminAdd")
- public String adminadd(Model model){
- List<Role> list = roleService.list();
- model.addAttribute("rolelist",list);
- return "syspage/admin-add";
- }
-
- @RequestMapping("/listUser")
- public String list(Model model, Page page){
-
- PageHelper.offsetPage(page.getStart(),page.getCount());//分页查询
- List<User> us= userService.list();
- int total = (int) new PageInfo<>(us).getTotal();//总条数
- page.setTotal(total);
-
- model.addAttribute("us", us);//所有用户
- model.addAttribute("total",total);
-
- Map<User,List<Role>> user_roles = new HashMap<>();
- //每个用户对应的权限
- for (User user : us) {
- List<Role> roles=roleService.listRoles(user);
- user_roles.put(user, roles);
- }
- model.addAttribute("user_roles", user_roles);
-
- return "syspage/admin-list";
- }
-
- /**
- * 修改管理员角色
- * @param model
- * @param id
- * @return
- */
- @RequestMapping("/editUser")
- public String edit(Model model,Long id){
- List<Role> rs = roleService.list();
- model.addAttribute("rs", rs);
- User user =userService.get(id);
- model.addAttribute("user", user);
- //当前拥有的角色
- List<Role> roles =roleService.listRoles(user);
- model.addAttribute("currentRoles", roles);
-
- return "syspage/admin-edit";
- }
-
- @RequestMapping("deleteUser")
- public String delete(Model model,long id){
- userService.delete(id);
- return "redirect:listUser";
- }
-
- @RequestMapping("updateUser")
- public String update(User user, long[] roleIds){
- userRoleService.setRoles(user,roleIds);
-
- String password=user.getPassword();
- //如果在修改的时候没有设置密码,就表示不改动密码
- if(user.getPassword().length()!=0) {
- String salt = new SecureRandomNumberGenerator().nextBytes().toString();
- int times = 2;
- String algorithmName = "md5";
- String encodedPassword = new SimpleHash(algorithmName,password,salt,times).toString();
- user.setSalt(salt);
- user.setPassword(encodedPassword);
- }
- else
- user.setPassword(null);
-
- userService.update(user);
-
- return "redirect:listUser";
-
- }
-
- @RequestMapping("addUser")
- public String add(User user,long[] roleIds){
-
- String salt = new SecureRandomNumberGenerator().nextBytes().toString();//生成随机数
- int times = 2;
- String algorithmName = "md5";
-
- String encodedPassword = new SimpleHash(algorithmName,user.getPassword(),salt,times).toString();
-
- User u = new User();
- u.setName(user.getName());
- u.setPassword(encodedPassword);
- u.setSalt(salt);
- u.setStatus(1);
- u.setAddress(user.getAddress());
- u.setPhone(user.getPhone());
- userService.add(u);
-
- userRoleService.setRoles(u,roleIds);
-
- return "redirect:listUser";
- }
-
- }
源码获取:俺的博客首页 "资源" 里下载!