| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- package com.template.controller;
- import com.template.annotation.DESRespondSecret;
- import com.template.annotation.PassToken;
- import com.template.api.LoginControllerAPI;
- import com.template.common.utils.DingTalkInterface;
- import com.template.model.enumModel.eStatu;
- import com.template.model.pojo.RepairAdmin;
- import com.template.model.pojo.SmartUser;
- import com.template.model.request.changePasswordRequest;
- import com.template.model.request.loginRequest;
- import com.template.model.result.CommonResult;
- import com.template.model.vo.LoginVO;
- import com.template.services.RepairAdminService;
- import com.template.common.utils.AesUtils;
- import com.template.common.utils.paramUtils;
- import com.template.common.utils.JWTUtil;
- import com.template.services.SmartUserService;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.validation.BindingResult;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestHeader;
- import org.springframework.web.bind.annotation.RestController;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @Author: binguo
- * @Date: 2023/7/5 星期三 9:28
- * @Description: com.template.controller
- * @Version: 1.0
- */
- @RestController
- //返回参数加密注解
- @DESRespondSecret
- public class LoginController implements LoginControllerAPI {
- @Autowired
- private RepairAdminService repairAdminService;
- @Autowired
- private SmartUserService smartUserService;
- private static Logger logger = LoggerFactory.getLogger(LoginController.class);
- /**
- * 查看系统版本号
- *
- * @return
- */
- @Override
- @PassToken
- @DESRespondSecret(validated = true)
- public CommonResult queryReduce() {
- logger.info("test success");
- logger.error("test error");
- List<String> params = new ArrayList<>();
- params.add("24");
- params.add("25");
- List<SmartUser> result = smartUserService.getSmartUserIds(params);
- return CommonResult.ok("200", "测试返回参数加密", result);
- }
- /**
- * 注册接口
- *
- * @param registerdo account 账号
- * password 密码
- * username 昵称
- * phone 手机号
- * @return
- */
- @Override
- @PassToken
- @DESRespondSecret(validated = true)
- public CommonResult Register(@RequestBody RepairAdmin registerdo, BindingResult bindingResult) {
- if (registerdo == null) {
- return CommonResult.fail("请传递参数");
- }
- if (bindingResult.hasErrors()) {
- String st = paramUtils.getParamError(bindingResult);
- return CommonResult.fail(st);
- }
- int result = 0;
- try {
- result = repairAdminService.insertRepairAdmin(registerdo);
- } catch (Exception e) {
- if (e.getCause().getMessage().contains("'repair_admin.account_un'")) {
- return CommonResult.fail("该账号已存在!");
- }
- return CommonResult.fail("系统异常,注册失败!");
- }
- if (result > 0) {
- return CommonResult.ok("注册成功!");
- }
- return CommonResult.fail("注册失败!");
- }
- /**
- * 登录接口
- *
- * @param loginRequest account 账号
- * password 密码
- * @return
- */
- @Override
- @PassToken
- @DESRespondSecret(validated = true)
- public CommonResult Login(@RequestBody loginRequest loginRequest, BindingResult bindingResult) {
- if (loginRequest == null) {
- return CommonResult.fail("请传递参数");
- }
- if (bindingResult.hasErrors()) {
- String st = paramUtils.getParamError(bindingResult);
- return CommonResult.fail(st);
- }
- RepairAdmin result = repairAdminService.getRepairByAccount(loginRequest.getAccount());
- if (result == null) {
- return CommonResult.fail("账号或密码错误");
- }
- if (result.getStatu() == eStatu.Freeze.getValue()) {
- return CommonResult.fail("该账号已被冻结");
- }
- String encPassword = AesUtils.encrypt(loginRequest.getPassword());
- if (!encPassword.equals(result.getPassword())) {
- return CommonResult.fail("密码错误");
- }
- SmartUser user = new SmartUser();
- String token = JWTUtil.getToken(user, null);
- LoginVO login = new LoginVO();
- login.setToken(token);
- login.setTokenTtl(JWTUtil.getExpired());
- login.setUserName(result.getUsername());
- login.setUserhead(AesUtils.encrypt(result.getId()));
- return CommonResult.ok("登录成功", login);
- }
- /**
- * 修改密码
- *
- * @param cpr oldPassword 旧密码
- * newPassword 新密码
- * confirmPassword 确认密码
- * @param userhead
- * @param bindingResult
- * @return
- */
- @Override
- @DESRespondSecret(validated = true)
- public CommonResult ChangePassword(changePasswordRequest cpr, @RequestHeader("user_head") String userhead, BindingResult bindingResult) {
- if (bindingResult.hasErrors()) {
- String st = paramUtils.getParamError(bindingResult);
- return CommonResult.fail(st);
- }
- if (!cpr.getNewPassword().equals(cpr.getConfirmPassword())) {
- return CommonResult.fail("确认密码和新密码不一致!");
- }
- String userID = AesUtils.decrypt(userhead);
- RepairAdmin operateData = repairAdminService.getRepairById(userID);
- if (operateData == null) {
- return CommonResult.fail("当前账号不合法!");
- }
- if (operateData.getStatu() == eStatu.Freeze.getValue()) {
- return CommonResult.fail("该账号已被冻结");
- }
- if (!AesUtils.encrypt(cpr.getOldPassword()).equals(operateData.getPassword())) {
- return CommonResult.fail("原密码错误!");
- }
- RepairAdmin ra = new RepairAdmin();
- ra.setId(userID);
- ra.setPassword(AesUtils.encrypt(cpr.getNewPassword()));
- int result = repairAdminService.updateRepairAdmin(ra);
- return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
- }
- }
|