SysIndexController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package com.ruoyi.web.controller.system;
  2. import java.util.*;
  3. import javax.servlet.http.Cookie;
  4. import javax.servlet.http.HttpServletResponse;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.auth0.jwt.JWT;
  7. import com.auth0.jwt.algorithms.Algorithm;
  8. import com.ruoyi.cc.domain.CcExtNum;
  9. import com.ruoyi.cc.domain.CcGateways;
  10. import com.ruoyi.cc.domain.SysDivisionData;
  11. import com.ruoyi.cc.service.ICcExtNumService;
  12. import com.ruoyi.cc.service.ICcGatewaysService;
  13. import com.ruoyi.cc.service.ICcParamsService;
  14. import com.ruoyi.cc.service.ISysDivisionDataService;
  15. import com.ruoyi.common.config.ServerConfig;
  16. import com.ruoyi.common.utils.*;
  17. import com.ruoyi.common.utils.file.FileUploadUtils;
  18. import com.ruoyi.common.utils.file.FileUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.beans.factory.annotation.Value;
  21. import org.springframework.stereotype.Controller;
  22. import org.springframework.ui.ModelMap;
  23. import org.springframework.web.bind.annotation.GetMapping;
  24. import org.springframework.web.bind.annotation.PathVariable;
  25. import org.springframework.web.bind.annotation.PostMapping;
  26. import org.springframework.web.bind.annotation.ResponseBody;
  27. import com.ruoyi.common.config.RuoYiConfig;
  28. import com.ruoyi.common.constant.ShiroConstants;
  29. import com.ruoyi.common.core.controller.BaseController;
  30. import com.ruoyi.common.core.domain.AjaxResult;
  31. import com.ruoyi.common.core.domain.entity.SysMenu;
  32. import com.ruoyi.common.core.domain.entity.SysUser;
  33. import com.ruoyi.common.core.text.Convert;
  34. import com.ruoyi.framework.shiro.service.SysPasswordService;
  35. import com.ruoyi.system.service.ISysConfigService;
  36. import com.ruoyi.system.service.ISysMenuService;
  37. import org.springframework.web.multipart.MultipartFile;
  38. /**
  39. * 首页 业务处理
  40. *
  41. * @author ruoyi
  42. */
  43. @Controller
  44. public class SysIndexController extends BaseController
  45. {
  46. @Autowired
  47. private ISysMenuService menuService;
  48. @Autowired
  49. private ISysConfigService configService;
  50. @Autowired
  51. private SysPasswordService passwordService;
  52. @Autowired
  53. private ICcExtNumService ccExtNumService;
  54. @Autowired
  55. private ICcGatewaysService ccGatewaysService;
  56. @Autowired
  57. private ICcParamsService ccParamsService;
  58. @Autowired
  59. private ServerConfig serverConfig;
  60. private String scriptServer;
  61. private String scriptPort;
  62. /**
  63. * 系统包版本
  64. */
  65. @Value("${sysconfig.sysVersion}")
  66. private String sysVersion;
  67. // 系统首页
  68. @GetMapping("/index")
  69. public String index(ModelMap mmap)
  70. {
  71. // 取身份信息
  72. SysUser user = getSysUser();
  73. // 根据用户id取出菜单
  74. List<SysMenu> menus = menuService.selectMenusByUser(user);
  75. mmap.put("menus", menus);
  76. mmap.put("user", user);
  77. mmap.put("sideTheme", configService.selectConfigByKey("sys.index.sideTheme"));
  78. mmap.put("skinName", configService.selectConfigByKey("sys.index.skinName"));
  79. Boolean footer = Convert.toBool(configService.selectConfigByKey("sys.index.footer"), true);
  80. Boolean tagsView = Convert.toBool(configService.selectConfigByKey("sys.index.tagsView"), true);
  81. mmap.put("footer", footer);
  82. mmap.put("tagsView", tagsView);
  83. mmap.put("mainClass", contentMainClass(footer, tagsView));
  84. mmap.put("copyrightYear", RuoYiConfig.getCopyrightYear());
  85. mmap.put("sysVersion", sysVersion);
  86. mmap.put("isLocalDebug", RuoYiConfig.isLocalDebug());
  87. mmap.put("localRemoteHost", RuoYiConfig.getLocalRemoteHost());
  88. if (user.getLoginName().equals("admin")) {
  89. mmap.put("demoEnabled", RuoYiConfig.isDemoEnabled());
  90. } else {
  91. mmap.put("demoEnabled", false);
  92. }
  93. mmap.put("isDefaultModifyPwd", initPasswordIsModify(user.getPwdUpdateDate()));
  94. mmap.put("isPasswordExpired", passwordIsExpiration(user.getPwdUpdateDate()));
  95. mmap.put("isMobile", ServletUtils.checkAgentIsMobile(ServletUtils.getRequest().getHeader("User-Agent")));
  96. // logo
  97. String logoPath = user.getLogo();
  98. if (StringUtils.isBlank(logoPath)) {
  99. logoPath = configService.selectConfigByKey("sys.logo", "/img/logo.png");
  100. }
  101. mmap.put("logoPath", logoPath);
  102. // 菜单导航显示风格
  103. String menuStyle = configService.selectConfigByKey("sys.index.menuStyle");
  104. // 移动端,默认使左侧导航菜单,否则取默认配置
  105. String indexStyle = ServletUtils.checkAgentIsMobile(ServletUtils.getRequest().getHeader("User-Agent")) ? "index" : menuStyle;
  106. // 优先Cookie配置导航菜单
  107. Cookie[] cookies = ServletUtils.getRequest().getCookies();
  108. for (Cookie cookie : cookies)
  109. {
  110. if (StringUtils.isNotEmpty(cookie.getName()) && "nav-style".equalsIgnoreCase(cookie.getName()))
  111. {
  112. indexStyle = cookie.getValue();
  113. break;
  114. }
  115. }
  116. String webIndex = "topnav".equalsIgnoreCase(indexStyle) ? "index-topnav" : "index";
  117. return webIndex;
  118. }
  119. // 锁定屏幕
  120. @GetMapping("/lockscreen")
  121. public String lockscreen(ModelMap mmap)
  122. {
  123. mmap.put("user", getSysUser());
  124. ServletUtils.getSession().setAttribute(ShiroConstants.LOCK_SCREEN, true);
  125. return "lock";
  126. }
  127. // 解锁屏幕
  128. @PostMapping("/unlockscreen")
  129. @ResponseBody
  130. public AjaxResult unlockscreen(String password)
  131. {
  132. SysUser user = getSysUser();
  133. if (StringUtils.isNull(user))
  134. {
  135. return AjaxResult.error("服务器超时,请重新登录");
  136. }
  137. if (passwordService.matches(user, password))
  138. {
  139. ServletUtils.getSession().removeAttribute(ShiroConstants.LOCK_SCREEN);
  140. return AjaxResult.success();
  141. }
  142. return AjaxResult.error("密码不正确,请重新输入。");
  143. }
  144. // 切换主题
  145. @GetMapping("/system/switchSkin")
  146. public String switchSkin()
  147. {
  148. return "skin";
  149. }
  150. // 切换菜单
  151. @GetMapping("/system/menuStyle/{style}")
  152. public void menuStyle(@PathVariable String style, HttpServletResponse response)
  153. {
  154. CookieUtils.setCookie(response, "nav-style", style);
  155. }
  156. // system main / agent workbench (embed=1 supported)
  157. @GetMapping("/system/main")
  158. public String main(ModelMap mmap)
  159. {
  160. mmap.put("version", RuoYiConfig.getVersion());
  161. boolean embed = "1".equals(ServletUtils.getParameter("embed"));
  162. mmap.put("embedMode", embed);
  163. mmap.put("isLocalDebug", RuoYiConfig.isLocalDebug());
  164. mmap.put("localRemoteHost", RuoYiConfig.getLocalRemoteHost());
  165. // current user
  166. SysUser currentUser = ShiroUtils.getSysUser();
  167. if (currentUser == null)
  168. {
  169. if (embed)
  170. {
  171. mmap.put("errorMsg", "登录会话无效,请重新外呼登录");
  172. mmap.put("errorCode", "NO_SESSION");
  173. return "error/embedWorkbench";
  174. }
  175. return "redirect:/login";
  176. }
  177. // extension number
  178. CcExtNum ccExtNum = ccExtNumService.selectCcExtNumByUserCodeTwo(currentUser.getLoginName());
  179. if (ccExtNum == null || ccExtNum.getExtNum() == null) {
  180. // embed 下禁止 redirect:/login(会再次触发 LOGIN_READY,被 HIS 误判为 Cookie 回弹)
  181. if (embed)
  182. {
  183. mmap.put("errorMsg", "未绑定分机号,请联系系统管理员");
  184. mmap.put("errorCode", "NO_EXT");
  185. return "error/embedWorkbench";
  186. }
  187. return "redirect:/login";
  188. }
  189. String extnum = ccExtNum.getExtNum().toString();
  190. String opnum = ccExtNum.getUserCode();
  191. String groupId = (String) CacheUtils.get("groupId-" + currentUser.getLoginName());
  192. if (StringUtils.isBlank(groupId)) {
  193. groupId = "1";
  194. }
  195. String skillLevel = "9";
  196. String projectId = "1";
  197. String loginToken = ccExtNumService.createToken(extnum, opnum, groupId, skillLevel, projectId);
  198. // myGateway from login; always restrict to purposes 1/3 (exclude 2 outbound tasks)
  199. String myGateway = (String) CacheUtils.get("myGateway-" + currentUser.getLoginName());
  200. CcGateways ccGatewaysVo = new CcGateways();
  201. Map<String, Object> params = new HashMap<>();
  202. params.put("purposes", Arrays.asList(1, 3));
  203. ccGatewaysVo.setParams(params);
  204. if (StringUtils.isNotBlank(myGateway)) {
  205. List<Long> gatewayIds = Arrays.stream(myGateway.split(","))
  206. .filter(StringUtils::isNotBlank)
  207. .map(String::trim)
  208. .map(Long::parseLong)
  209. .collect(java.util.stream.Collectors.toList());
  210. ccGatewaysVo.setGatewayIds(gatewayIds);
  211. }
  212. List<CcGateways> gatewaysList = ccGatewaysService.selectCcGatewaysList(ccGatewaysVo);
  213. List<JSONObject> gatewayList = new ArrayList<>();
  214. for (CcGateways ccGateways: gatewaysList) {
  215. JSONObject configGateway = new JSONObject();
  216. configGateway.put("uuid", ccGateways.getId().toString());
  217. configGateway.put("updateTime", ccGateways.getUpdateTime());
  218. configGateway.put("gatewayAddr", ccGateways.getGwAddr());
  219. configGateway.put("callerNumber", ccGateways.getCaller());
  220. configGateway.put("calleePrefix", ccGateways.getCalleePrefix());
  221. configGateway.put("callProfile", ccGateways.getProfileName());
  222. configGateway.put("priority", ccGateways.getPriority());
  223. configGateway.put("concurrency", ccGateways.getMaxConcurrency());
  224. configGateway.put("register", ccGateways.getRegister());
  225. configGateway.put("authUsername", ccGateways.getAuthUsername());
  226. configGateway.put("audioCodec", ccGateways.getCodec());
  227. configGateway.put("gwName", ccGateways.getGwName());
  228. gatewayList.add(configGateway);
  229. }
  230. JSONObject callConfig = new JSONObject();
  231. scriptServer = ccParamsService.getParamValueByCode("call-center-server-ip-addr", "");
  232. scriptPort = ccParamsService.getParamValueByCode("call-center-websocket-port", "");
  233. callConfig.put("scriptServer", scriptServer);
  234. callConfig.put("scriptPort", scriptPort);
  235. callConfig.put("loginToken", loginToken);
  236. callConfig.put("gatewayList", gatewayList);
  237. callConfig.put("extNum", extnum);
  238. callConfig.put("opNum", opnum);
  239. callConfig.put("myGateway", myGateway);
  240. mmap.put("callConfig", callConfig);
  241. String autoFree = ccParamsService.getParamValueByCode("phone-bar-free-auto", "false");
  242. mmap.put("autoFree", Boolean.valueOf(autoFree));
  243. return "main";
  244. }
  245. // content-main class
  246. public String contentMainClass(Boolean footer, Boolean tagsView)
  247. {
  248. if (!footer && !tagsView)
  249. {
  250. return "tagsview-footer-hide";
  251. }
  252. else if (!footer)
  253. {
  254. return "footer-hide";
  255. }
  256. else if (!tagsView)
  257. {
  258. return "tagsview-hide";
  259. }
  260. return StringUtils.EMPTY;
  261. }
  262. // 检查初始密码是否提醒修改
  263. public boolean initPasswordIsModify(Date pwdUpdateDate)
  264. {
  265. Integer initPasswordModify = Convert.toInt(configService.selectConfigByKey("sys.account.initPasswordModify"));
  266. return initPasswordModify != null && initPasswordModify == 1 && pwdUpdateDate == null;
  267. }
  268. // 检查密码是否过期
  269. public boolean passwordIsExpiration(Date pwdUpdateDate)
  270. {
  271. Integer passwordValidateDays = Convert.toInt(configService.selectConfigByKey("sys.account.passwordValidateDays"));
  272. if (passwordValidateDays != null && passwordValidateDays > 0)
  273. {
  274. if (StringUtils.isNull(pwdUpdateDate))
  275. {
  276. // 如果从未修改过初始密码,直接提醒过期
  277. return true;
  278. }
  279. Date nowDate = DateUtils.getNowDate();
  280. return DateUtils.differentDaysByMillisecond(nowDate, pwdUpdateDate) > passwordValidateDays;
  281. }
  282. return false;
  283. }
  284. @PostMapping("/uploadLogo")
  285. @ResponseBody
  286. public AjaxResult uploadLogo(MultipartFile file) throws Exception
  287. {
  288. try
  289. {
  290. // 上传文件路径
  291. String filePath = RuoYiConfig.getUploadPath();
  292. // 上传并返回新文件名称
  293. String fileName = FileUploadUtils.upload(filePath, file);
  294. String url = serverConfig.getUrl() + fileName;
  295. AjaxResult ajax = AjaxResult.success();
  296. ajax.put("url", url);
  297. ajax.put("fileName", fileName);
  298. ajax.put("newFileName", FileUtils.getName(fileName));
  299. ajax.put("originalFilename", file.getOriginalFilename());
  300. configService.updateConfigByKey("sys.logo", url);
  301. return ajax;
  302. }
  303. catch (Exception e)
  304. {
  305. return AjaxResult.error(e.getMessage());
  306. }
  307. }
  308. }