package com.loan.system.controller.wechat; import com.loan.system.constant.JwtClaimsConstant; import com.loan.system.domain.dto.UserLoginDTO; import com.loan.system.domain.dto.WeChatLoginDTO; import com.loan.system.domain.entity.Customer; import com.loan.system.domain.entity.User; import com.loan.system.domain.pojo.Result; import com.loan.system.domain.vo.CustomerLoginVO; import com.loan.system.domain.vo.UserLoginVO; import com.loan.system.properties.JwtProperties; import com.loan.system.service.CustomerService; import com.loan.system.service.Impl.UserServiceImpl; import com.loan.system.service.UserService; import com.loan.system.service.WxService; import com.loan.system.utils.JwtUtil; import com.loan.system.utils.ResultUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.ObjectUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @author Edwin * @date 2020/9/2 - 19:11 */ @RestController @RequestMapping("/WeChat") @Api(tags = "微信用户接口") public class UserController { @Autowired private UserService userService; @Autowired private JwtProperties jwtProperties; @Autowired private WxService wxService; @Autowired private CustomerService customerService; private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class); @GetMapping("/get_sessionId") @ApiOperation("获取微信openid") public Result get_sessionId(String code){ return userService.get_sessionId(code); } /** * 微信登陆 区分用户还是客户 如果都不是就注册成客户 * @param wxAuth * @return */ // @PostMapping("/authlogin") // @ApiOperation("微信授权登陆获取openid") // public Result authLogin(@RequestBody WXAuth wxAuth){ // try { // String json=wxService.wxDecrypt(wxAuth.getEncryptedData(),wxAuth.getIv(),wxAuth.getSessionKey()); // JSONObject jsonObject = JSON.parseObject(json); // String openid=jsonObject.getString("openId"); // log.info("获取到的用户openid:{}",openid); // if(openid!=null) { // return ResultUtil.success("success", openid); // } //// userLoginDTO.setTel(phoneNumber); // } catch (Exception e) { // e.printStackTrace(); // log.error("微信登陆异常:{}",e.getMessage()); // return ResultUtil.error(ExceptionEnum.WECHAT_LOGIN_ERROR); // } // return ResultUtil.error(ExceptionEnum.WECHAT_LOGIN_ERROR); // } /** * 客户登陆 * @param customer * @return */ public Result customer_login(Customer customer){ customer=customerService.findBymobileAndIsDelete(customer.getMobile()); if (ObjectUtils.isEmpty(customer)) throw new IllegalArgumentException("Customer object is null"); //为微信用户生成jwt令牌 Map claims=new HashMap<>(); //用openid还是id? //claims.put(JwtClaimsConstant.CUSTOMER_ID,customer.getOpenid()); claims.put(JwtClaimsConstant.USER_ID,customer.getId()); String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims); log.info("手机号码:{}",customer.getMobile()); CustomerLoginVO customerLoginVO = CustomerLoginVO.builder() .id(customer.getId()) .openid(customer.getOpenid()) .token(token) .name(customer.getName()) .is_customer(1) .build(); return ResultUtil.success("success", customerLoginVO); } /** * 用户登陆 * @param userLoginDTO * @return */ public Result user_login(UserLoginDTO userLoginDTO){ User user= userService.findByPhoneNumberAndIsDelete(userLoginDTO.getTel()); if (ObjectUtils.isEmpty(user)) throw new IllegalArgumentException("User object is null"); //为微信用户生成jwt令牌 Map claims=new HashMap<>(); claims.put(JwtClaimsConstant.USER_ID,user.getId()); String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims); log.info("手机号码:{}",userLoginDTO.getTel()); log.info("用户ID:{}",user.getId()); UserLoginVO userLoginVO = UserLoginVO.builder() .id(user.getId()) .openid(user.getOpenid()) .token(token) .is_customer(0) .username(user.getUsername()) .build(); return ResultUtil.success("success", userLoginVO); } /** * 注册客户 * @param customer * @return */ public Result register(Customer customer){ customer.setCreateTime(LocalDateTime.now() .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); customer.setUpdateTime(LocalDateTime.now() .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); customerService.createCustomer(customer); return this.customer_login(customer); } @PostMapping("/login_wechat") @ApiOperation("微信手机号登陆") public Result login(@RequestBody WeChatLoginDTO weChatLoginDTO){ String access_token=wxService.getAccessToken(); String phoneNumber=wxService.getUserPhoneNumber(access_token,weChatLoginDTO.getCode()); boolean user_is_exist = userService.existsByMobileAndIsDelete(phoneNumber); boolean customer_is_exist=customerService.existsBymobileAndIsDelete(phoneNumber); log.info("code:{}",phoneNumber); log.info("用户是否存在:{}",user_is_exist); if(!customer_is_exist&&!user_is_exist){ Customer customer = new Customer(); customer.setMobile(phoneNumber); customer.setOpenid(weChatLoginDTO.getOpenid()); return this.register(customer); }else if(customer_is_exist){ //存在客户 Customer customer=customerService.findBymobileAndIsDelete(phoneNumber); return this.customer_login(customer); }else { //存在用户 UserLoginDTO userLoginDTO = new UserLoginDTO(); userLoginDTO.setTel(phoneNumber); return this.user_login(userLoginDTO); } } @PostMapping("/login") @ApiOperation("账号登陆") public Result login(@RequestBody UserLoginDTO userLoginDTO){ String phoneNumber=userLoginDTO.getTel(); boolean user_is_exist = userService.existsByMobileAndIsDelete(phoneNumber); boolean customer_is_exist=customerService.existsBymobileAndIsDelete(phoneNumber); if(!customer_is_exist&&!user_is_exist){ Customer customer = new Customer(); customer.setMobile(phoneNumber); customer.setOpenid(userLoginDTO.getOpenid()); return this.register(customer); }else if(customer_is_exist){ Customer customer=customerService.findBymobileAndIsDelete(phoneNumber); return this.customer_login(customer); }else{ return this.user_login(userLoginDTO); } } }