|
@@ -1,24 +1,34 @@
|
|
|
package com.smcic.api.operate.service.impl;
|
|
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
|
|
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
|
|
import com.aliyun.teaopenapi.models.Config;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.google.common.collect.ImmutableMap;
|
|
|
+import com.smcic.api.operate.dto.TideUser;
|
|
|
+import com.smcic.api.operate.entity.KeysConst;
|
|
|
+import com.smcic.api.operate.entity.UserAuth;
|
|
|
+import com.smcic.api.operate.entity.UserPlatform;
|
|
|
import com.smcic.api.operate.entity.Users;
|
|
|
-import com.smcic.api.operate.entity.VerifyCode;
|
|
|
import com.smcic.api.operate.mapper.UsersMapper;
|
|
|
import com.smcic.api.operate.mapper.VerifyCodeMapper;
|
|
|
import com.smcic.api.operate.service.IUsersService;
|
|
|
import com.smcic.core.advice.APIException;
|
|
|
+import com.smcic.core.auth.AESUtil;
|
|
|
+import org.apache.http.util.TextUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.time.Duration;
|
|
|
+import java.util.Map;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
@@ -33,14 +43,11 @@ import java.util.regex.Pattern;
|
|
|
@Service
|
|
|
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
|
|
|
|
|
|
- @Resource
|
|
|
- private VerifyCodeMapper verifyCodeMapper;
|
|
|
-
|
|
|
@Autowired
|
|
|
private ObjectMapper objectMapper;
|
|
|
|
|
|
@Resource
|
|
|
- private OperateServiceImpl operateService;
|
|
|
+ private RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
|
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
|
|
|
|
@@ -58,16 +65,17 @@ public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void verifyCode(String phone, String code) {
|
|
|
- VerifyCode verifyCode = verifyCodeMapper.selectOne(new LambdaQueryWrapper<VerifyCode>()
|
|
|
- .eq(VerifyCode::getPhone, phone)
|
|
|
- .eq(VerifyCode::getCode, code)
|
|
|
- );
|
|
|
- if (verifyCode == null) {
|
|
|
+ public Map<String, String> verifyCode(String phone, String code) {
|
|
|
+ String verifyCode = redisTemplate.opsForValue().get(KeysConst.PHONE_CODE + phone);
|
|
|
+ if (TextUtils.isEmpty(verifyCode) || !code.equals(verifyCode)) {
|
|
|
throw new APIException(400, "验证码错误");
|
|
|
}
|
|
|
- if (System.currentTimeMillis() > verifyCode.getExpireTime()) {
|
|
|
- throw new APIException(400, "验证码已超时,请重新发送");
|
|
|
+ try {
|
|
|
+ return ImmutableMap.of("token", AESUtil.encryptHex(objectMapper.writeValueAsString(
|
|
|
+ new UserAuth(UserPlatform.OTHER, phone)
|
|
|
+ )));
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ throw new APIException(400, "验证码错误");
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -79,14 +87,9 @@ public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements
|
|
|
throw new APIException(400, "请输入正确的手机号码");
|
|
|
}
|
|
|
|
|
|
- VerifyCode verifyCode = verifyCodeMapper.selectOne(new LambdaQueryWrapper<VerifyCode>()
|
|
|
- .eq(VerifyCode::getPhone, phone)
|
|
|
- );
|
|
|
- if (verifyCode != null) {
|
|
|
- if (System.currentTimeMillis() - (verifyCode.getExpireTime() - 60 * 5 * 1000) < 60 * 1000) {
|
|
|
- throw new APIException("验证码已发送,请等待60秒后重新发送");
|
|
|
- }
|
|
|
- verifyCodeMapper.deleteById(phone);
|
|
|
+ Long expire = redisTemplate.getExpire(KeysConst.PHONE_CODE + phone);
|
|
|
+ if (expire != null && expire > 240) {
|
|
|
+ throw new APIException("验证码已发送,请等待60秒后重新发送");
|
|
|
}
|
|
|
|
|
|
String code = RandomUtil.randomNumbers(4);
|
|
@@ -109,71 +112,34 @@ public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- verifyCode = new VerifyCode();
|
|
|
- verifyCode.setPhone(phone);
|
|
|
- verifyCode.setCode(code);
|
|
|
- // 5分钟的过期时间
|
|
|
- verifyCode.setExpireTime(System.currentTimeMillis() + 60 * 5 * 1000);
|
|
|
- verifyCodeMapper.insert(verifyCode);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public void store(Users user){
|
|
|
- operateService.verify(user.getOperateId());
|
|
|
- verifyCode(user.getPhone(), user.getVerifyCode());
|
|
|
- Users one = lambdaQuery().eq(Users::getPhone, user.getPhone()).eq(Users::getOperateId, user.getOperateId()).one();
|
|
|
- if(null != one){
|
|
|
- one.setAddr(user.getAddr());
|
|
|
- one.setUserName(user.getUserName());
|
|
|
- updateById(one);
|
|
|
- }else{
|
|
|
- user.setMaxDrawNum(2);
|
|
|
- user.setDrawNum(1);
|
|
|
- save(user);
|
|
|
- }
|
|
|
-
|
|
|
+ redisTemplate.opsForValue().set(KeysConst.PHONE_CODE + phone, code, Duration.ofSeconds(300));
|
|
|
}
|
|
|
|
|
|
- public void update(Users user){
|
|
|
- operateService.verify(user.getOperateId());
|
|
|
- Users one = lambdaQuery().eq(Users::getPhone, user.getPhone()).eq(Users::getOperateId, user.getOperateId()).one();
|
|
|
- if(null == one){
|
|
|
- throw new APIException(400, "不存在的用户");
|
|
|
+ public Map<String, String> tideLogin(String jToken){
|
|
|
+ String body = redisTemplate.opsForValue().get(KeysConst.TIDE_TOKEN + jToken);
|
|
|
+ if(TextUtils.isEmpty(body)){
|
|
|
+ body = HttpRequest.post("https://ssp-api.sxtvs.com.cn/apiv4.3/api/user_information")
|
|
|
+ .body("jToken=" + jToken + "&site=76").execute().body();
|
|
|
+ redisTemplate.opsForValue().set(KeysConst.TIDE_TOKEN + jToken, body, Duration.ofDays(30));
|
|
|
}
|
|
|
- one.setAddr(user.getAddr());
|
|
|
- one.setUserName(user.getUserName());
|
|
|
- updateById(one);
|
|
|
|
|
|
- }
|
|
|
+ try {
|
|
|
+ TideUser tideUser = objectMapper.readValue(body, TideUser.class);
|
|
|
+ String uid = tideUser.getData().getResult().getMoblie();
|
|
|
+ if(TextUtils.isEmpty(uid)){
|
|
|
+ uid = tideUser.getData().getResult().getWeixinsign();
|
|
|
+ }
|
|
|
+ if(TextUtils.isEmpty(uid)){
|
|
|
+ uid = tideUser.getData().getResult().getWeibosign();
|
|
|
+ }
|
|
|
|
|
|
- public Users getUserByPhone(String phone, Integer operateId){
|
|
|
- Users one = lambdaQuery().eq(Users::getPhone, phone).eq(Users::getOperateId, operateId).one();
|
|
|
- if (null == one){
|
|
|
- throw new APIException(400, "不存在的用户");
|
|
|
+ return ImmutableMap.of("token", AESUtil.encryptHex(objectMapper.writeValueAsString(
|
|
|
+ new UserAuth(UserPlatform.SSP, uid))));
|
|
|
+ } catch (Exception e) {
|
|
|
+ redisTemplate.delete(KeysConst.TIDE_TOKEN + jToken);
|
|
|
+ throw new APIException(400, "获取用户失败");
|
|
|
}
|
|
|
-
|
|
|
- //userDrawNumService.drawNumHandle(one);
|
|
|
- return one;
|
|
|
}
|
|
|
|
|
|
- public void drawPlus(String phone, Integer operateId){
|
|
|
- Users one = lambdaQuery().eq(Users::getPhone, phone).eq(Users::getOperateId, operateId).one();
|
|
|
- if (null == one){
|
|
|
- throw new APIException(400, "不存在的用户");
|
|
|
- }
|
|
|
- //userDrawNumService.drawPlus(one);
|
|
|
|
|
|
- if(one.getDrawNum() + one.getDrewNum() >= one.getMaxDrawNum()){
|
|
|
- throw new APIException(400, "您的抽奖次数已达上限");
|
|
|
- }
|
|
|
-
|
|
|
- one.setDrawNum(one.getDrawNum() + 1);
|
|
|
- updateById(one);
|
|
|
- }
|
|
|
-
|
|
|
- public void drawDec(Users users){
|
|
|
- users.setDrawNum(users.getDrawNum() - 1);
|
|
|
- users.setDrewNum(users.getDrewNum() + 1);
|
|
|
- updateById(users);
|
|
|
- }
|
|
|
}
|