|
@@ -1,11 +1,15 @@
|
|
|
package com.zanxiang.game.module.sdk.service.impl;
|
|
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.zanxiang.game.module.base.pojo.enums.HttpStatusEnum;
|
|
|
+import com.zanxiang.game.module.mybatis.entity.User;
|
|
|
import com.zanxiang.game.module.sdk.constant.RedisKeyConstant;
|
|
|
import com.zanxiang.game.module.sdk.enums.ExpireTimeEnum;
|
|
|
import com.zanxiang.game.module.sdk.enums.SmsTypeEnum;
|
|
|
import com.zanxiang.game.module.sdk.pojo.dto.UserDTO;
|
|
|
+import com.zanxiang.game.module.sdk.pojo.param.BindPhoneParam;
|
|
|
import com.zanxiang.game.module.sdk.pojo.param.SmsCheckParam;
|
|
|
import com.zanxiang.game.module.sdk.pojo.param.SmsSendParam;
|
|
|
import com.zanxiang.game.module.sdk.pojo.param.UserData;
|
|
@@ -15,12 +19,17 @@ import com.zanxiang.game.module.sdk.util.RedisUtil;
|
|
|
import com.zanxiang.game.module.sdk.util.RegexUtil;
|
|
|
import com.zanxiang.module.sms.pojo.SendResult;
|
|
|
import com.zanxiang.module.sms.service.impl.AliSmsService;
|
|
|
+import com.zanxiang.module.util.exception.BaseException;
|
|
|
import com.zanxiang.module.util.pojo.ResultVO;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.logging.log4j.util.Strings;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
/**
|
|
@@ -41,6 +50,75 @@ public class SmsServiceImpl implements ISmsService {
|
|
|
@Autowired
|
|
|
private AliSmsService aliSmsService;
|
|
|
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getBindPhone(UserData userData) {
|
|
|
+ User user = userService.getById(userData.getUserId());
|
|
|
+ if (user == null) {
|
|
|
+ throw new BaseException("参数错误, 用户信息不存在");
|
|
|
+ }
|
|
|
+ if (Strings.isBlank(user.getMobile())) {
|
|
|
+ return Collections.singletonMap("isBindPhone", Boolean.FALSE);
|
|
|
+ }
|
|
|
+ Map<String, Object> resultMap = new HashMap<>(2);
|
|
|
+ resultMap.put("isBindPhone", Boolean.TRUE);
|
|
|
+ resultMap.put("phone", user.getShowPhoneNum());
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> bindPhoneSend(String phone, UserData userData) {
|
|
|
+ //验证手机号
|
|
|
+ if (!RegexUtil.checkPhone(phone)) {
|
|
|
+ return this.buildResultMap(Boolean.FALSE, "手机号不合法, 请重新输入");
|
|
|
+ }
|
|
|
+ //校验手机号是否已经绑定了其他账号
|
|
|
+ if (userService.count(new LambdaQueryWrapper<User>()
|
|
|
+ .eq(User::getGameId, userData.getGameId())
|
|
|
+ .eq(User::getMobile, phone)
|
|
|
+ ) > 0) {
|
|
|
+ return this.buildResultMap(Boolean.FALSE, "手机号已被其他账号绑定, 请联系客服");
|
|
|
+ }
|
|
|
+ //验证码缓存key
|
|
|
+ String key = this.smsKey(phone, SmsTypeEnum.SMS_BIND.getType());
|
|
|
+ //判断是否已经发送
|
|
|
+ if (Strings.isNotBlank(redisUtil.getCache(key))) {
|
|
|
+ return this.buildResultMap(Boolean.FALSE, "验证码已经发送, 请勿重复操作");
|
|
|
+ }
|
|
|
+ //发送验证码
|
|
|
+ String randomCode = this.randomCode();
|
|
|
+ SendResult sendResult = aliSmsService.sendCode(randomCode, phone);
|
|
|
+ //发送失败
|
|
|
+ if (!sendResult.isSuccess()) {
|
|
|
+ return this.buildResultMap(Boolean.FALSE, "验证码发送失败, 请稍后重试或者联系客服");
|
|
|
+ }
|
|
|
+ //发送成功设置缓存, 时效5分钟
|
|
|
+ redisUtil.setCache(key, randomCode, ExpireTimeEnum.FIVE_MIN.getTime());
|
|
|
+ return this.buildResultMap(Boolean.TRUE, "发送成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> buildResultMap(Boolean sendResult, String msg) {
|
|
|
+ Map<String, Object> resultMap = new HashMap<>(2);
|
|
|
+ resultMap.put("result", sendResult);
|
|
|
+ resultMap.put("msg", msg);
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> bindPhoneCheck(BindPhoneParam param, UserData userData) {
|
|
|
+ //验证码验证
|
|
|
+ HttpStatusEnum statusEnum = this.smsCheck(SmsTypeEnum.SMS_BIND.getType(), param.getMobile(), param.getCode());
|
|
|
+ if (!Objects.equals(statusEnum, HttpStatusEnum.SUCCESS)) {
|
|
|
+ return this.buildResultMap(Boolean.FALSE, statusEnum.getMsg());
|
|
|
+ }
|
|
|
+ //更新用户手机号
|
|
|
+ userService.update(new LambdaUpdateWrapper<User>()
|
|
|
+ .set(User::getMobile, param.getMobile())
|
|
|
+ .set(User::getUpdateTime, LocalDateTime.now())
|
|
|
+ .eq(User::getId, userData.getUserId()));
|
|
|
+ //返回结果
|
|
|
+ return this.buildResultMap(Boolean.TRUE, "绑定成功");
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public ResultVO<Boolean> smsSend(SmsSendParam smsSendParam, UserData userData) {
|
|
|
//类型
|