From 3da4b9061e3f34b3e413b5f7e0466e977692c58b Mon Sep 17 00:00:00 2001 From: Sum1Dream <418471657@qq.com> Date: Fri, 3 Feb 2023 17:14:20 +0800 Subject: [PATCH] Merge branch 'master' into 2.0-dev MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * master: 修改配置星巴克 肯德基 会员充值 话费 微信商户号 修改配置星巴克 肯德基 会员充值 话费 微信商户号 # Conflicts: # hai-cweb/src/main/java/com/cweb/controller/pay/CzOrderController.java # hai-cweb/src/main/java/com/cweb/controller/pay/ThirdOrderController.java --- .../com/cweb/config/WxMaConfiguration.java | 33 ----- .../java/com/cweb/config/WxMsgConfig.java | 120 ------------------ .../controller/HighThirdPartyController.java | 4 +- .../com/cweb/controller/NotifyController.java | 2 +- .../com/cweb/controller/WechatController.java | 3 +- .../com/hai/config/ThirdProductConfig.java | 28 +++- .../impl/OrderCreateHandleServiceImpl.java | 2 +- .../service/impl/HighOrderServiceImpl.java | 2 +- .../com/web/config/WxMaConfiguration.java | 31 ----- .../com/web/controller/LoginController.java | 2 +- 10 files changed, 31 insertions(+), 196 deletions(-) delete mode 100644 hai-cweb/src/main/java/com/cweb/config/WxMaConfiguration.java delete mode 100644 hai-cweb/src/main/java/com/cweb/config/WxMsgConfig.java delete mode 100644 hai-user/src/main/java/com/web/config/WxMaConfiguration.java diff --git a/hai-cweb/src/main/java/com/cweb/config/WxMaConfiguration.java b/hai-cweb/src/main/java/com/cweb/config/WxMaConfiguration.java deleted file mode 100644 index 11709bcf..00000000 --- a/hai-cweb/src/main/java/com/cweb/config/WxMaConfiguration.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.cweb.config; - -import cn.binarywang.wx.miniapp.api.WxMaService; -import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; -import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; -import org.springframework.context.annotation.Configuration; - -import javax.annotation.PostConstruct; - -@Configuration -public class WxMaConfiguration { - - private static WxMaService maService; - - public static WxMaService getMaService() { - if (maService == null) { - throw new IllegalArgumentException(String.format("未找到对应的配置,请核实!")); - } - - return maService; - } - - @PostConstruct - public void init() { - WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); - config.setAppid(SysConst.getSysConfig().getWxAppId()); - config.setSecret(SysConst.getSysConfig().getWxAppSecret()); - - maService = new WxMaServiceImpl(); - maService.setWxMaConfig(config); - } - -} diff --git a/hai-cweb/src/main/java/com/cweb/config/WxMsgConfig.java b/hai-cweb/src/main/java/com/cweb/config/WxMsgConfig.java deleted file mode 100644 index df40b7f9..00000000 --- a/hai-cweb/src/main/java/com/cweb/config/WxMsgConfig.java +++ /dev/null @@ -1,120 +0,0 @@ -package com.cweb.config; - -import cn.binarywang.wx.miniapp.api.WxMaMsgService; -import cn.binarywang.wx.miniapp.api.WxMaService; -import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage; -import com.hai.common.utils.DateUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.*; - -public class WxMsgConfig { - - private static Logger log = LoggerFactory.getLogger(WxMsgConfig.class); - - public static void pushOneUser(String orderName , String price , String orderNo , Date payTime , String remark , Long orderId , String openId) { - - try { - List list = new ArrayList<>(); - - Map m = new HashMap<>(); - - m.put("thing1", orderName); - m.put("amount2", price + "元"); - m.put("character_string3", orderNo); - m.put("time4", DateUtil.date2String(payTime , "yyyy年MM月dd日 HH:mm:ss")); - m.put("thing6", remark); - - for (String key: m.keySet()) { - WxMaSubscribeMessage.Data msgElement = new WxMaSubscribeMessage.Data(); - msgElement.setName(key); - msgElement.setValue(m.get(key)); - list.add(msgElement); - } - - WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage(); - subscribeMessage.setToUser(openId); // 小程序openId - subscribeMessage.setTemplateId("oUvaCPeeOg4wH6HTvCcSabU6FnzXUXOBXsqBYAPOV-U"); - subscribeMessage.setData(list); - subscribeMessage.setPage("pages/user/order_details/order_details?id=" + orderId); - subscribeMessage.setMiniprogramState("developer"); - - final WxMaService wxService = WxMaConfiguration.getMaService(); - WxMaMsgService maMsgService = wxService.getMsgService(); - maMsgService.sendSubscribeMsg(subscribeMessage); - } catch (Exception e) { - log.error(String.valueOf(e)); - } - - } - - public static void rechargedSuccess(String orderName , String price , String orderNo , Date payTime , String payType , String openId) { - - try { - List list = new ArrayList<>(); - - Map m = new HashMap<>(); - - m.put("character_string1", orderNo); // 订单号 - m.put("thing11", orderName); // 充值项目 - m.put("thing10", payType); // 支付方式 - m.put("amount8", price); // 支付金额 - m.put("date5", DateUtil.date2String(payTime , "yyyy年MM月dd日 HH:mm:ss")); - - for (String key: m.keySet()) { - WxMaSubscribeMessage.Data msgElement = new WxMaSubscribeMessage.Data(); - msgElement.setName(key); - msgElement.setValue(m.get(key)); - list.add(msgElement); - } - - WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage(); - subscribeMessage.setToUser(openId); // 小程序openId - subscribeMessage.setTemplateId("ZO6kC3oJv0zj9QzL0htsF-mM9vAElcgPZEcUGglxsoU"); - subscribeMessage.setData(list); - - final WxMaService wxService = WxMaConfiguration.getMaService(); - WxMaMsgService maMsgService = wxService.getMsgService(); - maMsgService.sendSubscribeMsg(subscribeMessage); - } catch (Exception e) { - log.error(String.valueOf(e)); - } - - } - - public static void rechargedFail(String orderName , String price , String openId) { - - try { - List list = new ArrayList<>(); - - Map m = new HashMap<>(); - - m.put("thing1", orderName); // 订单内容 - m.put("thing2", "充值通道爆满!"); // 失败原因 - m.put("amount3", price); // 退款金额 - m.put("thing4", "退款将在1-3日内原路退回"); // 温馨提示 - - for (String key: m.keySet()) { - WxMaSubscribeMessage.Data msgElement = new WxMaSubscribeMessage.Data(); - msgElement.setName(key); - msgElement.setValue(m.get(key)); - list.add(msgElement); - } - - WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage(); - subscribeMessage.setToUser(openId); // 小程序openId - subscribeMessage.setTemplateId("xjacmzuWSRdiKGz4IJVeuvyGE0vOhoy0JVQPgOr-PuI"); - subscribeMessage.setData(list); - - final WxMaService wxService = WxMaConfiguration.getMaService(); - WxMaMsgService maMsgService = wxService.getMsgService(); - maMsgService.sendSubscribeMsg(subscribeMessage); - } catch (Exception e) { - log.error(String.valueOf(e)); - } - - } - - -} diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighThirdPartyController.java b/hai-cweb/src/main/java/com/cweb/controller/HighThirdPartyController.java index 3eebe97d..0d6f2804 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighThirdPartyController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighThirdPartyController.java @@ -302,10 +302,12 @@ public class HighThirdPartyController { @ApiOperation(value = "查询第三方产品详情") public ResponseData getThirdPartyByDetail(@RequestParam(name = "platformId", required = true) Integer platformId, @RequestParam(name = "productType", required = true) Integer productType, + @RequestParam(name = "productId", required = false) Long productId, + @RequestParam(name = "orderNo", required = false) String orderNo, @RequestParam(name = "companyId", required = true) Long companyId) { try { - return ResponseMsgUtil.success(thirdProductConfig.getThirdPartyByDetail(platformId , productType , companyId)); + return ResponseMsgUtil.success(thirdProductConfig.getThirdPartyByDetail(platformId , productType , companyId , productId , orderNo)); } catch (Exception e) { log.error("HighUserCardController --> oilCardRefund() error!", e); diff --git a/hai-cweb/src/main/java/com/cweb/controller/NotifyController.java b/hai-cweb/src/main/java/com/cweb/controller/NotifyController.java index 25f10dec..1c92442c 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/NotifyController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/NotifyController.java @@ -6,7 +6,7 @@ import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; import com.alibaba.fastjson.JSONObject; import com.cweb.config.SysConst; -import com.cweb.config.WxMaConfiguration; + import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; diff --git a/hai-cweb/src/main/java/com/cweb/controller/WechatController.java b/hai-cweb/src/main/java/com/cweb/controller/WechatController.java index 80d83863..17e3421a 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/WechatController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/WechatController.java @@ -7,7 +7,7 @@ import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; import com.alibaba.fastjson.JSONObject; import com.cweb.config.SessionKeyCache; import com.cweb.config.SysConst; -import com.cweb.config.WxMaConfiguration; + import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @@ -16,6 +16,7 @@ import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; import com.hai.common.security.*; import com.hai.common.utils.*; +import com.hai.config.WxMaConfiguration; import com.hai.entity.HighUser; import com.hai.model.HighUserModel; import com.hai.model.ResponseData; diff --git a/hai-service/src/main/java/com/hai/config/ThirdProductConfig.java b/hai-service/src/main/java/com/hai/config/ThirdProductConfig.java index dbfc6271..ee304935 100644 --- a/hai-service/src/main/java/com/hai/config/ThirdProductConfig.java +++ b/hai-service/src/main/java/com/hai/config/ThirdProductConfig.java @@ -11,6 +11,7 @@ import com.hai.entity.HighOrder; import com.hai.order.model.CreateOrderChildModel; import com.hai.service.ApiProductService; import com.hai.service.BsConfigService; +import com.hai.service.HighOrderService; import org.springframework.stereotype.Component; import javax.annotation.Resource; @@ -33,6 +34,9 @@ public class ThirdProductConfig { @Resource private ApiProductService apiProductService; + @Resource + private HighOrderService highOrderService; + // 产品折扣比例 public BigDecimal getProductConfigNum(Integer productType , Long companyId) { @@ -49,7 +53,7 @@ public class ThirdProductConfig { } // 产品折扣比例 - public BsProductConfig getProductConfig(Integer productType , Long companyId) { + public BsProductConfig getProductConfig(Integer productType , Long companyId , Long productId) { Map listMap = new HashMap<>(); listMap.put("productType", productType); @@ -63,10 +67,10 @@ public class ThirdProductConfig { return null; } - public JSONObject getThirdPartyByDetail(Integer platformId , Integer productType , Long companyId) { - BsProductConfig productConfig = getProductConfig(productType, companyId); + public JSONObject getThirdPartyByDetail(Integer platformId , Integer productType , Long companyId , Long productId , String orderNo) { + BsProductConfig productConfig = getProductConfig(productType, companyId , productId); - if (productConfig == null) { + if (productConfig == null && productType != 9) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "暂未配置当前内容"); } @@ -75,13 +79,25 @@ public class ThirdProductConfig { Map listMap = new HashMap<>(); listMap.put("productType", productType); listMap.put("returnType", 1); - listMap.put("sourceId", productConfig.getId()); + if (productType == 9) { + if (orderNo != null) { + HighOrder highOrder = highOrderService.getOrderByOrderNo(orderNo); + listMap.put("sourceId", highOrder.getHighChildOrderList().get(0).getGoodsId()); + } + + } else { + listMap.put("sourceId", productConfig.getId()); + } + listMap.put("platformId", platformId); if (bsConfigService.getProductDiscountByMap(listMap) != null) { object.put("integralDiscount" , bsConfigService.getProductDiscountByMap(listMap).getDiscount()); } - object.put("priceDiscount" , productConfig.getDiscount()); + if (productConfig != null) { + object.put("priceDiscount" , productConfig.getDiscount()); + } + object.put("productPayType" , bsConfigService.getProductPayTypeByMap(listMap)); object.put("productPlatform" , bsConfigService.getProductPlatformByMap(listMap)); diff --git a/hai-service/src/main/java/com/hai/order/service/impl/OrderCreateHandleServiceImpl.java b/hai-service/src/main/java/com/hai/order/service/impl/OrderCreateHandleServiceImpl.java index 940d60f0..d399c5cd 100644 --- a/hai-service/src/main/java/com/hai/order/service/impl/OrderCreateHandleServiceImpl.java +++ b/hai-service/src/main/java/com/hai/order/service/impl/OrderCreateHandleServiceImpl.java @@ -614,7 +614,7 @@ public class OrderCreateHandleServiceImpl implements OrderCreateHandleService { String orderNo; // 查询详单订单的实际 - JSONObject productDetail = thirdProductConfig.getThirdPartyByDetail(object.getInteger("platformId"), object.getInteger("productType"), object.getLong("companyId")); + JSONObject productDetail = thirdProductConfig.getThirdPartyByDetail(object.getInteger("platformId"), object.getInteger("productType"), object.getLong("companyId") , object.getLong("productId") , null); // 获取token 千猪请求接口token JSONObject userToken = QianZhuConfig.getTokenV2(user.getId().toString(), user.getName(), user.getPhone()); diff --git a/hai-service/src/main/java/com/hai/service/impl/HighOrderServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighOrderServiceImpl.java index b30bdde1..292b9f57 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighOrderServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighOrderServiceImpl.java @@ -1426,7 +1426,7 @@ public class HighOrderServiceImpl implements HighOrderService { BigDecimal marketPrice = new BigDecimal(0); // 查询详单订单的实际 - JSONObject productDetail = thirdProductConfig.getThirdPartyByDetail(object.getInteger("platformId"), object.getInteger("productType"), object.getLong("companyId")); + JSONObject productDetail = thirdProductConfig.getThirdPartyByDetail(object.getInteger("platformId"), object.getInteger("productType"), object.getLong("companyId") , object.getLong("productId") , null); highOrder.setProductType(object.getInteger("productType")); diff --git a/hai-user/src/main/java/com/web/config/WxMaConfiguration.java b/hai-user/src/main/java/com/web/config/WxMaConfiguration.java deleted file mode 100644 index c960714d..00000000 --- a/hai-user/src/main/java/com/web/config/WxMaConfiguration.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.web.config; - -import cn.binarywang.wx.miniapp.api.WxMaService; -import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; -import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; -import org.springframework.context.annotation.Configuration; - -import javax.annotation.PostConstruct; - -@Configuration -public class WxMaConfiguration { - - private static WxMaService maService; - - public static WxMaService getMaService() { - if (maService == null) { - throw new IllegalArgumentException(String.format("未找到对应的配置,请核实!")); - } - return maService; - } - - @PostConstruct - public void init() { - WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); - config.setAppid(SysConst.getSysConfig().getWxMiniAppId()); - config.setSecret(SysConst.getSysConfig().getWxMiniAppSecret()); - maService = new WxMaServiceImpl(); - maService.setWxMaConfig(config); - } - -} diff --git a/hai-user/src/main/java/com/web/controller/LoginController.java b/hai-user/src/main/java/com/web/controller/LoginController.java index 084821df..ffdbfef3 100644 --- a/hai-user/src/main/java/com/web/controller/LoginController.java +++ b/hai-user/src/main/java/com/web/controller/LoginController.java @@ -1 +1 @@ -package com.web.controller; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; import com.alibaba.fastjson.JSONObject; import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; import com.hai.common.security.SessionObject; import com.hai.common.security.UserCenter; import com.hai.common.utils.*; import com.hai.config.UnionUserConfig; import com.hai.entity.HighUser; import com.hai.enum_type.LoginPlatform; import com.hai.model.HighUserModel; import com.hai.model.ResponseData; import com.hai.service.HighUserService; import com.web.config.WxMaConfiguration; import com.web.type.LoginType; import com.hai.enum_type.RedisFileFolder; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @Auther: 胡锐 * @Description: * @Date: 2021/3/26 23:08 */ @Controller @RequestMapping(value = "/login") @Api(value = "登录业务") public class LoginController { private static Logger log = LoggerFactory.getLogger(LoginController.class); @Resource private RedisUtil redisUtil; @Resource private HighUserService userService; @Resource private UserCenter userCenter; @Resource private WxMaConfiguration wxMaConfiguration; @Resource private UnionUserConfig unionUserConfig; @RequestMapping(value = "/phone", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "手机号登录") public ResponseData phone(@RequestBody JSONObject body, HttpServletRequest request, HttpServletResponse response) { try { if (body == null || StringUtils.isBlank(body.getString("platform")) || StringUtils.isBlank(body.getString("type")) || StringUtils.isBlank(body.getString("phone")) ) { log.error("LoginController --> phone() error!", "请求参数校验失败"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String phone = body.getString("phone"); // 客户端 LoginPlatform platform = LoginPlatform.getDataByType(body.getString("platform")); if (platform == null) { log.error("LoginController --> phone() error!", "未知客户端"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知客户端"); } // 校验手机号格式 if (MemberValidateUtil.validatePhone(phone) == false) { log.error("LoginController --> phone() error!", "请输入正确的手机号"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); } // 登录类型 LoginType loginType = LoginType.getDataByType(body.getString("type")); if (loginType == null) { log.error("LoginController --> phone() error!", "未知登录类型;" + body.getString("type")); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知登录类型"); } if (body.getString("type").equals(LoginType.SMS.getCode())) { if (StringUtils.isBlank(body.getString("smsCode"))) { log.error("LoginController --> phone() error!", "请输入短信验证码"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入短信验证码"); } // 手机号的验证码 Object phoneCodeObject = redisUtil.get(RedisFileFolder.SMS_CODE.getCode() + phone); if (phoneCodeObject == null) { log.error("LoginController --> phone() error!", "短信验证码错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "短信验证码错误"); } if (!body.getString("smsCode").equals(phoneCodeObject.toString())) { log.error("LoginController --> phone() error!", "短信验证码错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "短信验证码错误"); } redisUtil.del(RedisFileFolder.SMS_CODE.getCode() + phone); return ResponseMsgUtil.success(userService.loginAndRegister(platform, phone, body.getLong("popularizeUserId"), new HashMap<>(), request, response)); } throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "登录失败"); } catch (Exception e) { log.error("LoginController --> phone() error!", e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/wxMiniHandleCode", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "微信小程序code解析") public ResponseData wxMiniHandleCode(@RequestBody JSONObject body) { try { if (body == null|| StringUtils.isBlank(body.getString("code"))) { log.error("LoginController --> phone() error!", "请求参数校验失败"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } //请求微信api,获取用户session_key以及openId WxMaJscode2SessionResult session = WxMaConfiguration.getMaService().jsCode2SessionInfo(body.getString("code")); //保存小程序用户登录的openId及sessionKey信息 redisUtil.hset("WX_OPENID_SESSION_REDIS", session.getOpenid(), session); JSONObject jo = new JSONObject(); jo.put("openId", session.getOpenid()); return ResponseMsgUtil.success(jo); } catch (Exception e) { return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/wxMini", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "微信小程序") public ResponseData wxMini(@RequestBody JSONObject body,HttpServletRequest request, HttpServletResponse response) { try { if (body == null || StringUtils.isBlank(body.getString("encryptedData")) || StringUtils.isBlank(body.getString("iv")) || StringUtils.isBlank(body.getString("openId"))) { log.error("LoginController --> phone() error!", "请求参数校验失败"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String encryptedData = body.getString("encryptedData").replace(" ", "+"); String iv = body.getString("iv").replace(" ", "+"); //请求微信api,获取用户session_key以及openId Object skObject = redisUtil.hget("WX_OPENID_SESSION_REDIS", body.getString("openId")); if (skObject == null) { throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_LOGIN_ERROR); } WxMaJscode2SessionResult session = (WxMaJscode2SessionResult) skObject; WxMaPhoneNumberInfo phoneNoInfo = WxMaConfiguration .getMaService() .getUserService() .getPhoneNoInfo(session.getSessionKey(), encryptedData, iv); if (phoneNoInfo == null || StringUtils.isEmpty(phoneNoInfo.getPurePhoneNumber())) { log.error("LoginController --> wxMini() error!", "登录失败! 用户手机号解析失败"); throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "登录失败! 用户手机号解析失败"); } Map other = new HashMap<>(); other.put("openId", session.getOpenid()); return ResponseMsgUtil.success(userService.loginAndRegister(LoginPlatform.WXAPPLETS, phoneNoInfo.getPurePhoneNumber(), body.getLong("popularizeUserId"), other, request, response)); } catch (Exception e) { log.error("LoginController --> wxMini() error!", e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/union", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "银联授权手机号登录接口") public ResponseData union(@RequestBody JSONObject body,HttpServletRequest request, HttpServletResponse response) { try { if (body == null || StringUtils.isBlank(body.getString("code"))) { log.error("LoginController --> phone() error!", "请求参数校验失败"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } // 获取应用访问令牌 String backendToken = unionUserConfig.getBackendToken(); // 获取授权访问令牌 JSONObject token = unionUserConfig.getAccessToken(backendToken, body.getString("code")); if (token == null || !token.getString("resp").equals("00")) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, token.getString("msg")); } // 获取用户手机号 JSONObject userMobile = unionUserConfig.getUserMobile(backendToken, token.getJSONObject("params").getString("accessToken"), token.getJSONObject("params").getString("openId")); if (userMobile == null || !userMobile.getString("resp").equals("00")) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, userMobile.getString("msg")); } // 解密,获取手机号 String mobile = UnionUtils.getDecryptedValue(userMobile.getJSONObject("params").getString("mobile"), unionUserConfig.getRsaKey()); Map other = new HashMap<>(); other.put("unionId", token.getJSONObject("params").getString("openId")); other.put("unionUnionId", token.getJSONObject("params").getString("unionId")); return ResponseMsgUtil.success(userService.loginAndRegister(LoginPlatform.UNIONPAY, mobile, body.getLong("popularizeUserId"), other, request, response)); } catch (Exception e) { return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/out", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "退出登录") public ResponseData out(HttpServletRequest request, HttpServletResponse response) { try { HighUserModel userInfoModel = userCenter.getSessionModel(HighUserModel.class); if (userInfoModel == null) { log.error("CoresController --> outLogin() error!", "用户身份错误或已过期"); throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, ""); } userCenter.remove(request, response); return ResponseMsgUtil.success("退出登录成功"); } catch (Exception e) { log.error("CoresController --> outLogin() error!", e); return ResponseMsgUtil.exception(e); } } } \ No newline at end of file +package com.web.controller; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; import com.alibaba.fastjson.JSONObject; import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; import com.hai.common.security.UserCenter; import com.hai.common.utils.*; import com.hai.config.UnionUserConfig; import com.hai.config.WxMaConfiguration; import com.hai.enum_type.LoginPlatform; import com.hai.model.HighUserModel; import com.hai.model.ResponseData; import com.hai.service.HighUserService; import com.web.type.LoginType; import com.hai.enum_type.RedisFileFolder; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; /** * @Auther: 胡锐 * @Description: * @Date: 2021/3/26 23:08 */ @Controller @RequestMapping(value = "/login") @Api(value = "登录业务") public class LoginController { private static Logger log = LoggerFactory.getLogger(LoginController.class); @Resource private RedisUtil redisUtil; @Resource private HighUserService userService; @Resource private UserCenter userCenter; @Resource private WxMaConfiguration wxMaConfiguration; @Resource private UnionUserConfig unionUserConfig; @RequestMapping(value = "/phone", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "手机号登录") public ResponseData phone(@RequestBody JSONObject body, HttpServletRequest request, HttpServletResponse response) { try { if (body == null || StringUtils.isBlank(body.getString("platform")) || StringUtils.isBlank(body.getString("type")) || StringUtils.isBlank(body.getString("phone")) ) { log.error("LoginController --> phone() error!", "请求参数校验失败"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String phone = body.getString("phone"); // 客户端 LoginPlatform platform = LoginPlatform.getDataByType(body.getString("platform")); if (platform == null) { log.error("LoginController --> phone() error!", "未知客户端"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知客户端"); } // 校验手机号格式 if (MemberValidateUtil.validatePhone(phone) == false) { log.error("LoginController --> phone() error!", "请输入正确的手机号"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); } // 登录类型 LoginType loginType = LoginType.getDataByType(body.getString("type")); if (loginType == null) { log.error("LoginController --> phone() error!", "未知登录类型;" + body.getString("type")); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知登录类型"); } if (body.getString("type").equals(LoginType.SMS.getCode())) { if (StringUtils.isBlank(body.getString("smsCode"))) { log.error("LoginController --> phone() error!", "请输入短信验证码"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入短信验证码"); } // 手机号的验证码 Object phoneCodeObject = redisUtil.get(RedisFileFolder.SMS_CODE.getCode() + phone); if (phoneCodeObject == null) { log.error("LoginController --> phone() error!", "短信验证码错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "短信验证码错误"); } if (!body.getString("smsCode").equals(phoneCodeObject.toString())) { log.error("LoginController --> phone() error!", "短信验证码错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "短信验证码错误"); } redisUtil.del(RedisFileFolder.SMS_CODE.getCode() + phone); return ResponseMsgUtil.success(userService.loginAndRegister(platform, phone, body.getLong("popularizeUserId"), new HashMap<>(), request, response)); } throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "登录失败"); } catch (Exception e) { log.error("LoginController --> phone() error!", e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/wxMiniHandleCode", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "微信小程序code解析") public ResponseData wxMiniHandleCode(@RequestBody JSONObject body) { try { if (body == null|| StringUtils.isBlank(body.getString("code"))) { log.error("LoginController --> phone() error!", "请求参数校验失败"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } //请求微信api,获取用户session_key以及openId WxMaJscode2SessionResult session = WxMaConfiguration.getMaService().jsCode2SessionInfo(body.getString("code")); //保存小程序用户登录的openId及sessionKey信息 redisUtil.hset("WX_OPENID_SESSION_REDIS", session.getOpenid(), session); JSONObject jo = new JSONObject(); jo.put("openId", session.getOpenid()); return ResponseMsgUtil.success(jo); } catch (Exception e) { return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/wxMini", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "微信小程序") public ResponseData wxMini(@RequestBody JSONObject body,HttpServletRequest request, HttpServletResponse response) { try { if (body == null || StringUtils.isBlank(body.getString("encryptedData")) || StringUtils.isBlank(body.getString("iv")) || StringUtils.isBlank(body.getString("openId"))) { log.error("LoginController --> phone() error!", "请求参数校验失败"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } String encryptedData = body.getString("encryptedData").replace(" ", "+"); String iv = body.getString("iv").replace(" ", "+"); //请求微信api,获取用户session_key以及openId Object skObject = redisUtil.hget("WX_OPENID_SESSION_REDIS", body.getString("openId")); if (skObject == null) { throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_LOGIN_ERROR); } WxMaJscode2SessionResult session = (WxMaJscode2SessionResult) skObject; WxMaPhoneNumberInfo phoneNoInfo = WxMaConfiguration .getMaService() .getUserService() .getPhoneNoInfo(session.getSessionKey(), encryptedData, iv); if (phoneNoInfo == null || StringUtils.isEmpty(phoneNoInfo.getPurePhoneNumber())) { log.error("LoginController --> wxMini() error!", "登录失败! 用户手机号解析失败"); throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "登录失败! 用户手机号解析失败"); } Map other = new HashMap<>(); other.put("openId", session.getOpenid()); return ResponseMsgUtil.success(userService.loginAndRegister(LoginPlatform.WXAPPLETS, phoneNoInfo.getPurePhoneNumber(), body.getLong("popularizeUserId"), other, request, response)); } catch (Exception e) { log.error("LoginController --> wxMini() error!", e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/union", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "银联授权手机号登录接口") public ResponseData union(@RequestBody JSONObject body,HttpServletRequest request, HttpServletResponse response) { try { if (body == null || StringUtils.isBlank(body.getString("code"))) { log.error("LoginController --> phone() error!", "请求参数校验失败"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } // 获取应用访问令牌 String backendToken = unionUserConfig.getBackendToken(); // 获取授权访问令牌 JSONObject token = unionUserConfig.getAccessToken(backendToken, body.getString("code")); if (token == null || !token.getString("resp").equals("00")) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, token.getString("msg")); } // 获取用户手机号 JSONObject userMobile = unionUserConfig.getUserMobile(backendToken, token.getJSONObject("params").getString("accessToken"), token.getJSONObject("params").getString("openId")); if (userMobile == null || !userMobile.getString("resp").equals("00")) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, userMobile.getString("msg")); } // 解密,获取手机号 String mobile = UnionUtils.getDecryptedValue(userMobile.getJSONObject("params").getString("mobile"), unionUserConfig.getRsaKey()); Map other = new HashMap<>(); other.put("unionId", token.getJSONObject("params").getString("openId")); other.put("unionUnionId", token.getJSONObject("params").getString("unionId")); return ResponseMsgUtil.success(userService.loginAndRegister(LoginPlatform.UNIONPAY, mobile, body.getLong("popularizeUserId"), other, request, response)); } catch (Exception e) { return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/out", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "退出登录") public ResponseData out(HttpServletRequest request, HttpServletResponse response) { try { HighUserModel userInfoModel = userCenter.getSessionModel(HighUserModel.class); if (userInfoModel == null) { log.error("CoresController --> outLogin() error!", "用户身份错误或已过期"); throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, ""); } userCenter.remove(request, response); return ResponseMsgUtil.success("退出登录成功"); } catch (Exception e) { log.error("CoresController --> outLogin() error!", e); return ResponseMsgUtil.exception(e); } } } \ No newline at end of file