SysController.java 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. package com.qmth.distributed.print.api;
  2. import com.alibaba.fastjson.JSON;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.qmth.boot.api.annotation.Aac;
  5. import com.qmth.boot.api.constant.ApiConstant;
  6. import com.qmth.boot.api.exception.ApiException;
  7. import com.qmth.boot.core.rateLimit.annotation.RateLimit;
  8. import com.qmth.boot.core.rateLimit.entity.RateLimitTarget;
  9. import com.qmth.distributed.print.business.bean.params.LoginParam;
  10. import com.qmth.teachcloud.common.bean.result.*;
  11. import com.qmth.distributed.print.business.bean.result.EditResult;
  12. import com.qmth.distributed.print.business.entity.TBSyncTask;
  13. import com.qmth.distributed.print.business.enums.DictionaryEnum;
  14. import com.qmth.distributed.print.business.enums.LoginTypeEnum;
  15. import com.qmth.distributed.print.business.service.*;
  16. import com.qmth.distributed.print.business.util.PdfUtil;
  17. import com.qmth.teachcloud.common.annotation.OperationLogDetail;
  18. import com.qmth.teachcloud.common.bean.auth.AuthBean;
  19. import com.qmth.teachcloud.common.config.DictionaryConfig;
  20. import com.qmth.teachcloud.common.contant.SystemConstant;
  21. import com.qmth.teachcloud.common.entity.*;
  22. import com.qmth.teachcloud.common.enums.*;
  23. import com.qmth.teachcloud.common.enums.log.OperationTypeEnum;
  24. import com.qmth.teachcloud.common.service.*;
  25. import com.qmth.teachcloud.common.util.*;
  26. import io.swagger.annotations.*;
  27. import org.apache.commons.collections4.CollectionUtils;
  28. import org.apache.commons.lang3.StringUtils;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import org.springframework.beans.BeanUtils;
  32. import org.springframework.beans.factory.annotation.Value;
  33. import org.springframework.transaction.annotation.Transactional;
  34. import org.springframework.validation.BindingResult;
  35. import org.springframework.web.bind.annotation.*;
  36. import org.springframework.web.multipart.MultipartFile;
  37. import javax.annotation.Resource;
  38. import javax.servlet.http.HttpServletResponse;
  39. import javax.validation.Valid;
  40. import java.io.File;
  41. import java.io.IOException;
  42. import java.io.InputStream;
  43. import java.security.NoSuchAlgorithmException;
  44. import java.util.*;
  45. import java.util.stream.Collectors;
  46. import java.util.stream.Stream;
  47. /**
  48. * @Date: 2021/3/30.
  49. */
  50. @Api(tags = "系统Controller")
  51. @RestController
  52. @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_COMMON)
  53. public class SysController {
  54. private final static Logger log = LoggerFactory.getLogger(SysController.class);
  55. @Value("${com.qmth.logging.file-path}")
  56. private String sysLogPath;
  57. @Resource
  58. SysUserService sysUserService;
  59. @Resource
  60. BasicSchoolService basicSchoolService;
  61. @Resource
  62. BasicVerifyCodeService basicVerifyCodeService;
  63. @Resource
  64. DictionaryConfig dictionaryConfig;
  65. @Resource
  66. CommonCacheService commonCacheService;
  67. @Resource
  68. PrintCommonService printCommonService;
  69. @Resource
  70. TBTaskService tbTaskService;
  71. @Resource
  72. BasicAttachmentService basicAttachmentService;
  73. @Resource
  74. SysUserRoleService sysUserRoleService;
  75. @Resource
  76. TeachcloudCommonService teachcloudCommonService;
  77. @Resource
  78. BasicCourseService basicCourseService;
  79. @Resource
  80. BasicSemesterService basicSemesterService;
  81. @Resource
  82. private FileUploadService fileUploadService;
  83. @Resource
  84. TBSyncTaskService tbSyncTaskService;
  85. @Resource
  86. AuthInfoService authInfoService;
  87. @Resource
  88. BasicOperationLogService basicOperationLogService;
  89. @Resource
  90. ConditionService conditionService;
  91. @Resource
  92. BasicTeachClazzService basicTeachClazzService;
  93. /**
  94. * 登录
  95. *
  96. * @param login
  97. * @return
  98. */
  99. @ApiOperation(value = "登录")
  100. @RequestMapping(value = "/login", method = RequestMethod.POST)
  101. @ApiResponses({@ApiResponse(code = 200, message = "用户信息", response = LoginResult.class)})
  102. @Aac(auth = false)
  103. public Result login(@ApiParam(value = "用户信息", required = true) @Valid @RequestBody LoginParam login, BindingResult bindingResult) throws NoSuchAlgorithmException {
  104. if (bindingResult.hasErrors()) {
  105. return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
  106. }
  107. if (Objects.nonNull(login.getSchoolCode()) && login.getSchoolCode().contains("#")) {
  108. login.setSchoolCode(login.getSchoolCode().substring(0, login.getSchoolCode().indexOf("#")));
  109. }
  110. BasicSchool basicSchool = null;
  111. if (!login.getSchoolCode().equalsIgnoreCase(SystemConstant.ADMIN_CODE)) {
  112. basicSchool = commonCacheService.schoolCache(login.getSchoolCode());
  113. if (Objects.isNull(basicSchool)) {
  114. throw ExceptionResultEnum.SCHOOL_NO_DATA.exception();
  115. }
  116. if (Objects.nonNull(basicSchool.getEnable()) && !basicSchool.getEnable()) {
  117. throw ExceptionResultEnum.SCHOOL_ENABLE.exception();
  118. }
  119. }
  120. QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
  121. if (Objects.nonNull(basicSchool)) {
  122. wrapper.lambda().eq(SysUser::getSchoolId, basicSchool.getId());
  123. }
  124. // 账号密码登录
  125. List<SysUser> userList = null;
  126. if (LoginTypeEnum.ACCOUNT.name().equals(login.getType())) {
  127. String loginName = login.getLoginName();
  128. String password = login.getPassword();
  129. // 非空校验
  130. if (StringUtils.isBlank(loginName)) {
  131. throw ExceptionResultEnum.ERROR.exception("用户名不能为空");
  132. }
  133. if (StringUtils.isBlank(password)) {
  134. throw ExceptionResultEnum.ERROR.exception("密码不能为空");
  135. }
  136. wrapper.lambda().and(w -> w.eq(SysUser::getLoginName, loginName).or().eq(SysUser::getCode, loginName));
  137. userList = sysUserService.list(wrapper);
  138. //用户不存在
  139. if (userList == null || userList.isEmpty()) {
  140. throw ExceptionResultEnum.ERROR.exception("用户名或密码错误");
  141. }
  142. if (userList.size() > 1) {
  143. throw ExceptionResultEnum.ERROR.exception("用户名或密码错误");
  144. }
  145. SysUser sysUser = userList.get(0);
  146. if (!sysUser.getEnable()) {
  147. throw ExceptionResultEnum.ERROR.exception("用户被禁用");
  148. }
  149. // String decodePassword = Base64Util.encode(AesECBUtil.decryptSimple(password).getBytes());
  150. String decodePassword = Base64Util.encode(RSAUtil.decrypt(password).getBytes());
  151. if (!decodePassword.equals(userList.get(0).getPassword())) {
  152. throw ExceptionResultEnum.ERROR.exception("用户名或密码错误");
  153. }
  154. //2022.11.10日加入用户/密码模式是否开启短信验证
  155. if (!login.getSchoolCode().equalsIgnoreCase(SystemConstant.ADMIN_CODE)) {
  156. SysConfig sysConfigAccount = commonCacheService.addSysConfigCache(basicSchool.getId(), SystemConstant.ACCOUNT_SMS_VERIFY);
  157. if (Objects.nonNull(sysConfigAccount) && Objects.nonNull(sysConfigAccount.getConfigValue())
  158. && Boolean.valueOf(sysConfigAccount.getConfigValue())) {
  159. // 校验验证码
  160. sysUserService.checkSmsCode(sysUser.getId(), sysUser.getMobileNumber(), login.getCode());
  161. // 如果不是共用验证码再过期
  162. SysConfig sysConfig = commonCacheService.addSysConfigCache(SystemConstant.SMS_NORMAL_CODE);
  163. Optional.ofNullable(sysConfig).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置万能短信验证码"));
  164. if (!sysConfig.getConfigValue().equals(login.getCode())) {
  165. sysUserService.expiredVerifyCode(sysUser.getId(), sysUser.getMobileNumber());
  166. }
  167. }
  168. }
  169. } else if (LoginTypeEnum.PHONE.name().equals(login.getType())) {
  170. String mobileNumber = login.getMobileNumber();
  171. String code = login.getCode();
  172. // 非空校验
  173. if (StringUtils.isBlank(mobileNumber)) {
  174. throw ExceptionResultEnum.ERROR.exception("手机号不能为空");
  175. }
  176. if (StringUtils.isBlank(code)) {
  177. throw ExceptionResultEnum.ERROR.exception("验证码不能为空");
  178. }
  179. wrapper.lambda().eq(SysUser::getMobileNumber, mobileNumber);
  180. userList = sysUserService.list(wrapper);
  181. //用户不存在
  182. if (userList == null || userList.isEmpty()) {
  183. throw ExceptionResultEnum.ERROR.exception("用户不存在");
  184. }
  185. if (userList.size() > 1) {
  186. throw ExceptionResultEnum.ERROR.exception("查出多个用户");
  187. }
  188. SysUser sysUser = userList.get(0);
  189. // 校验验证码
  190. sysUserService.checkSmsCode(sysUser.getId(), sysUser.getMobileNumber(), code);
  191. // 如果不是共用验证码再过期
  192. SysConfig sysConfig = commonCacheService.addSysConfigCache(SystemConstant.SMS_NORMAL_CODE);
  193. Optional.ofNullable(sysConfig).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置万能短信验证码"));
  194. if (!sysConfig.getConfigValue().equals(login.getCode())) {
  195. sysUserService.expiredVerifyCode(sysUser.getId(), sysUser.getMobileNumber());
  196. }
  197. } else {
  198. throw ExceptionResultEnum.ERROR.exception("登录参数错误");
  199. }
  200. if (login.getSchoolCode().equalsIgnoreCase(SystemConstant.ADMIN_CODE)) {
  201. userList.forEach(o -> {
  202. AuthBean authBean = teachcloudCommonService.getUserAuth(o.getId());
  203. if (Objects.nonNull(authBean) && Objects.nonNull(authBean.getRoleList()) && authBean.getRoleList().size() > 0) {
  204. if (Objects.nonNull(authBean.getSchool())) {
  205. throw ExceptionResultEnum.ERROR.exception("用户无法通过当前页面登录");
  206. }
  207. }
  208. });
  209. }
  210. SysUser sysUser = userList.get(0);
  211. LoginResult loginResult = teachcloudCommonService.login(login.getPassword(), sysUser, AppSourceEnum.SYSTEM);
  212. loginResult.setUiSetting(sysUser.getUiSetting());
  213. // 机器人参数
  214. SysConfig aiRobotSwitch = commonCacheService.addSysConfigCache(sysUser.getSchoolId(), SystemConstant.AI_ROBOT_ENABLE);
  215. SysConfig aiRobotUrl = commonCacheService.addSysConfigCache(sysUser.getSchoolId(), SystemConstant.AI_ROBOT_URL);
  216. loginResult.setAiRobotResult(new AiRobotResult(aiRobotSwitch != null && StringUtils.isNotBlank(aiRobotSwitch.getConfigValue()) ? Boolean.valueOf(aiRobotSwitch.getConfigValue()) : false, aiRobotUrl != null && StringUtils.isNotBlank(aiRobotUrl.getConfigValue()) ? aiRobotUrl.getConfigValue() : null));
  217. try {
  218. basicOperationLogService.saveOperateLogWithNoAuth(sysUser.getSchoolId(), sysUser.getId(), "用户登录", OperationTypeEnum.LOGIN, JSON.toJSONString(login));
  219. } catch (Exception e) {
  220. log.error(e.getMessage());
  221. }
  222. return ResultUtil.ok(loginResult);
  223. }
  224. /**
  225. * 登出
  226. *
  227. * @return
  228. */
  229. @ApiOperation(value = "登出")
  230. @RequestMapping(value = "/logout", method = RequestMethod.POST)
  231. @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
  232. @OperationLogDetail(detail = "用户登出(注销)", operationType = OperationTypeEnum.LOGOUT)
  233. public Result logout() throws NoSuchAlgorithmException {
  234. SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
  235. teachcloudCommonService.removeUserInfo(sysUser.getId(), false);
  236. return ResultUtil.ok();
  237. }
  238. /**
  239. * 发送验证码
  240. *
  241. * @param loginParam
  242. * @return
  243. */
  244. @ApiOperation(value = "发送验证码")
  245. @RequestMapping(value = "/get_verify_code", method = RequestMethod.POST)
  246. @Aac(auth = false)
  247. public Result getVerifyCode(@RequestBody LoginParam loginParam) {
  248. String mobileNumber = loginParam.getMobileNumber();
  249. if (StringUtils.isBlank(mobileNumber)) {
  250. throw ExceptionResultEnum.ERROR.exception("手机号不能为空");
  251. }
  252. QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
  253. if (StringUtils.isNotBlank(loginParam.getSchoolCode())) {
  254. if (loginParam.getSchoolCode().contains("#")) {
  255. loginParam.setSchoolCode(loginParam.getSchoolCode().substring(0, loginParam.getSchoolCode().indexOf("#")));
  256. }
  257. BasicSchool basicSchool = commonCacheService.schoolCache(loginParam.getSchoolCode());
  258. if (Objects.nonNull(basicSchool)) {
  259. wrapper.lambda().eq(SysUser::getSchoolId, basicSchool.getId());
  260. }
  261. }
  262. wrapper.lambda().eq(SysUser::getMobileNumber, mobileNumber);
  263. List<SysUser> userList = sysUserService.list(wrapper);
  264. //用户不存在
  265. if (userList == null || userList.isEmpty()) {
  266. throw ExceptionResultEnum.ERROR.exception("手机号不存在");
  267. }
  268. if (userList.size() > 1) {
  269. throw ExceptionResultEnum.ERROR.exception("手机号绑定了多个用户");
  270. }
  271. SysUser sysUser = userList.get(0);
  272. if (!sysUser.getEnable()) {
  273. throw ExceptionResultEnum.ERROR.exception("手机号被禁用");
  274. }
  275. if (SystemConstant.strNotNull(mobileNumber)) {
  276. basicVerifyCodeService.sendVeirfyCode(mobileNumber, sysUser);
  277. }
  278. int pwdCount = sysUser.getPwdCount();
  279. List<SysRole> sysRoleList = sysUserRoleService.listRoleByUserId(sysUser.getId());
  280. if (sysRoleList.stream().map(SysRole::getType).collect(Collectors.toSet()).contains(RoleTypeEnum.ADMIN)) {
  281. mobileNumber = sysUser.getLoginName() + "(特权用户)";
  282. pwdCount = 1;
  283. }
  284. UserLoginCheckResult userLoginCheckResult = new UserLoginCheckResult(sysUser.getId(), mobileNumber, pwdCount);
  285. return ResultUtil.ok(userLoginCheckResult, "");
  286. }
  287. /**
  288. * 发送验证码
  289. *
  290. * @param loginParam
  291. * @return
  292. */
  293. @ApiOperation(value = "发送验证码")
  294. @RequestMapping(value = "/get_verify_code_by_account", method = RequestMethod.POST)
  295. @Aac(auth = false)
  296. public Object getverifyCodeByAccount(@RequestBody LoginParam loginParam) {
  297. String loginName = loginParam.getLoginName();
  298. String password = loginParam.getPassword();
  299. QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
  300. if (StringUtils.isNotBlank(loginParam.getSchoolCode())) {
  301. if (loginParam.getSchoolCode().contains("#")) {
  302. loginParam.setSchoolCode(loginParam.getSchoolCode().substring(0, loginParam.getSchoolCode().indexOf("#")));
  303. }
  304. BasicSchool basicSchool = commonCacheService.schoolCache(loginParam.getSchoolCode());
  305. wrapper.lambda().eq(SysUser::getSchoolId, basicSchool.getId());
  306. }
  307. wrapper.lambda().eq(SysUser::getLoginName, loginName);
  308. SysUser user = sysUserService.getOne(wrapper);
  309. //用户不存在
  310. if (Objects.isNull(user)) {
  311. throw ExceptionResultEnum.ERROR.exception("用户不存在");
  312. }
  313. if (!password.equals(user.getPassword())) {
  314. throw ExceptionResultEnum.ERROR.exception("密码错误");
  315. }
  316. String mobileNumber = user.getMobileNumber();
  317. if (Objects.isNull(mobileNumber)) {
  318. throw ExceptionResultEnum.ERROR.exception("用户未绑定手机号码");
  319. }
  320. basicVerifyCodeService.sendVeirfyCode(mobileNumber, user);
  321. return ResultUtil.ok(mobileNumber, "");
  322. }
  323. /**
  324. * 发送验证码(绑定手机号)
  325. *
  326. * @param loginParam
  327. * @return
  328. */
  329. @ApiOperation(value = "发送验证码(绑定手机号)")
  330. @RequestMapping(value = "/get_verify_code_for_bind", method = RequestMethod.POST)
  331. @Aac(auth = false)
  332. public Result getVerifyCodeForBind(@RequestBody LoginParam loginParam) {
  333. String loginName = loginParam.getLoginName();
  334. String password = loginParam.getPassword();
  335. String schoolCode = loginParam.getSchoolCode();
  336. if (Objects.nonNull(schoolCode) && schoolCode.contains("#")) {
  337. schoolCode = schoolCode.substring(0, schoolCode.indexOf("#"));
  338. }
  339. String mobileNumber = loginParam.getMobileNumber();
  340. if (StringUtils.isBlank(schoolCode)) {
  341. throw ExceptionResultEnum.ERROR.exception("学校代码不能为空");
  342. }
  343. QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
  344. BasicSchool basicSchool = commonCacheService.schoolCache(schoolCode);
  345. if (Objects.isNull(basicSchool)) {
  346. throw ExceptionResultEnum.ERROR.exception("学校不存在");
  347. }
  348. if (StringUtils.isBlank(mobileNumber)) {
  349. throw ExceptionResultEnum.ERROR.exception("手机号不能为空");
  350. }
  351. wrapper.lambda().eq(SysUser::getSchoolId, basicSchool.getId())
  352. .eq(SysUser::getMobileNumber, mobileNumber);
  353. List<SysUser> userList = sysUserService.list(wrapper);
  354. if (userList != null && userList.size() > 0) {
  355. throw ExceptionResultEnum.ERROR.exception("手机号已使用");
  356. }
  357. // 根据账号/密码取用户ID
  358. if (StringUtils.isBlank(loginName)) {
  359. throw ExceptionResultEnum.ERROR.exception("账号不能为空");
  360. }
  361. if (StringUtils.isBlank(password)) {
  362. throw ExceptionResultEnum.ERROR.exception("密码不能为空");
  363. }
  364. QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
  365. sysUserQueryWrapper.lambda().eq(SysUser::getSchoolId, basicSchool.getId())
  366. .and(w -> w.eq(SysUser::getLoginName, loginName).or().eq(SysUser::getCode, loginName))
  367. .eq(SysUser::getPassword, password);
  368. SysUser sysUser = sysUserService.getOne(sysUserQueryWrapper);
  369. if (sysUser == null) {
  370. throw ExceptionResultEnum.ERROR.exception("用户数据异常");
  371. }
  372. // 发送短信验证码
  373. basicVerifyCodeService.sendVeirfyCode(mobileNumber, sysUser);
  374. int pwdCount = sysUser.getPwdCount();
  375. List<SysRole> sysRoleList = sysUserRoleService.listRoleByUserId(sysUser.getId());
  376. if (sysRoleList.stream().map(SysRole::getType).collect(Collectors.toSet()).contains(RoleTypeEnum.ADMIN)) {
  377. mobileNumber = sysUser.getLoginName() + "(特权用户)";
  378. pwdCount = 1;
  379. }
  380. UserLoginCheckResult userLoginCheckResult = new UserLoginCheckResult(sysUser.getId(), mobileNumber, pwdCount);
  381. return ResultUtil.ok(userLoginCheckResult, "");
  382. }
  383. @ApiOperation(value = "根据机构代码查询机构信息接口")
  384. @RequestMapping(value = "/school/query_by_school_code", method = RequestMethod.POST)
  385. @ApiResponses({@ApiResponse(code = 200, message = "学校信息", response = EditResult.class)})
  386. @Aac(auth = false)
  387. public Result queryBySchoolCode(@ApiParam(value = "机构code") @RequestParam(required = false) String code) {
  388. if (Objects.nonNull(code) && code.contains("#")) {
  389. code = code.substring(0, code.indexOf("#"));
  390. }
  391. Map<String, Object> map = new HashMap<>();
  392. map.put(SystemConstant.VERSION, SystemConstant.VERSION_VALUE);
  393. if (StringUtils.isBlank(code) || !SystemConstant.ADMIN_CODE_LIST.contains(code.toLowerCase())) {
  394. BasicSchool basicSchool = null;
  395. if (StringUtils.isNotBlank(code)) {
  396. basicSchool = commonCacheService.schoolCache(code);
  397. }
  398. if (basicSchool == null) {
  399. List<BasicSchool> basicSchoolList = basicSchoolService.listByDefaultSchool(true);
  400. if (CollectionUtils.isNotEmpty(basicSchoolList)) {
  401. basicSchool = basicSchoolList.get(0);
  402. }
  403. }
  404. Optional.ofNullable(basicSchool).orElseThrow(() -> ExceptionResultEnum.SCHOOL_NO_DATA.exception());
  405. code = basicSchool.getCode();
  406. authInfoService.appHasExpired(code);
  407. SysConfig sysConfig = commonCacheService.addSysConfigCache(basicSchool.getId(), SystemConstant.ACCOUNT_SMS_VERIFY);
  408. SysConfig cardSize = commonCacheService.addSysConfigCache(basicSchool.getId(), SystemConstant.CARD_SIZE_LIST);
  409. map.put(SystemConstant.LOGO, basicSchool.getLogo());
  410. String backgroundImageUrl = null;
  411. if (StringUtils.isNotBlank(basicSchool.getBackgroundImage())) {
  412. try {
  413. backgroundImageUrl = fileUploadService.filePreview(basicSchool.getBackgroundImage());
  414. } catch (Exception e) {
  415. // 不处理,返回null
  416. }
  417. }
  418. map.put("backgroundImageUrl", backgroundImageUrl);
  419. map.put("name", basicSchool.getName());
  420. map.put("accountSmsVerify", Objects.nonNull(sysConfig) ? Boolean.valueOf(sysConfig.getConfigValue()) : false);
  421. // 若为在线激活,则允许手机号登录
  422. map.put("phoneLogin", authInfoService.allowPhoneLogin(basicSchool.getId()));
  423. map.put("schoolCode", code);
  424. // 广药命题界面提示信息
  425. map.put("examTaskInstr", basicSchool.getExamTaskInstr());
  426. map.put("cardSize", Objects.nonNull(cardSize) ? Arrays.asList(
  427. cardSize.getConfigValue().replaceAll("\\[", "").replaceAll("\\]", "").split(", ")) : null);
  428. } else {
  429. SysConfig sysConfig = commonCacheService.addSysConfigCache(SystemConstant.ADMIN_LOGO_URL);
  430. map.put(SystemConstant.LOGO, Objects.nonNull(sysConfig) ? sysConfig.getConfigValue() : null);
  431. map.put("backgroundImageUrl", null);
  432. map.put("name", null);
  433. map.put("accountSmsVerify", false);
  434. // 若为在线激活,则允许手机号登录,超管默认false
  435. map.put("phoneLogin", false);
  436. map.put("schoolCode", SystemConstant.ADMIN_CODE);
  437. }
  438. return ResultUtil.ok(map);
  439. }
  440. @ApiOperation(value = "文件上传接口")
  441. @RequestMapping(value = "/file/upload", method = RequestMethod.POST)
  442. @Transactional
  443. @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
  444. public Result fileUpload(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
  445. @ApiParam(value = "上传文件类型", required = true) @RequestParam UploadFileEnum type) {
  446. BasicAttachment basicAttachment = null;
  447. try {
  448. basicAttachment = printCommonService.saveAttachment(file, ServletUtil.getRequestMd5(), type);
  449. if (Objects.isNull(basicAttachment)) {
  450. throw ExceptionResultEnum.ATTACHMENT_ERROR.exception();
  451. }
  452. } catch (Exception e) {
  453. log.error(SystemConstant.LOG_ERROR, e);
  454. if (Objects.nonNull(basicAttachment)) {
  455. basicAttachmentService.deleteAttachment(basicAttachment);
  456. }
  457. if (e instanceof ApiException) {
  458. ResultUtil.error((ApiException) e, ((ApiException) e).getCode(), e.getMessage());
  459. } else {
  460. ResultUtil.error(e.getMessage());
  461. }
  462. }
  463. return ResultUtil.ok(new EditResult(basicAttachment.getId(), teachcloudCommonService.filePreview(basicAttachment.getPath()), basicAttachment.getPages()));
  464. }
  465. @ApiOperation(value = "考试试卷文件上传接口")
  466. @RequestMapping(value = "/file/exam_paper_upload", method = RequestMethod.POST)
  467. @Transactional
  468. @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
  469. public Result fileExamPaperUpload(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
  470. @ApiParam(value = "上传文件类型", required = true) @RequestParam UploadFileEnum type) {
  471. Map<String, EditResult> resultMap = new HashMap<>();
  472. BasicAttachment basicOriginalAttachment = null;
  473. BasicAttachment basicTranslateAttachment = null;
  474. File pdfOriginalFile = null;
  475. File localTempFile = null;
  476. try {
  477. pdfOriginalFile = FileUtil.multipartFileToFile(file);
  478. // PageSizeEnum pageSizeEnum1= PdfUtil.getPdfFormat(pdfOriginalFile);
  479. basicOriginalAttachment = printCommonService.saveAttachment(file, ServletUtil.getRequestMd5(), type);
  480. if (Objects.isNull(basicOriginalAttachment)) {
  481. throw ExceptionResultEnum.ATTACHMENT_ERROR.exception();
  482. }
  483. EditResult original = new EditResult(basicOriginalAttachment.getId(), teachcloudCommonService.filePreview(basicOriginalAttachment.getPath()), basicOriginalAttachment.getPages(), null);
  484. EditResult translate = new EditResult();
  485. BeanUtils.copyProperties(original, translate);
  486. // switch (pageSizeEnum) {
  487. // case A3:
  488. // basicOriginalAttachment = printCommonService.saveAttachment(file, ServletUtil.getRequestMd5(), type);
  489. // if (Objects.isNull(basicOriginalAttachment)) {
  490. // throw ExceptionResultEnum.ATTACHMENT_ERROR.exception();
  491. // }
  492. // original.setId(basicOriginalAttachment.getId());
  493. // original.setUrl(teachcloudCommonService.filePreview(basicOriginalAttachment.getPath()));
  494. // original.setPages(basicOriginalAttachment.getPages());
  495. //
  496. // BeanUtils.copyProperties(original, translate);
  497. // break;
  498. // case A4:
  499. // basicOriginalAttachment = printCommonService.saveAttachment(file, ServletUtil.getRequestMd5(), type);
  500. // if (Objects.isNull(basicOriginalAttachment)) {
  501. // throw ExceptionResultEnum.ATTACHMENT_ERROR.exception();
  502. // }
  503. // original.setId(basicOriginalAttachment.getId());
  504. // original.setUrl(teachcloudCommonService.filePreview(basicOriginalAttachment.getPath()));
  505. // original.setPages(basicOriginalAttachment.getPages());
  506. //
  507. // String destUrl = dictionaryConfig.fssLocalPdfDomain().getConfig() + File.separator + file.getOriginalFilename();
  508. // localTempFile = new File(destUrl);
  509. // OutputStream out = new FileOutputStream(localTempFile);
  510. // PdfUtil.concatPDFs(new FileInputStream(pdfOriginalFile), out, false);
  511. // // File转换成MutipartFile
  512. // FileInputStream inputStream = new FileInputStream(localTempFile);
  513. // MultipartFile multipartFile = new MockMultipartFile(localTempFile.getName(), localTempFile.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
  514. // basicTranslateAttachment = printCommonService.saveAttachment(multipartFile, DigestUtils.md5Hex(multipartFile.getBytes()), type);
  515. // translate.setId(basicTranslateAttachment.getId());
  516. // translate.setUrl(teachcloudCommonService.filePreview(basicTranslateAttachment.getPath()));
  517. // translate.setPages(basicTranslateAttachment.getPages());
  518. // break;
  519. // default:
  520. // throw ExceptionResultEnum.ERROR.exception("请上传A4或A3格式的试卷文件");
  521. // }
  522. resultMap.put("original", original);
  523. resultMap.put("translate", translate);
  524. } catch (Exception e) {
  525. log.error(SystemConstant.LOG_ERROR, e);
  526. if (Objects.nonNull(basicOriginalAttachment)) {
  527. basicAttachmentService.deleteAttachment(basicOriginalAttachment);
  528. }
  529. if (Objects.nonNull(basicTranslateAttachment)) {
  530. basicAttachmentService.deleteAttachment(basicTranslateAttachment);
  531. }
  532. if (e instanceof ApiException) {
  533. ResultUtil.error((ApiException) e, ((ApiException) e).getCode(), e.getMessage());
  534. } else {
  535. ResultUtil.error(e.getMessage());
  536. }
  537. } finally {
  538. if (Objects.nonNull(localTempFile) && Objects.nonNull(dictionaryConfig.sysDomain()) && dictionaryConfig.sysDomain().isOss()) {
  539. FileUtil.deleteFile(localTempFile);
  540. }
  541. if (Objects.nonNull(pdfOriginalFile) && Objects.nonNull(dictionaryConfig.sysDomain())) {
  542. FileUtil.deleteFile(pdfOriginalFile);
  543. }
  544. }
  545. return ResultUtil.ok(resultMap);
  546. }
  547. @ApiOperation(value = "文件下载接口")
  548. @RequestMapping(value = "/file/download", method = RequestMethod.POST)
  549. @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
  550. public Result fileDownload(@ApiParam(value = "任务id", required = true) @RequestParam String id,
  551. @ApiParam(value = "下载文件类型", required = true) @RequestParam DownloadFileEnum type) {
  552. String path = null;
  553. if (DownloadFileEnum.SYNC_REPORT.equals(type)) {
  554. TBSyncTask tbSyncTask = tbSyncTaskService.getById(SystemConstant.convertIdToLong(id));
  555. if (Objects.isNull(tbSyncTask)) {
  556. throw ExceptionResultEnum.SYNC_TASK_NO_DATA.exception();
  557. }
  558. path = tbSyncTask.getReportFilePath();
  559. } else {
  560. TBTask tbTask = tbTaskService.getById(Long.parseLong(id));
  561. if (Objects.isNull(tbTask)) {
  562. throw ExceptionResultEnum.TASK_NO_DATA.exception();
  563. }
  564. switch (type) {
  565. case IMPORT_FILE:
  566. path = tbTask.getImportFilePath();
  567. break;
  568. case TASK_REPORT:
  569. path = tbTask.getReportFilePath();
  570. break;
  571. case RESULT:
  572. path = tbTask.getResultFilePath();
  573. break;
  574. case ERROR_FILE:
  575. path = tbTask.getErrorFilePath();
  576. break;
  577. default:
  578. break;
  579. }
  580. }
  581. if (Objects.isNull(path)) {
  582. throw ExceptionResultEnum.PATH_NO_DATA.exception();
  583. }
  584. return ResultUtil.ok(new EditResult(teachcloudCommonService.filePreview(path)));
  585. }
  586. @ApiOperation(value = "文件预览接口")
  587. @RequestMapping(value = "/file/preview", method = RequestMethod.POST)
  588. @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
  589. public Result filePreview(@ApiParam(value = "附件id", defaultValue = "") @RequestParam(value = "ids", required = false) List<Long> ids) {
  590. if (CollectionUtils.isEmpty(ids)) {
  591. throw ExceptionResultEnum.ERROR.exception("没有找到文件");
  592. } else {
  593. List<EditResult> list = new ArrayList<>();
  594. for (Long id : ids) {
  595. BasicAttachment basicAttachment = basicAttachmentService.getById(id);
  596. list.add(new EditResult(teachcloudCommonService.filePreview(basicAttachment.getPath())));
  597. }
  598. return ResultUtil.ok(list);
  599. }
  600. }
  601. @ApiOperation(value = "PDF加水印")
  602. @RequestMapping(value = "/pdf/watermark/preview", method = RequestMethod.POST)
  603. @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
  604. @Aac(auth = false)
  605. public void filePreview(@ApiParam(value = "附件id", defaultValue = "") @RequestParam(value = "id", required = false) Long id,
  606. HttpServletResponse response) {
  607. if (id == null) {
  608. throw ExceptionResultEnum.ERROR.exception("请求参数有误");
  609. }
  610. basicAttachmentService.addMaterMark(id, response);
  611. }
  612. @ApiOperation(value = "查询用户权限")
  613. @RequestMapping(value = "/get_menu", method = RequestMethod.POST)
  614. public Result getMenu() {
  615. return ResultUtil.ok(sysUserRoleService.listByUserId());
  616. }
  617. @ApiOperation(value = "获取服务器时间")
  618. @RequestMapping(value = "/get_system_time", method = RequestMethod.POST)
  619. @Aac(auth = false)
  620. public Result getSystemTime() {
  621. return ResultUtil.ok(System.currentTimeMillis());
  622. }
  623. @ApiOperation(value = "共用接口-查询字典")
  624. @RequestMapping(value = "/get_dictionary", method = RequestMethod.POST)
  625. @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
  626. public Result findDictionaryList(@ApiParam(value = "学生学院id") @RequestParam(required = false) String collegeId,
  627. @ApiParam(value = "专业id") @RequestParam(required = false) String majorId,
  628. @ApiParam(value = "班级id") @RequestParam(required = false) String clazzId,
  629. @ApiParam(value = "学生id") @RequestParam(required = false) String studentId,
  630. @ApiParam(value = "考试id") @RequestParam(required = false) String examId,
  631. @ApiParam(value = "查询字典对象", required = true) @RequestParam DictionaryEnum dictionaryEnum) {
  632. Long schoolId = SystemConstant.convertIdToLong(ServletUtil.getRequestHeaderSchoolId().toString());
  633. List<DictionaryResult> dictionaryResultList = new ArrayList<>();
  634. switch (dictionaryEnum) {
  635. case SEMESTER:
  636. List<BasicSemester> basicSemesterList = basicSemesterService.list(
  637. new QueryWrapper<BasicSemester>().lambda().eq(BasicSemester::getSchoolId, schoolId).eq(BasicSemester::getEnable, true).orderByDesc(BasicSemester::getCreateTime));
  638. dictionaryResultList = basicSemesterList.stream().map(e -> {
  639. DictionaryResult dictionaryResult = new DictionaryResult();
  640. dictionaryResult.setId(e.getId());
  641. dictionaryResult.setCode(e.getCode());
  642. dictionaryResult.setName(e.getName());
  643. return dictionaryResult;
  644. }).collect(Collectors.toList());
  645. break;
  646. case COLLEGE:
  647. List<SysOrg> sysOrgs = conditionService.listCollege();
  648. dictionaryResultList = sysOrgs.stream().map(e -> {
  649. DictionaryResult dictionaryResult = new DictionaryResult();
  650. dictionaryResult.setId(e.getId());
  651. dictionaryResult.setName(e.getName());
  652. return dictionaryResult;
  653. }).collect(Collectors.toList());
  654. break;
  655. case CLAZZ:
  656. QueryWrapper<BasicTeachClazz> clazzQueryWrapper = new QueryWrapper<>();
  657. clazzQueryWrapper.lambda().eq(BasicTeachClazz::getSchoolId, schoolId).eq(BasicTeachClazz::getExamId, examId);
  658. List<BasicTeachClazz> basicTeachClazzList = basicTeachClazzService.list(clazzQueryWrapper);
  659. List<DictionaryResult> resultBasicClazzList = basicTeachClazzList.stream().map(e -> {
  660. DictionaryResult dictionaryResult = new DictionaryResult();
  661. dictionaryResult.setId(e.getId());
  662. dictionaryResult.setName(e.getClazzName());
  663. return dictionaryResult;
  664. }).collect(Collectors.toList());
  665. dictionaryResultList.addAll(resultBasicClazzList);
  666. break;
  667. case ALL_CLAZZ: //针对归档管理临时处理用
  668. throw ExceptionResultEnum.ERROR.exception("使用枚举[CLAZZ]");
  669. case COURSE: // 考生字典查询课程
  670. dictionaryResultList = basicCourseService.list(
  671. new QueryWrapper<BasicCourse>().lambda().eq(BasicCourse::getSchoolId, schoolId)).stream().flatMap(e -> {
  672. DictionaryResult dictionaryResult = new DictionaryResult();
  673. dictionaryResult.setId(e.getId());
  674. dictionaryResult.setCode(e.getCode());
  675. dictionaryResult.setName(e.getName());
  676. return Stream.of(dictionaryResult);
  677. }).collect(Collectors.toList());
  678. break;
  679. // case STUDENT:
  680. // QueryWrapper<BasicExamStudent> studentQueryWrapper = new QueryWrapper<>();
  681. // studentQueryWrapper.lambda().eq(BasicExamStudent::getSchoolId, schoolId);
  682. // if (SystemConstant.longNotNull(SystemConstant.convertIdToLong(clazzId))) {
  683. // studentQueryWrapper.lambda().eq(BasicExamStudent::getClazzId, clazzId);
  684. // }
  685. // if (SystemConstant.longNotNull(SystemConstant.convertIdToLong(studentId))) {
  686. // studentQueryWrapper.lambda().eq(BasicExamStudent::getId, studentId);
  687. // }
  688. // List<BasicExamStudent> basicExamStudentList = basicExamStudentService.list(studentQueryWrapper);
  689. // dictionaryResultList = basicExamStudentList.stream().map(e -> {
  690. // DictionaryResult dictionaryResult = new DictionaryResult();
  691. // dictionaryResult.setId(e.getId());
  692. // dictionaryResult.setCode(e.getStudentCode());
  693. // dictionaryResult.setName(e.getStudentName());
  694. // return dictionaryResult;
  695. // }).collect(Collectors.toList());
  696. // break;
  697. default:
  698. break;
  699. }
  700. return ResultUtil.ok(dictionaryResultList);
  701. }
  702. /**
  703. * 获取用户阅卷角色
  704. *
  705. * @return
  706. */
  707. @ApiOperation(value = "获取用户阅卷角色")
  708. @RequestMapping(value = "/get_open_role", method = RequestMethod.POST)
  709. public Result getOpenRole() {
  710. SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
  711. // SpecialPrivilegeEnum userSpecialPrivilege = sysUserService.findUserSpecialPrivilegeByUserId(sysUser.getId(), sysUser.getSchoolId());
  712. List list = new ArrayList<>();
  713. // if (!SpecialPrivilegeEnum.UNIDENTIFIED.equals(userSpecialPrivilege)) {
  714. // if (SpecialPrivilegeEnum.COMPOSITE.equals(userSpecialPrivilege)) {
  715. // list.add(SpecialPrivilegeEnum.MARKER);
  716. // } else {
  717. // list.add(userSpecialPrivilege);
  718. // }
  719. // }
  720. return ResultUtil.ok(list);
  721. }
  722. /**
  723. * 下载导入模板
  724. *
  725. * @return
  726. */
  727. @ApiOperation(value = "下载导入模板")
  728. @RequestMapping(value = "/download_import_template", method = RequestMethod.POST)
  729. public void downloadImportTemplate(@RequestParam(value = "type") String type, HttpServletResponse response) throws IOException {
  730. List<String> templateEnums = Stream.of(ImportTemplateEnum.values()).map(m -> m.name()).collect(Collectors.toList());
  731. if (!templateEnums.contains(type)) {
  732. throw ExceptionResultEnum.ERROR.exception("不支持的模板类型");
  733. }
  734. ImportTemplateEnum importTemplateEnum = ImportTemplateEnum.valueOf(type);
  735. InputStream inputStream = FileUtil.getStream("temps/" + importTemplateEnum.getTemplateName());
  736. // 导出
  737. FileUtil.outputFile(response, inputStream, importTemplateEnum.getFileName());
  738. }
  739. @ApiOperation(value = "系统日志下载")
  740. @RequestMapping(value = "/log/download", method = RequestMethod.POST)
  741. @Aac(auth = false)
  742. public void sysLogDownload(HttpServletResponse response) {
  743. FileUtil.outputFile(response, new File(sysLogPath), "sys.log");
  744. }
  745. }