소스 검색

Merge branch 'user_bind_account'

zoujiajian 3 년 전
부모
커밋
ada6b078ba

+ 2 - 0
netflix-dao/src/main/java/com/cyksj/model/entity/UserBindPhone.java

@@ -15,4 +15,6 @@ public class UserBindPhone extends BaseEntity{
 	private Long userId;
 
 	private String phone;
+
+	private String email;
 }

+ 59 - 8
netflix-web/src/main/java/com/cyksj/web/controller/market/TaskFrontController.java

@@ -1,5 +1,7 @@
 package com.cyksj.web.controller.market;
 
+import cn.hutool.core.lang.Validator;
+import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.common.annotation.NoSubmit;
 import com.cyksj.common.constant.TaskTypeEnum;
@@ -14,6 +16,7 @@ import com.cyksj.model.entity.UserBindPhone;
 import com.cyksj.model.request.LoginPhoneReq;
 import com.cyksj.model.request.TaskFollowReq;
 import com.cyksj.model.views.TaskFrontView;
+import com.cyksj.redis.RedisService;
 import com.cyksj.service.market.MarketFrontService;
 import com.cyksj.service.market.task.TaskService;
 import com.cyksj.service.phone.PhoneCodeService;
@@ -27,6 +30,7 @@ import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
+import java.util.Optional;
 
 /*
  *项目名: netflix
@@ -53,6 +57,8 @@ public class TaskFrontController {
 
 	private final TaskTypeMapper taskTypeMapper;
 
+	private final RedisService redisService;
+
 	/**
 	 * 任务列表
 	 */
@@ -89,18 +95,21 @@ public class TaskFrontController {
 		if (other != null) {
 			throw BusinessRuntimeException.getInstance("该手机号已被绑定");
 		}
-		UserBindPhone bindPhone = userBindPhoneMapper.selectOne(Wrappers.lambdaQuery(UserBindPhone.class)
-				.eq(UserBindPhone::getUserId, userId).last("limit 1"));
-		if (bindPhone != null) {
+		UserBindPhone bindPhone = Optional.ofNullable(userBindPhoneMapper.selectOne(Wrappers.lambdaQuery(UserBindPhone.class)
+				.eq(UserBindPhone::getUserId, userId).last("limit 1"))).orElse(new UserBindPhone());
+		if (StrUtil.isNotBlank(bindPhone.getPhone())) {
 			throw BusinessRuntimeException.getInstance("您已绑定手机号");
 		}
 		//校验
 		phoneCodeService.checkPhone(phoneReq.getPhone(), phoneReq.getCode());
-		UserBindPhone userBindPhone = new UserBindPhone();
-		userBindPhone.setUserId(userId);
-		userBindPhone.setPhone(phoneReq.getPhone());
+		bindPhone.setUserId(userId);
+		bindPhone.setPhone(phoneReq.getPhone());
 		try {
-			userBindPhoneMapper.insert(userBindPhone);
+			if (bindPhone.getId() == null) {
+				userBindPhoneMapper.insert(bindPhone);
+			}else {
+				userBindPhoneMapper.updateById(bindPhone);
+			}
 		} catch (DuplicateKeyException e) {
 
 		}
@@ -111,6 +120,48 @@ public class TaskFrontController {
 		return GatewayResponse.SUCCESS.newBuilder().toResult("绑定手机号成功");
 	}
 
+	/**
+	 * 绑定邮箱
+	 */
+	@PostMapping("/bind/email")
+	public Result<String> bindEmail(@RequestBody LoginPhoneReq phoneReq) {
+		Long userId = StpUserUtil.getLoginIdAsLong();
+		if (!Validator.isEmail(phoneReq.getEmail())) {
+			throw BusinessRuntimeException.getInstance("请输入正确的邮箱");
+		}
+		UserBindPhone other = userBindPhoneMapper.selectOne(Wrappers.lambdaQuery(UserBindPhone.class)
+				.eq(UserBindPhone::getEmail, phoneReq.getEmail()).ne(UserBindPhone::getUserId, userId).select(UserBindPhone::getId).last("limit 1"));
+		if (other != null) {
+			throw BusinessRuntimeException.getInstance("该邮箱已被绑定");
+		}
+		UserBindPhone bindPhone = Optional.ofNullable(userBindPhoneMapper.selectOne(Wrappers.lambdaQuery(UserBindPhone.class)
+				.eq(UserBindPhone::getUserId, userId).last("limit 1"))).orElse(new UserBindPhone());
+		if (StrUtil.isNotBlank(bindPhone.getEmail())) {
+			throw BusinessRuntimeException.getInstance("您已绑定邮箱");
+		}
+		String code = redisService.getStr(RedisService.key.EMAIL_CODE_KEY.getNameFormat(phoneReq.getEmail()));
+		if (StrUtil.isEmpty(code)) {
+			throw BusinessRuntimeException.getInstance("邮箱验证码已失效,请重新获取");
+		}
+		if (!StrUtil.equals(phoneReq.getCode(), code)) {
+			throw BusinessRuntimeException.getInstance("邮箱验证码错误,请重新输入");
+		}
+		bindPhone.setEmail(phoneReq.getEmail());
+		bindPhone.setUserId(userId);
+		try {
+			if (bindPhone.getId() == null) {
+				userBindPhoneMapper.insert(bindPhone);
+			}else {
+				userBindPhoneMapper.updateById(bindPhone);
+			}
+		} catch (DuplicateKeyException e) {
+
+		}
+		redisService.del(RedisService.key.EMAIL_CODE_KEY.getNameFormat(phoneReq.getEmail()));
+		return GatewayResponse.SUCCESS.newBuilder().toResult("绑定邮箱成功");
+	}
+
+
 	/**
 	 * 修改手机号
 	 */
@@ -119,7 +170,7 @@ public class TaskFrontController {
 		Long userId = StpUserUtil.getLoginIdAsLong();
 		UserBindPhone bindPhone = userBindPhoneMapper.selectOne(Wrappers.lambdaQuery(UserBindPhone.class)
 				.eq(UserBindPhone::getUserId, userId).last("limit 1"));
-		if (bindPhone == null) {
+		if (bindPhone == null || StrUtil.isEmpty(bindPhone.getPhone())) {
 			throw BusinessRuntimeException.getInstance("您未绑定手机号");
 		}
 		UserBindPhone other = userBindPhoneMapper.selectOne(Wrappers.lambdaQuery(UserBindPhone.class)

+ 20 - 0
netflix-web/src/main/java/com/cyksj/web/controller/payment/OrderController.java

@@ -3,6 +3,7 @@ package com.cyksj.web.controller.payment;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.extra.qrcode.QrCodeUtil;
 import cn.hutool.extra.qrcode.QrConfig;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.common.exception.BusinessRuntimeException;
 import com.cyksj.common.util.IoKit;
 import com.cyksj.common.util.Xmls;
@@ -11,9 +12,11 @@ import com.cyksj.config.zfb.ZfbProductCode;
 import com.cyksj.dto.Result;
 import com.cyksj.enums.GatewayResponse;
 import com.cyksj.mapper.OrderDonMapper;
+import com.cyksj.mapper.market.task.UserBindPhoneMapper;
 import com.cyksj.model.dto.AliPayParams;
 import com.cyksj.model.dto.H5JsPayParams;
 import com.cyksj.model.entity.OrderDon;
+import com.cyksj.model.entity.UserBindPhone;
 import com.cyksj.model.manage.views.OrderDonView;
 import com.cyksj.model.request.AlipayOrderDonReq;
 import com.cyksj.model.request.OrderPayRequest;
@@ -63,6 +66,9 @@ public class OrderController {
     @Autowired
     private OrderDonMapper orderDonMapper;
 
+    @Autowired
+    private UserBindPhoneMapper userBindPhoneMapper;
+
     /**
      * 支付
      * @author chan
@@ -355,6 +361,20 @@ public class OrderController {
         return GatewayResponse.SUCCESS.newBuilder().toResult(search);
     }
 
+    /**
+     * 是否绑定了邮箱或手机号
+     */
+    @GetMapping("/isBind")
+    public Result<Boolean> isBindPhoneOrEmail() {
+        Long userId = StpUserUtil.getLoginIdAsLong();
+        UserBindPhone userBindPhone = userBindPhoneMapper.selectOne(Wrappers.lambdaQuery(UserBindPhone.class)
+                .eq(UserBindPhone::getUserId, userId));
+        if (userBindPhone == null) {
+            return GatewayResponse.SUCCESS.newBuilder().toResult(false);
+        }
+        return GatewayResponse.SUCCESS.newBuilder().toResult(true);
+    }
+
     /**
      * 获取支付宝回调参数
      */