diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/controller/LogController.java b/pig-admin-service/src/main/java/com/github/pig/admin/controller/LogController.java index 18bd92e8..2badf745 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/controller/LogController.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/controller/LogController.java @@ -47,7 +47,6 @@ public class LogController extends BaseController { */ @DeleteMapping("/{id}") public R delete(@PathVariable Integer id) { - int o = 11 / 0; return new R<>(sysLogService.updateByLogId(id)); } } diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java b/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java index e7a09a92..4eec6179 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/controller/UserController.java @@ -104,7 +104,7 @@ public class UserController extends BaseController { * @param userDto 用户信息 * @return R */ - @ApiOperation(value="更新用户详细信息", notes="根据传过来的UserDto信息来更新用户详细信息") + @ApiOperation(value = "更新用户详细信息", notes = "根据传过来的UserDto信息来更新用户详细信息") @ApiImplicitParams({ @ApiImplicitParam(name = "userDto", value = "用户详细实体user", required = true, dataType = "UserDto") }) @@ -125,6 +125,17 @@ public class UserController extends BaseController { return userService.findUserByUsername(username); } + /** + * 通过手机号查询用户及其角色信息 + * + * @param mobile 手机号 + * @return UseVo 对象 + */ + @GetMapping("/findUserByMobile/{mobile}") + public UserVo findUserByMobile(@PathVariable String mobile) { + return userService.findUserByMobile(mobile); + } + /** * 分页查询用户 * diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/controller/ImageCodeController.java b/pig-admin-service/src/main/java/com/github/pig/admin/controller/ValidateCodeController.java similarity index 68% rename from pig-admin-service/src/main/java/com/github/pig/admin/controller/ImageCodeController.java rename to pig-admin-service/src/main/java/com/github/pig/admin/controller/ValidateCodeController.java index 00261be1..0f2974b8 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/controller/ImageCodeController.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/controller/ValidateCodeController.java @@ -2,12 +2,15 @@ package com.github.pig.admin.controller; import com.github.pig.admin.service.UserService; import com.github.pig.common.constant.SecurityConstants; +import com.github.pig.common.util.Assert; +import com.github.pig.common.util.R; import com.google.code.kaptcha.Producer; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.ResponseBody; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; @@ -18,9 +21,10 @@ import java.awt.image.BufferedImage; /** * @author lengleng * @date 2017/12/18 + * 验证码提供 */ @Controller -public class ImageCodeController { +public class ValidateCodeController { @Autowired private Producer producer; @Autowired @@ -35,15 +39,30 @@ public class ImageCodeController { @GetMapping(SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/{randomStr}") public void createCode(@PathVariable String randomStr, HttpServletRequest request, HttpServletResponse response) throws Exception { + Assert.isBlank(randomStr, "机器码不能为空"); response.setHeader("Cache-Control", "no-store, no-cache"); response.setContentType("image/jpeg"); //生成文字验证码 String text = producer.createText(); //生成图片验证码 BufferedImage image = producer.createImage(text); - userService.save(randomStr, text); + userService.saveImageCode(randomStr, text); ServletOutputStream out = response.getOutputStream(); ImageIO.write(image, "JPEG", out); IOUtils.closeQuietly(out); } + + /** + * 发送手机验证码 + * 后期要加接口限制 + * + * @param mobile 手机号 + * @return R + */ + @ResponseBody + @GetMapping(SecurityConstants.MOBILE_VALIDATE_CODE_URL_PREFIX + "/{mobile}") + public R createCode(@PathVariable String mobile) { + Assert.isBlank(mobile, "手机号不能为空"); + return new R<>(userService.sendSmsCode(mobile)); + } } diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/mapper/SysUserMapper.java b/pig-admin-service/src/main/java/com/github/pig/admin/mapper/SysUserMapper.java index 430c5481..f5a01113 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/mapper/SysUserMapper.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/mapper/SysUserMapper.java @@ -33,4 +33,12 @@ public interface SysUserMapper extends BaseMapper { * @return list */ List selectUserVoPage(Query query, Map params); + + /** + * 通过手机号查询用户信息(含有角色信息) + * + * @param mobile 用户名 + * @return userVo + */ + UserVo selectUserVoByMobile(String mobile); } \ No newline at end of file diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/service/UserService.java b/pig-admin-service/src/main/java/com/github/pig/admin/service/UserService.java index 70d2c598..f1f72bee 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/service/UserService.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/service/UserService.java @@ -41,7 +41,7 @@ public interface UserService extends IService { * 保存验证码 * @param randomStr 随机串 * @param imageCode 验证码*/ - void save(String randomStr, String imageCode); + void saveImageCode(String randomStr, String imageCode); /** * 删除用户 @@ -65,4 +65,18 @@ public interface UserService extends IService { * @return */ Boolean updateUser(UserDto userDto, String username); + + /** + * 通过手机号查询用户信息 + * @param mobile 手机号 + * @return 用户信息 + */ + UserVo findUserByMobile(String mobile); + + /** + * 发送验证码 + * @param mobile 手机号 + * @return true、false + */ + Boolean sendSmsCode(String mobile); } diff --git a/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/UserServiceImpl.java b/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/UserServiceImpl.java index 811bde27..f31310bc 100644 --- a/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/UserServiceImpl.java +++ b/pig-admin-service/src/main/java/com/github/pig/admin/service/impl/UserServiceImpl.java @@ -15,6 +15,9 @@ import com.github.pig.common.constant.SecurityConstants; import com.github.pig.common.util.Query; import com.github.pig.common.vo.SysRole; import com.github.pig.common.vo.UserVo; +import com.xiaoleilu.hutool.util.RandomUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; @@ -35,6 +38,7 @@ import java.util.concurrent.TimeUnit; */ @Service public class UserServiceImpl extends ServiceImpl implements UserService { + private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class); private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder(); @Autowired private SysMenuService sysMenuService; @@ -56,7 +60,7 @@ public class UserServiceImpl extends ServiceImpl impleme //设置角色列表 List roleList = userVo.getRoleList(); List roleNames = new ArrayList<>(); - for (SysRole sysRole:roleList){ + for (SysRole sysRole : roleList) { roleNames.add(sysRole.getRoleName()); } String[] roles = roleNames.toArray(new String[roleNames.size()]); @@ -73,6 +77,18 @@ public class UserServiceImpl extends ServiceImpl impleme return sysUserMapper.selectUserVoByUsername(username); } + /** + * 通过手机号查询用户信息 + * + * @param mobile 手机号 + * @return 用户信息 + */ + @Override + @Cacheable(value = "user_details_mobile", key = "#mobile") + public UserVo findUserByMobile(String mobile) { + return sysUserMapper.selectUserVoByMobile(mobile); + } + @Override public Page selectWithRolePage(Query query) { query.setRecords(sysUserMapper.selectUserVoPage(query, query.getCondition())); @@ -86,10 +102,34 @@ public class UserServiceImpl extends ServiceImpl impleme * @param imageCode 验证码信息 */ @Override - public void save(String randomStr, String imageCode) { + public void saveImageCode(String randomStr, String imageCode) { redisTemplate.opsForValue().set(SecurityConstants.DEFAULT_CODE_KEY + randomStr, imageCode, SecurityConstants.DEFAULT_IMAGE_EXPIRE, TimeUnit.SECONDS); } + /** + * 发送验证码 + *

+ * 1. 先去redis 查询是否 60S内已经发送 + * 2. 未发送: 产生4位数字 手机号-验证码 + * 3. 调用短信网关发送信息 + * 4. 保存redis + * + * @param mobile 手机号 + * @return true、false + */ + @Override + public Boolean sendSmsCode(String mobile) { + Object tempCode = redisTemplate.opsForValue().get(SecurityConstants.DEFAULT_CODE_KEY + mobile); + boolean result = false; + if (tempCode == null) { + String code = RandomUtil.randomNumbers(4); + logger.info("短信发送成功 -> 手机号:{} -> 验证码:{}", mobile, code); + redisTemplate.opsForValue().set(SecurityConstants.DEFAULT_CODE_KEY + mobile, code, SecurityConstants.DEFAULT_IMAGE_EXPIRE, TimeUnit.SECONDS); + result = true; + } + return result; + } + /** * 删除用户 * diff --git a/pig-admin-service/src/main/resources/mapper/SysUserMapper.xml b/pig-admin-service/src/main/resources/mapper/SysUserMapper.xml index f9413681..a43bd2fd 100644 --- a/pig-admin-service/src/main/resources/mapper/SysUserMapper.xml +++ b/pig-admin-service/src/main/resources/mapper/SysUserMapper.xml @@ -58,6 +58,32 @@ WHERE `user`.username = #{username} + + +