Commit c1237d99 authored by m1991's avatar m1991

手机号登录功能实现

parent 88ac6902
......@@ -68,4 +68,14 @@ public interface UsersMapper extends BaseMapper<User> {
*/
List<User> getAllGoldUser();
/**
* 查询用户
* @param param
* @return
*/
public List<User> getUsersListByMap(Map<String,Object> param);
//根据手机号查询用户Integer
Integer queryUsersByPhone(@Param("beInvitedCode")String userId);
}
......@@ -150,4 +150,69 @@
insert into user(user_id,password) value (#{userId},#{password})
</insert>
<select id="queryUsersByPhone" resultType="java.lang.Integer">
select
user_id
from
<include refid="table"/>
<where>
user_id=#{userId}
</where>
</select>
<!--分页查询所有用户信息 -->
<select id="getUsersListByMap" resultType="cn.wisenergy.model.app.User" parameterType="java.util.Map">
select id as id,
user_id as userId,
password as password,
user_level as userLevel,
cross_border_line as crossBorderLine,
id_card_number as idCardNumber,
fans_nickname as fansNickname,
invite_code as inviteCode,
be_invited_code as beInvitedCode,
create_time as createTime,
update_time as updateTime
from user_info
<trim prefix="where" prefixOverrides="and | or">
<if test="id != null">
and id=#{id}
</if>
<if test="account != null and account!=''">
and userId=#{userId}
</if>
<if test="password != null and password!=''">
and password=#{password}
</if>
<if test="salt != null and salt!=''">
and userLevel=#{userLevel}
</if>
<if test="userName != null and userName!=''">
and crossBorderLine=#{crossBorderLine}
</if>
<if test="sex != null">
and idCardNumber=#{idCardNumber}
</if>
<if test="isAuthentication != null">
and fansNickname=#{fansNickname}
</if>
<if test="name != null and name!=''">
and inviteCode=#{inviteCode}
</if>
<if test="cardNo != null and cardNo!=''">
and beInvitedCode=#{beInvitedCode}
</if>
<if test="null!=creatdTime">
and creatdTime=#{creatdTime}
</if>
<if test="null!=updatedTime">
and updatedTime=#{updatedTime}
</if>
</trim>
<if test="beginPos != null and pageSize != null ">
limit #{beginPos},#{pageSize}
</if>
</select>
</mapper>
package cn.wisenergy.model.app;
/**
* Created by m1991 on 2021/3/1 14:00
*/
public class UsersDto extends User{
//用户token
private String token;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
......@@ -28,4 +28,7 @@ public interface UserService {
*/
User getByUserId(String userId);
//根据手机号查询用户
public User queryUsersByPhone(String id);
}
......@@ -8,8 +8,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author 86187
......@@ -35,4 +38,17 @@ public class UserServiceImpl extends ServiceImpl<UsersMapper, User> implements U
return usersMapper.getByUserId(userId);
}
@Override
public User queryUsersByPhone(String phone) {
Map<String,Object> param=new HashMap<String, Object>();
param.put("phone",phone);
List<User> usersList=usersMapper.getUsersListByMap(param);
if(!CollectionUtils.isEmpty(usersList)){
return usersList.get(0);
}
return null;
}
}
......@@ -22,7 +22,6 @@
<artifactId>wisenergy-service</artifactId>
<version>${moduleVersion.wisenergy-service}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
......@@ -49,7 +48,11 @@
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<!-- MAVEN构建 -->
......
package cn.wisenergy.controller;
import cn.wisenergy.model.app.User;
import cn.wisenergy.service.app.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by m1991 on 2021/2/23 15:45
*/
@RestController
@RequestMapping("/sys")
public class UserSysController {
@Autowired
UserService userService;
/**
* 登录
*/
@PostMapping("/login")
public String login(User user){
return
userService.login(user);
}
/**
* 注册
* @param user
* @return
*/
@PostMapping("/regist")
public String regist(User user){
return userService.regist(user);
}
}
\ No newline at end of file
......@@ -2,15 +2,22 @@ package cn.wisenergy.web.admin.controller.app;
import cn.wisenergy.common.constant.RedisConsts;
import cn.wisenergy.common.utils.R;
import cn.wisenergy.common.utils.RedisUtils;
import cn.wisenergy.common.utils.StringUtil;
import cn.wisenergy.model.app.User;
import cn.wisenergy.model.app.UsersDto;
import cn.wisenergy.service.app.UserService;
import cn.wisenergy.web.common.BaseController;
import cn.wisenergy.web.config.JwtConfig;
import cn.wisenergy.web.shiro.JwtUtil;
import cn.wisenergy.web.sms.*;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -18,6 +25,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 86187
*/
......@@ -39,6 +47,9 @@ public class UserController extends BaseController {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private RedisUtils redisUtils;
@ApiOperation(value = "获取用户信息", notes = "获取用户信息", httpMethod = "GET")
@ApiImplicitParam(name = "userId", value = "用户id", dataType = "String")
@GetMapping("/getByUserId")
......@@ -67,4 +78,50 @@ public class UserController extends BaseController {
redisTemplate.opsForValue().set(RedisConsts.JWT_ACCESS_TOKEN + token, token);
return R.ok(token);
}
/**
* 手机号登录
* @param userId
* @param sms
* @return
* @throws Exception
*/
@ApiOperation(value = "获取用户手机号登录接口", notes = "获取用户手机号登录接口", httpMethod = "POST")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户手机号", required = true, dataType = "String"),
@ApiImplicitParam(name = "sms", value = "短信验证码", required = true, dataType = "String")})
@RequestMapping("/login/sms")
public Result loginBySms(String userId, String sms)throws Exception{
User users=null;
String key= StringUtil.formatKeyWithPrefix(Constants.RedisKey.PROJECT_PRIFIX,Constants.RedisKey.SMS_PRIFIX,userId,Constants.Sms.CodeType.LOGIN_OR_REGISTER+"");
String redisCode=redisUtils.getValue(key);
if(StringUtil.isBlank(redisCode) || !sms.equals(redisCode)){
throw new BaseException(ResultEnum.FAIL_VERIFY);
}
redisUtils.delete(key);
//根据手机号判断用户是否存在
//不存在则保存用户信息
users=userService.queryUsersByPhone(userId);
if(null==users){
users=new User();
// users.setAccount(phone);
// users.setUserName(phone);
// userService.qdtxAddUsers(users);
}
String token=createToken(users);
if(!StringUtil.isBlank(token)){
return ResultUtils.returnDataSuccess(StringUtil.createSimpleMap("token",token));
}
return ResultUtils.returnFail();
}
public String createToken(User users)throws Exception{
String token=StringUtil.createToken();
//保存token
String tokenKey=StringUtil.formatKeyWithPrefix(Constants.RedisKey.PROJECT_PRIFIX,Constants.RedisKey.TOKEN_PRIFIX,token);
UsersDto usersDto=new UsersDto();
BeanUtils.copyProperties(users,usersDto);
redisUtils.set(tokenKey, JSONObject.toJSONString(usersDto),Constants.Duration.HALF_HOUR_INT);
return token;
}
}
......@@ -122,7 +122,17 @@ public class MvcConfiguration implements WebMvcConfigurer {
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// swagger2配置
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
WebMvcConfigurer.super.addResourceHandlers(registry);
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
......
package cn.wisenergy.web.sms;
import java.util.zip.CRC32;
/**
*
* @author eaves.zhu
*/
public class DeviceIdUtil {
/**
* @author eaves.zhu
* @param deviceId
* @return int
* CRC32
*/
public static long getCRC32(String deviceId){
CRC32 crc32 = new CRC32();
crc32.update(deviceId.getBytes());
long tmp = crc32.getValue();
return tmp;
}
}
\ No newline at end of file
package cn.wisenergy.web.sms;
import org.apache.log4j.Logger;
/**
*
* @author zen.wang zenyes@gmail.com
*/
public class ShareCodeUtil {
/** 自定义进制(0,1没有加入,容易与o,l混淆) */
private static final char[] r=new char[]{'F', 'L', 'G', 'W', '5', 'X', 'C', '3', '9', 'Z', 'M', '6', '7', 'Y', 'R', 'T', '2', 'H', 'S', '8', 'D', 'V', 'E', 'J', '4', 'K', 'Q', 'P', 'U', 'A', 'N', 'B'};
/** 进制长度 */
private static final int binLen=r.length;
private static Logger logger = Logger.getLogger(ShareCodeUtil.class);
private static final long startNumber = 100048576L;
// private static final long startNumber = 0L;
/**
*
* @param id ID
* @return 随机码
*/
public static String idToCode(long id,long costomStartNumber) {
if(costomStartNumber<0){
costomStartNumber = startNumber;
}
id += costomStartNumber;
char[] buf=new char[32];
int charPos=32;
while((id / binLen) > 0) {
int ind=(int)(id % binLen);
// System.out.println(num + "-->" + ind);
buf[--charPos]=r[ind];
id /= binLen;
}
buf[--charPos]=r[(int)(id % binLen)];
// System.out.println(num + "-->" + num % binLen);
String str=new String(buf, charPos, (32 - charPos));
return str.toUpperCase();
}
public static String idToCode(long idL){
return idToCode(idL,-1L);
}
public static String idToCode(String id){
long idL = Long.parseLong(id);
return idToCode(idL,-1L);
}
public static String idToCode(String id,long costomStartNumber){
long idL = Long.parseLong(id);
return idToCode(idL,costomStartNumber);
}
public static long codeToId(String code) {
code = code.toUpperCase();
char chs[]=code.toCharArray();
long res=0L;
for(int i=0; i < chs.length; i++) {
int ind=0;
for(int j=0; j < binLen; j++) {
if(chs[i] == r[j]) {
ind=j;
break;
}
}
if(i > 0) {
res=res * binLen + ind;
} else {
res=ind;
}
// logger.debug(ind + "-->" + res);
}
res -= startNumber;
return res;
}
}
\ No newline at end of file
......@@ -47,6 +47,14 @@ public class TestController {
// return result2;
// }
public static void main(String [] args){
String b = ShareCodeUtil.idToCode(123456);
String a= ShareCodeUtil.idToCode(0,0+1);
System.out.println(b);
}
@RequestMapping("/testSms")
@ResponseBody
public Result testException()throws Exception{
......
......@@ -59,15 +59,15 @@ public class LoginInterceptor extends HandlerInterceptorAdapter {
}
}
// if (StringUtil.isBlank(params.get("timestamp"))) {
// log.error("时间戳不能为空");
// ResponseOutput.outputJson(response, ResultEnum.FAIL_TIMESTAMP_NOT_NULL);
// return false;
// } else if (StringUtil.isBlank(params.get("source-type"))) {
// log.error("访问来源不能为空");
// ResponseOutput.outputJson(response, ResultEnum.FAIL_VISIT_SOURCE_NOT_NULL);
// return false;
// }
if (StringUtil.isBlank(params.get("timestamp"))) {
log.error("时间戳不能为空");
ResponseOutput.outputJson(response, ResultEnum.FAIL_TIMESTAMP_NOT_NULL);
return false;
} else if (StringUtil.isBlank(params.get("source-type"))) {
log.error("访问来源不能为空");
ResponseOutput.outputJson(response, ResultEnum.FAIL_VISIT_SOURCE_NOT_NULL);
return false;
}
for (String s : VERIFY_URI) {
if (url.contains(s)) {
......
......@@ -44,32 +44,32 @@ spring:
# 总限制
max-request-size: 20MB
# redis:
# database: 0
# host: 192.168.110.165
# port: 6379
# password: adm4HYservice$ # 密码(默认为空)
# timeout: 6000ms # 连接超时时长(毫秒)
# jedis:
# pool:
# max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
# max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
# max-idle: 10 # 连接池中的最大空闲连接
# min-idle: 5 # 连接池中的最小空闲连
#redis
redis:
host: 192.168.0.105
port: 6379
password: 123456
#连接0库
database: 0
host: 192.168.110.165
port: 6379
password: adm4HYservice$ # 密码(默认为空)
timeout: 6000ms # 连接超时时长(毫秒)
jedis:
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连
#redis
# redis:
# host: 192.168.0.105
# port: 6379
# password: 123456
# #连接0库
# database: 0
# timeout: 6000ms # 连接超时时长(毫秒)
# jedis:
# pool:
# max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
# max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
# max-idle: 10 # 连接池中的最大空闲连接
# min-idle: 5 # 连接池中的最小空闲连
###上传文件配置 :该配置可根据部署的系统或开发人员自定义路径,每次部署之前需要修改location
uploadFile:
resourceHandler: /upload_flowChart/** #请求 url 中的资源映射也是保存到数据库中的父级路径
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment