package com.mzl.flower.web.register; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import com.mzl.flower.base.BaseController; import com.mzl.flower.base.R; import com.mzl.flower.base.ReturnDataDTO; import com.mzl.flower.base.cache.StringCacheClient; import com.mzl.flower.config.exception.ValidationException; import com.mzl.flower.constant.Constants; import com.mzl.flower.dto.request.register.RegisterCustomerDTO; import com.mzl.flower.dto.request.register.RegisterDTO; import com.mzl.flower.dto.request.register.RegisterPartnerDTO; import com.mzl.flower.entity.system.User; import com.mzl.flower.service.register.RegisterService; import com.mzl.flower.service.system.UserService; import com.mzl.flower.service.system.WeChatService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/register") @Api(value = "注册管理", tags = "注册管理") @Validated @Slf4j public class RegisterController extends BaseController { public static final String SMS_CODE_KEY = "SMS-CODE-KEY"; public static final String SEPARATOR = ":"; private final RegisterService registerService; private final StringCacheClient stringCacheClient; private final WeChatService weChatService; private final UserService userService; public RegisterController(RegisterService registerService, StringCacheClient stringCacheClient, WeChatService weChatService, UserService userService) { this.registerService = registerService; this.stringCacheClient = stringCacheClient; this.weChatService = weChatService; this.userService = userService; } @PostMapping("/supplier") @ApiOperation(value = "供应商账号注册") public ResponseEntity> registerSupplier(@Validated @RequestBody RegisterDTO dto) { //从缓存中获取验证码 String smsCacheCode = stringCacheClient.get(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.supplier.name() + SEPARATOR + dto.getTel()); if (StringUtils.isBlank(smsCacheCode)) { throw new ValidationException("手机验证码已过期"); } if (!StringUtils.equals(dto.getSmsCode(), smsCacheCode)) { throw new ValidationException("手机验证码不正确"); } registerService.registerUser(dto, Constants.USER_TYPE.supplier.name()); //删除缓存中的验证码 stringCacheClient.delete(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.supplier.name() + SEPARATOR + dto.getTel()); return returnData(R.SUCCESS.getCode(),null); } @PostMapping("/partner") @ApiOperation(value = "合伙人账号注册") public ResponseEntity> registerPartner(@Validated @RequestBody RegisterPartnerDTO dto) { //从缓存中获取验证码 String smsCacheCode = stringCacheClient.get(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.partner.name() + SEPARATOR + dto.getTel()); if (StringUtils.isBlank(smsCacheCode)) { throw new ValidationException("手机验证码已过期"); } if (!StringUtils.equals(dto.getSmsCode(), smsCacheCode)) { throw new ValidationException("手机验证码不正确"); } registerService.registerPartnerUser(dto); //删除缓存中的验证码 stringCacheClient.delete(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.partner.name() + SEPARATOR + dto.getTel()); return returnData(R.SUCCESS.getCode(),null); } @PostMapping("/customer") @ApiOperation(value = "商户账号注册") public ResponseEntity> registerCustomer(@Validated @RequestBody RegisterCustomerDTO dto) throws Exception{ //从缓存中获取验证码 String smsCacheCode = stringCacheClient.get(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.customer.name() + SEPARATOR + dto.getTel()); if (StringUtils.isBlank(smsCacheCode)) { throw new ValidationException("手机验证码已过期"); } if (!StringUtils.equals(dto.getSmsCode(), smsCacheCode)) { throw new ValidationException("手机验证码不正确"); } WxMaJscode2SessionResult session = weChatService.getWxMaSessionInfo(dto.getWxcode()); String openId = session.getOpenid(); String sessionKey = session.getSessionKey(); String unionId = session.getUnionid(); registerService.registerCustomerUser(dto, Constants.USER_TYPE.customer.name(),openId,sessionKey,unionId); //删除缓存中的验证码 stringCacheClient.delete(SMS_CODE_KEY + SEPARATOR + Constants.USER_TYPE.customer.name() + SEPARATOR + dto.getTel()); return returnData(R.SUCCESS.getCode(),null); } }