From 2cf242edee071ae8d5fed3da8983e4c19b99482a Mon Sep 17 00:00:00 2001 From: zhp <746006956@qq.com> Date: Thu, 19 Sep 2024 01:01:01 +0800 Subject: [PATCH] =?UTF-8?q?2024-09-19=20=E6=B3=A8=E5=86=8C=E9=A1=B5?= =?UTF-8?q?=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/core/domain/http/Customer.java | 3 + .../common/security/service/TokenService.java | 41 ++++++++++ .../impl/SysPublicHalfServiceImpl.java | 2 +- .../system/controller/CustomerController.java | 4 +- .../system/service/ICustomerService.java | 3 +- .../service/impl/ChannelServiceImpl.java | 14 ++++ .../service/impl/CustomerServiceImpl.java | 74 ++++++++++++++++--- .../service/impl/MerchantServiceImpl.java | 6 +- 8 files changed, 131 insertions(+), 16 deletions(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/domain/http/Customer.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/domain/http/Customer.java index 08a4562f..637dc64f 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/domain/http/Customer.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/domain/http/Customer.java @@ -38,6 +38,9 @@ public class Customer extends BaseEntity @Excel(name = "0 男 1 女") private Integer sex; + @Excel(name="身份证号") + private String idCard; + /** 昵称 */ @Excel(name = "昵称") private String name; diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/service/TokenService.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/service/TokenService.java index 29717928..2d8e32c6 100644 --- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/service/TokenService.java +++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/service/TokenService.java @@ -4,6 +4,8 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; import javax.servlet.http.HttpServletRequest; + +import com.ruoyi.common.core.constant.RedisConstant; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -42,6 +44,11 @@ public class TokenService private final static Long MILLIS_MINUTE_TEN = CacheConstants.REFRESH_TIME * MILLIS_MINUTE; + /** + * token过期时间 + */ + private static final Long EXPIRE_TIME = 30 * 24 * 60 * 60L; + /** * 创建令牌 */ @@ -69,6 +76,40 @@ public class TokenService return rspMap; } + /** + * 创建令牌 + */ + public String createTokenApp(Long customerId,Long channelId) + { + String token = IdUtils.fastUUID(); +// Long userId = loginUser.getSysUser().getUserId(); +// String userName = loginUser.getSysUser().getUserName(); +// loginUser.setToken(token); +// loginUser.setUserid(userId); +// loginUser.setUsername(userName); +// loginUser.setIpaddr(IpUtils.getIpAddr()); +// refreshToken(loginUser); + + // Jwt存储信息 + Map claimsMap = new HashMap(); + claimsMap.put(SecurityConstants.USER_KEY, token); + claimsMap.put(SecurityConstants.DETAILS_USER_ID, customerId); + claimsMap.put(SecurityConstants.DETAILS_USERNAME, "userName"); + + // 接口返回信息 + Map rspMap = new HashMap(); + String token1 = JwtUtils.createToken(claimsMap); + rspMap.put("access_token", token1); + rspMap.put("expires_in", expireTime); + + redisService.setCacheObject(RedisConstant.APP_CUSTOMER_USERNAME_KEY + token1, customerId, EXPIRE_TIME, TimeUnit.SECONDS); + redisService.setCacheObject(RedisConstant.APP_CUSTOMER_KEY + customerId, token1, EXPIRE_TIME, TimeUnit.SECONDS); + redisService.setCacheObject(RedisConstant.APP_CUSTOMER_CHANNEL_KEY + token1, channelId, EXPIRE_TIME, TimeUnit.SECONDS); + redisService.setCacheObject(RedisConstant.APP_CUSTOMER_TOKEN_KEY + token1, customerId, EXPIRE_TIME, TimeUnit.SECONDS); + redisService.setCacheObject( CacheConstants.LOGIN_TOKEN_KEY+token,customerId,EXPIRE_TIME,TimeUnit.SECONDS); + return token1; + } + /** * 获取用户身份信息 * diff --git a/ruoyi-modules/ruoyi-btc/src/main/java/com/ruoyi/btc/service/impl/SysPublicHalfServiceImpl.java b/ruoyi-modules/ruoyi-btc/src/main/java/com/ruoyi/btc/service/impl/SysPublicHalfServiceImpl.java index a1b9ee53..6d139db9 100644 --- a/ruoyi-modules/ruoyi-btc/src/main/java/com/ruoyi/btc/service/impl/SysPublicHalfServiceImpl.java +++ b/ruoyi-modules/ruoyi-btc/src/main/java/com/ruoyi/btc/service/impl/SysPublicHalfServiceImpl.java @@ -225,7 +225,7 @@ public class SysPublicHalfServiceImpl implements ISysPublicHalfService if (customerInfoByPhoneMd5.getCode()==200){ remoteCustomerService.updateByPhoneMd5(customer,SecurityConstants.INNER); }else { - return AjaxResult.error("今日未撞库"); + remoteCustomerService.add(customer,SecurityConstants.INNER); } //匹配资质 造轮子 返回多个符合的商户 List merchants = matchMerchant(customer); diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CustomerController.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CustomerController.java index e2e3ad7b..27401a16 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CustomerController.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CustomerController.java @@ -152,8 +152,8 @@ public class CustomerController extends BaseController * H5用户登录 */ @GetMapping("/customerLogin") - public AjaxResult customerLogin(@RequestParam("phone")String phone,@RequestParam("code")Integer code){ - return customerService.customerLogin(phone,code); + public AjaxResult customerLogin(@RequestParam("phone")String phone,@RequestParam("code")Integer code,HttpServletRequest request){ + return customerService.customerLogin(phone,code,request); } /** diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ICustomerService.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ICustomerService.java index feeaee5e..e20e4ace 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ICustomerService.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ICustomerService.java @@ -90,9 +90,10 @@ public interface ICustomerService extends IService * h5用户登录 * @param phone * @param code + * @param request * @return */ - AjaxResult customerLogin(String phone, Integer code); + AjaxResult customerLogin(String phone, Integer code,HttpServletRequest request); /** * 保存用户留资信息 diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ChannelServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ChannelServiceImpl.java index b8e550d8..8ea8d736 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ChannelServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ChannelServiceImpl.java @@ -9,6 +9,7 @@ import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.redis.service.RedisService; import com.ruoyi.system.mapper.ChannelMapper; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.RandomStringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; @@ -17,6 +18,8 @@ import com.ruoyi.common.core.domain.http.Channel; import com.ruoyi.system.service.IChannelService; import org.springframework.transaction.annotation.Transactional; +import javax.annotation.PostConstruct; + /** * 渠道配置Service业务层处理 * @@ -24,6 +27,7 @@ import org.springframework.transaction.annotation.Transactional; * @date 2024-09-15 */ @Service +@Slf4j public class ChannelServiceImpl implements IChannelService { @Autowired @@ -31,6 +35,16 @@ public class ChannelServiceImpl implements IChannelService @Autowired private RedisService redisService; + @PostConstruct + public void init(){ + log.info("初始化渠道数据开始"); + List channels = channelMapper.selectList(new LambdaQueryWrapper()); + for (Channel channel:channels) { + redisService.setCacheObject(CacheConstants.CHANNEL_ID+channel.getId(),channel); + redisService.setCacheObject(CacheConstants.CHANNEL_SIGN+channel.getChannelSign(),channel); + } + } + /** * 查询渠道配置 * diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CustomerServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CustomerServiceImpl.java index 1d8522a3..0e6b8f36 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CustomerServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CustomerServiceImpl.java @@ -1,23 +1,23 @@ package com.ruoyi.system.service.impl; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; +import java.util.*; +import cn.hutool.core.util.IdcardUtil; +import cn.hutool.crypto.digest.MD5; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.constant.RedisConstant; -import com.ruoyi.common.core.constant.SecurityConstants; import com.ruoyi.common.core.domain.R; -import com.ruoyi.common.core.domain.http.Merchant; +import com.ruoyi.common.core.domain.http.Channel; import com.ruoyi.common.core.utils.DateUtils; import com.ruoyi.common.core.utils.EncryptUtil; import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.redis.service.CustomerTokenService; import com.ruoyi.common.redis.service.RedisService; +import com.ruoyi.common.security.service.TokenService; import com.ruoyi.system.config.SystemConfig; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -43,6 +43,8 @@ public class CustomerServiceImpl extends ServiceImpl i private final SystemConfig systemConfig; private final CustomerTokenService customerTokenService; private final RedisService redisService; + private final TokenService tokenService; + /** * 查询客户信息 * @@ -154,7 +156,33 @@ public class CustomerServiceImpl extends ServiceImpl i String token = customerTokenService.getToken(customer.getId()); if (StringUtils.isEmpty(token)) { //生成一个长60的token - token = customerTokenService.generateToken(customer.getId(), customer.getPhone(), "ANDROID", customer.getChannelId()); + //token = customerTokenService.generateToken(customer.getId(), customer.getPhone(), "ANDROID", customer.getChannelId()); + token = tokenService.createTokenApp(customer.getId(),customer.getChannelId()); + } + return token; + } + + /** + * 注册并返回token + * @param phone + * @return + */ + public String registAndretrunToken(String phone,Long channelId){ + Customer customer = new Customer(); + customer.setChannelId(channelId); + customer.setPhone(EncryptUtil.AESencode(phone, systemConfig.getAESkey())); + customer.setPhoneMd5(MD5.create().digestHex(phone).toLowerCase(Locale.ROOT)); + customer.setIsAuth(false); + customer.setFirstLoginTime(new Date()); + customer.setLastLoginTime(new Date()); + customer.setStatus(1); + customer.setCreateTime(new Date()); + customerMapper.insert(customer); + String token = customerTokenService.getToken(customer.getId()); + if (StringUtils.isEmpty(token)) { + //生成一个长60的token + //token = customerTokenService.generateToken(customer.getId(), customer.getPhone(), "ANDROID", customer.getChannelId()); + token = tokenService.createTokenApp(customer.getId(),channelId); } return token; } @@ -163,10 +191,16 @@ public class CustomerServiceImpl extends ServiceImpl i * H5用户登陆 * @param phone * @param code + * @param request * @return */ @Override - public AjaxResult customerLogin(String phone, Integer code) { + public AjaxResult customerLogin(String phone, Integer code, HttpServletRequest request) { + String sign = request.getHeader("sign"); + if (StringUtils.isEmpty(sign)){ + return AjaxResult.error("渠道标识不存在"); + } + Channel channel = redisService.getCacheObject(CacheConstants.CHANNEL_SIGN+sign); Boolean aBoolean = redisService.hasKey(RedisConstant.H5_LOGIN_CACHE + phone); if (!aBoolean){ return AjaxResult.error("验证码不存在"); @@ -175,7 +209,7 @@ public class CustomerServiceImpl extends ServiceImpl i if (cacheCode!=code){ return AjaxResult.success("验证码错误"); } - String customerToken = getCustomerToken(phone); + String customerToken = registAndretrunToken(phone,channel.getId()); return AjaxResult.success("登录成功",customerToken); } @@ -190,10 +224,32 @@ public class CustomerServiceImpl extends ServiceImpl i public AjaxResult saveCustomerInfo(Customer customer, HttpServletRequest request) { String authorization = request.getHeader("Authorization"); Long customerId = customerTokenService.getCustomerId(authorization, false); + String sign = request.getHeader("sign"); + if (StringUtils.isEmpty(sign)){ + return AjaxResult.error("渠道标识不存在"); + } + Channel channel = redisService.getCacheObject(CacheConstants.CHANNEL_SIGN+sign); if (customerId==null){ return AjaxResult.error("用户不存在或未登录"); } + if (StringUtils.isEmpty(customer.getIdCard())){ + return AjaxResult.error("身份证好不能为空"); + } + boolean validCard = IdcardUtil.isValidCard(customer.getIdCard()); + if (validCard){ + return AjaxResult.error("身份证号码异常"); + } + if (StringUtils.isEmpty(customer.getActurlName())){ + return AjaxResult.error("姓名不能为空"); + } + int ageByIdCard = IdcardUtil.getAgeByIdCard(customer.getIdCard()); + customer.setAge(ageByIdCard); + int genderByIdCard = IdcardUtil.getGenderByIdCard(customer.getIdCard()); + customer.setSex(genderByIdCard==0?1:0); customer.setId(customerId); + customer.setChannelId(channel.getId()); + customer.setIsAuth(true); + customer.setStatus(1); updateById(customer); return AjaxResult.success("保存成功"); } diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/MerchantServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/MerchantServiceImpl.java index 2702589f..652391e3 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/MerchantServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/MerchantServiceImpl.java @@ -167,14 +167,14 @@ public class MerchantServiceImpl extends ServiceImpl i for (Merchant merchant:listR.getData()) { //限量判定 Integer sum = customerApplyLogService.getApplySum(merchant.getId()); - if (merchant.getLimitType()==1&&merchant.getLimitNum()<=sum){ + if (merchant.getLimitType()!=null&&merchant.getLimitType()==1&&merchant.getLimitNum()<=sum){ continue; } - if (customer.getAge()merchant.getAgeLimitEnd()){ + if (merchant.getAgeLimitStart()!=null&&merchant.getAgeLimitEnd()!=null&&(customer.getAge()merchant.getAgeLimitEnd())){ continue; } - if (merchant.getChannelLimitType()==1||merchant.getChannelLimitType()==2){ + if (merchant.getChannelLimitType()!=null&&(merchant.getChannelLimitType()==1||merchant.getChannelLimitType()==2)){ List list = Arrays.asList(merchant.getChannelLimit().split(",")).stream().map(val->Long.parseLong(val)).collect(Collectors.toList()); if (merchant.getChannelLimitType()==1&& !list.contains(customer.getChannelId())){