diff --git a/cweb/src/main/java/com/cweb/config/WxMsgConfig.java b/cweb/src/main/java/com/cweb/config/WxMsgConfig.java deleted file mode 100644 index b882ab3..0000000 --- a/cweb/src/main/java/com/cweb/config/WxMsgConfig.java +++ /dev/null @@ -1,55 +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.hfkj.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)); - } - - } - - - - -} diff --git a/cweb/src/main/java/com/cweb/controller/BsUserController.java b/cweb/src/main/java/com/cweb/controller/BsUserController.java index d9ed37b..96ceef4 100644 --- a/cweb/src/main/java/com/cweb/controller/BsUserController.java +++ b/cweb/src/main/java/com/cweb/controller/BsUserController.java @@ -1,5 +1,7 @@ package com.cweb.controller; +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; +import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; import com.alibaba.fastjson.JSONObject; import com.alipay.api.request.AlipaySystemOauthTokenRequest; import com.alipay.api.response.AlipaySystemOauthTokenResponse; @@ -12,6 +14,7 @@ import com.hfkj.common.utils.HttpsUtils; import com.hfkj.common.utils.MemberValidateUtil; import com.hfkj.common.utils.RedisUtil; import com.hfkj.common.utils.ResponseMsgUtil; +import com.hfkj.config.WxMaConfiguration; import com.hfkj.model.ResponseData; import com.hfkj.model.SecUserSessionObject; import com.hfkj.model.UserSessionObject; @@ -28,6 +31,7 @@ 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; @@ -113,6 +117,59 @@ public class BsUserController { } } + @RequestMapping(value = "/wxMaHandleCode", method = RequestMethod.POST) + @ResponseBody + @ApiOperation(value = "微信小程序code解析") + public ResponseData wxMaHandleCode(@RequestBody JSONObject body) { + try { + if (body == null|| StringUtils.isBlank(body.getString("code"))) { + 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 = "/wxMaLogin", method = RequestMethod.POST) + @ResponseBody + @ApiOperation(value = "微信小程序手机号的登录") + public ResponseData wxMaLogin(@RequestBody JSONObject body) { + try { + if (body == null + || StringUtils.isBlank(body.getString("encryptedData")) + || StringUtils.isBlank(body.getString("iv")) + || StringUtils.isBlank(body.getString("openId"))) { + 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())) { + throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "登录失败! 用户手机号解析失败"); + } + Map other = new HashMap<>(); + return ResponseMsgUtil.success(userService.login(UserLoginPlatform.H5, UserLoginType.WECHAT_MA_PHONE, phoneNoInfo.getPhoneNumber(), other)); + + } catch (Exception e) { + return ResponseMsgUtil.exception(e); + } + } + @RequestMapping(value = "/queryUser", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "查询用户信息") diff --git a/cweb/src/main/java/com/cweb/controller/order/OrderPayController.java b/cweb/src/main/java/com/cweb/controller/order/OrderPayController.java index 7611ce0..5295060 100644 --- a/cweb/src/main/java/com/cweb/controller/order/OrderPayController.java +++ b/cweb/src/main/java/com/cweb/controller/order/OrderPayController.java @@ -7,6 +7,7 @@ import com.hfkj.common.exception.SysCode; import com.hfkj.common.utils.DateUtil; import com.hfkj.common.utils.RandomUtils; import com.hfkj.common.utils.ResponseMsgUtil; +import com.hfkj.config.CommonSysConst; import com.hfkj.entity.BsGasChannelConfig; import com.hfkj.entity.BsMerchant; import com.hfkj.entity.BsMerchantPayConfig; @@ -111,8 +112,13 @@ public class OrderPayController { ledgerAccountFlag = payConfig.getLedgerAccountFlag(); } order.setPayType(OrderPayTypeEnum.type1.getCode()); + + // appIdType 1: 公众号 2:小程序 + if (body.getInteger("appIdType") != null && body.getInteger("appIdType") == 2) { + body.put("appId", CommonSysConst.getSysConfig().getWxMaAppId()); + } // 请求支付渠道 - Map preorder = HuiPayService.preorder(payMerNo, payMerKey, body.getString("openId"), ledgerAccountFlag, order); + Map preorder = HuiPayService.preorder(payMerNo, payMerKey, body.getString("appId"), body.getString("openId"), ledgerAccountFlag, order); // 交易入账 BsOrderSettle orderSettle = orderSettleService.getData(order.getOrderNo()); if (orderSettle == null) { @@ -166,7 +172,7 @@ public class OrderPayController { } order.setPayType(OrderPayTypeEnum.type2.getCode()); // 请求支付渠道 - Map preorder = HuiPayService.preorder(merPay.getChannelMerNo(), merPay.getChannelMerKey(), body.getString("userId"), merPay.getLedgerAccountFlag(), order); + Map preorder = HuiPayService.preorder(merPay.getChannelMerNo(), merPay.getChannelMerKey(), null, body.getString("userId"), merPay.getLedgerAccountFlag(), order); // 交易入账 BsOrderSettle orderSettle = orderSettleService.getData(order.getOrderNo()); diff --git a/service/src/main/java/com/hfkj/config/CommonSysConfig.java b/service/src/main/java/com/hfkj/config/CommonSysConfig.java index 21fa1d9..72e9a16 100644 --- a/service/src/main/java/com/hfkj/config/CommonSysConfig.java +++ b/service/src/main/java/com/hfkj/config/CommonSysConfig.java @@ -14,6 +14,16 @@ public class CommonSysConfig { * 文件存放地址 可访问 */ private String filesystem; + + /** + * 微信小程序APPID + */ + private String wxMaAppId; + /** + * 微信小程序Secret + */ + private String wxMaAppSecret; + /** * 能链【团油】请求地址 */ diff --git a/cweb/src/main/java/com/cweb/config/WxMaConfiguration.java b/service/src/main/java/com/hfkj/config/WxMaConfiguration.java similarity index 66% rename from cweb/src/main/java/com/cweb/config/WxMaConfiguration.java rename to service/src/main/java/com/hfkj/config/WxMaConfiguration.java index b877321..e3f6ef3 100644 --- a/cweb/src/main/java/com/cweb/config/WxMaConfiguration.java +++ b/service/src/main/java/com/hfkj/config/WxMaConfiguration.java @@ -1,4 +1,4 @@ -package com.cweb.config; +package com.hfkj.config; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; @@ -14,20 +14,17 @@ public class WxMaConfiguration { public static WxMaService getMaService() { if (maService == null) { - throw new IllegalArgumentException(String.format("未找到对应的配置,请核实!")); + 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()); - + WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); + config.setAppid(CommonSysConst.getSysConfig().getWxMaAppId()); + config.setSecret(CommonSysConst.getSysConfig().getWxMaAppSecret()); maService = new WxMaServiceImpl(); - maService.setWxMaConfig(config);*/ + maService.setWxMaConfig(config); } } diff --git a/service/src/main/java/com/hfkj/pay/HuiPayService.java b/service/src/main/java/com/hfkj/pay/HuiPayService.java index 0ebe653..07163e3 100644 --- a/service/src/main/java/com/hfkj/pay/HuiPayService.java +++ b/service/src/main/java/com/hfkj/pay/HuiPayService.java @@ -14,6 +14,7 @@ import com.hfkj.entity.BsOrderSettle; import com.hfkj.entity.BsOrderSettleLedger; import com.hfkj.model.order.OrderModel; import com.hfkj.sysenum.order.OrderPayTypeEnum; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,7 +38,7 @@ public class HuiPayService { * @return * @throws Exception */ - public static Map preorder(String merNo,String merKey,String openId,boolean profitSharing,OrderModel order) throws Exception { + public static Map preorder(String merNo,String merKey,String appId,String openId,boolean profitSharing,OrderModel order) throws Exception { try { log.info("=============== start 惠支付 start =================="); Map param = new HashMap<>(); @@ -51,6 +52,9 @@ public class HuiPayService { } param.put("totalAmount", order.getPayRealPrice()); param.put("profitSharing", profitSharing?1:0); + if (StringUtils.isNotBlank(appId)) { + param.put("subAppid", appId); + } param.put("subject", "在线加油"); param.put("userId", openId); param.put("notifyUrl", CommonSysConst.getSysConfig().getHuiPayPreorderNotifyUrl()); diff --git a/service/src/main/resources/dev/commonConfig.properties b/service/src/main/resources/dev/commonConfig.properties index 56d2d9c..0db1aa5 100644 --- a/service/src/main/resources/dev/commonConfig.properties +++ b/service/src/main/resources/dev/commonConfig.properties @@ -1,6 +1,9 @@ filesystem=/home/project/oil/filesystem huiPayPreorderNotifyUrl=https://test-oil.dctpay.com/crest/notify/huipay domainName=https://test-oil.dctpay.com + +wxMaAppId=wx8d49e2f83025229d +wxMaAppSecret=d8d6dcaef77d3b659258a01b5ddba5df # newLinkReqUrl=https://hcs.czb365.com # newLinkAppKey=305490138943968 # newLinkAppSecret=dbf7dca3de20c2f2a41fd64cfb682be8 diff --git a/service/src/main/resources/prod/commonConfig.properties b/service/src/main/resources/prod/commonConfig.properties index 9e25961..98cbe3f 100644 --- a/service/src/main/resources/prod/commonConfig.properties +++ b/service/src/main/resources/prod/commonConfig.properties @@ -1,6 +1,10 @@ filesystem=/home/project/oil/filesystem huiPayPreorderNotifyUrl=https://oil.dctpay.com/crest/notify/huipay domainName=https://oil.dctpay.com + +wxMaAppId=wx8d49e2f83025229d +wxMaAppSecret=d8d6dcaef77d3b659258a01b5ddba5df + newLinkReqUrl=https://hcs.czb365.com newLinkAppKey=305490138943968 newLinkAppSecret=dbf7dca3de20c2f2a41fd64cfb682be8