总览:
1、service处理
在api模块下service包,创建一个UserService接口:(根据手机号查询数据queryByPhone(String phone))
package com.bjpowernode.api.service;
import com.bjpowernode.api.model.User;
import com.bjpowernode.api.pojo.UserAccountInfo;
public interface UserService {
/**
* 根据手机号查询数据
*/
User queryByPhone(String phone);
/*用户注册*/
int userRegister(String phone, String password);
/*登录*/
User userLogin(String phone, String pword);
/*更新实名认证信息*/
boolean modifyRealname(String phone, String name, String idCard);
/*获取用户和资金信息*/
UserAccountInfo queryUserAllInfo(Integer uid);
/*查询用户*/
User queryById(Integer uid);
}
2、serviceImpl处理
在dataservice模块service包,实现UserService接口,创建UserServiceImpl实现类:
package com.bjpowernode.dataservice.service;
import com.bjpowernode.api.model.FinanceAccount;
import com.bjpowernode.api.model.User;
import com.bjpowernode.api.pojo.UserAccountInfo;
import com.bjpowernode.api.service.UserService;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.dataservice.mapper.FinanceAccountMapper;
import com.bjpowernode.dataservice.mapper.UserMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
@DubboService(interfaceClass = UserService.class,version = "1.0")
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Resource
private FinanceAccountMapper financeAccountMapper;
@Value("${ylb.config.password-salt}")
private String passwordSalt;
@Override
public User queryByPhone(String phone) {
User user = null;
if(CommonUtil.checkPhone(phone)){
user = userMapper.selectByPhone(phone);
}
return user;
}
/*用户注册*/
@Transactional(rollbackFor = Exception.class)
@Override
public synchronized int userRegister(String phone, String password) {
int result = 0;//默认参数不正确
if( CommonUtil.checkPhone(phone)
&& (password != null && password.length()==32)){
//判断手机号在库中是否存在
User queryUser = userMapper.selectByPhone(phone);
if(queryUser == null){
//注册密码的md5二次加密。 给原始的密码加盐(salt)
String newPassword = DigestUtils.md5Hex( password + passwordSalt);
//注册u_user
User user = new User();
user.setPhone(phone);
user.setLoginPassword(newPassword);
user.setAddTime(new Date());
userMapper.insertReturnPrimaryKey(user);
//获取主键user.getId()
FinanceAccount account = new FinanceAccount();
account.setUid(user.getId());
account.setAvailableMoney(new BigDecimal("0"));
financeAccountMapper.insertSelective(account);
//成功result = 1
result = 1;
} else {
//手机号存在
result = 2;
}
}
return result;
}
/*登录*/
@Transactional(rollbackFor = Exception.class)
@Override
public User userLogin(String phone, String password) {
User user = null;
if( CommonUtil.checkPhone(phone) && (password != null && password.length() == 32)) {
String newPassword = DigestUtils.md5Hex( password + passwordSalt);
user = userMapper.selectLogin(phone,newPassword);
//更新最后登录时间
if( user != null){
user.setLastLoginTime(new Date());
userMapper.updateByPrimaryKeySelective(user);
}
}
return user;
}
/*更新实名认证信息*/
@Override
public boolean modifyRealname(String phone, String name, String idCard) {
int rows = 0;
if(!StringUtils.isAnyBlank(phone,name,idCard)){
rows = userMapper.updateRealname(phone,name,idCard);
}
return rows > 0 ;
}
/*获取用户和资金信息*/
@Override
public UserAccountInfo queryUserAllInfo(Integer uid) {
UserAccountInfo info = null;
if( uid != null && uid > 0 ) {
info = userMapper.selectUserAccountById(uid);
}
return info ;
}
/*查询用户*/
@Override
public User queryById(Integer uid) {
User user = null;
if( uid != null && uid > 0 ){
user = userMapper.selectByPrimaryKey(uid);
}
return user;
}
}
其中:
1、使用手机号查询用户:(需要在dataservice模块mapper包下的UserMapper接口添加方法,并在resources/mappers/UserMapper.xml编写SQL语句):
/*使用手机号查询用户*/
User selectByPhone(@Param("phone") String phone);
<!--使用手机号查询用户-->
<select id="selectByPhone" resultMap="BaseResultMap">
select <include refid="Base_Column_List"></include>
from u_user
where phone = #{phone}
</select>
3、controller处理
在web模块下controller包,公用类BaseController添加:(用户服务对象)
package com.bjpowernode.front.controller;
import com.bjpowernode.api.service.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.Resource;
/**
* 公用controller类
*/
public class BaseController {
//声明公共的方法,属性的等
@Resource
protected StringRedisTemplate stringRedisTemplate;
//平台信息服务
@DubboReference(interfaceClass = PlatBaseInfoService.class,version = "1.0")
protected PlatBaseInfoService platBaseInfoService;
//产品服务
@DubboReference(interfaceClass = ProductService.class,version = "1.0")
protected ProductService productService;
//投资服务
@DubboReference(interfaceClass = InvestService.class,version = "1.0")
protected InvestService investService;
//用户服务
@DubboReference(interfaceClass = UserService.class,version = "1.0")
protected UserService userService;
//充值服务
@DubboReference(interfaceClass = RechargeService.class,version = "1.0")
protected RechargeService rechargeService;
}
在web模块下controller包,创建用户功能控制UserController类:(手机号是否存在phoneExists(@RequestParam(“phone”) String phone))文章来源:https://www.toymoban.com/news/detail-597367.html
package com.bjpowernode.front.controller;
import com.bjpowernode.api.model.User;
import com.bjpowernode.api.pojo.UserAccountInfo;
import com.bjpowernode.common.enums.RCode;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.common.util.JwtUtil;
import com.bjpowernode.front.service.RealnameServiceImpl;
import com.bjpowernode.front.service.SmsService;
import com.bjpowernode.front.view.RespResult;
import com.bjpowernode.front.vo.RealnameVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.jute.compiler.generated.Rcc;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.management.relation.Relation;
import java.util.HashMap;
import java.util.Map;
@Api(tags = "用户功能")
@RestController
@RequestMapping("/v1/user")
public class UserController extends BaseController {
@Resource(name = "smsCodeRegisterImpl")
private SmsService smsService;
@Resource(name = "smsCodeLoginImpl")
private SmsService loginSmsService;
@Resource
private RealnameServiceImpl realnameService;
@Resource
private JwtUtil jwtUtil;
/**手机号注册用户*/
@ApiOperation(value = "手机号注册用户")
@PostMapping("/register")
public RespResult userRegister(@RequestParam String phone,
@RequestParam String pword,
@RequestParam String scode){
RespResult result = RespResult.fail();
//1.检查参数
if( CommonUtil.checkPhone(phone)){
if(pword !=null && pword.length() == 32 ){
//检查短信验证码
if( smsService.checkSmsCode(phone,scode)){
//可以注册
int registerResult = userService.userRegister(phone,pword);
if( registerResult == 1 ){
result = RespResult.ok();
} else if( registerResult == 2 ){
result.setRCode(RCode.PHONE_EXISTS);
} else {
result.setRCode(RCode.REQUEST_PARAM_ERR);
}
} else {
//短信验证码无效
result.setRCode(RCode.SMS_CODE_INVALID);
}
} else {
result.setRCode(RCode.REQUEST_PARAM_ERR);
}
} else {
//手机号格式不正确
result.setRCode(RCode.PHONE_FORMAT_ERR);
}
return result;
}
/** 手机号是否存在 */
@ApiOperation(value = "手机号是否注册过",notes = "在注册功能中,判断手机号是否可以注册")
@ApiImplicitParam(name = "phone",value = "手机号")
@GetMapping("/phone/exists")
public RespResult phoneExists(@RequestParam("phone") String phone){
RespResult result = new RespResult();
result.setRCode(RCode.PHONE_EXISTS);
//1.检查请求参数是否符合要求
if(CommonUtil.checkPhone(phone)){
//可以执行逻辑 ,查询数据库,调用数据服务
User user = userService.queryByPhone(phone);
if( user == null ){
//可以注册
result = RespResult.ok();
}
//把查询到的手机号放入redis。 然后检查手机号是否存在,可以查询redis
} else {
result.setRCode(RCode.PHONE_FORMAT_ERR);
}
return result;
}
/** 登录,获取token-jwt*/
@ApiOperation(value = "用户登录-获取访问token")
@PostMapping("/login")
public RespResult userLogin(@RequestParam String phone,
@RequestParam String pword,
@RequestParam String scode) throws Exception{
RespResult result = RespResult.fail();
if(CommonUtil.checkPhone(phone) && (pword != null && pword.length() == 32) ){
if(loginSmsService.checkSmsCode(phone,scode)){
//访问data-service
User user = userService.userLogin(phone,pword);
if( user != null){
//登录成功,生成token
Map<String, Object> data = new HashMap<>();
data.put("uid",user.getId());
String jwtToken = jwtUtil.createJwt(data,120);
result = RespResult.ok();
result.setAccessToken(jwtToken);
Map<String,Object> userInfo = new HashMap<>();
userInfo.put("uid",user.getId());
userInfo.put("phone",user.getPhone());
userInfo.put("name",user.getName());
result.setData(userInfo);
} else {
result.setRCode(RCode.PHONE_LOGIN_PASSWORD_INVALID);
}
} else {
result.setRCode(RCode.SMS_CODE_INVALID);
}
} else {
result.setRCode(RCode.REQUEST_PARAM_ERR);
}
return result;
}
/** 实名认证 vo: value object*/
@ApiOperation(value = "实名认证",notes = "提供手机号和姓名,身份证号。 认证姓名和身份证号是否一致")
@PostMapping("/realname")
public RespResult userRealname(@RequestBody RealnameVO realnameVO){
RespResult result = RespResult.fail();
result.setRCode(RCode.REQUEST_PARAM_ERR);
//1验证请求参数
if( CommonUtil.checkPhone(realnameVO.getPhone())){
if(StringUtils.isNotBlank(realnameVO.getName()) &&
StringUtils.isNotBlank(realnameVO.getIdCard())){
//判断用户已经做过
User user = userService.queryByPhone(realnameVO.getPhone());
if( user != null ){
if( StringUtils.isNotBlank(user.getName())){
result.setRCode(RCode.REALNAME_RETRY);
} else {
//有短信验证码,先不写
//调用第三方接口,判断认证结果
boolean realnameResult = realnameService.handleRealname(
realnameVO.getPhone(),realnameVO.getName(),
realnameVO.getIdCard());
if( realnameResult == true ){
result = RespResult.ok();
} else {
result.setRCode(RCode.REALNAME_FAIL);
}
}
}
}
}
return result;
}
/** 用户中心 */
@ApiOperation(value = "用户中心")
@GetMapping("/usercenter")
public RespResult userCenter(@RequestHeader(value = "uid",required = false) Integer uid){
RespResult result = RespResult.fail();
if( uid != null && uid > 0 ){
UserAccountInfo userAccountInfo = userService.queryUserAllInfo(uid);
if( userAccountInfo != null ){
result = RespResult.ok();
Map<String,Object> data = new HashMap<>();
data.put("name",userAccountInfo.getName());
data.put("phone",userAccountInfo.getPhone());
data.put("headerUrl",userAccountInfo.getHeaderImage());
data.put("money",userAccountInfo.getAvailableMoney());
if( userAccountInfo.getLastLoginTime() != null){
data.put("loginTime", DateFormatUtils.format(
userAccountInfo.getLastLoginTime(),"yyyy-MM-dd HH:mm:ss"));
} else {
data.put("loginTime","-");
}
result.setData(data);
}
}
return result;
}
}
手机号格式判断(正则表达式)
在common模块util包,CommonUtil工具类添加方法:(checkPhone(String phone))文章来源地址https://www.toymoban.com/news/detail-597367.html
package com.bjpowernode.common.util;
import java.math.BigDecimal;
import java.util.regex.Pattern;
public class CommonUtil {
/*处理pageNo*/
public static int defaultPageNo(Integer pageNo) {
int pNo = pageNo;
if (pageNo == null || pageNo < 1) {
pNo = 1;
}
return pNo;
}
/*处理pageSize*/
public static int defaultPageSize(Integer pageSize) {
int pSize = pageSize;
if (pageSize == null || pageSize < 1) {
pSize = 1;
}
return pSize;
}
/*手机号脱敏*/
public static String tuoMinPhone(String phone) {
String result = "***********";
if (phone != null && phone.trim().length() == 11) {
result = phone.substring(0,3) + "******" + phone.substring(9,11);
}
return result;
}
/*手机号格式 true:格式正确;false不正确*/
public static boolean checkPhone(String phone){
boolean flag = false;
if( phone != null && phone.length() == 11 ){
//^1[1-9]\\d{9}$
flag = Pattern.matches("^1[1-9]\\d{9}$",phone);
}
return flag;
}
/*比较BigDecimal n1 >=n2 :true ,false*/
public static boolean ge(BigDecimal n1, BigDecimal n2){
if( n1 == null || n2 == null){
throw new RuntimeException("参数BigDecimal是null");
}
return n1.compareTo(n2) >= 0;
}
}
到了这里,关于ylb-接口6验证手机号是否注册的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!