package com.wyz.shop.admin.controller;/*
* @author 吴彦组
* @date 2021/4/10 12:45
*@v 1.0
* */
import com.wyz.shop.admin.pojo.param.UserParam;
import com.wyz.shop.admin.pojo.system.SysAdmin;
import com.wyz.shop.admin.pojo.system.SysPermission;
import com.wyz.shop.admin.pojo.result.LoginUser;
import com.wyz.shop.admin.pojo.result.SysMenu;
import com.wyz.shop.admin.pojo.shop.ShopStore;
import com.wyz.shop.admin.pojo.system.SysUser;
import com.wyz.shop.admin.service.shop.ShopStoreService;
import com.wyz.shop.admin.service.system.SysAmdinService;
import com.wyz.shop.admin.service.system.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
@Controller
public class ViewController {
@Autowired
private ShopStoreService shopStoreService;
@Autowired
private SysAmdinService sysAmdinService;
@Autowired
private SysUserService sysUserService;
@Autowired
private PasswordEncoder passwordEncoder;
/**
* 我的店铺 销售总额
* */
@RequestMapping("/views/home/homepage2.html")
public String tohmoepage(Model model, ShopStore shopStore, HttpSession session) {
//获取当前用户权限列表
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//从用户认证信息中拿到LoginUser
LoginUser user = (LoginUser) authentication.getPrincipal();
if (user.getType()==2){
shopStore= this.shopStoreService.queryStoreInfoByUserId(user.getSysUser().getId());
}
model.addAttribute("shopStore",shopStore);
session.setAttribute("shopStore1",shopStore);
return "views/home/homepage2";
}
//管理者信息查询
@RequestMapping("/views/template/list/list/addresslist.html")
public String toAddresslist(Model model, UserParam userParam) {
List<SysAdmin> sysAdmins = this.sysAmdinService.querySysUserList(userParam);
model.addAttribute("adminList", sysAdmins);
return "views/template/list/list/addresslist";
}
//修改密码
@RequestMapping("/views/set/user/user/user/user/password.html")
public String toPassword(Model model) {
//获取当前用户权限列表
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//从用户认证信息中拿到LoginUser
LoginUser user = (LoginUser) authentication.getPrincipal();
/**
* 需要得到用户的密码,id,type
* 密码已经加码,需要从数据库拿
* 获取登录信息
*
* **/
String passored=null;
if (user.getType()==2){
SysUser sysUser = user.getSysUser();
Integer id = sysUser.getId();
model.addAttribute("id",id);
SysUser sysUser1 = this.sysUserService.selectUserById(id);
passored=sysUser1.getPassword();
}else if (user.getType()==1){
SysAdmin sysAdmin = user.getSysAdmin();
Integer id=sysAdmin.getId();
model.addAttribute("id",id);
SysAdmin sysAdmin1 = this.sysAmdinService.selectAdminBuId(sysAdmin.getId());
passored=sysAdmin1.getPassword();
}
/* String encode = passwordEncoder.encode(passored);
passwordEncoder.matches(encode,passored);*/
model.addAttribute("password",passored);//可用于判断admin/user
model.addAttribute("choose",user.getType());
;
return "views/set/user/password";
}
//修改基本资料
@RequestMapping("/views/set/user/user/user/user/info.html")
public String toSetInfo(Model model) {
//获取当前用户权限列表
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//从用户认证信息中拿到LoginUser
LoginUser user = (LoginUser) authentication.getPrincipal();
if (user.getType()==2){
SysUser sysUser = user.getSysUser();
//更新用户信息
model.addAttribute("user",sysUser);
}else if (user.getType()==1){
SysAdmin sysAdmin = user.getSysAdmin();
model.addAttribute("user",sysAdmin);
}
model.addAttribute("type",user.getType());//可用于判断admin/user
return "views/set/user/info";
}
@RequestMapping("views/index.html")
public String index(Model model,HttpSession session) {
//获取当前用户权限列表
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//从用户认证信息中拿到LoginUser
LoginUser user = (LoginUser) authentication.getPrincipal();
//判断当前用户的登录类型
if (user.getType() == 2) {
//如果是商家身份登录,那么先检查该用户是否有店铺
ShopStore store = user.getShopStore();
//检查店铺是否存在,若没有,则为该用户添加一个店铺
if (null == store) {
//重定向到添加店铺页面
return "redirect:../shop/addShop.html";
}
//获取用户名和用户头像,并保存到Model中
if (StringUtils.isEmpty(user.getSysUser().getHead())) {
model.addAttribute("headImg", "/image/default_header.png");
} else {
model.addAttribute("headImg", user.getSysUser().getHead());
}
if (StringUtils.isEmpty(user.getSysUser().getNickname())) {
model.addAttribute("nickname", user.getUsername());
} else {
model.addAttribute("nickname", user.getSysUser().getNickname());
}
}else if (user.getType()==1){
//获取用户名和用户头像,并保存到Model中
if (StringUtils.isEmpty(user.getSysAdmin().getHead())) {
model.addAttribute("head", "/image/default_header.png");
} else {
model.addAttribute("head", user.getSysAdmin().getHead());
}
if (StringUtils.isEmpty(user.getSysAdmin().getNickname())) {
model.addAttribute("nickname", user.getUsername());
} else {
model.addAttribute("nickname", user.getSysAdmin().getNickname());
}
}
//将用户信息保存到session中
session.setAttribute("suer",user);
//获取菜单
List<SysPermission> permissions = (List<SysPermission>) authentication.getAuthorities();
List<SysMenu> menus = listToTree(permissions, 0);
model.addAttribute("menus", menus);
//查询列表
return "views/index";
}
private List<SysMenu> listToTree(List<SysPermission> permissions, int pid) {
List<SysMenu> list = new ArrayList<>();
for (int i = 0; i < permissions.size(); i++) {
SysPermission permission = permissions.get(i);
if (permission.getPid() == pid) { //pid=0,一级目录
SysMenu menu = new SysMenu(permission);
if (permission.getType() == 1) { //pid=1,二级目录
List<SysMenu> sons = listT