123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727 |
- package com.qmth.themis.backend.api;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.google.gson.Gson;
- import com.qmth.themis.business.annotation.ApiJsonObject;
- import com.qmth.themis.business.annotation.ApiJsonProperty;
- import com.qmth.themis.business.constant.SystemConstant;
- import com.qmth.themis.business.dto.AuthDto;
- import com.qmth.themis.business.dto.MqDto;
- import com.qmth.themis.business.dto.response.TBUserDto;
- import com.qmth.themis.business.entity.TBOrg;
- import com.qmth.themis.business.entity.TBSession;
- import com.qmth.themis.business.entity.TBUser;
- import com.qmth.themis.business.entity.TBUserRole;
- import com.qmth.themis.business.enums.FieldUniqueEnum;
- import com.qmth.themis.business.enums.MqTagEnum;
- import com.qmth.themis.business.enums.MqTopicEnum;
- import com.qmth.themis.business.enums.SystemOperationEnum;
- import com.qmth.themis.business.service.*;
- import com.qmth.themis.business.util.EhcacheUtil;
- import com.qmth.themis.business.util.RedisUtil;
- import com.qmth.themis.business.util.ServletUtil;
- import com.qmth.themis.business.util.SessionUtil;
- import com.qmth.themis.common.contanst.Constants;
- import com.qmth.themis.common.enums.ExceptionResultEnum;
- import com.qmth.themis.common.enums.Platform;
- import com.qmth.themis.common.enums.Source;
- import com.qmth.themis.common.exception.BusinessException;
- import com.qmth.themis.common.signature.SignatureInfo;
- import com.qmth.themis.common.signature.SignatureType;
- import com.qmth.themis.common.util.AesUtil;
- import com.qmth.themis.common.util.Result;
- import com.qmth.themis.common.util.ResultUtil;
- import io.swagger.annotations.*;
- import org.apache.commons.lang3.RandomStringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.dao.DuplicateKeyException;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.bind.annotation.*;
- import javax.annotation.Resource;
- import java.security.NoSuchAlgorithmException;
- import java.util.*;
- /**
- * @Description: 用户 前端控制器
- * @Param:
- * @return:
- * @Author: wangliang
- * @Date: 2020/6/25
- */
- @Api(tags = "用户Controller")
- @RestController
- @RequestMapping("/${prefix.url.admin}/user")
- public class TBUserController {
- private final static Logger log = LoggerFactory.getLogger(TBUserController.class);
- @Resource
- TBUserService tbUserService;
- @Resource
- CacheService cacheService;
- @Resource
- RedisUtil redisUtil;
- @Resource
- MqDtoService mqDtoService;
- @Resource
- TBUserRoleService tbUserRoleService;
- @Resource
- TBOrgService tbOrgService;
- @ApiOperation(value = "用户登录接口")
- @RequestMapping(value = "/login/account", method = RequestMethod.POST)
- @ApiResponses({@ApiResponse(code = 200, message = "用户信息", response = TBUser.class)})
- public Result login(@ApiJsonObject(name = "loginAccount", value = {
- @ApiJsonProperty(key = "loginName", description = "登录名"),
- @ApiJsonProperty(key = "password", description = "密码"),
- @ApiJsonProperty(key = "code", description = "机构代码")
- }) @ApiParam(value = "用户信息", required = true) @RequestBody Map<String, Object> mapParameter) throws NoSuchAlgorithmException {
- if (Objects.isNull(mapParameter)) {
- throw new BusinessException(ExceptionResultEnum.USER_INFO_IS_NULL);
- }
- if (Objects.isNull(mapParameter.get("loginName")) || Objects.equals(mapParameter.get("loginName"), "")) {
- throw new BusinessException(ExceptionResultEnum.LOGIN_NAME_IS_NULL);
- }
- if (Objects.isNull(mapParameter.get("password")) || Objects.equals(mapParameter.get("password"), "")) {
- throw new BusinessException(ExceptionResultEnum.PASSWORD_IS_NULL);
- }
- if (Objects.isNull(mapParameter.get("code")) || Objects.equals(mapParameter.get("code"), "")) {
- throw new BusinessException(ExceptionResultEnum.ORG_CODE_IS_NULL);
- }
- String loginName = String.valueOf(mapParameter.get("loginName"));
- String password = String.valueOf(mapParameter.get("password"));
- String orgCode = String.valueOf(mapParameter.get("code"));
- TBOrg tbOrg = (TBOrg) EhcacheUtil.get(SystemConstant.orgCodeCache, orgCode);
- if (Objects.isNull(tbOrg)) {
- QueryWrapper<TBOrg> tbOrgQueryWrapper = new QueryWrapper<>();
- tbOrgQueryWrapper.lambda().eq(TBOrg::getCode, orgCode);
- tbOrg = tbOrgService.getOne(tbOrgQueryWrapper);
- if (Objects.nonNull(tbOrg)) {
- EhcacheUtil.put(SystemConstant.orgCodeCache, orgCode, tbOrg);
- }
- }
- if (Objects.isNull(tbOrg)) {
- throw new BusinessException(ExceptionResultEnum.ORG_NO);
- }
- QueryWrapper<TBUser> wrapper = new QueryWrapper<>();
- wrapper.lambda().eq(TBUser::getLoginName, loginName);
- TBUser user = tbUserService.getOne(wrapper);
- //用户不存在
- if (Objects.isNull(user)) {
- throw new BusinessException(ExceptionResultEnum.USER_NO);
- }
- if (Objects.nonNull(user.getOrgId()) && user.getOrgId().longValue() != tbOrg.getId().longValue()) {
- throw new BusinessException("用户机构不匹配");
- }
- String loginPassword = AesUtil.decryptCs7(password, Constants.AES_RULE);
- //密码错误
- String aesPassword = AesUtil.decryptCs7(user.getPassword(), Constants.AES_RULE);
- if (!Objects.equals(loginPassword, aesPassword)) {
- throw new BusinessException(ExceptionResultEnum.PASSWORD_ERROR);
- }
- return userLoginCommon(user);
- }
- @ApiOperation(value = "短信验证码登陆接口")
- @RequestMapping(value = "/login/verifyCode", method = RequestMethod.POST)
- @ApiResponses({@ApiResponse(code = 200, message = "用户信息", response = TBUser.class)})
- public Result verifyCode(@ApiParam(value = "用户信息", required = true) @RequestBody Map<String, Object> mapParameter) throws NoSuchAlgorithmException {
- if (Objects.isNull(mapParameter.get("loginName")) || Objects.equals(mapParameter.get("loginName"), "")) {
- throw new BusinessException(ExceptionResultEnum.LOGIN_NAME_IS_NULL);
- }
- if (Objects.isNull(mapParameter.get("verifyCode")) || Objects.equals(mapParameter.get("verifyCode"), "")) {
- throw new BusinessException(ExceptionResultEnum.VERIFYCODE_IS_NULL);
- }
- String loginName = String.valueOf(mapParameter.get("loginName"));
- String verifyCode = String.valueOf(mapParameter.get("verifyCode"));
- QueryWrapper<TBUser> wrapper = new QueryWrapper<>();
- wrapper.lambda().eq(TBUser::getLoginName, loginName);
- TBUser user = tbUserService.getOne(wrapper);
- //用户不存在
- if (Objects.isNull(user)) {
- throw new BusinessException(ExceptionResultEnum.USER_NO);
- }
- //todo 加入验证码校验逻辑
- return userLoginCommon(user);
- }
- /**
- * 用户登录公用
- *
- * @param user
- * @return
- * @throws NoSuchAlgorithmException
- */
- public Result userLoginCommon(TBUser user) throws NoSuchAlgorithmException {
- //停用
- if (user.getEnable().intValue() == 0) {
- throw new BusinessException(ExceptionResultEnum.USER_ENABLE);
- }
- Platform platform = Platform.valueOf(ServletUtil.getRequestPlatform());
- String deviceId = ServletUtil.getRequestDeviceId();
- //添加用户鉴权缓存
- AuthDto authDto = cacheService.addAccountCache(user.getId());
- //生成token
- String token = RandomStringUtils.randomAlphanumeric(32);
- //添加用户缓存
- redisUtil.setUser(user.getId(), user);
- String source = null;
- if (Objects.equals(platform.name(), Platform.WIN.name()) || Objects.equals(platform.name(), Platform.MAC.name()) || Objects.equals(platform.name(), Platform.IOS.name()) || Objects.equals(platform.name(), Platform.ANDROID.name())) {
- source = platform.getSource().split(",")[0];
- } else {
- source = platform.getSource();
- }
- //添加用户会话缓存
- String sessionId = SessionUtil.digest(user.getId(), Math.abs(authDto.getRoleCodes().toString().hashCode()), source);
- Map<String, Object> expireMap = SystemConstant.getExpireTime(platform);
- Date expire = (Date) expireMap.get("date");
- Long redisExpire = Long.parseLong(String.valueOf(expireMap.get("redisExpire")));
- TBSession tbSession = new TBSession(sessionId, String.valueOf(user.getId()), authDto.getRoleCodes().toString(), source, platform.name(), deviceId, ServletUtil.getRequest().getLocalAddr(), token, expire.getTime());
- redisUtil.setUserSession(sessionId, tbSession, redisExpire);
- //mq发送消息start
- MqDto mqDto = new MqDto(MqTopicEnum.THEMIS_TOPIC.getCode(), platform.name(), tbSession, MqTagEnum.valueOf(platform.name()), tbSession.getId(), user.getLoginName());
- mqDtoService.assembleSendOneWayMsg(mqDto);
- MqDto mqDtoLog = new MqDto(MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.USER.name(), SystemOperationEnum.LOGIN, MqTagEnum.USER, String.valueOf(user.getId()), user.getLoginName());
- mqDtoService.assembleSendOneWayMsg(mqDtoLog);
- //mq发送消息end
- //测试
- String test = SignatureInfo.build(SignatureType.TOKEN, sessionId, token);
- Map<String, Object> map = new HashMap<>();
- // map.put(SystemConstant.ACCESS_TOKEN, token);
- map.put(SystemConstant.ACCESS_TOKEN, test);
- map.put(SystemConstant.ACCOUNT, user);
- map.put(SystemConstant.SESSION_ID, sessionId);
- map.put("roleCodes", authDto.getRoleCodes());
- if (Objects.nonNull(authDto.getTbOrg())) {
- Map orgMap = new HashMap();
- orgMap.put("name", authDto.getTbOrg().getName());
- orgMap.put("logo", authDto.getTbOrg().getLogo());
- orgMap.put("enableMonitorRecord", authDto.getTbOrg().getEnableMonitorRecord());
- map.put(SystemConstant.ORG_INFO, orgMap);
- redisUtil.setOrg(authDto.getTbOrg().getId(), authDto.getTbOrg());
- }
- return ResultUtil.ok(map);
- }
- // @ApiOperation(value = "es查询接口")
- // @RequestMapping(value = "/websocketPush", method = RequestMethod.POST)
- // public Result websocketPush(@RequestBody String message) throws IOException {
- // TBUser tbUser = (TBUser) ServletUtil.getRequestAccount(request);
- // WebSocketServer.sendInfo(message, String.valueOf(tbUser.getId()));
- // return ResultUtil.ok(SystemConstant.SUCCESS);
- // }
- // @Resource
- // TEExamActivityService teExamActivityService;
- //
- // @Resource
- // TEExamService teExamService;
- //
- // @Resource
- // TEStudentService teStudentService;
- //
- // @Resource
- // TEExamStudentService teExamStudentService;
- //
- // @Resource
- // TEExamCourseService teExamCourseService;
- //
- // @Resource
- // TEExamPaperService teExamPaperService;
- //
- // @Value("${db.name}")
- // String dbName;
- //
- // @Resource
- // MongoTemplate mongoTemplate;
- //
- // @Resource
- // ETEStudentService eteStudentService;
- //
- // @Resource
- // ETEStudentRepo eteStudentRepo;
- // @ApiOperation(value = "es查询接口")
- // @RequestMapping(value = "/es/list", method = RequestMethod.POST)
- // @Transactional
- // public Result esList() {
- //// eteStudentService.createIndex();
- //// List<ETEStudentEntity> list = new ArrayList<>();
- //// for (int i = 0; i < 10; i++) {
- //// //学生档案
- //// ETEStudentEntity eteStudentEntity = new ETEStudentEntity();
- //// eteStudentEntity.setId(Constants.idGen.next());
- //// eteStudentEntity.setOrgId(1L);
- //// eteStudentEntity.setIdentity("test" + i);
- //// eteStudentEntity.setPassword("123456");
- //// eteStudentEntity.setIdcardNumber(RandomStringUtils.randomAlphanumeric(18));
- //// eteStudentEntity.setMobileNumber(RandomStringUtils.randomNumeric(11));
- //// eteStudentEntity.setName("java" + i + RandomStringUtils.randomAlphanumeric(30));
- //// eteStudentEntity.setGender(1);
- //// eteStudentEntity.setBasePhotoPath("http://11111");
- //// eteStudentEntity.setCreateTime(new Date());
- //// list.add(eteStudentEntity);
- //// }
- //// eteStudentService.saveAll(list);
- ////// Iterator<ETEStudentEntity> iterator = eteStudentService.findAll();
- //// org.springframework.data.domain.Page<ETEStudentEntity> eteStudentEntityPage1 = (org.springframework.data.domain.Page<ETEStudentEntity>) eteStudentService.queryName("2");
- ////// List<ETEStudentEntity> eteStudentEntityList = eteStudentService.findByNameLike("java");
- //// org.springframework.data.domain.Page<ETEStudentEntity> eteStudentEntityPage2 = (org.springframework.data.domain.Page<ETEStudentEntity>) eteStudentService.queryMobileNumber("2");
- ////// Optional<ETEStudentEntity> eteStudentEntity = eteStudentRepo.findById(eteStudentEntityPage.getContent().get(0).getId());
- //// Map map = new HashMap();
- //// map.put(SystemConstant.RECORDS, eteStudentEntityPage1);
- //// map.put(SystemConstant.RECORDS + 2, eteStudentEntityPage2);
- ////// map.put("bean", eteStudentEntity.get());
- //// return ResultUtil.ok(map);
- //
- //// for (int i = 0; i < 5; i++) {
- //
- //// mqDtoService.assembleSendOneWayMsg("imTopic", "teacher", "老师发送的一条消息1", MqEnum.MESSAGE_LOG.name(), "1", "2");
- //// mqDtoService.assembleSendOneWayMsg("imTopic", "user", "用户发送的一条消息2", MqEnum.MESSAGE_LOG.name(), "1", "2");
- //// mqDtoService.assembleSendOneWayMsg("imTopic", "user", "学生发送的一条消息3", MqEnum.MESSAGE_LOG.name(), "1", "2");
- // Map map = new HashMap();
- // map.put("sendUserId", "10");
- // map.put("toUserId", "20");
- // map.put("model", MessageModel.BROADCASTING);
- //// map.put("model", MessageModel.CLUSTERING);
- // MqDto mqDto = new MqDto("websocketImTopic", "im", "学生发送的一条消息4", MqEnum.MESSAGE_LOG, "1", map, "2");
- //// mqDtoService.assembleSendOneWayMsg("imTopic", "user", "学生发送的一条消息3", MqEnum.MESSAGE_LOG.name(), "1", "2");
- // mqDtoService.assembleSendOneWayMsg(mqDto);
- // // }
- // return ResultUtil.ok(SystemConstant.SUCCESS);
- // }
- // @ApiOperation(value = "用户查询接口")
- // @RequestMapping(value = "/list", method = RequestMethod.POST)
- // @Transactional
- // public Result list() {
- // String tableName = "t_e_exam_activity_test1";
- // Integer count = teExamActivityService.existTable(tableName, dbName);
- // log.info("count:{}", count);
- // if (count == 0) {
- // teExamActivityService.createNewTable(tableName);
- // }
- // //学生档案
- // TEStudent teStudent = new TEStudent();
- // teStudent.setId(Constants.idGen.next());
- // teStudent.setOrgId(1L);
- // teStudent.setIdentity(RandomStringUtils.randomAlphanumeric(30));
- // teStudent.setPassword("123456");
- // teStudent.setIdcardNumber(RandomStringUtils.randomAlphanumeric(18));
- // teStudent.setMobileNumber(RandomStringUtils.randomNumeric(11));
- // teStudent.setName("aaa");
- // teStudent.setGender(1);
- // teStudent.setBasePhotoPath("http://11111");
- // teStudent.setEnable(1);
- // teStudentService.save(teStudent);
- //
- // //考试批次
- // TEExam teExam = new TEExam();
- // teExam.setId(Constants.idGen.next());
- // teExam.setOrgId(1L);
- // teExam.setCode(RandomStringUtils.randomAlphanumeric(20));
- // teExam.setName("123");
- // teExam.setTag("test1");
- // teExam.setStartTime(new Date());
- // teExam.setEndTime(new Date());
- // teExam.setStatus(0);
- // teExam.setShortCode("123456");
- // teExam.setPrepareSeconds(30);
- // teExam.setMinDurationSeconds(30);
- // teExam.setPreNotice("99999");
- // teExam.setPreNoticeStaySeconds(30);
- // teExam.setPostNotice("88888");
- // teExam.setExamCount(5);
- // teExam.setBreakResumeCount(3);
- // teExam.setBreakExpireSeconds(30);
- // teExam.setCameraPhotoUpload(1);
- // teExam.setReexamAuditing(1);
- // teExam.setShowObjectiveScore(1);
- // teExam.setMode(0);
- // teExam.setEnable(1);
- // teExam.setArchived(0);
- // teExam.setOpeningSeconds(30);
- // teExam.setMaxDurationSeconds(30);
- // teExam.setForceFinish(1);
- // teExam.setEntryAuthenticationPolicy(1);
- // teExam.setInProcessFaceVerify(1);
- // teExam.setInProcessFaceStrangerIgnore(1);
- // teExam.setInProcessLivenessVerify(1);
- // teExam.setInProcessLivenessIntervalSeconds(30);
- // teExam.setInProcessLivenessJudgePolicy(1);
- // teExam.setRecordSelectStrategy(1);
- // teExam.setEnableIpLimit(1);
- // teExam.setIpAllow("1");
- // teExam.setScoreStatus(1);
- // teExamService.save(teExam);
- //
- // //考试场次
- // TEExamActivity teExamActivity = new TEExamActivity();
- // teExamActivity.setId(Constants.idGen.next());
- // teExamActivity.setExamId(teExam.getId());
- // teExamActivity.setCode(RandomStringUtils.randomAlphanumeric(20));
- // teExamActivity.setPrepareSeconds(30);
- // teExamActivity.setMaxDurationSeconds(30);
- // teExamActivity.setEnable(1);
- // teExamActivity.setOpeningSeconds(30);
- // teExamActivity.setStartTime(new Date());
- // teExamActivity.setFinishTime(new Date());
- // teExamActivity.setCreateTime(new Date());
- // teExamActivityService.insertInfo(tableName, teExamActivity);
- //
- // //考试科目
- // TEExamCourse teExamCourse = new TEExamCourse();
- // teExamCourse.setId(Constants.idGen.next());
- // teExamCourse.setExamId(teExam.getId());
- // teExamCourse.setCourseCode(RandomStringUtils.randomAlphanumeric(5));
- // teExamCourse.setCourseName("测试科目1");
- // teExamCourse.setObjectiveShuffle(1);
- // teExamCourse.setOptionShuffle(1);
- // teExamCourse.setPaperCount(1);
- // teExamCourse.setHasAnswer(1);
- // teExamCourse.setAudioPlayCount(1);
- // teExamCourseService.save(teExamCourse);
- //
- // //考试试卷
- // TEExamPaper teExamPaper = new TEExamPaper();
- // teExamPaper.setId(Constants.idGen.next());
- // teExamPaper.setName("test1");
- // teExamPaper.setTotalScore(100D);
- // teExamPaper.setPaperPath("123");
- // teExamPaper.setDecryptSecret("345");
- // teExamPaper.setEncryptMode(1);
- // teExamPaper.setNeedVoiceAnswer(1);
- // teExamPaper.setExamId(teExam.getId());
- // teExamPaper.setCode("test1");
- // teExamPaper.setCourseCode(teExamCourse.getCourseCode());
- // teExamPaper.setAnswerPath("123");
- // teExamPaper.setHasAudio(1);
- // teExamPaper.setWeight(1D);
- // teExamPaper.setAudioPlayCount(1);
- // teExamPaperService.save(teExamPaper);
- //
- // //考生
- // TEExamStudent teExamStudent = new TEExamStudent();
- // teExamStudent.setId(Constants.idGen.next());
- // teExamStudent.setExamId(teExam.getId());
- // teExamStudent.setExamActivityId(teExamActivity.getId());
- // teExamStudent.setStudentId(teStudent.getId());
- // teExamStudent.setCourseCode(teExamCourse.getCourseCode());
- // teExamStudent.setRoomCode("1");
- // teExamStudent.setIdentity(RandomStringUtils.randomAlphanumeric(20));
- // teExamStudent.setName("132");
- // Map stuMap = new HashMap();
- // stuMap.put("examTest1", "aaa");
- // stuMap.put("examTest2", "bbb");
- // teExamStudent.setParameter(JacksonUtil.parseJson(stuMap));
- // teExamStudent.setLeftExamCount(1);
- // teExamStudent.setRoomName("test1");
- // teExamStudent.setEnable(1);
- // teExamStudentService.save(teExamStudent);
- //
- // //todo
- //// Gson gson = new Gson();
- //// MTEStudentEntity mteStudentEntity = gson.fromJson(gson.toJson(teStudent), MTEStudentEntity.class);
- //// MTEExamEntity mteExamEntity = gson.fromJson(gson.toJson(teExam), MTEExamEntity.class);
- //// MTEExamActivityEntity mteExamActivityEntity = gson.fromJson(gson.toJson(teExamActivity), MTEExamActivityEntity.class);
- //// MTEExamPaperEntity mteExamPaperEntity = gson.fromJson(gson.toJson(teExamPaper), MTEExamPaperEntity.class);
- //// MTEExamCourseEntity mteExamCourseEntity = gson.fromJson(gson.toJson(teExamCourse), MTEExamCourseEntity.class);
- //// MTEExamStudentEntity mteExamStudentEntity = gson.fromJson(gson.toJson(teExamStudent), MTEExamStudentEntity.class);
- // //todo
- //
- //// List list = Arrays.asList(mteExamPaperEntity);
- //// mteExamCourseEntity.setMteExamPaperEntityList(list);
- //
- //// list = Arrays.asList(mteExamActivityEntity);
- //// mteExamEntity.setMteExamActivityEntityList(list);
- //
- //// list = Arrays.asList(mteExamCourseEntity);
- //// mteExamStudentEntity.setMteExamEntity(mteExamEntity);
- //// mteExamStudentEntity.setMteExamActivityEntity(mteExamActivityEntity);
- //// mteExamStudentEntity.setMteStudentEntity(mteStudentEntity);
- //// mteExamStudentEntity.setMteExamCourseEntityList(list);
- //
- // //todo
- //// mongoTemplate.save(mteStudentEntity);
- //// mongoTemplate.save(mteExamEntity);
- //// mongoTemplate.save(mteExamActivityEntity);
- //// mongoTemplate.save(mteExamPaperEntity);
- //// mongoTemplate.save(mteExamCourseEntity);
- //// mongoTemplate.save(mteExamStudentEntity);
- ////
- //// IPage<TEExamActivity> map = teExamActivityService.selectListPage(new Page<>(0, 10), tableName);
- //// log.info("mysql map:{}", JacksonUtil.parseJson(map));
- ////
- //// Query query = new Query();
- ////// query.addCriteria(Criteria.where("questionId").in(setQuestionId).andOperator(Criteria.where("examRecordDataId").is(examSessionInfo.getExamRecordDataId())));
- //// query.skip((1 - 1) * 10).limit(10);//设置起始数和查询条数
- //// List<MTEStudentEntity> mteStudentEntityList = mongoTemplate.find(query, MTEStudentEntity.class);
- //// log.info("mongodb mteStudentEntityList:{}", JacksonUtil.parseJson(mteStudentEntityList));
- ////// List<MTEStudentEntity> mteStudentEntityList = mongoTemplate.findAll(MTEStudentEntity.class);
- ////// log.info("mongodb mteStudentEntityList:{}", JacksonUtil.parseJson(mteStudentEntityList));
- ////
- //// List<MTEExamEntity> mteExamEntityList = mongoTemplate.findAll(MTEExamEntity.class);
- //// log.info("mongodb mteExamEntityList:{}", JacksonUtil.parseJson(mteExamEntityList));
- ////
- //// List<MTEExamActivityEntity> mteExamActivityEntityList = mongoTemplate.findAll(MTEExamActivityEntity.class);
- //// log.info("mongodb mteExamActivityEntityList:{}", JacksonUtil.parseJson(mteExamActivityEntityList));
- ////// List<TEExamActivity> ps = new ArrayList<>();
- ////// mteExamActivityEntityList.forEach(s -> {
- ////// TEExamActivity t = gson.fromJson(gson.toJson(s), TEExamActivity.class);
- ////// ps.add(t);
- ////// });
- //////// List<TEExamActivity> ps = gson.fromJson(JacksonUtil.parseJson(mteExamActivityEntityList), new TypeToken<List<TEExamActivity>>(){}.getType());
- ////// map.setRecords(ps);
- ////
- //// List<MTEExamPaperEntity> mteExamPaperEntityList = mongoTemplate.findAll(MTEExamPaperEntity.class);
- //// log.info("mongodb mteExamPaperEntityList:{}", JacksonUtil.parseJson(mteExamPaperEntityList));
- ////
- //// List<MTEExamCourseEntity> mteExamCourseEntityList = mongoTemplate.findAll(MTEExamCourseEntity.class);
- //// log.info("mongodb mteExamCourseEntityList:{}", JacksonUtil.parseJson(mteExamCourseEntityList));
- ////
- //// List<MTEExamStudentEntity> mteExamStudentEntityList = mongoTemplate.findAll(MTEExamStudentEntity.class);
- //// log.info("mongodb mteExamStudentEntityList:{}", JacksonUtil.parseJson(mteExamStudentEntityList));
- //// List<TEExamStudent> ps = new ArrayList<>();
- //// mteExamStudentEntityList.forEach(s -> {
- //// TEExamStudent t = gson.fromJson(gson.toJson(s), TEExamStudent.class);
- //// ps.add(t);
- //// });
- //// List<MTEExamStudentEntity> ps1 = new ArrayList<>();
- //// List<TEExamStudent> list1 = teExamStudentService.list();
- //// list1.forEach(s -> {
- //// MTEExamStudentEntity t = gson.fromJson(gson.toJson(s), MTEExamStudentEntity.class);
- //// ps1.add(t);
- //// });
- // //todo
- //// teExamActivityService.dropTable(tableName);
- // return ResultUtil.ok(SystemConstant.SUCCESS);
- // }
- @ApiOperation(value = "登出接口")
- @RequestMapping(value = "/logout", method = RequestMethod.POST)
- @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
- public Result logout() throws NoSuchAlgorithmException {
- TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
- TBSession tbSession = (TBSession) ServletUtil.getRequestSession();
- AuthDto authDto = (AuthDto) redisUtil.get(SystemConstant.userOauth + "::" + tbUser.getId());
- if (Objects.isNull(tbSession)) {
- throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
- }
- redisUtil.deleteUserSession(tbSession.getId());
- //循环检查该用户下其他平台是否存在session,不存在则删除用户缓存和鉴权缓存
- boolean delete = true;
- for (Source s : Source.values()) {
- String sessionId = SessionUtil.digest(tbUser.getId(), Math.abs(authDto.getRoleCodes().toString().hashCode()), s.name());
- if (Objects.nonNull(redisUtil.getUserSession(sessionId))) {
- delete = false;
- break;
- }
- }
- if (delete) {
- redisUtil.deleteUser(tbUser.getId());
- cacheService.removeAccountCache(tbUser.getId());
- }
- //mq发送消息start
- MqDto mqDto = new MqDto(MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.USER.name(), SystemOperationEnum.LOGOUT, MqTagEnum.USER, String.valueOf(tbUser.getId()), tbUser.getLoginName());
- mqDtoService.assembleSendOneWayMsg(mqDto);
- //mq发送消息end
- return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
- }
- // @CachePut(value = "user_cache", key = "'userCacheQuery'")
- @ApiOperation(value = "用户查询接口")
- @RequestMapping(value = "/query", method = RequestMethod.POST)
- @ApiResponses({@ApiResponse(code = 200, message = "用户信息", response = TBUserDto.class)})
- public Result query(@ApiParam(value = "用户id", required = false) @RequestParam(required = false) Long id, @ApiParam(value = "登录名", required = false) @RequestParam(required = false) String loginName, @ApiParam(value = "姓名", required = false) @RequestParam(required = false) String name, @ApiParam(value = "角色", required = false) @RequestParam(required = false) String roleCode, @ApiParam(value = "是否启用", required = false) @RequestParam(required = false) Integer enable, @ApiParam(value = "分页页码", required = true) @RequestParam int pageNumber, @ApiParam(value = "分页数", required = true) @RequestParam int pageSize) {
- IPage<TBUserDto> tbUserIPage = tbUserService.userQuery(new Page<>(pageNumber, pageSize), id, loginName, name, roleCode, enable);
- tbUserIPage.getRecords().forEach(s -> {
- if (Objects.nonNull(s.getRoleNameStr())) {
- s.setRoleName(Arrays.asList(s.getRoleNameStr().split(",")));
- }
- if (Objects.nonNull(s.getRoleCodeStr())) {
- s.setRoleCode(Arrays.asList(s.getRoleCodeStr().split(",")));
- }
- });
- return ResultUtil.ok(tbUserIPage);
- }
- // @CacheEvict(value = "user_cache", key = "'userCacheQuery'")
- @ApiOperation(value = "用户新增/编辑接口")
- @RequestMapping(value = "/save", method = RequestMethod.POST)
- @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
- @Transactional
- public Result save(@ApiJsonObject(name = "userSave", value = {
- @ApiJsonProperty(key = "id", type = "long", example = "1", description = "新增不需要主键"),
- @ApiJsonProperty(key = "orgId", example = "1", description = "机构id"),
- @ApiJsonProperty(key = "loginName", description = "登录名,新增必须"),
- @ApiJsonProperty(key = "name", description = "姓名,新增必须"),
- @ApiJsonProperty(key = "password", description = "密码,新增必须"),
- @ApiJsonProperty(key = "enable", type = "int", example = "1", description = "是否启用"),
- @ApiJsonProperty(key = "mobileNumber", description = "手机号"),
- @ApiJsonProperty(key = "roleCode", description = "角色")
- }) @ApiParam(value = "用户信息", required = true) @RequestBody Map<String, Object> mapParameter) {
- if (Objects.isNull(mapParameter)) {
- throw new BusinessException(ExceptionResultEnum.USER_INFO_IS_NULL);
- }
- if (Objects.isNull(mapParameter.get("orgId"))) {
- throw new BusinessException(ExceptionResultEnum.ORG_ID_IS_NULL);
- }
- TBUser loginUser = (TBUser) ServletUtil.getRequestAccount();
- Long orgId = Long.parseLong(String.valueOf(mapParameter.get("orgId")));
- try {
- Gson gson = new Gson();
- TBUser tbUser = gson.fromJson(gson.toJson(mapParameter), TBUser.class);
- List<String> roleList = (List<String>) mapParameter.get("roleCode");
- if (Objects.isNull(roleList) || roleList.size() == 0) {
- throw new BusinessException("请选择角色");
- }
- Set<String> roleSet = new HashSet<>(roleList);
- if (roleSet.size() > 1) {
- throw new BusinessException("暂不支持多个角色");
- }
- if (Objects.isNull(tbUser.getId())) {
- tbUser.setId(Constants.idGen.next());
- tbUser.setOrgId(orgId);
- tbUser.setCreateId(loginUser.getId());
- if (Objects.nonNull(roleSet) && roleSet.size() > 0) {
- TBUser finalTbUser = tbUser;
- roleSet.forEach(s -> {
- TBUserRole tbUserRole = new TBUserRole(finalTbUser.getId(), s);
- tbUserRoleService.save(tbUserRole);
- });
- }
- } else {
- if (Objects.nonNull(roleSet) && roleSet.size() > 0) {
- QueryWrapper<TBUserRole> tbUserRoleQueryWrapper = new QueryWrapper<>();
- tbUserRoleQueryWrapper.lambda().eq(TBUserRole::getUserId, tbUser.getId());
- tbUserRoleService.remove(tbUserRoleQueryWrapper);
- TBUser finalTbUser1 = tbUser;
- roleSet.forEach(s -> {
- TBUserRole tbUserRole = new TBUserRole(finalTbUser1.getId(), s);
- tbUserRoleService.save(tbUserRole);
- });
- }
- tbUser.setUpdateId(loginUser.getId());
- }
- tbUserService.saveOrUpdate(tbUser);
- //清除用户缓存
- if (Objects.nonNull(roleSet) && roleSet.size() > 0) {
- AuthDto authDto = (AuthDto) redisUtil.get(SystemConstant.userOauth + "::" + tbUser.getId());
- if (Objects.nonNull(authDto)) {
- for (Source s : Source.values()) {
- String sessionId = SessionUtil.digest(tbUser.getId(), Math.abs(authDto.getRoleCodes().toString().hashCode()), s.name());
- redisUtil.deleteUserSession(sessionId);
- }
- }
- redisUtil.deleteUser(tbUser.getId());
- cacheService.removeAccountCache(tbUser.getId());
- }
- } catch (Exception e) {
- log.error("请求出错", e);
- if (e instanceof DuplicateKeyException) {
- String errorColumn = e.getCause().toString();
- String columnStr = errorColumn.substring(errorColumn.lastIndexOf("key") + 3, errorColumn.length()).replaceAll("'", "");
- throw new BusinessException("机构id[" + orgId + "]下的" + FieldUniqueEnum.convertToCode(columnStr) + "数据不允许重复插入");
- } else if (e instanceof BusinessException) {
- throw new BusinessException(e.getMessage());
- } else {
- throw new RuntimeException(e);
- }
- }
- return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
- }
- @ApiOperation(value = "获取短信验证码接口")
- @RequestMapping(value = "/getVerifyCode", method = RequestMethod.POST)
- @ApiResponses({@ApiResponse(code = 200, message = "{\"verifyCode\":123456}", response = Result.class)})
- public Result getVerifyCode(@ApiParam(value = "登录名", required = true) @RequestParam String loginName) {
- if (Objects.isNull(loginName) || Objects.equals(loginName, "")) {
- throw new BusinessException(ExceptionResultEnum.LOGIN_NAME_IS_NULL);
- }
- Map map = new HashMap();
- return ResultUtil.ok(map);
- }
- @ApiOperation(value = "获取短信验证码接口")
- @RequestMapping(value = "/validate/verifyCode", method = RequestMethod.POST)
- @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
- public Result validateVerifyCode(@ApiParam(value = "验证码", required = true) @RequestParam String verifyCode) {
- if (Objects.isNull(verifyCode) || Objects.equals(verifyCode, "")) {
- throw new BusinessException(ExceptionResultEnum.VERIFYCODE_IS_NULL);
- }
- return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
- }
- @ApiOperation(value = "二次验证获取短信验证码接口")
- @RequestMapping(value = "/validate/getVerifyCode", method = RequestMethod.POST)
- @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
- public Result validateGetVerifyCode() {
- return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
- }
- @ApiOperation(value = "用户停用/启用接口")
- @RequestMapping(value = "/enable", method = RequestMethod.POST)
- @Transactional
- @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
- public Result enableUser(@ApiJsonObject(name = "enableUser", value = {
- @ApiJsonProperty(key = "id", type = "long", example = "1", description = "用户id"),
- @ApiJsonProperty(key = "enable", type = "int", example = "1", description = "停用/启用")
- }) @ApiParam(value = "用户信息", required = true) @RequestBody Map<String, Object> user) {
- if (Objects.isNull(user.get("id")) || Objects.equals(user.get("id"), "")) {
- throw new BusinessException(ExceptionResultEnum.USER_ID_IS_NULL);
- }
- Long userId = Long.parseLong(String.valueOf(user.get("id")));
- if (Objects.isNull(user.get("enable")) || Objects.equals(user.get("enable"), "")) {
- throw new BusinessException(ExceptionResultEnum.ENABLE_IS_NULL);
- }
- Integer enable = Integer.parseInt(String.valueOf(user.get("enable")));
- TBUser tbUser = tbUserService.getById(userId);
- if (Objects.isNull(tbUser)) {
- throw new BusinessException(ExceptionResultEnum.USER_NO);
- }
- //保存用户
- tbUser.setEnable(enable);
- tbUserService.updateById(tbUser);
- redisUtil.setUser(tbUser.getId(), tbUser);
- return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
- }
- @ApiOperation(value = "用户修改密码接口")
- @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
- @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
- @Transactional
- public Result userUpdatePwd(@ApiJsonObject(name = "userUpdatePwd", value = {
- @ApiJsonProperty(key = "id", type = "long", example = "1", description = "用户ID"),
- @ApiJsonProperty(key = "password", description = "新密码")
- }) @ApiParam(value = "用户信息", required = true) @RequestBody Map<String, Object> mapParameter) {
- if (Objects.isNull(mapParameter.get("id")) || Objects.equals(mapParameter.get("id"), "")) {
- throw new BusinessException(ExceptionResultEnum.USER_ID_IS_NULL);
- }
- Long id = Long.parseLong(String.valueOf(mapParameter.get("id")));
- if (Objects.isNull(mapParameter.get("password")) || Objects.equals(mapParameter.get("password"), "")) {
- throw new BusinessException(ExceptionResultEnum.PASSWORD_IS_NULL);
- }
- String password = String.valueOf(mapParameter.get("password"));
- TBUser tbUser = tbUserService.getById(id);
- if (Objects.isNull(tbUser)) {
- throw new BusinessException(ExceptionResultEnum.USER_NO);
- }
- TBUser currentUser = (TBUser) ServletUtil.getRequestAccount();
- tbUser.setPassword(password);
- tbUser.setUpdateId(currentUser.getId());
- tbUserService.updateById(tbUser);
- redisUtil.deleteUser(tbUser.getId());
- return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
- }
- }
|