|
@@ -5,13 +5,15 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.sun.deploy.net.HttpResponse;
|
|
import com.sun.deploy.net.HttpResponse;
|
|
import com.zhentao.domain.UserLogin;
|
|
import com.zhentao.domain.UserLogin;
|
|
-import com.zhentao.dto.NoteDto;
|
|
|
|
|
|
+import com.zhentao.dto.user.NoteDto;
|
|
import com.zhentao.dto.Result;
|
|
import com.zhentao.dto.Result;
|
|
-import com.zhentao.dto.UserDto;
|
|
|
|
|
|
+import com.zhentao.dto.user.UserDto;
|
|
|
|
+import com.zhentao.dto.user.UserDtoup;
|
|
|
|
+import com.zhentao.dto.user.UserRegister;
|
|
import com.zhentao.enums.ApiServerException;
|
|
import com.zhentao.enums.ApiServerException;
|
|
import com.zhentao.exception.AsynException;
|
|
import com.zhentao.exception.AsynException;
|
|
-import com.zhentao.service.UserLoginService;
|
|
|
|
import com.zhentao.mapper.UserLoginMapper;
|
|
import com.zhentao.mapper.UserLoginMapper;
|
|
|
|
+import com.zhentao.service.UserLoginService;
|
|
import com.zhentao.tool.HttpUtils;
|
|
import com.zhentao.tool.HttpUtils;
|
|
import com.zhentao.tool.TokenUtils;
|
|
import com.zhentao.tool.TokenUtils;
|
|
import org.redisson.api.RLock;
|
|
import org.redisson.api.RLock;
|
|
@@ -20,10 +22,12 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.data.redis.core.ValueOperations;
|
|
import org.springframework.data.redis.core.ValueOperations;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.util.DigestUtils;
|
|
|
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
+import java.util.UUID;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -40,13 +44,15 @@ public class UserLoginServiceImpl extends ServiceImpl<UserLoginMapper, UserLogin
|
|
private RedissonClient redissonClient;
|
|
private RedissonClient redissonClient;
|
|
@Override
|
|
@Override
|
|
public Result login(UserDto userDto) {
|
|
public Result login(UserDto userDto) {
|
|
- RLock lock = redissonClient.getLock(userDto.getPhone());
|
|
|
|
|
|
+ RLock lock = redissonClient.getLock(userDto.getPhone()+"phone");
|
|
try {
|
|
try {
|
|
boolean b = lock.tryLock(10, 20, TimeUnit.SECONDS);
|
|
boolean b = lock.tryLock(10, 20, TimeUnit.SECONDS);
|
|
if (b){
|
|
if (b){
|
|
|
|
+ System.err.println(userDto);
|
|
String s = stringRedisTemplate.opsForValue().get(userDto.getPhone());
|
|
String s = stringRedisTemplate.opsForValue().get(userDto.getPhone());
|
|
|
|
+ System.err.println(s);
|
|
// 验证码比较
|
|
// 验证码比较
|
|
- if (s.equals(userDto.getCode())){
|
|
|
|
|
|
+ if (!s.equals(userDto.getCode())){
|
|
throw new AsynException(ApiServerException.NOTE_ERROR);
|
|
throw new AsynException(ApiServerException.NOTE_ERROR);
|
|
}
|
|
}
|
|
try{
|
|
try{
|
|
@@ -75,8 +81,6 @@ public class UserLoginServiceImpl extends ServiceImpl<UserLoginMapper, UserLogin
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
@Override
|
|
@Override
|
|
public Result note(NoteDto noteDto) {
|
|
public Result note(NoteDto noteDto) {
|
|
int randomSixDigit = 100000 + (int) (Math.random() * 900000); // 生成六位随机数
|
|
int randomSixDigit = 100000 + (int) (Math.random() * 900000); // 生成六位随机数
|
|
@@ -85,40 +89,128 @@ public class UserLoginServiceImpl extends ServiceImpl<UserLoginMapper, UserLogin
|
|
ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
|
|
ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
|
|
stringStringValueOperations.set(noteDto.getPhone(),randomSixDigit+"");
|
|
stringStringValueOperations.set(noteDto.getPhone(),randomSixDigit+"");
|
|
|
|
|
|
- String host = "https://gyytz.market.alicloudapi.com";
|
|
|
|
- String path = "/sms/smsSend";
|
|
|
|
- String method = "POST";
|
|
|
|
- String appcode = "03a7c868bfcb4de291d6d609a6b625d3";
|
|
|
|
- Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
- //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
|
|
|
|
- headers.put("Authorization", "APPCODE " + appcode);
|
|
|
|
- Map<String, String> querys = new HashMap<String, String>();
|
|
|
|
- querys.put("mobile", noteDto.getPhone());
|
|
|
|
- querys.put("param", "**code**:"+randomSixDigit+",**minute**:5");
|
|
|
|
-
|
|
|
|
-//smsSignId(短信前缀)和templateId(短信模板),可登录国阳云控制台自助申请。参考文档:http://help.guoyangyun.com/Problem/Qm.html
|
|
|
|
-
|
|
|
|
- querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");
|
|
|
|
- querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad");
|
|
|
|
- Map<String, String> bodys = new HashMap<String, String>();
|
|
|
|
-
|
|
|
|
|
|
+// String host = "https://gyytz.market.alicloudapi.com";
|
|
|
|
+// String path = "/sms/smsSend";
|
|
|
|
+// String method = "POST";
|
|
|
|
+// String appcode = "03a7c868bfcb4de291d6d609a6b625d3";
|
|
|
|
+// Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+// //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
|
|
|
|
+// headers.put("Authorization", "APPCODE " + appcode);
|
|
|
|
+// Map<String, String> querys = new HashMap<String, String>();
|
|
|
|
+// querys.put("mobile", noteDto.getPhone());
|
|
|
|
+// querys.put("param", "**code**:"+randomSixDigit+",**minute**:5");
|
|
|
|
+//
|
|
|
|
+////smsSignId(短信前缀)和templateId(短信模板),可登录国阳云控制台自助申请。参考文档:http://help.guoyangyun.com/Problem/Qm.html
|
|
|
|
+//
|
|
|
|
+// querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");
|
|
|
|
+// querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad");
|
|
|
|
+// Map<String, String> bodys = new HashMap<String, String>();
|
|
|
|
+//
|
|
|
|
+//
|
|
|
|
+// try {
|
|
|
|
+// /**
|
|
|
|
+// * 重要提示如下:
|
|
|
|
+// * HttpUtils请从\r\n\t \t* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java\r\n\t \t* 下载
|
|
|
|
+// *
|
|
|
|
+// * 相应的依赖请参照
|
|
|
|
+// * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
|
|
|
|
+// */
|
|
|
|
+// HttpResponse response = (HttpResponse) HttpUtils.doPost(host, path, method, headers, querys, bodys);
|
|
|
|
+// System.out.println(response.toString());
|
|
|
|
+// //获取response的body
|
|
|
|
+// //System.out.println(EntityUtils.toString(response.getEntity()));
|
|
|
|
+// } catch (Exception e) {
|
|
|
|
+// e.printStackTrace();
|
|
|
|
+// }
|
|
|
|
+ return Result.OK("发送成功",null);
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public Result login_up(UserDtoup userDtoup) {
|
|
|
|
+ RLock lock = redissonClient.getLock(userDtoup.username+"username");
|
|
try {
|
|
try {
|
|
- /**
|
|
|
|
- * 重要提示如下:
|
|
|
|
- * HttpUtils请从\r\n\t \t* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java\r\n\t \t* 下载
|
|
|
|
- *
|
|
|
|
- * 相应的依赖请参照
|
|
|
|
- * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
|
|
|
|
- */
|
|
|
|
- HttpResponse response = (HttpResponse) HttpUtils.doPost(host, path, method, headers, querys, bodys);
|
|
|
|
- System.out.println(response.toString());
|
|
|
|
- //获取response的body
|
|
|
|
- //System.out.println(EntityUtils.toString(response.getEntity()));
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
+ boolean b = lock.tryLock(10, 20, TimeUnit.SECONDS);
|
|
|
|
+ if (b){
|
|
|
|
+ QueryWrapper<UserLogin> queryWrapper = new QueryWrapper<>();
|
|
|
|
+ queryWrapper.eq("user_username",userDtoup.getUsername());
|
|
|
|
+ UserLogin one = this.getOne(queryWrapper);
|
|
|
|
+ if (one==null){
|
|
|
|
+ throw new AsynException(ApiServerException.NULL_USERNAME);
|
|
|
|
+ }
|
|
|
|
+ String salt = one.getSalt();
|
|
|
|
+ String s = DigestUtils.md5DigestAsHex((salt + userDtoup.getPassword()).getBytes());
|
|
|
|
+ if (!s.equals(one.getUserPassword())){
|
|
|
|
+ throw new AsynException(ApiServerException.NULL_PASSWORD);
|
|
|
|
+ }
|
|
|
|
+ String jwtToken = TokenUtils.createJwtToken(one.getId()+"");
|
|
|
|
+ return Result.OK("登录成功",jwtToken);
|
|
|
|
+ }
|
|
|
|
+ }catch (InterruptedException e){
|
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
|
+ }finally {
|
|
|
|
+ lock.unlock();
|
|
}
|
|
}
|
|
- return Result.OK("发送成功",null);
|
|
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+//注册
|
|
|
|
+ @Override
|
|
|
|
+ public Result register(UserRegister userRegister) {
|
|
|
|
+ System.err.println(userRegister);
|
|
|
|
+ RLock lock = redissonClient.getLock(userRegister.getPhone()+userRegister.getUsername());
|
|
|
|
+ try{
|
|
|
|
+ boolean b = lock.tryLock(10, 20, TimeUnit.SECONDS);
|
|
|
|
+ if (b){
|
|
|
|
+// 用来判断验证码是否正确
|
|
|
|
+ String s = stringRedisTemplate.opsForValue().get(userRegister.getPhone());
|
|
|
|
+ System.err.println("redis取出来的验证码:"+s);
|
|
|
|
+ if (!s.equals(userRegister.getCode())){
|
|
|
|
+ throw new AsynException(ApiServerException.NOTE_ERROR);
|
|
|
|
+ }
|
|
|
|
+// 根据手机号查询信息
|
|
|
|
+ QueryWrapper<UserLogin> queryWrapper = new QueryWrapper<>();
|
|
|
|
+ queryWrapper.eq("user_mobile",userRegister.getPhone());
|
|
|
|
+ UserLogin one = this.getOne(queryWrapper);
|
|
|
|
+ if (one==null){
|
|
|
|
+ UserLogin userLogin = new UserLogin();
|
|
|
|
+ userLogin.setUserMobile(userRegister.getPhone());
|
|
|
|
+ userLogin.setUserUsername(userRegister.getUsername());
|
|
|
|
+// 随机字符串
|
|
|
|
+ String uuid = String.valueOf(UUID.randomUUID());
|
|
|
|
+ userLogin.setSalt(uuid);
|
|
|
|
+// md5加密
|
|
|
|
+ String s1 = DigestUtils.md5DigestAsHex((uuid + userRegister.getPassword()).getBytes());
|
|
|
|
+ userLogin.setUserPassword(s1);
|
|
|
|
+ long l = IdUtil.getSnowflake(1, 1).nextId();
|
|
|
|
+// 进行注册
|
|
|
|
+ boolean save = this.save(userLogin);
|
|
|
|
+ if (save){
|
|
|
|
+ return Result.OK("注册成功",save);
|
|
|
|
+ }else {
|
|
|
|
+ return Result.ERR("注册失败",save);
|
|
|
|
+ }
|
|
|
|
+ }else {
|
|
|
|
+ one.setUserUsername(userRegister.getUsername());
|
|
|
|
+ // 随机字符串
|
|
|
|
+ String uuid = String.valueOf(UUID.randomUUID());
|
|
|
|
+ one.setSalt(uuid);
|
|
|
|
+// md5加密
|
|
|
|
+ String s1 = DigestUtils.md5DigestAsHex((uuid + userRegister.getPassword()).getBytes());
|
|
|
|
+ one.setUserPassword(s1);
|
|
|
|
+// 进行修改
|
|
|
|
+ boolean b1 = this.updateById(one);
|
|
|
|
+ if (b1){
|
|
|
|
+ return Result.OK("注册成功",b1);
|
|
|
|
+ }else {
|
|
|
|
+ return Result.ERR("注册失败",b1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }catch (InterruptedException e){
|
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
|
+ }finally {
|
|
|
|
+ lock.unlock();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|