diff --git a/ad-platform-common/ad-platform-common-core/pom.xml b/ad-platform-common/ad-platform-common-core/pom.xml index 492e8ff4..55c8f9ab 100644 --- a/ad-platform-common/ad-platform-common-core/pom.xml +++ b/ad-platform-common/ad-platform-common-core/pom.xml @@ -107,8 +107,15 @@ com.alibaba easyexcel + + + com.auth0 + java-jwt + + + diff --git a/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/constant/DefaultNumberConstants.java b/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/constant/DefaultNumberConstants.java index fc1861d9..eab27a4f 100644 --- a/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/constant/DefaultNumberConstants.java +++ b/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/constant/DefaultNumberConstants.java @@ -75,6 +75,10 @@ public class DefaultNumberConstants { */ public static final int TEN_NUMBER = 10; + /** + * 11 + */ + public static final int ELEVEN_NUMBER = 11; /** * 15 @@ -131,6 +135,16 @@ public class DefaultNumberConstants { */ public static final int FIVE_THOUSAND = 5000; + /** + * 十万 + */ + public static final int ONE_HUNDRED_THOUSAND = 100000; + + + /** + * 999999 + */ + public static final int LESS_THAN_ONE_MILLION = 999999; /** * 一百万 @@ -139,7 +153,7 @@ public class DefaultNumberConstants { /** - * 一千万 + * 9999999 */ public static final int TEN_MILLION = 9999999; diff --git a/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/manager/UserTokenManager.java b/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/manager/UserTokenManager.java new file mode 100644 index 00000000..10ec0929 --- /dev/null +++ b/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/manager/UserTokenManager.java @@ -0,0 +1,31 @@ +package com.baiye.manager; + + +import com.baiye.util.JwtHelper; + +/** + * @author Enzo + * 维护用户token + */ +public class UserTokenManager { + + public static String generateToken(Long id) { + JwtHelper jwtHelper = new JwtHelper(); + return jwtHelper.createToken(id); + } + + + public static Long getUserId(String token) { + JwtHelper jwtHelper = new JwtHelper(); + + Long userId = jwtHelper.verifyTokenAndGetUserId(token); + if(userId == null || userId == 0){ + return null; + } + return userId; + } + + + + +} diff --git a/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/util/JwtHelper.java b/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/util/JwtHelper.java new file mode 100644 index 00000000..3d9aca89 --- /dev/null +++ b/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/util/JwtHelper.java @@ -0,0 +1,112 @@ +package com.baiye.util; + +import com.auth0.jwt.JWT; +import com.auth0.jwt.JWTVerifier; +import com.auth0.jwt.algorithms.Algorithm; +import com.auth0.jwt.exceptions.JWTCreationException; +import com.auth0.jwt.exceptions.JWTVerificationException; +import com.auth0.jwt.interfaces.Claim; +import com.auth0.jwt.interfaces.DecodedJWT; +import com.baiye.constant.DefaultNumberConstants; +import com.baiye.exception.BadRequestException; + +import java.util.*; + +/** + * @author Enz + */ +public class JwtHelper { + + /** + * 秘钥 + */ + static final String SECRET = "WX-MALL-TOKEN"; + + /** + * 签名是有谁生成 + */ + static final String USER = "ENZO"; + + /** + * 签名的主题 + */ + static final String SUBJECT = "USER-FORM-TOKEN"; + + /** + * 签名的观众 + */ + static final String AUDIENCE = "USER-FORM-USER"; + + + public String createToken(Long userId) { + try { + Algorithm algorithm = Algorithm.HMAC256(SECRET); + Map map = new HashMap<>(DefaultNumberConstants.SIXTEEN_NUMBER); + Date nowDate = new Date(); + map.put("alg", "HS256"); + map.put("typ", "JWT"); + return JWT.create() + // 设置头部信息 Header + .withHeader(map) + // 设置 载荷 Payload + .withClaim("userId", userId) + .withIssuer(USER) + .withSubject(SUBJECT) + .withAudience(AUDIENCE) + // 生成签名的时间 + .withIssuedAt(nowDate) + // 签名过期的时间 + //.withExpiresAt(expireDate) + // 签名 Signature + .sign(algorithm); + } catch (JWTCreationException exception) { + throw new BadRequestException("生成token失败"); + } + } + + public Long verifyTokenAndGetUserId(String token) { + try { + Algorithm algorithm = Algorithm.HMAC256(SECRET); + JWTVerifier verifier = JWT.require(algorithm) + .withIssuer(USER) + .build(); + DecodedJWT jwt = verifier.verify(token); + Map claims = jwt.getClaims(); + Claim claim = claims.get("userId"); + return claim.asLong(); + } catch (JWTVerificationException exception) { + throw new BadRequestException("校验失败"); + } + } + + + public Date getAfterDate(Date date, int year, int month, int day, int hour, int minute, int second) { + if (date == null) { + date = new Date(); + } + + Calendar cal = new GregorianCalendar(); + + cal.setTime(date); + if (year != 0) { + cal.add(Calendar.YEAR, year); + } + if (month != 0) { + cal.add(Calendar.MONTH, month); + } + if (day != 0) { + cal.add(Calendar.DATE, day); + } + if (hour != 0) { + cal.add(Calendar.HOUR_OF_DAY, hour); + } + if (minute != 0) { + cal.add(Calendar.MINUTE, minute); + } + if (second != 0) { + cal.add(Calendar.SECOND, second); + } + return cal.getTime(); + } + +} diff --git a/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/util/MobileUtil.java b/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/util/MobileUtil.java new file mode 100644 index 00000000..031a5420 --- /dev/null +++ b/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/util/MobileUtil.java @@ -0,0 +1,112 @@ +package com.baiye.util; + +import org.apache.commons.lang3.StringUtils; + +import java.util.regex.Pattern; + +/** + * @author Enzo + * @date : 2022/4/18 + */ +public class MobileUtil { + + /** + * 中国电信号码格式验证 手机段: 133,149,153,173,177,180,181,189,199,1349,1410,1700,1701,1702 + **/ + private static final String CHINA_TELECOM_PATTERN = "(?:^(?:\\+86)?1(?:33|49|53|7[37]|8[019]|99)\\d{8}$)|(?:^(?:\\+86)?1349\\d{7}$)|(?:^(?:\\+86)?1410\\d{7}$)|(?:^(?:\\+86)?170[0-2]\\d{7}$)"; + + /** + * 中国联通号码格式验证 手机段:130,131,132,145,146,155,156,166,171,175,176,185,186,1704,1707,1708,1709 + **/ + private static final String CHINA_UNICOM_PATTERN = "(?:^(?:\\+86)?1(?:3[0-2]|4[56]|5[56]|66|7[156]|8[56])\\d{8}$)|(?:^(?:\\+86)?170[47-9]\\d{7}$)"; + + /** + * 中国移动号码格式验证 + * 手机段:134,135,136,137,138,139,147,148,150,151,152,157,158,159,178,182,183,184,187,188,198,1440,1703,1705,1706 + **/ + private static final String CHINA_MOBILE_PATTERN = "(?:^(?:\\+86)?1(?:3[4-9]|4[78]|5[0-27-9]|78|8[2-478]|98)\\d{8}$)|(?:^(?:\\+86)?1440\\d{7}$)|(?:^(?:\\+86)?170[356]\\d{7}$)"; + + /** + * 中国大陆手机号码校验 + * + * @param phone + * + * @return + */ + public static boolean checkPhone(String phone) { + if (StringUtils.isNotBlank(phone)) { + if (checkChinaMobile(phone) || checkChinaUnicom(phone) || checkChinaTelecom(phone)) { + return true; + } + } + return false; + } + + /** + * 中国移动手机号码校验 + * + * @param phone + * + * @return + */ + public static boolean checkChinaMobile(String phone) { + if (StringUtils.isNotBlank(phone)) { + Pattern regexp = Pattern.compile(CHINA_MOBILE_PATTERN); + if (regexp.matcher(phone).matches()) { + return true; + } + } + return false; + } + + /** + * 中国联通手机号码校验 + * + * @param phone + * + * @return + */ + public static boolean checkChinaUnicom(String phone) { + if (StringUtils.isNotBlank(phone)) { + Pattern regexp = Pattern.compile(CHINA_UNICOM_PATTERN); + if (regexp.matcher(phone).matches()) { + return true; + } + } + return false; + } + + /** + * 中国电信手机号码校验 + * + * @param phone + * + * @return + */ + public static boolean checkChinaTelecom(String phone) { + if (StringUtils.isNotBlank(phone)) { + Pattern regexp = Pattern.compile(CHINA_TELECOM_PATTERN); + if (regexp.matcher(phone).matches()) { + return true; + } + } + return false; + } + + /** + * 隐藏手机号中间四位 + * + * @param phone + * + * @return java.lang.String + */ + public static String hideMiddleMobile(String phone) { + if (StringUtils.isNotBlank(phone)) { + phone = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"); + } + return phone; + } + + + +} diff --git a/ad-platform-pojo/src/main/java/com/baiye/model/enums/ResponseCode.java b/ad-platform-pojo/src/main/java/com/baiye/model/enums/ResponseCode.java index 0c220213..530b2150 100644 --- a/ad-platform-pojo/src/main/java/com/baiye/model/enums/ResponseCode.java +++ b/ad-platform-pojo/src/main/java/com/baiye/model/enums/ResponseCode.java @@ -23,6 +23,21 @@ public enum ResponseCode { */ RECHARGE_SUCCESS("1020","充值成功"), + /** + * 手机号码不正确 + */ + PHONE_NUMBER_IS_INCORRECT("1022","手机号码不正确"), + + /** + * 验证码错误 + */ + VERIFICATION_CODE_PARAMETER_ERROR("1023","验证码错误"), + + /** + * 验证码不存在或已过期 + */ + VERIFICATION_NOT_EXIST_HAS_EXPIRED("1024","验证码不存在或已过期"), + /** * 读取文件失败 */ @@ -35,7 +50,7 @@ public enum ResponseCode { /** * 用户信息错误 */ - USER_INFORMATION_ERROR("1002", "用户信息错误,不能通过审核"), + USER_INFORMATION_ERROR("1002", "用户信息错误"), /** * 支付宝支付失败 diff --git a/manage/ad-platform-management/src/main/java/com/baiye/modules/security/config/SpringSecurityConfig.java b/manage/ad-platform-management/src/main/java/com/baiye/modules/security/config/SpringSecurityConfig.java index 805c1904..7545a773 100644 --- a/manage/ad-platform-management/src/main/java/com/baiye/modules/security/config/SpringSecurityConfig.java +++ b/manage/ad-platform-management/src/main/java/com/baiye/modules/security/config/SpringSecurityConfig.java @@ -134,6 +134,7 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { .antMatchers("/api/users/info/findByName").permitAll() .antMatchers("/api/users/info/deptIds").permitAll() .antMatchers("/api/roles/user/authority").permitAll() + .antMatchers( "/api/form/user/**").permitAll() .antMatchers( "/api/report/organize").permitAll() .antMatchers( "/api/download/task").permitAll() diff --git a/manage/ad-platform-management/src/main/java/com/baiye/modules/system/domain/Debt.java b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/domain/Debt.java index 87648c4e..03ab5303 100644 --- a/manage/ad-platform-management/src/main/java/com/baiye/modules/system/domain/Debt.java +++ b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/domain/Debt.java @@ -27,6 +27,10 @@ public class Debt { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @ApiModelProperty(value = "邀请用户id") + @Column(name = "user_id") + private Long userId; + @ApiModelProperty(value = "姓名") @NotBlank(message = "姓名不能为空") @Column(name = "name") diff --git a/manage/ad-platform-management/src/main/java/com/baiye/modules/system/domain/FormUser.java b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/domain/FormUser.java new file mode 100644 index 00000000..cc74a572 --- /dev/null +++ b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/domain/FormUser.java @@ -0,0 +1,48 @@ +package com.baiye.modules.system.domain; + +import com.baiye.model.base.BaseEntity; +import io.swagger.annotations.ApiModelProperty; +import lombok.Getter; +import lombok.Setter; + +import javax.persistence.*; +import javax.validation.constraints.NotNull; +import java.io.Serializable; +import java.util.Date; + +/** + * @author Enzo + * @date : 2022/4/18 + */ +@Entity +@Getter +@Setter +@Table(name="tb_user_form") +public class FormUser extends BaseEntity implements Serializable { + + private static final long serialVersionUID = 7676856944264681235L; + + @Id + @Column(name = "id") + @NotNull(groups = BaseEntity.Update.class) + @GeneratedValue(strategy = GenerationType.IDENTITY) + @ApiModelProperty(value = "ID", hidden = true) + private Long id; + + @Column(name = "phone") + @ApiModelProperty(value = "手机号码") + private String phone; + + @Column(name = "qr_code_path") + @ApiModelProperty(value = "二维码地址") + private String qrCodePath; + + @Column(name = "invitation_nums") + @ApiModelProperty(value = "邀请数量") + private String invitationNums; + + @Column(name = "last_login_time") + @ApiModelProperty(value = "最后登录时间") + private Date lastLoginTime; + +} diff --git a/manage/ad-platform-management/src/main/java/com/baiye/modules/system/repository/DebtFormRepository.java b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/repository/DebtFormRepository.java index c8e4c529..bea1343c 100644 --- a/manage/ad-platform-management/src/main/java/com/baiye/modules/system/repository/DebtFormRepository.java +++ b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/repository/DebtFormRepository.java @@ -3,8 +3,49 @@ package com.baiye.modules.system.repository; import com.baiye.modules.system.domain.Debt; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.Date; +import java.util.Map; + @Repository public interface DebtFormRepository extends JpaRepository, JpaSpecificationExecutor { + + + /** + * 时间查询拉取人数 + * @param dateStr + * @param beginDate + * @param endDate + * @param userId + * @return + */ + @Query(value = "SELECT" + + " `date`," + + " MAX( `sum` ) AS `sum` " + + " FROM " + + " (" + + " SELECT" + + " @cdate := DATE_ADD( @cdate, INTERVAL - 1 DAY ) `date`," + + " 0 AS `sum` " + + " FROM" + + " ( SELECT @cdate := DATE_ADD( CURDATE( ), INTERVAL + 1 DAY ) FROM tb_bd_backdata ) t1" + + " WHERE" + + " @cdate > ?1 UNION ALL" + + " SELECT" + + " DATE_FORMAT( gmt_create, '%Y-%m-%d' ) AS `date`," + + " COUNT( * ) AS 'sum' " + + " FROM" + + " `tb_bd_backdata`" + + " WHERE" + + " tb_bd_backdata.`gmt_create` >= ?2" + + " AND tb_bd_backdata.`gmt_create` <= ?3" + + " AND tb_bd_backdata.`user_id` <= ?4" + + " GROUP BY" + + " DATE DESC" + + " ) _tmpAllTable" + + " GROUP BY" + + " `date` DESC", nativeQuery = true) + Map findByDate(String dateStr, Date beginDate, Date endDate, Long userId); } diff --git a/manage/ad-platform-management/src/main/java/com/baiye/modules/system/repository/FormUserRepository.java b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/repository/FormUserRepository.java new file mode 100644 index 00000000..f7da4a51 --- /dev/null +++ b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/repository/FormUserRepository.java @@ -0,0 +1,21 @@ + +package com.baiye.modules.system.repository; + +import com.baiye.modules.system.domain.FormUser; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +/** + * @author Enzo + * @date 2022-4-18 + */ +public interface FormUserRepository extends JpaRepository, JpaSpecificationExecutor { + + + /** + * 手机号码查询 + * @param phoneNumber + * @return + */ + FormUser findByPhone(String phoneNumber); +} diff --git a/manage/ad-platform-management/src/main/java/com/baiye/modules/system/rest/FormUserController.java b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/rest/FormUserController.java new file mode 100644 index 00000000..605ea10e --- /dev/null +++ b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/rest/FormUserController.java @@ -0,0 +1,48 @@ +package com.baiye.modules.system.rest; + +import com.baiye.http.CommonResponse; +import com.baiye.modules.system.service.FormUserService; +import com.baiye.modules.system.service.dto.UserLoginDTO; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import javax.validation.constraints.NotNull; +import java.util.Map; + +/** + * @author Enzo + * @date : 2022/4/18 + */ +@RestController +@RequiredArgsConstructor +@Api(tags = "用户授权") +@RequestMapping("/api/form/user") +public class FormUserController { + + private final FormUserService formUserService; + + + @GetMapping("/verifyCode") + @ApiOperation("更改手机号码发送验证码") + public CommonResponse getVerifyCode(@NotNull(message = "不能为空") String phone) { + return formUserService.createRandomCodeByPhone(phone); + } + + + @PostMapping("/login") + @ApiOperation("账号登录") + public CommonResponse login(@Validated @RequestBody UserLoginDTO loginDTO) { + return formUserService.userLogin(loginDTO); + } + + @PostMapping("/userInfo") + @ApiOperation("获取用户详情") + public CommonResponse> userInfo(@NotNull(message = "不能为空") String userToken) { + return formUserService.getUserDetail(userToken); + } + + +} diff --git a/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/FormUserService.java b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/FormUserService.java new file mode 100644 index 00000000..45e44b8a --- /dev/null +++ b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/FormUserService.java @@ -0,0 +1,42 @@ +package com.baiye.modules.system.service; + +import com.baiye.http.CommonResponse; +import com.baiye.modules.system.domain.FormUser; +import com.baiye.modules.system.service.dto.UserLoginDTO; + +import java.util.Map; + +/** + * @author Enzo + * @date : 2022/4/18 + */ +public interface FormUserService { + /** + * 手机号码生成验证码 + * @param phone + * @return + */ + CommonResponse createRandomCodeByPhone(String phone); + + /** + * 用户登录 + * @param loginDTO + * @return + */ + CommonResponse userLogin(UserLoginDTO loginDTO); + + /** + * 获取用户详情 + * @param userToken + * @return + */ + CommonResponse> getUserDetail(String userToken); + + + /** + * id查找用户 + * @param userId + * @return + */ + FormUser findUserById(Long userId); +} diff --git a/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/dto/DebtQueryResult.java b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/dto/DebtQueryResult.java new file mode 100644 index 00000000..6505e361 --- /dev/null +++ b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/dto/DebtQueryResult.java @@ -0,0 +1,15 @@ +package com.baiye.modules.system.service.dto; + +import lombok.Data; + +/** + * @author Enzo + * @date : 2022/4/18 + */ +@Data +public class DebtQueryResult { + private String date; + + private Integer num; + +} diff --git a/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/dto/FormUserDto.java b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/dto/FormUserDto.java new file mode 100644 index 00000000..ceceb9d3 --- /dev/null +++ b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/dto/FormUserDto.java @@ -0,0 +1,53 @@ +/* + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.baiye.modules.system.service.dto; + +import com.baiye.model.base.BaseDTO; +import io.swagger.annotations.ApiModelProperty; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.io.Serializable; +import java.util.Date; + +/** +* @author Zheng Jie +* @date 2019-03-29 +*/ +@Getter +@Setter +@NoArgsConstructor +public class FormUserDto extends BaseDTO implements Serializable { + + private static final long serialVersionUID = -2035716596706011470L; + + @ApiModelProperty(value = "id") + private Integer id; + + @ApiModelProperty(value = "二维码地址") + private String qrCodePath; + + @ApiModelProperty(value = "手机号码") + private String phone; + + @ApiModelProperty(value = "最后登录时间") + private Date lastLoginTime; + + + + +} diff --git a/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/dto/UserLoginDTO.java b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/dto/UserLoginDTO.java new file mode 100644 index 00000000..07e24bdb --- /dev/null +++ b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/dto/UserLoginDTO.java @@ -0,0 +1,29 @@ +package com.baiye.modules.system.service.dto; + +import com.baiye.constant.DefaultNumberConstants; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +/** + * @author Enzo + * @date : 2022/4/18 + */ +@Data +public class UserLoginDTO implements Serializable { + + private static final long serialVersionUID = -5500739143732174543L; + + @NotNull(message = "手机号码不能为空") + @Length(min = DefaultNumberConstants.ELEVEN_NUMBER,max = DefaultNumberConstants.ELEVEN_NUMBER) + @ApiModelProperty("手机号码") + private String phone; + + + @NotNull(message = "验证码不能为空") + @ApiModelProperty("验证码") + private Integer verificationCode; +} diff --git a/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/impl/FormUserServiceImpl.java b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/impl/FormUserServiceImpl.java new file mode 100644 index 00000000..214f6eab --- /dev/null +++ b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/impl/FormUserServiceImpl.java @@ -0,0 +1,134 @@ +package com.baiye.modules.system.service.impl; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.convert.Convert; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.RandomUtil; +import com.baiye.constant.DefaultNumberConstants; +import com.baiye.http.CommonResponse; +import com.baiye.manager.UserTokenManager; +import com.baiye.model.enums.ResponseCode; +import com.baiye.modules.system.domain.FormUser; +import com.baiye.modules.system.repository.DebtFormRepository; +import com.baiye.modules.system.repository.FormUserRepository; +import com.baiye.modules.system.service.FormUserService; +import com.baiye.modules.system.service.dto.DebtQueryResult; +import com.baiye.modules.system.service.dto.UserLoginDTO; +import com.baiye.util.MobileUtil; +import com.baiye.util.SmsUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +/** + * @author Enzo + * @date : 2022/4/18 + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class FormUserServiceImpl implements FormUserService { + + private final RedisTemplate redisTemplate; + + private final FormUserRepository formUserRepository; + + private final DebtFormRepository debtFormRepository; + + private final SmsUtil smsUtil; + + + @Override + public CommonResponse createRandomCodeByPhone(String phone) { + if (!MobileUtil.checkPhone(phone)) { + return CommonResponse.createByErrorMessage(ResponseCode.PHONE_NUMBER_IS_INCORRECT.getDesc()); + } + int randomNum = RandomUtil.randomInt + (DefaultNumberConstants.ONE_HUNDRED_THOUSAND, DefaultNumberConstants.LESS_THAN_ONE_MILLION); + smsUtil.sendAliYunSms(phone, "{\"code\":\"" + randomNum + "\"}"); + redisTemplate.opsForValue().set(phone, String.valueOf(randomNum), 300, TimeUnit.SECONDS); + return CommonResponse.createBySuccess(); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public CommonResponse userLogin(UserLoginDTO loginDTO) { + + if (!NumberUtils.isParsable(loginDTO.getVerificationCode().toString())) { + return CommonResponse.createByErrorMessage(ResponseCode.VERIFICATION_CODE_PARAMETER_ERROR.getDesc()); + } + Integer verifyCode = loginDTO.getVerificationCode(); + String phone = loginDTO.getPhone(); + + String phoneVerifyCode = redisTemplate.opsForValue().get(phone); + if (phoneVerifyCode == null || !phoneVerifyCode.equalsIgnoreCase(verifyCode.toString())) { + return CommonResponse.createByErrorMessage(ResponseCode.VERIFICATION_NOT_EXIST_HAS_EXPIRED.getDesc()); + } + // 删除验证码 + redisTemplate.delete(phone); + FormUser formUser = formUserRepository.findByPhone(phone); + // 已存在用户 + if (ObjectUtil.isNotNull(formUser)) { + formUser.setLastLoginTime(DateUtil.date()); + // token + String userToken = + UserTokenManager.generateToken(formUser.getId()); + formUserRepository.save(formUser); + return CommonResponse.createBySuccess(userToken); + } + // TODO 生成二维码 + formUser = new FormUser(); + formUser.setPhone(phone); + formUser.setQrCodePath(""); + formUser.setLastLoginTime(DateUtil.date()); + FormUser user = formUserRepository.save(formUser); + // token + String userToken = + UserTokenManager.generateToken(user.getId()); + return CommonResponse.createBySuccess(userToken); + } + + @Override + public CommonResponse> getUserDetail(String userToken) { + Map map = new HashMap<>(DefaultNumberConstants.SIXTEEN_NUMBER); + Long userId = UserTokenManager.getUserId(userToken); + Date now = DateUtil.date(); + if (userId == null) { + return CommonResponse.createByErrorMessage(ResponseCode.DECRYPTION_FAILED.getDesc()); + } + FormUser formUser = findUserById(userId); + if (formUser == null || StringUtils.isBlank(formUser.getQrCodePath())) { + return CommonResponse.createByErrorMessage(ResponseCode.USER_INFORMATION_ERROR.getDesc()); + } + + map.put("qrCode", formUser.getQrCodePath()); + // 提前一周 + Date dateTime = DateUtil.offsetWeek(now, DefaultNumberConstants.MINUS_ONE_NUMBER); + String formatDate = DateUtil.formatDate(dateTime); + Map debtFormRepositoryByDate = + debtFormRepository.findByDate(formatDate, dateTime, now, userId); + if (CollUtil.isNotEmpty(debtFormRepositoryByDate)) { + List debtQueryResults = Convert.toList(DebtQueryResult.class, debtFormRepositoryByDate); + map.put("dateResult", debtQueryResults); + } + return CommonResponse.createBySuccess(map); + } + + @Override + public FormUser findUserById(Long userId) { + return formUserRepository.findById(userId).orElseGet(FormUser::new); + } + + +} diff --git a/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/mapstruct/FormUserMapper.java b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/mapstruct/FormUserMapper.java new file mode 100644 index 00000000..bbb8a195 --- /dev/null +++ b/manage/ad-platform-management/src/main/java/com/baiye/modules/system/service/mapstruct/FormUserMapper.java @@ -0,0 +1,16 @@ + +package com.baiye.modules.system.service.mapstruct; + +import com.baiye.model.base.BaseMapper; +import com.baiye.modules.system.domain.FormUser; +import com.baiye.modules.system.service.dto.FormUserDto; +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; + +/** + * @author Enzo + * @date 2022-4-18 + */ +@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE) +public interface FormUserMapper extends BaseMapper { +} diff --git a/manage/ad-platform-management/src/main/java/com/baiye/util/SmsUtil.java b/manage/ad-platform-management/src/main/java/com/baiye/util/SmsUtil.java index 850fff30..0794fec9 100644 --- a/manage/ad-platform-management/src/main/java/com/baiye/util/SmsUtil.java +++ b/manage/ad-platform-management/src/main/java/com/baiye/util/SmsUtil.java @@ -7,12 +7,14 @@ import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.profile.DefaultProfile; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; /** * @author wujingtao * @date 2022/01/21 */ @Slf4j +@Component public class SmsUtil { @Value("${sms.aliyun.accessKeyId}") diff --git a/pom.xml b/pom.xml index 78b473dd..86f17435 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,7 @@ - 3.4.1 + 3.9.0 4.0.0 5.7.1 22.0 @@ -135,6 +135,12 @@ ${jjwt.version} + + com.auth0 + java-jwt + ${jwt.version} + + net.dreamlu