UserController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. package com.ruoyi.web.controller.front;
  2. import cn.hutool.core.lang.Dict;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.ruoyi.common.annotation.Anonymous;
  5. import com.ruoyi.common.annotation.Log;
  6. import com.ruoyi.common.core.content.VistorContextHolder;
  7. import com.ruoyi.common.core.controller.BaseController;
  8. import com.ruoyi.common.core.domain.AjaxResult;
  9. import com.ruoyi.common.core.domain.entity.SysDept;
  10. import com.ruoyi.common.core.domain.entity.SysUser;
  11. import com.ruoyi.common.core.domain.model.LoginBody;
  12. import com.ruoyi.common.core.domain.model.LoginCard;
  13. import com.ruoyi.common.core.domain.model.LoginUser;
  14. import com.ruoyi.common.core.page.TableDataInfo;
  15. import com.ruoyi.common.enums.*;
  16. import com.ruoyi.common.utils.NumberUtils;
  17. import com.ruoyi.common.utils.SecurityUtils;
  18. import com.ruoyi.dz.domain.*;
  19. import com.ruoyi.dz.mapper.DzClassesMapper;
  20. import com.ruoyi.dz.mapper.DzSchoolMapper;
  21. import com.ruoyi.dz.service.*;
  22. import com.ruoyi.enums.CardDistributeStatus;
  23. import com.ruoyi.enums.CardStatus;
  24. import com.ruoyi.enums.UserTypeEnum;
  25. import com.ruoyi.framework.web.service.SysPermissionService;
  26. import com.ruoyi.framework.web.service.TokenService;
  27. import com.ruoyi.system.service.ISysConfigService;
  28. import com.ruoyi.system.service.ISysDeptService;
  29. import com.ruoyi.system.service.ISysUserService;
  30. import com.ruoyi.web.service.CommService;
  31. import com.ruoyi.web.service.SysLoginService;
  32. import io.swagger.annotations.Api;
  33. import io.swagger.annotations.ApiOperation;
  34. import io.swagger.annotations.ApiParam;
  35. import org.apache.commons.compress.utils.Lists;
  36. import org.apache.commons.lang3.StringUtils;
  37. import org.springframework.transaction.annotation.Transactional;
  38. import org.springframework.util.CollectionUtils;
  39. import org.springframework.web.bind.annotation.*;
  40. import javax.servlet.http.HttpServletRequest;
  41. import java.util.*;
  42. import java.util.stream.Collectors;
  43. @RestController
  44. @RequestMapping("/front/user")
  45. @Api(tags = "前台-用户")
  46. public class UserController extends BaseController {
  47. private final IDzControlService dzControlService;
  48. private final SysLoginService loginService;
  49. private final ISysUserService userService;
  50. private final SysPermissionService permissionService;
  51. private final TokenService tokenService;
  52. private final CommService commService;
  53. private final ISysConfigService configService;
  54. private final IDzCardsService dzCardsService;
  55. private final IDzSchoolService dzSchoolService;
  56. private final IDzClassesService dzClassesService;
  57. private final DzSchoolMapper dzSchoolMapper;
  58. private final DzClassesMapper dzClassesMapper;
  59. private final IDzAgentService agentService;
  60. private final ISysDeptService deptService;
  61. private final IDzTeacherService dzTeacherService;
  62. private final IDzSubjectService dzSubjectService;
  63. public UserController(IDzControlService dzControlService, SysLoginService loginService, ISysUserService userService, SysPermissionService permissionService, TokenService tokenService, CommService commService, ISysConfigService configService, IDzCardsService dzCardsService, IDzSchoolService dzSchoolService, IDzClassesService dzClassesService, DzSchoolMapper dzSchoolMapper, DzClassesMapper dzClassesMapper, IDzAgentService agentService, ISysDeptService deptService, IDzTeacherService dzTeacherService, IDzSubjectService dzSubjectService) {
  64. this.dzControlService = dzControlService;
  65. this.loginService = loginService;
  66. this.userService = userService;
  67. this.permissionService = permissionService;
  68. this.tokenService = tokenService;
  69. this.commService = commService;
  70. this.configService = configService;
  71. this.dzCardsService = dzCardsService;
  72. this.dzSchoolService = dzSchoolService;
  73. this.dzClassesService = dzClassesService;
  74. this.dzSchoolMapper = dzSchoolMapper;
  75. this.dzClassesMapper = dzClassesMapper;
  76. this.agentService = agentService;
  77. this.deptService = deptService;
  78. this.dzTeacherService = dzTeacherService;
  79. this.dzSubjectService = dzSubjectService;
  80. }
  81. @GetMapping(value = "provinces")
  82. @Anonymous
  83. @ApiOperation("省份列表")
  84. public AjaxResult provinces()
  85. {
  86. DzControl cond = new DzControl();
  87. cond.setIsValid(1);
  88. return AjaxResult.success(dzControlService.selectDzControlList(cond).stream().map(t -> {
  89. JSONObject o = new JSONObject();
  90. o.put("dictValue", t.getLocation());
  91. o.put("dictLabel", t.getLocation());
  92. return o;
  93. }).collect(Collectors.toList()));
  94. }
  95. @GetMapping(value = "examTypes")
  96. @Anonymous
  97. @ApiOperation("考生类型列表")
  98. public AjaxResult examTypes(@RequestParam String location)
  99. {
  100. DzControl cond = new DzControl();
  101. cond.setIsValid(1);
  102. cond.setLocation(location);
  103. List<DzControl> list = dzControlService.selectDzControlList(cond);
  104. String examTypes;
  105. if(CollectionUtils.isEmpty(list) || StringUtils.isBlank(examTypes = list.get(0).getExamTypes())) {
  106. return AjaxResult.success(Collections.emptyList());
  107. }
  108. return AjaxResult.success(Arrays.stream(examTypes.split(",")).map(t -> {
  109. JSONObject o = new JSONObject();
  110. o.put("dictValue", t);
  111. o.put("dictLabel", ExamType.valueOf(t).title());
  112. return o;
  113. }).collect(Collectors.toList()));
  114. }
  115. @GetMapping(value = "examMajors")
  116. @Anonymous
  117. @ApiOperation("专业类别列表")
  118. public AjaxResult examMajor(@RequestParam String location, @RequestParam ExamType examType)
  119. {
  120. if (StringUtils.isBlank(location) || null ==examType) {
  121. examType = VistorContextHolder.getExamType();
  122. location = VistorContextHolder.getLocation();
  123. }
  124. // 构建查询条件
  125. DzSubject cond = new DzSubject();
  126. cond.setLocations(location);
  127. cond.setExamTypes(examType.name());
  128. // 查询科目列表
  129. List<DzSubject> subjectList = dzSubjectService.selectDzSubjectList(cond);
  130. // 转换为返回格式
  131. List<JSONObject> list = subjectList.stream().map(subject -> {
  132. JSONObject o = new JSONObject();
  133. o.put("dictValue", subject.getSubjectId());
  134. o.put("dictLabel", subject.getSubjectName());
  135. return o;
  136. }).collect(Collectors.toList());
  137. return AjaxResult.success(list);
  138. }
  139. @GetMapping(value = "graduateYears")
  140. @Anonymous
  141. @ApiOperation("毕业年份列表")
  142. public AjaxResult graduateYear(@RequestParam String location, @RequestParam ExamType examType)
  143. {
  144. // DzControl cond = new DzControl();
  145. // cond.setIsValid(1);
  146. // cond.setLocation(location);
  147. // List<DzControl> list = dzControlService.selectDzControlList(cond);
  148. // Integer year;
  149. // if(CollectionUtils.isEmpty(list) || null == (year = list.get(0).getSubmitYear())) {
  150. // year = Calendar.getInstance().get(Calendar.YEAR);
  151. // }
  152. // List<JSONObject> resultList = new ArrayList<>();
  153. // for(int i = year; i <= year + 6; i++) {
  154. // JSONObject o = new JSONObject();
  155. // o.put("dictValue", i);
  156. // o.put("dictLabel", String.valueOf(i));
  157. // resultList.add(o);
  158. // }ss
  159. int begin = 2026;
  160. int end = ExamType.VHS.equals(examType) ? 2028 : 2026;
  161. List<JSONObject> resultList = new ArrayList<>();
  162. for(int i = begin; i<= end; i++) {
  163. JSONObject o = new JSONObject();
  164. o.put("dictValue", i);
  165. o.put("dictLabel", String.valueOf(i));
  166. resultList.add(o);
  167. }
  168. return AjaxResult.success(resultList);
  169. }
  170. @GetMapping("/getSchoolList")
  171. @Anonymous
  172. @ApiOperation("查询学校列表")
  173. public TableDataInfo getSchoolList(@ApiParam("examType") @RequestParam(required = false) String examType, @ApiParam("keyword") @RequestParam(required = false) String keyword)
  174. {
  175. DzSchool dzSchool = new DzSchool();
  176. dzSchool.setCampus(false);
  177. dzSchool.setName(keyword);
  178. dzSchool.setExamTypes(StringUtils.isNotBlank(examType) ? examType : VistorContextHolder.getExamType().name());
  179. startPage();
  180. List<DzSchool> list = dzSchoolService.selectDzSchoolList(dzSchool);
  181. return getDataTable(list);
  182. }
  183. @GetMapping("/getClassList")
  184. @Anonymous
  185. @ApiOperation("查询学校班级列表")
  186. public AjaxResult getClassList(@ApiParam("schoolId") Long schoolId)
  187. {
  188. DzClasses cCond = new DzClasses();
  189. cCond.setSchoolId(schoolId);
  190. List<DzClasses> list = dzClassesService.selectDzClassesList(cCond);
  191. return AjaxResult.success(list);
  192. }
  193. /**
  194. * 登录方法
  195. *
  196. * @param loginBody 登录信息i
  197. * @return 结果
  198. */
  199. @PostMapping("userLogin")
  200. @ApiOperation("前台登陆")
  201. @Anonymous
  202. public AjaxResult userLogin(@RequestBody LoginBody loginBody)
  203. {
  204. // 生成令牌
  205. AjaxResult ajax = loginService.login(loginBody.getMobile(), loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid(),AccessFromType.frontApp.name());
  206. return ajax;
  207. }
  208. @GetMapping("getUserBindCard")
  209. @ApiOperation("取用户卡绑定状态")
  210. public AjaxResult getUserBindCard() {
  211. SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
  212. if(null != sysUser.getBindStatus() && BindStatus.Pend.code().equals(sysUser.getBindStatus())) {
  213. DzCards dzCards = dzCardsService.selectDzCardsByCardId(sysUser.getCardId());
  214. return AjaxResult.success(Dict.of("cardNo", dzCards.getCardNo(), "password", dzCards.getPassword()));
  215. }
  216. return AjaxResult.success();
  217. }
  218. /**
  219. * 获取用户信息
  220. *
  221. * @return 用户信息
  222. */
  223. @GetMapping("getInfo")
  224. @ApiOperation("前台个人信息")
  225. public AjaxResult getInfo(HttpServletRequest request)
  226. {
  227. LoginUser loginUser = SecurityUtils.getLoginUser();
  228. SysUser user = loginUser.getUser();
  229. // 角色集合
  230. Set<String> roles = permissionService.getRolePermission(user);
  231. // 权限集合
  232. Set<String> permissions = permissionService.getMenuPermission(user);
  233. if (!loginUser.getPermissions().equals(permissions))
  234. {
  235. loginUser.setPermissions(permissions);
  236. tokenService.refreshToken(loginUser);
  237. }
  238. DzTeacher dzTeacher;
  239. if(UserTypeEnum.isTeacher(user.getUserType()) && null != (dzTeacher = dzTeacherService.selectDzTeacherByTeacherId(user.getUserTypeId()))) {
  240. user.setSchoolId(dzTeacher.getSchoolId());
  241. user.setSchoolName(dzTeacher.getSchoolName());
  242. if(null != dzTeacher.getCampusId()) {
  243. user.setCampusName(dzSchoolMapper.selectDzSchoolById(dzTeacher.getCampusId()).getName());
  244. }
  245. List<String> campusClassList = Lists.newArrayList();
  246. List<String> schoolClassList = Lists.newArrayList();
  247. for(DzClasses dzClass : dzClassesMapper.selectClassesForTeacher(dzTeacher.getTeacherId())) {
  248. if(null == dzClass.getDeptId()) {
  249. schoolClassList.add(dzClass.getName());
  250. } else {
  251. campusClassList.add(dzClass.getName());
  252. }
  253. }
  254. user.setSchoolClassName(StringUtils.join(schoolClassList, ","));
  255. user.setCampusClassName(StringUtils.join(campusClassList, ","));
  256. }
  257. DzCards cards=null;
  258. if(null != user.getCardId()) {
  259. cards = dzCardsService.selectDzCardsByCardId(user.getCardId());
  260. if (null!=cards.getSchoolId()){
  261. DzSchool school = dzSchoolService.selectDzSchoolById(cards.getSchoolId());
  262. if(null != school){
  263. user.setClassSelect(school.getClassSelect());
  264. }
  265. }
  266. }
  267. AjaxResult ajax = AjaxResult.success(user);
  268. ajax.put("card", loginUser.getCard());
  269. ajax.put("roles", roles);
  270. ajax.put("permissions", permissions);
  271. ajax.put("isDefaultModifyPwd", commService.initPasswordIsModify(user.getPwdUpdateDate()));
  272. ajax.put("isPasswordExpired", commService.passwordIsExpiration(user.getPwdUpdateDate()));
  273. // 添加组织信息.卡号/邀请码开始找:再逐步找到 代理商-机构
  274. Long agentId = null;
  275. if(null != cards) {
  276. agentId = cards.getAgentId();
  277. }else{
  278. if (null != user.getInviteCode()) {
  279. String inviteCode = user.getInviteCode();
  280. // 如果邀请码全部是数字,则转换为Long
  281. if (NumberUtils.isNumeric(inviteCode)) {
  282. try {
  283. agentId = Long.parseLong(inviteCode);
  284. } catch (NumberFormatException e) {
  285. // 如果转换失败,保持为null
  286. agentId = null;
  287. }
  288. }
  289. }
  290. }
  291. // 通过代理商查询机构信息
  292. Long deptId = null;
  293. if (null != agentId&&0!=agentId) {
  294. DzAgent agent = agentService.selectDzAgentByAgentId(agentId);
  295. if(null != agent && null != agent.getDeptId()) {
  296. deptId = agent.getDeptId();
  297. }
  298. }else{
  299. if (null!=user.getDeptId()){
  300. deptId = user.getDeptId();
  301. }
  302. }
  303. if(null==deptId){
  304. if (UserTypeEnum.isTeacher(user.getUserType())){
  305. DzTeacher teacher = dzTeacherService.selectDzTeacherByUserId(user.getUserId());
  306. if(null != teacher && null != teacher.getDeptId()){
  307. deptId = teacher.getDeptId();
  308. }
  309. }else if (UserTypeEnum.isAgent(user.getUserType())){
  310. DzAgent agent = agentService.selectDzAgentByUserId(user.getUserId());
  311. if(null != agent && null != agent.getDeptId()){
  312. deptId = agent.getDeptId();
  313. }
  314. }
  315. }
  316. if (null==deptId){
  317. //默认使用单招一卡通
  318. deptId = 101L;
  319. }
  320. // 构建组织信息对象
  321. // 查询机构信息
  322. SysDept dept = deptService.selectDeptById(deptId);
  323. JSONObject org = new JSONObject();
  324. if(null != dept) {
  325. // 优先使用机构中的信息
  326. if(StringUtils.isNotBlank(dept.getLogo())) {
  327. org.put("logo", dept.getLogo());
  328. }
  329. if(StringUtils.isNotBlank(dept.getDeptName())) {
  330. org.put("orgName", dept.getDeptName());
  331. }
  332. if(StringUtils.isNotBlank(dept.getContactPhone())) {
  333. org.put("contactPhone", dept.getContactPhone());
  334. }
  335. }
  336. ajax.put("org", org);
  337. //获取app的配置文件
  338. String type = request.getHeader("type");
  339. //H5 wechat frontApp均需要菜单
  340. if(AccessFromType.isFrontApp(type)||AccessFromType.isH5(type)||AccessFromType.isWechat(type)){
  341. String appConfig = configService.selectConfigByKey("app.common.config");
  342. com.alibaba.fastjson.JSONObject json = com.alibaba.fastjson.JSONObject.parseObject(appConfig);
  343. ajax.put("appConfig", json);
  344. }
  345. // if (null!=user.getCardId()){
  346. // user.setAccountType(AccountType.common.getValue());
  347. // }else {
  348. // user.setAccountType(AccountType.audit.getValue());
  349. // }
  350. //
  351. // //处理accountType。1正常,2审核账号 user.data.accountType=2
  352. // String auditAccount = configService.selectConfigByKey("audit.account");
  353. // if (StringUtils.isNotBlank(auditAccount)){
  354. // if (auditAccount.contains(user.getPhonenumber())||auditAccount.contains(user.getUserName())){
  355. // user.setAccountType(AccountType.audit.getValue());
  356. // }
  357. // }
  358. return ajax;
  359. }
  360. /**
  361. * 修改用户
  362. */
  363. @Log(title = "个人信息", businessType = BusinessType.UPDATE)
  364. @PutMapping("userInfo")
  365. @Transactional(rollbackFor = Exception.class)
  366. @ApiOperation("更新个人信息")
  367. public AjaxResult updateProfile(@RequestBody SysUser user)
  368. {
  369. LoginUser loginUser = getLoginUser();
  370. SysUser currentUser = loginUser.getUser();
  371. boolean updateUserName = null != user.getPhonenumber() && !user.getPhonenumber().equals(currentUser.getPhonenumber()) && currentUser.getUserName().equals(currentUser.getPhonenumber());
  372. currentUser.setNickName(user.getNickName());
  373. currentUser.setEmail(user.getEmail());
  374. currentUser.setPhonenumber(user.getPhonenumber());
  375. currentUser.setSex(user.getSex());
  376. currentUser.setScores(user.getScores());
  377. if (com.ruoyi.common.utils.StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(currentUser))
  378. {
  379. return error("修改用户'" + loginUser.getUsername() + "'失败,手机号码已存在");
  380. }
  381. if (com.ruoyi.common.utils.StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(currentUser))
  382. {
  383. return error("修改用户'" + loginUser.getUsername() + "'失败,邮箱账号已存在");
  384. }
  385. if(NumberUtils.isPositive(user.getCardId())) {
  386. DzCards dzCards = new DzCards();
  387. dzCards.setCardId(user.getCardId());
  388. dzCards.setSchoolId(user.getSchoolId());
  389. dzCards.setClassId(user.getClassId());
  390. dzCardsService.updateDzCards(dzCards);
  391. LoginCard loginCard = loginUser.getCard();
  392. if(null != user.getSchoolId() && !user.getSchoolId().equals(loginCard.getSchoolId())) {
  393. loginCard.setSchoolId(user.getSchoolId());
  394. loginCard.setSchoolName(dzSchoolMapper.selectDzSchoolById(user.getSchoolId()).getName());
  395. }
  396. if(null != user.getClassId() && !user.getClassId().equals(loginCard.getClassId())) {
  397. loginCard.setClassId(user.getClassId());
  398. loginCard.setClassName(dzClassesMapper.selectDzClassesByClassId(user.getClassId()).getName());
  399. }
  400. }
  401. if(!UserTypeEnum.isCard(user.getUserType())) {
  402. currentUser.setLocation(user.getLocation());
  403. currentUser.setExamType(user.getExamType());
  404. currentUser.setEndYear(user.getEndYear());
  405. }
  406. if (updateUserName) {
  407. currentUser.setUserName(user.getPhonenumber());
  408. }
  409. if (userService.updateUserProfile(currentUser) > 0)
  410. {
  411. // 更新缓存用户信息
  412. tokenService.setLoginUser(loginUser);
  413. return success();
  414. }
  415. return error("修改个人信息异常,请联系管理员");
  416. }
  417. @PostMapping("verifyCard")
  418. @ApiOperation("校验卡")
  419. @Anonymous
  420. public AjaxResult verifyCard(@ApiParam("卡号") @RequestParam String cardNo, @ApiParam("密码") @RequestParam String password)
  421. {
  422. DzCards cards = dzCardsService.selectDzCardsByCardNo(cardNo);
  423. if(null == cards || !cards.getPassword().equals(password)) {
  424. return AjaxResult.error("卡号或密码不正确");
  425. }
  426. // if(CardStatus.Free.getVal().equals(cards.getStatus())||CardStatus.Open.getVal().equals(cards.getStatus())) {
  427. // return AjaxResult.error("卡未分配");
  428. // }else if(CardStatus.Active.getVal().equals(cards.getStatus())) {
  429. // return AjaxResult.error("卡已使用");
  430. // }
  431. if(!CardDistributeStatus.Assign.getVal().equals(cards.getDistributeStatus()) || !CardStatus.Free.getVal().equals(cards.getStatus())) {
  432. return AjaxResult.error("无效卡");
  433. }
  434. if(null != cards.getAssignSchoolId()) {
  435. cards.setAssignSchoolName(dzSchoolService.selectDzSchoolById(cards.getAssignSchoolId()).getName());
  436. }
  437. return AjaxResult.success(cards);
  438. }
  439. }