|  |  |  | 
|---|
|  |  |  | package com.zy.asrs.wms.system.controller; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.zy.asrs.common.domain.CodeRes; | 
|---|
|  |  |  | import com.zy.asrs.common.domain.enums.LoginSystemType; | 
|---|
|  |  |  | import com.zy.asrs.common.utils.BarcodeUtils; | 
|---|
|  |  |  | import com.zy.asrs.common.utils.QrCode; | 
|---|
|  |  |  | import com.zy.asrs.framework.annotations.ManagerAuth; | 
|---|
|  |  |  | import com.zy.asrs.framework.common.BaseRes; | 
|---|
|  |  |  | import com.zy.asrs.framework.common.Cools; | 
|---|
|  |  |  | import com.zy.asrs.framework.common.R; | 
|---|
|  |  |  | import com.zy.asrs.framework.exception.CoolException; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.zy.asrs.wms.system.controller.param.UpdatePasswordParam; | 
|---|
|  |  |  | import com.zy.asrs.wms.system.controller.result.LoginResult; | 
|---|
|  |  |  | import com.zy.asrs.wms.system.entity.*; | 
|---|
|  |  |  | import com.zy.asrs.wms.system.license.timer.LicenseTimer; | 
|---|
|  |  |  | import com.zy.asrs.wms.system.service.*; | 
|---|
|  |  |  | import com.zy.asrs.wms.utils.JwtUtil; | 
|---|
|  |  |  | import com.zy.asrs.wms.utils.Utils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.security.access.prepost.PreAuthorize; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import javax.imageio.ImageIO; | 
|---|
|  |  |  | import javax.servlet.http.HttpServletRequest; | 
|---|
|  |  |  | import javax.servlet.http.HttpServletResponse; | 
|---|
|  |  |  | import java.awt.image.BufferedImage; | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | private UserRoleService userRoleService; | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private MenuService menuService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LicenseTimer licenseTimer; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostMapping("/login") | 
|---|
|  |  |  | public R login(@RequestBody LoginParam param, HttpServletRequest request) { | 
|---|
|  |  |  | if (!licenseTimer.getSystemSupport()) {//许可证已失效 | 
|---|
|  |  |  | return R.parse(CodeRes.SYSTEM_20001); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String username = param.getUsername(); | 
|---|
|  |  |  | Long hostId = param.getHostId(); | 
|---|
|  |  |  | User user = userService.getByUsername(username, hostId); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @GetMapping("/auth/menu") | 
|---|
|  |  |  | public R userMenu() { | 
|---|
|  |  |  | List<Menu> menus = roleMenuService.listMenuByUserId(getLoginUserId(), Menu.TYPE_MENU, getHostId()); | 
|---|
|  |  |  | boolean superRole = false;//超级管理员 | 
|---|
|  |  |  | User loginUser = getLoginUser(); | 
|---|
|  |  |  | List<Role> roles = userRoleService.listByUserId(loginUser.getId()); | 
|---|
|  |  |  | for (Role role : roles) { | 
|---|
|  |  |  | if (role.getId() == 1) { | 
|---|
|  |  |  | superRole = true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Menu> menus = null; | 
|---|
|  |  |  | if (superRole) { | 
|---|
|  |  |  | menus = roleMenuService.listMenuByUserId(null, Menu.TYPE_MENU, getHostId()); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | menus = roleMenuService.listMenuByUserId(getLoginUserId(), Menu.TYPE_MENU, getHostId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok().add(Utils.toTreeData(menus, 0L, Menu::getParentId, Menu::getId, Menu::setChildren)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | public R router() { | 
|---|
|  |  |  | List<UserRole> userRoles = userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, getLoginUserId())); | 
|---|
|  |  |  | if (userRoles.isEmpty()) { | 
|---|
|  |  |  | return R.error(); | 
|---|
|  |  |  | return R.error("请先分配角色"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | boolean superRole = false;//超级管理员 | 
|---|
|  |  |  | ArrayList<Long> roles = new ArrayList<>(); | 
|---|
|  |  |  | for (UserRole userRole : userRoles) { | 
|---|
|  |  |  | roles.add(userRole.getRoleId()); | 
|---|
|  |  |  | if (userRole.getRoleId() == 1) { | 
|---|
|  |  |  | superRole = true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ArrayList<Long> menus = new ArrayList<>(); | 
|---|
|  |  |  | List<RoleMenu> roleMenus = roleMenuService.list(new LambdaQueryWrapper<RoleMenu>().in(RoleMenu::getRoleId, roles)); | 
|---|
|  |  |  | for (RoleMenu roleMenu : roleMenus) { | 
|---|
|  |  |  | if(!menus.contains(roleMenu.getMenuId())) { | 
|---|
|  |  |  | menus.add(roleMenu.getMenuId()); | 
|---|
|  |  |  | if(superRole) { | 
|---|
|  |  |  | List<Menu> allMenus = menuService.selectByHostId(getHostId()); | 
|---|
|  |  |  | for (Menu menu : allMenus) { | 
|---|
|  |  |  | if(!menus.contains(menu.getId())) { | 
|---|
|  |  |  | menus.add(menu.getId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | List<RoleMenu> roleMenus = roleMenuService.list(new LambdaQueryWrapper<RoleMenu>().in(RoleMenu::getRoleId, roles)); | 
|---|
|  |  |  | for (RoleMenu roleMenu : roleMenus) { | 
|---|
|  |  |  | if(!menus.contains(roleMenu.getMenuId())) { | 
|---|
|  |  |  | menus.add(roleMenu.getMenuId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/code/auth") | 
|---|
|  |  |  | //    @ManagerAuth(memo = "物料编码条形码获取(type:1(条形码);2(二维码)") | 
|---|
|  |  |  | public R matCodeBarcode(@RequestParam(defaultValue = "1") Integer type | 
|---|
|  |  |  | , @RequestParam String param | 
|---|
|  |  |  | , HttpServletResponse response) throws Exception { | 
|---|
|  |  |  | response.setContentType("image/jpg"); | 
|---|
|  |  |  | if (Cools.isEmpty(param)){ | 
|---|
|  |  |  | return R.parse(BaseRes.EMPTY); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | BufferedImage img; | 
|---|
|  |  |  | if (type == 1) { | 
|---|
|  |  |  | img = BarcodeUtils.encode(param); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | img = QrCode.createImg(param); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!ImageIO.write(img, "jpg", response.getOutputStream())) { | 
|---|
|  |  |  | throw new IOException("Could not write an image of format jpg"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | response.getOutputStream().flush(); | 
|---|
|  |  |  | response.getOutputStream().close(); | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|