前台完善注册登录接口后端
This commit is contained in:
parent
6d73b4a1d3
commit
53c62fb3f1
@ -1,9 +1,9 @@
|
||||
package com.yovinchen.xlcs.model.user;
|
||||
|
||||
import com.yovinchen.xlcs.enums.UserType;
|
||||
import com.yovinchen.xlcs.model.base.BaseEntity;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.yovinchen.xlcs.enums.UserType;
|
||||
import com.yovinchen.xlcs.model.base.BaseEntity;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
@ -26,6 +26,14 @@ public class User extends BaseEntity {
|
||||
@TableField("nick_name")
|
||||
private String nickName;
|
||||
|
||||
@ApiModelProperty(value = "邮箱")
|
||||
@TableField("email")
|
||||
private String email;
|
||||
|
||||
@ApiModelProperty(value = "密码")
|
||||
@TableField("password")
|
||||
private String password;
|
||||
|
||||
@ApiModelProperty(value = "身份证号码")
|
||||
@TableField("id_no")
|
||||
private String idNo;
|
||||
|
@ -21,6 +21,12 @@ public class UserLoginVo implements Serializable {
|
||||
@ApiModelProperty(value = "昵称")
|
||||
private String nickName;
|
||||
|
||||
@ApiModelProperty(value = "邮箱")
|
||||
private String email;
|
||||
|
||||
@ApiModelProperty(value = "密码")
|
||||
private String password;
|
||||
|
||||
@ApiModelProperty(value = "小程序open id")
|
||||
private String openId;
|
||||
|
||||
|
@ -0,0 +1,62 @@
|
||||
package com.yovinchen.xlcs.user.controller;
|
||||
|
||||
import com.yovinchen.xlcs.common.exception.xlcsException;
|
||||
import com.yovinchen.xlcs.common.result.Result;
|
||||
import com.yovinchen.xlcs.common.result.ResultCodeEnum;
|
||||
import com.yovinchen.xlcs.model.user.User;
|
||||
import com.yovinchen.xlcs.user.service.LoginService;
|
||||
import com.yovinchen.xlcs.user.service.UserService;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ClassName: UserController
|
||||
* Package: com.yovinchen.xlcs.user.controller
|
||||
*
|
||||
* @author yovinchen
|
||||
* @since 2024/2/23 10:08
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("api/user/h5")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private LoginService loginService;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@ApiOperation(value = "手机号密码登录")
|
||||
@PostMapping("login")
|
||||
public Result login(@RequestBody Map<String, String> loginRequest) {
|
||||
String phone = loginRequest.get("phone");
|
||||
String password = loginRequest.get("password");
|
||||
Map<String, Object> map = loginService.login(phone, password);
|
||||
return Result.ok(map);
|
||||
}
|
||||
|
||||
@PostMapping("auth/updateUser")
|
||||
@ApiOperation(value = "更新用户昵称与头像")
|
||||
public Result updateUser(@RequestBody User user) {
|
||||
userService.updateUser(user);
|
||||
return Result.ok(null);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "手机号密码注册")
|
||||
@PostMapping("register")
|
||||
public Result register(@RequestBody Map<String, String> loginRequest) {
|
||||
String phone = loginRequest.get("phone");
|
||||
String password = loginRequest.get("password");
|
||||
if (phone.isEmpty()) {
|
||||
throw new xlcsException(ResultCodeEnum.PHONE_NULL_ERROR);
|
||||
}
|
||||
Map<String, Object> map = userService.register(phone, password);
|
||||
return Result.ok(map);
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.yovinchen.xlcs.user.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.yovinchen.xlcs.common.constant.RedisConst;
|
||||
import com.yovinchen.xlcs.common.exception.xlcsException;
|
||||
import com.yovinchen.xlcs.common.result.ResultCodeEnum;
|
||||
import com.yovinchen.xlcs.common.utils.JwtHelper;
|
||||
import com.yovinchen.xlcs.common.utils.MD5;
|
||||
import com.yovinchen.xlcs.model.user.User;
|
||||
import com.yovinchen.xlcs.vo.user.LeaderAddressVo;
|
||||
import com.yovinchen.xlcs.vo.user.UserLoginVo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* ClassName: LoginService
|
||||
* Package: com.yovinchen.xlcs.user.service
|
||||
*
|
||||
* @author yovinchen
|
||||
* @since 2024/2/23 10:09
|
||||
*/
|
||||
@Service
|
||||
public class LoginService {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
public Map<String, Object> login(String phone, String password) {
|
||||
User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, phone)
|
||||
.eq(User::getPassword, MD5.encrypt(password)));
|
||||
if (user.getPhone() == null) {
|
||||
throw new xlcsException(ResultCodeEnum.ACCOUNT_PASSWORD_ERROR);
|
||||
}
|
||||
// 根据userId查询配送点和配送员信息
|
||||
////配送点 user表 user_delivery表
|
||||
////配送员 leader表
|
||||
LeaderAddressVo leaderAddressVo = userService.getLeaderAddressByUserId(user.getId());
|
||||
|
||||
//6 使用JWT工具根据userId和userName生成token字符串
|
||||
String token = JwtHelper.createToken(user.getId(), user.getNickName());
|
||||
|
||||
//7 获取当前登录用户信息,放到Redis里面,设置有效时间
|
||||
UserLoginVo userLoginVo = userService.getUserLoginVo(user.getId());
|
||||
redisTemplate.opsForValue()
|
||||
.set(RedisConst.USER_LOGIN_KEY_PREFIX + user.getId(), userLoginVo, RedisConst.USERKEY_TIMEOUT, TimeUnit.DAYS);
|
||||
|
||||
//8 需要数据封装到map返回
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("user", user);
|
||||
map.put("token", token);
|
||||
map.put("leaderAddressVo", leaderAddressVo);
|
||||
return map;
|
||||
}
|
||||
}
|
@ -5,6 +5,8 @@ import com.yovinchen.xlcs.model.user.User;
|
||||
import com.yovinchen.xlcs.vo.user.LeaderAddressVo;
|
||||
import com.yovinchen.xlcs.vo.user.UserLoginVo;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ClassName: UserService
|
||||
* Package: com.yovinchen.xlcs.user.service
|
||||
@ -43,4 +45,13 @@ public interface UserService extends IService<User> {
|
||||
* @param user
|
||||
*/
|
||||
void updateUser(User user);
|
||||
|
||||
/**
|
||||
* 手机号注册账号
|
||||
*
|
||||
* @param phone
|
||||
* @param password
|
||||
* @return
|
||||
*/
|
||||
Map<String, Object> register(String phone, String password);
|
||||
}
|
||||
|
@ -3,6 +3,9 @@ package com.yovinchen.xlcs.user.service.impl;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.yovinchen.xlcs.common.auth.AuthContextHolder;
|
||||
import com.yovinchen.xlcs.common.exception.xlcsException;
|
||||
import com.yovinchen.xlcs.common.result.ResultCodeEnum;
|
||||
import com.yovinchen.xlcs.enums.UserType;
|
||||
import com.yovinchen.xlcs.model.user.Leader;
|
||||
import com.yovinchen.xlcs.model.user.User;
|
||||
import com.yovinchen.xlcs.model.user.UserDelivery;
|
||||
@ -16,6 +19,10 @@ import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* ClassName: UserServiceImpl
|
||||
* Package: com.yovinchen.xlcs.user.service.impl
|
||||
@ -31,19 +38,43 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
@Autowired
|
||||
private LeaderMapper leaderMapper;
|
||||
|
||||
//// 判断是否是第一次使用微信授权登录:如何判断?openId
|
||||
/**
|
||||
* 使用正则表达式验证手机号格式
|
||||
*
|
||||
* @param phoneNumber
|
||||
* @return
|
||||
*/
|
||||
public boolean isValidPhoneNumber(String phoneNumber) {
|
||||
// 使用正则表达式验证手机号格式
|
||||
String regex = "^1[3456789]\\d{9}$"; // 手机号的正则表达式
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(phoneNumber);
|
||||
return matcher.matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是第一次使用微信授权登录:如何判断?openId
|
||||
*
|
||||
* @param openid
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public User getUserByOpenId(String openid) {
|
||||
User user = baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openid));
|
||||
return user;
|
||||
}
|
||||
|
||||
//5 根据userId查询提货点和配送员信息
|
||||
/**
|
||||
* 根据userId查询提货点和配送员信息
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public LeaderAddressVo getLeaderAddressByUserId(Long userId) {
|
||||
//根据userId查询用户默认的配送员id
|
||||
UserDelivery userDelivery = userDeliveryMapper.selectOne(new LambdaQueryWrapper<UserDelivery>().eq(UserDelivery::getUserId, userId)
|
||||
.eq(UserDelivery::getIsDefault, 1));
|
||||
.eq(UserDelivery::getIsDefault, 1));
|
||||
if (userDelivery == null) {
|
||||
return null;
|
||||
}
|
||||
@ -61,7 +92,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
return leaderAddressVo;
|
||||
}
|
||||
|
||||
//7 获取当前登录用户信息,
|
||||
/**
|
||||
* 获取当前登录用户信息
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public UserLoginVo getUserLoginVo(Long id) {
|
||||
User user = baseMapper.selectById(id);
|
||||
@ -73,7 +109,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
userLoginVo.setOpenId(user.getOpenId());
|
||||
|
||||
UserDelivery userDelivery = userDeliveryMapper.selectOne(new LambdaQueryWrapper<UserDelivery>().eq(UserDelivery::getUserId, id)
|
||||
.eq(UserDelivery::getIsDefault, 1));
|
||||
.eq(UserDelivery::getIsDefault, 1));
|
||||
if (userDelivery != null) {
|
||||
userLoginVo.setLeaderId(userDelivery.getLeaderId());
|
||||
userLoginVo.setWareId(userDelivery.getWareId());
|
||||
@ -84,14 +120,55 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
return userLoginVo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户信息
|
||||
*
|
||||
* @param user
|
||||
*/
|
||||
@Override
|
||||
public void updateUser(User user) {
|
||||
//获取当前登录用户id
|
||||
User user1 = baseMapper.selectById(AuthContextHolder.getUserId());
|
||||
//把昵称更新为微信用户
|
||||
user1.setNickName(user.getNickName()
|
||||
.replaceAll("[ue000-uefff]", "*"));
|
||||
.replaceAll("[ue000-uefff]", "*"));
|
||||
user1.setPhotoUrl(user.getPhotoUrl());
|
||||
baseMapper.updateById(user1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机号注册账号
|
||||
*
|
||||
* @param phone
|
||||
* @param password
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> register(String phone, String password) {
|
||||
|
||||
// 检测手机号格式
|
||||
if (!isValidPhoneNumber(phone)) {
|
||||
throw new xlcsException(ResultCodeEnum.PHONE_FORMAT_ERROR);
|
||||
}
|
||||
|
||||
User user = baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPhone, phone));
|
||||
|
||||
// 查看用户信息是否存在
|
||||
if (user != null) {
|
||||
throw new xlcsException(ResultCodeEnum.PHONE_REGISTERED_ERROR);
|
||||
}
|
||||
|
||||
//TODO 短信验证
|
||||
|
||||
|
||||
//构建用户信息
|
||||
user = new User();
|
||||
user.setNickName(phone);
|
||||
user.setPhotoUrl("");
|
||||
user.setUserType(UserType.USER);
|
||||
user.setIsNew(1);
|
||||
baseMapper.insert(user);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user