用户资料完善/redis引入/三种登录方式

This commit is contained in:
kuang.yife 2024-03-12 13:34:07 +08:00
parent 4b2888d06d
commit 61353a17f4
9 changed files with 125 additions and 184 deletions

View File

@ -2,6 +2,7 @@ package com.playlet.web.controller.app;
import com.playlet.common.core.domain.Result; import com.playlet.common.core.domain.Result;
import com.playlet.system.domain.PlayletUser; import com.playlet.system.domain.PlayletUser;
import com.playlet.web.req.PlayUserReq;
import com.playlet.web.service.app.PlayletUserAppService; import com.playlet.web.service.app.PlayletUserAppService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
@ -31,4 +32,38 @@ public class PlayletUserAppController {
} }
} }
@ResponseBody
@PostMapping(value = "/loginByPhone")
@ApiOperation(value = "小程序通过手机验证码登录系统", httpMethod = "POST")
public Result<PlayletUser> loginByOpenid(@RequestBody PlayUserReq userReq){
try {
return Result.success(playletUserAppService.getByPhone(userReq));
}catch (Exception e){
return Result.error(e.getMessage());
}
}
@ResponseBody
@PostMapping(value = "/loginByPassword")
@ApiOperation(value = "小程序通过手机和密码登录系统", httpMethod = "POST")
public Result<PlayletUser> loginByPassword(@RequestBody PlayUserReq userReq){
try {
return Result.success(playletUserAppService.getByPassword(userReq));
}catch (Exception e){
return Result.error(e.getMessage());
}
}
@ResponseBody
@PostMapping(value = "/updatePlayletUser")
@ApiOperation(value = "修改用户资料", httpMethod = "POST")
public Result<String> updatePlayletUser(@RequestBody PlayletUser playletUser){
try {
playletUserAppService.updatePlayletUser(playletUser);
return Result.success();
}catch (Exception e){
return Result.error(e.getMessage());
}
}
} }

View File

@ -1,6 +1,5 @@
package com.playlet.web.controller.tool; package com.playlet.web.controller.tool;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;

View File

@ -1,183 +0,0 @@
package com.playlet.web.controller.tool;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.playlet.common.core.controller.BaseController;
import com.playlet.common.core.domain.R;
import com.playlet.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
/**
* swagger 用户测试方法
*
* @author ruoyi
*/
//@Api("用户信息管理")
@RestController
@RequestMapping("/test/user")
public class TestController extends BaseController
{
private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
{
users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
}
@ApiOperation("获取用户列表")
@GetMapping("/list")
public R<List<UserEntity>> userList()
{
List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
return R.ok(userList);
}
@ApiOperation("获取用户详细")
@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
@GetMapping("/{userId}")
public R<UserEntity> getUser(@PathVariable Integer userId)
{
if (!users.isEmpty() && users.containsKey(userId))
{
return R.ok(users.get(userId));
}
else
{
return R.fail("用户不存在");
}
}
@ApiOperation("新增用户")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", dataTypeClass = Integer.class),
@ApiImplicitParam(name = "username", value = "用户名称", dataType = "String", dataTypeClass = String.class),
@ApiImplicitParam(name = "password", value = "用户密码", dataType = "String", dataTypeClass = String.class),
@ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String", dataTypeClass = String.class)
})
@PostMapping("/save")
public R<String> save(UserEntity user)
{
if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
{
return R.fail("用户ID不能为空");
}
users.put(user.getUserId(), user);
return R.ok();
}
@ApiOperation("更新用户")
@PutMapping("/update")
public R<String> update(@RequestBody UserEntity user)
{
if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
{
return R.fail("用户ID不能为空");
}
if (users.isEmpty() || !users.containsKey(user.getUserId()))
{
return R.fail("用户不存在");
}
users.remove(user.getUserId());
users.put(user.getUserId(), user);
return R.ok();
}
@ApiOperation("删除用户信息")
@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
@DeleteMapping("/{userId}")
public R<String> delete(@PathVariable Integer userId)
{
if (!users.isEmpty() && users.containsKey(userId))
{
users.remove(userId);
return R.ok();
}
else
{
return R.fail("用户不存在");
}
}
}
@ApiModel(value = "UserEntity", description = "用户实体")
class UserEntity
{
@ApiModelProperty("用户ID")
private Integer userId;
@ApiModelProperty("用户名称")
private String username;
@ApiModelProperty("用户密码")
private String password;
@ApiModelProperty("用户手机")
private String mobile;
public UserEntity()
{
}
public UserEntity(Integer userId, String username, String password, String mobile)
{
this.userId = userId;
this.username = username;
this.password = password;
this.mobile = mobile;
}
public Integer getUserId()
{
return userId;
}
public void setUserId(Integer userId)
{
this.userId = userId;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getMobile()
{
return mobile;
}
public void setMobile(String mobile)
{
this.mobile = mobile;
}
}

View File

@ -1,6 +1,7 @@
package com.playlet.web.req; package com.playlet.web.req;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
/** /**
@ -13,10 +14,13 @@ public class PlayUserReq {
private String nickName; private String nickName;
@ApiModelProperty(value = "手机号")
private String phone; private String phone;
@ApiModelProperty(value = "验证码")
private String code; private String code;
@ApiModelProperty(value = "密码")
private String password; private String password;
private String inviteCode; private String inviteCode;

View File

@ -1,6 +1,7 @@
package com.playlet.web.service.app; package com.playlet.web.service.app;
import com.playlet.system.domain.PlayletUser; import com.playlet.system.domain.PlayletUser;
import com.playlet.web.req.PlayUserReq;
/** /**
* <p>短剧小程序用户service</p> * <p>短剧小程序用户service</p>
@ -14,4 +15,21 @@ public interface PlayletUserAppService {
*/ */
PlayletUser getByOpenId(String openid); PlayletUser getByOpenId(String openid);
/**
* @param playUserReq 手机号登陆
* @return 结果
*/
PlayletUser getByPhone(PlayUserReq playUserReq) throws Exception;
/**
* @param playUserReq 密码登录
* @return 结果
*/
PlayletUser getByPassword(PlayUserReq playUserReq) throws Exception;
/**
* @param playletUser 填写用户资料
*/
void updatePlayletUser(PlayletUser playletUser);
} }

View File

@ -2,12 +2,16 @@ package com.playlet.web.service.app.impl;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.playlet.common.constant.PlayletConstants; import com.playlet.common.constant.PlayletConstants;
import com.playlet.common.constant.RedisConstants;
import com.playlet.system.domain.PlayletUser; import com.playlet.system.domain.PlayletUser;
import com.playlet.system.service.IPlayletUserService; import com.playlet.system.service.IPlayletUserService;
import com.playlet.web.req.PlayUserReq;
import com.playlet.web.service.app.PlayletUserAppService; import com.playlet.web.service.app.PlayletUserAppService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date; import java.util.Date;
@ -19,6 +23,8 @@ public class PlayletUserAppServiceImpl implements PlayletUserAppService {
private final IPlayletUserService iPlayletUserService; private final IPlayletUserService iPlayletUserService;
private final StringRedisTemplate stringRedisTemplate;
@Override @Override
public PlayletUser getByOpenId(String openid) { public PlayletUser getByOpenId(String openid) {
PlayletUser playletUser = iPlayletUserService.lambdaQuery().eq(PlayletUser::getOpenId, openid).one(); PlayletUser playletUser = iPlayletUserService.lambdaQuery().eq(PlayletUser::getOpenId, openid).one();
@ -32,4 +38,33 @@ public class PlayletUserAppServiceImpl implements PlayletUserAppService {
return playletUser; return playletUser;
} }
@Override
public PlayletUser getByPhone(PlayUserReq playUserReq) throws Exception{
String alreadyCode = stringRedisTemplate.opsForValue().get(RedisConstants.SMS_CODE_PREFIX + playUserReq.getPhone());
if(StringUtils.isEmpty(alreadyCode)){
throw new Exception("验证码已过期!");
}
if(!alreadyCode.equals(playUserReq.getCode())){
throw new Exception("短信验证码错误!");
}
return iPlayletUserService.lambdaQuery()
.eq(PlayletUser::getPhone, playUserReq.getPhone()).one();
}
@Override
public PlayletUser getByPassword(PlayUserReq playUserReq) throws Exception {
PlayletUser playletUser = iPlayletUserService.lambdaQuery()
.eq(PlayletUser::getPhone, playUserReq.getPhone())
.eq(PlayletUser::getPassword, playUserReq.getPassword()).one();
if(playletUser == null){
throw new Exception("账号或密码错误!");
}else {
return playletUser;
}
}
@Override
public void updatePlayletUser(PlayletUser playletUser) {
iPlayletUserService.updateById(playletUser);
}
} }

View File

@ -5,14 +5,18 @@ import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse; import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config; import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions; import com.aliyun.teautil.models.RuntimeOptions;
import com.playlet.common.constant.RedisConstants;
import com.playlet.web.core.config.SmsConfig; import com.playlet.web.core.config.SmsConfig;
import com.playlet.web.service.SmsService; import com.playlet.web.service.SmsService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Slf4j @Slf4j
@Service @Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired)) @RequiredArgsConstructor(onConstructor = @__(@Autowired))
@ -25,6 +29,10 @@ public class SmsServiceImpl implements SmsService {
@Override @Override
public void sendSmsCode(String phone) throws Exception{ public void sendSmsCode(String phone) throws Exception{
// 短信验证码 // 短信验证码
String alreadyCode = stringRedisTemplate.opsForValue().get(RedisConstants.SMS_CODE_PREFIX + phone);
if(StringUtils.isNotEmpty(alreadyCode)){
throw new Exception("已发的验证码还在有效期!");
}
String code = String.valueOf((int)((Math.random() * 9 + 1) * Math.pow(10,5))); String code = String.valueOf((int)((Math.random() * 9 + 1) * Math.pow(10,5)));
// 工程代码泄露可能会导致AccessKey泄露并威胁账号下所有资源的安全性以下代码示例仅供参考建议使用更安全的 STS 方式更多鉴权访问方式请参见https://help.aliyun.com/document_detail/378657.html // 工程代码泄露可能会导致AccessKey泄露并威胁账号下所有资源的安全性以下代码示例仅供参考建议使用更安全的 STS 方式更多鉴权访问方式请参见https://help.aliyun.com/document_detail/378657.html
Client client = createClient(smsConfig.getAccessKeyId(), smsConfig.getAccessKeySecret()); Client client = createClient(smsConfig.getAccessKeyId(), smsConfig.getAccessKeySecret());
@ -37,6 +45,7 @@ public class SmsServiceImpl implements SmsService {
// 复制代码运行请自行打印 API 的返回值 // 复制代码运行请自行打印 API 的返回值
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime); SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
log.info("发送给{}短信响应为{}", phone, sendSmsResponse); log.info("发送给{}短信响应为{}", phone, sendSmsResponse);
stringRedisTemplate.opsForValue().set(RedisConstants.SMS_CODE_PREFIX + phone, code, 300, TimeUnit.SECONDS);
} }
/** /**

View File

@ -45,6 +45,20 @@ user:
# Spring配置 # Spring配置
spring: spring:
# redis配置
redis:
database: 1
host: 121.62.23.77
port: 6379
password: Clunt@12345
timeout: 6000ms # 连接超时时长(毫秒)
lettuce:
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
# 模板引擎 # 模板引擎
thymeleaf: thymeleaf:
mode: HTML mode: HTML

View File

@ -0,0 +1,10 @@
package com.playlet.common.constant;
public class RedisConstants {
/**
* 短剧APP验证码缓存前缀
*/
public static final String SMS_CODE_PREFIX = "PLAYLET_SMS_PREFIX_";
}