| | |
| | | package com.ruoyi.company.controller.front; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.ruoyi.common.core.constant.CacheConstants; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.exception.CaptchaException; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.common.core.utils.bean.BeanUtils; |
| | | import com.ruoyi.common.redis.service.RedisService; |
| | | import com.ruoyi.common.security.annotation.InnerAuth; |
| | | import com.ruoyi.common.security.utils.SecurityUtils; |
| | | import com.ruoyi.company.api.domain.Company; |
| | | import com.ruoyi.company.api.domain.User; |
| | | import com.ruoyi.company.api.domain.dto.MgtCompanyDTO; |
| | | import com.ruoyi.company.api.model.RegisterUser; |
| | | import com.ruoyi.company.api.model.UpdatePassword; |
| | | import com.ruoyi.company.api.model.UpdatePhone; |
| | | import com.ruoyi.company.api.model.UserDetail; |
| | | import com.ruoyi.company.service.CompanyService; |
| | | import com.ruoyi.company.service.UserService; |
| | | import com.ruoyi.system.api.model.AppUser; |
| | | import io.swagger.v3.oas.annotations.Operation; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.experimental.StandardException; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | |
| | | @RequiredArgsConstructor(onConstructor_ = {@Lazy}) |
| | | public class UserController { |
| | | private final UserService userService; |
| | | private final CompanyService companyService; |
| | | private final RedisService redisService; |
| | | |
| | | @PostMapping("/register") |
| | | public R register(@RequestBody RegisterUser registerUser) { |
| | | userService.register(registerUser); |
| | | public R register(@RequestBody MgtCompanyDTO mgtCompanyDTO) { |
| | | companyService.saveCompany(mgtCompanyDTO); |
| | | return R.ok(); |
| | | } |
| | | |
| | |
| | | public R<UserDetail> getUserDetail() { |
| | | AppUser appLoginUser = SecurityUtils.getAppLoginUser(); |
| | | User user = userService.getById(appLoginUser.getUserId()); |
| | | Company company = companyService.getOne(new LambdaQueryWrapper<Company>() |
| | | .eq(Company::getUserId, user.getUserId())); |
| | | UserDetail userDetail = new UserDetail(); |
| | | BeanUtils.copyProperties(user, userDetail); |
| | | BeanUtils.copyProperties(company, userDetail); |
| | | userDetail.setRegisterTime(user.getCreateTime()); |
| | | return R.ok(userDetail); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | @PutMapping("/updateUser") |
| | | @InnerAuth |
| | | public R<Void> updateUser(@RequestBody User user){ |
| | | user.setUpdateTime(LocalDateTime.now()); |
| | | userService.updateById(user); |
| | | return R.ok(); |
| | | } |
| | | |
| | | /** |
| | | * 编辑联系人信息 |
| | | */ |
| | | @PutMapping("/updateUserDetail") |
| | | @Operation(summary = "编辑联系人信息",tags = {"企业端"}) |
| | | public R<Void> updateUserDetail(@RequestBody UserDetail userDetail) { |
| | | AppUser appLoginUser = SecurityUtils.getAppLoginUser(); |
| | | userDetail.setUserId(appLoginUser.getUserId()); |
| | | User user = userService.getById(appLoginUser.getUserId()); |
| | | BeanUtils.copyProperties(userDetail, user); |
| | | userService.updateById(user); |
| | | return R.ok(); |
| | | } |
| | | |
| | | /** |
| | | * 修改手机号 |
| | | */ |
| | | @PutMapping("/updatePhone") |
| | | @Operation(summary = "修改手机号",tags = {"企业端"}) |
| | | public R<Void> updatePhone(@RequestBody UpdatePhone updatePhone) { |
| | | AppUser appLoginUser = SecurityUtils.getAppLoginUser(); |
| | | User user = userService.getById(appLoginUser.getUserId()); |
| | | if (!user.getPhone().equals(updatePhone.getOldPhone())) { |
| | | return R.fail("旧手机号错误"); |
| | | } |
| | | String verifyKey = CacheConstants.PHONE_CODE_KEY + StringUtils.nvl(updatePhone.getNewPhone(), ""); |
| | | String captcha = redisService.getCacheObject(verifyKey); |
| | | String[] split = captcha.split(":"); |
| | | |
| | | if (captcha == null) { |
| | | throw new CaptchaException("验证码错误"); |
| | | } |
| | | |
| | | long l = Long.parseLong(split[1]); |
| | | long l1 = System.currentTimeMillis(); |
| | | // 判断是否大于两分钟 |
| | | if (l1 - l > 2 * 60 * 1000) { |
| | | throw new CaptchaException("验证码已失效"); |
| | | } |
| | | captcha = split[0]; |
| | | if (!updatePhone.getCode().equalsIgnoreCase(captcha)) { |
| | | throw new CaptchaException("验证码错误"); |
| | | } |
| | | |
| | | if (!SecurityUtils.matchesPassword(updatePhone.getPassword(), user.getPassword())) { |
| | | return R.fail("密码错误"); |
| | | } |
| | | user.setPhone(updatePhone.getNewPhone()); |
| | | userService.updateById(user); |
| | | return R.ok(); |
| | | } |
| | | |
| | | /** |
| | | * 修改密码 |
| | | */ |
| | | @PutMapping("/updatePassword") |
| | | @Operation(summary = "修改密码",tags = {"企业端"}) |
| | | public R<Void> updatePassword(@RequestBody UpdatePassword updatePassword) { |
| | | AppUser appLoginUser = SecurityUtils.getAppLoginUser(); |
| | | User user = userService.getById(appLoginUser.getUserId()); |
| | | if (!SecurityUtils.matchesPassword(updatePassword.getOldPassword(), user.getPassword())) { |
| | | return R.fail("旧密码错误"); |
| | | } |
| | | if (!updatePassword.getNewPassword().equals(updatePassword.getConfirmPassword())){ |
| | | return R.fail("两次密码不一致"); |
| | | } |
| | | user.setPassword(SecurityUtils.encryptPassword(updatePassword.getNewPassword())); |
| | | userService.updateById(user); |
| | | return R.ok(); |
| | | } |
| | | |
| | | /** |
| | | * 通过公司名称和法人身份证号码查询用户信息 |
| | | */ |
| | | @GetMapping("/getUserByCompanyNameAndLegalId") |
| | | public R<User> getUserByCompanyNameAndLegalId(String companyName, String cardId) { |
| | | Company company = companyService.getOne(new LambdaQueryWrapper<Company>() |
| | | .eq(Company::getCompanyName, companyName) |
| | | .eq(Company::getIdCardNumber, cardId)); |
| | | User user = userService.getById(company.getUserId()); |
| | | return R.ok(user); |
| | | } |
| | | } |