From f495c1655f3c5ba26ea2bf4492b72e61b9ac18ce Mon Sep 17 00:00:00 2001 From: "925116093-qq.com" <925116093@qq.com> Date: Tue, 17 Jun 2025 15:41:15 +0800 Subject: [PATCH] 202506171541 --- .../system/controller/AppletController.java | 110 +++++++++-- .../controllerUtil/AppletControllerUtil.java | 121 +++++++++++- .../system/controllerUtil/GaoDeMapUtil.java | 113 +++++++++++ .../system/controllerUtil/WXsendMsgUtil.java | 177 +++++++++++++----- .../system/controllerUtil/WechatPayUtil.java | 4 +- .../domain/AppleDoMain/TemplateData.java | 19 ++ .../system/domain/AppleDoMain/WxMssVo.java | 42 +++++ 7 files changed, 510 insertions(+), 76 deletions(-) create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/GaoDeMapUtil.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/domain/AppleDoMain/TemplateData.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/domain/AppleDoMain/WxMssVo.java diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/controller/AppletController.java b/ruoyi-system/src/main/java/com/ruoyi/system/controller/AppletController.java index 6eb9d80..504b512 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/controller/AppletController.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/controller/AppletController.java @@ -1895,9 +1895,9 @@ public class AppletController extends BaseController { if (!(Boolean) userValidation.get("valid")) { return error("用户未登录或token无效"); } - + WechatPayUtil wechatPayUtil = new WechatPayUtil(); // 2. 调用微信支付统一下单 - Map payResult = WechatPayUtil.unifiedOrder(params); + Map payResult = wechatPayUtil.unifiedOrder((String) params.get("openid"), (String) params.get("orderNo"), (int) params.get("totalFee"), (String) params.get("body"), (String) params.get("notifyUrl"), (String) params.get("attach")); // 3. 返回结果 boolean success = (Boolean) payResult.get("success"); @@ -1928,9 +1928,9 @@ public class AppletController extends BaseController { if (!(Boolean) userValidation.get("valid")) { return error("用户未登录或token无效"); } - + WechatPayUtil wechatPayUtil = new WechatPayUtil(); // 2. 查询订单状态 - Map queryResult = WechatPayUtil.queryOrder(orderNo, null); + Map queryResult = wechatPayUtil.queryOrder(orderNo, null); // 3. 返回结果 boolean success = (Boolean) queryResult.get("success"); @@ -1972,9 +1972,9 @@ public class AppletController extends BaseController { if (!(Boolean) userValidation.get("valid")) { return error("用户未登录或token无效"); } - + WechatPayUtil wechatPayUtil = new WechatPayUtil(); // 2. 创建代付订单 - Map payForResult = WechatPayUtil.createPayForOrder(params); + Map payForResult = wechatPayUtil.unifiedOrder(params.get("payerOpenid").toString(), params.get("orderNo").toString(),Integer.valueOf(params.get("totalFee").toString()), params.get("body").toString(), params.get("notifyUrl").toString(), params.get("remark").toString()); // 3. 返回结果 boolean success = (Boolean) payForResult.get("success"); @@ -2001,8 +2001,9 @@ public class AppletController extends BaseController { @PostMapping(value = "/api/pay/notify") public String handlePayNotify(HttpServletRequest request) { try { + WechatPayUtil wechatPayUtil = new WechatPayUtil(); // 1. 处理支付回调 - Map notifyResult = WechatPayUtil.handlePayNotify(request); + Map notifyResult = wechatPayUtil.handlePayNotify(request); // 2. 获取支付信息 boolean success = (Boolean) notifyResult.get("success"); @@ -2052,9 +2053,9 @@ public class AppletController extends BaseController { if (!(Boolean) userValidation.get("valid")) { return error("用户未登录或token无效"); } - + WechatPayUtil wechatPayUtil = new WechatPayUtil(); // 2. 申请退款 - Map refundResult = WechatPayUtil.refund(params); + Map refundResult = wechatPayUtil.refund(params); // 3. 返回结果 boolean success = (Boolean) refundResult.get("success"); @@ -2093,9 +2094,9 @@ public class AppletController extends BaseController { if (!(Boolean) userValidation.get("valid")) { return error("用户未登录或token无效"); } - + WechatPayUtil wechatPayUtil = new WechatPayUtil(); // 2. 企业付款 - Map transferResult = WechatPayUtil.transferToUser(params); + Map transferResult = wechatPayUtil.transferToUser(params); // 3. 返回结果 boolean success = (Boolean) transferResult.get("success"); @@ -3070,6 +3071,36 @@ public class AppletController extends BaseController { } } + + + /** + * 小程序下单时用户订阅消息推送接口(前端不传值,后端返回固定结构) + * @return 订阅消息推送ID分类结果 + */ + @GetMapping("/api/public/massge/notice") + public Map subscribeMessageNotice() { + Map data = new HashMap<>(); + data.put("make_success", Arrays.asList( + "YKnuTCAD-oEEhNGoI3LUVkAqNsykOMTcyrf71S9vev8", + "5lA-snytEPl25fBS7rf6rQi8Y0i5HOSdG0JMVdUnMcU" + )); + data.put("pay_success", Arrays.asList( + "pv3cba-wPoinUbBZSskp0KpDNnJwrHqS0rvGBfDNQ1M" + )); + data.put("integral", Arrays.asList( + "pv3cba-wPoinUbBZSskp0KpDNnJwrHqS0rvGBfDNQ1M" + )); + data.put("worker", Arrays.asList( + "5lA-snytEPl25fBS7rf6rQi8Y0i5HOSdG0JMVdUnMcU" + )); + Map result = new HashMap<>(); + result.put("code", 200); + result.put("msg", "OK"); + result.put("data", data); + return result; + } + + /** * 积分商品兑换接口 * @@ -4354,7 +4385,7 @@ public class AppletController extends BaseController { Long productId = Long.valueOf(orderParams.get("product_id").toString()); Long addressId = Long.valueOf(orderParams.get("address_id").toString()); String makeTime = orderParams.get("make_time").toString(); - Long num = Long.valueOf(orderParams.get("num").toString()); + String sku = orderParams.get("sku") != null ? orderParams.get("sku").toString() : ""; // 5. 查询商品信息 @@ -4368,7 +4399,9 @@ public class AppletController extends BaseController { if (userAddress == null) { return error("地址不存在"); } + //判断如果是商品就添加商品订单,如果是服务就添加服务订单 if(serviceGoods.getType()==2){ + Long num = Long.valueOf(orderParams.get("num").toString()); GoodsOrder goodsOrder = new GoodsOrder(); goodsOrder.setType(2); goodsOrder.setMainOrderId("WX" + System.currentTimeMillis()); @@ -4403,6 +4436,7 @@ public class AppletController extends BaseController { order.setAddressId(addressId); order.setSku(sku); + // 解析预约时间 String[] makeTimeArr = makeTime.split(" "); if (makeTimeArr.length != 2) { @@ -4418,7 +4452,7 @@ public class AppletController extends BaseController { return error("预约时间格式错误"); } - order.setNum(num); // 默认数量为1 + order.setNum(1l); // 默认数量为1 order.setTotalPrice(serviceGoods.getPrice()); // 总价等于商品价格 order.setGoodPrice(serviceGoods.getPrice()); // 商品金额 order.setServicePrice(BigDecimal.ZERO); // 服务金额默认为0 @@ -4428,6 +4462,7 @@ public class AppletController extends BaseController { order.setIsAccept(0); // 0:未接单 order.setIsComment(0); // 0:未评价 order.setIsPause(1); // 0:未暂停 + order.setDeduction(new BigDecimal(0)); // 8. 生成订单号 String orderId = generateOrderId(); order.setOrderId(orderId); @@ -4445,6 +4480,46 @@ public class AppletController extends BaseController { orderLog.setType(new BigDecimal(1.0)); orderLog.setContent(jsonObject.toString()); orderLogService.insertOrderLog(orderLog); + //完成订单的初步创建之后开始进行派单和发送订阅,以及预约师傅的操作 + //完成订单后的第一步,向用户发送订阅消息 + Order orderNewData= orderService.selectOrderById(order.getId()); + String wxsendmsg=WXsendMsgUtil.sendMsgForUserInfo(user.getOpenid(), orderNewData, serviceGoods); + //第二步系统派单 + Users worker = AppletControllerUtil.creatWorkerForOrder(orderNewData); + //第三步向师傅发送订阅消息 + if(worker!=null){ + //确定派单之后,更新订单状态为已派单,并添加订单日志记录 + //修改订单状态为派单开始------------------------------------------- + orderNewData.setWorkerId(worker.getId()); + orderNewData.setStatus(2l); + orderNewData.setIsPause(1); + orderNewData.setReceiveTime(new Date()); + orderNewData.setReceiveType(3l); + orderNewData.setLogStatus(9); + JSONObject jSONObject =new JSONObject(); + jSONObject.put("type",9); + orderNewData.setLogJson(jSONObject.toJSONString()); + orderService.updateOrder(orderNewData); + OrderLog orderLognew = new OrderLog(); + orderLognew.setOid(orderNewData.getId()); + orderLognew.setOrderId(orderNewData.getOrderId()); + orderLognew.setTitle("平台派单"); + orderLognew.setType(new BigDecimal(1.1)); + JSONObject jSONObject1 =new JSONObject(); + jSONObject1.put("name","师傅收到派单信息"); + orderLognew.setContent(jSONObject1.toJSONString()); + orderLognew.setWorkerId(worker.getId()); + orderLognew.setWorkerLogId(worker.getId()); + orderLogService.insertOrderLog(orderLognew); + //修改订单状态为派单结束------------------------------------------- + //给师傅发送微信订阅消息进行通知需要他进行接单 + WXsendMsgUtil.sendMsgForWorkerInfo(worker.getOpenid(), orderNewData, serviceGoods); + //第四步给师傅进行电话通知 + YunXinPhoneUtilAPI.httpsAxbTransfer(worker.getPhone(), "404168"); + } + + + System.out.println("wxsendmsg:" + wxsendmsg); return success("预约成功"); } else { return error("预约失败,请稍后重试"); @@ -4577,6 +4652,13 @@ public class AppletController extends BaseController { order.setReceiveTime(new Date()); order.setIsAccept(1); // 1:已接单 order.setJsonStatus(2); // 服务进度:2=接单 + order.setLogStatus(9); + JSONObject json=new JSONObject(); + json.put("type",1); + order.setLogJson(json.toJSONString()); + + //绑定号码 + // 5. 写入日志 OrderUtil orderUtil = new OrderUtil(); orderUtil.SaveOrderLog(order); @@ -4903,7 +4985,7 @@ public class AppletController extends BaseController { data.put("name", order.getName()); data.put("phone", order.getPhone()); data.put("address", address); - data.put("make_time", order.getMakeTime()); + data.put("make_time", AppletControllerUtil.timeStamp2Date(order)); data.put("make_hour", order.getMakeHour()); data.put("num", order.getNum()); data.put("total_price", order.getTotalPrice()); diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/AppletControllerUtil.java b/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/AppletControllerUtil.java index 3346a24..f8c5ca8 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/AppletControllerUtil.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/AppletControllerUtil.java @@ -2,17 +2,15 @@ package com.ruoyi.system.ControllerUtil; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; -import com.ruoyi.system.domain.ServiceCate; -import com.ruoyi.system.domain.ServiceGoods; -import com.ruoyi.system.domain.Users; +import com.ruoyi.common.utils.spring.SpringUtils; +import com.ruoyi.system.domain.*; import com.ruoyi.system.service.IServiceGoodsService; +import com.ruoyi.system.service.IUserAddressService; import com.ruoyi.system.service.IUsersService; import com.github.pagehelper.PageInfo; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.text.SimpleDateFormat; +import java.util.*; /** * 小程序控制器工具类 @@ -30,7 +28,11 @@ import java.util.Map; * @version 1.0 */ public class AppletControllerUtil { - + + + private static final IUsersService usersService= SpringUtils.getBean(IUsersService.class); + private static final IUserAddressService userAddressService= SpringUtils.getBean(IUserAddressService.class); + // ============================== 统一响应处理方法 ============================== /** @@ -2114,7 +2116,7 @@ public class AppletControllerUtil { orderDetail.put("name", order.getName()); orderDetail.put("phone", order.getPhone()); orderDetail.put("address", order.getAddress()); - orderDetail.put("make_time", order.getMakeTime()); + orderDetail.put("make_time", AppletControllerUtil.timeStamp2Date(order)); orderDetail.put("make_hour", order.getMakeHour()); orderDetail.put("num", order.getNum()); @@ -2358,6 +2360,28 @@ public class AppletControllerUtil { return timeSlotList; } + /** + * 订单生成给师傅派单 + * + * @param order 订单主键 + * @return 是否可用 + */ + public static Users creatWorkerForOrder(Order order) { + GaoDeMapUtil gaoDeMapUtil = new GaoDeMapUtil(); + UserAddress userAddress = userAddressService.selectUserAddressById(order.getAddressId()); +// if (userAddress != null){ +// String city = gaoDeMapUtil.getCityByLocation(userAddress.getLongitude(), userAddress.getLatitude()); +// if (city != null){ +// +// }else{ +// +// } +// } + Users worker = usersService.selectUsersById(2l); + return worker; + } + + /** * 判断时间段是否可用 * @@ -2730,4 +2754,83 @@ public class AppletControllerUtil { } return resultMap; } + //完成订单的初步创建之后开始进行派单和发送订阅,以及预约师傅的操作 + public static Map creatOrderTheNext(String imageUrl) { + Map imageData = new HashMap<>(); + + //第一步给客户发送订阅消息,告诉客户预约成功 + + imageData.put("url", imageUrl); + imageData.put("thumb", imageUrl); + return imageData; + } + + /** + * 发送微信小程序订阅消息(自动获取accessToken) + * @param touser 接收者(用户)的openid + * @param templateId 订阅消息模板ID + * @param page 跳转页面路径(可选,可为null) + * @param data 模板内容(如{"thing1":{"value":"内容"}}) + * @return 微信接口响应字符串(JSON格式) + * @throws Exception 异常信息 + * + * 使用示例: + * Map data = new HashMap<>(); + * Map thing1 = new HashMap<>(); + * thing1.put("value", "测试内容"); + * data.put("thing1", thing1); + * String result = AppletControllerUtil.WXSendMsgUtil(openid, templateId, "pages/index/index", data); + */ + public static String WXSendMsgUtil(String touser, String templateId, String page, Map data) throws Exception { + // 1. 获取accessToken + String accessToken = com.ruoyi.system.ControllerUtil.WechatApiUtil.getAccessToken().get("access_token").toString(); + // 2. 微信订阅消息接口地址 + String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken; + // 3. 组装请求参数 + Map param = new HashMap<>(); + param.put("touser", touser); + param.put("template_id", templateId); + if (page != null && !page.isEmpty()) { + param.put("page", page); + } + param.put("data", data); + String body = com.alibaba.fastjson2.JSON.toJSONString(param); + // 4. 发送POST请求 + java.net.HttpURLConnection conn = (java.net.HttpURLConnection) new java.net.URL(url).openConnection(); + conn.setRequestMethod("POST"); + conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); + conn.setDoOutput(true); + try (java.io.OutputStream os = conn.getOutputStream()) { + os.write(body.getBytes(java.nio.charset.StandardCharsets.UTF_8)); + } + // 5. 读取响应 + java.io.InputStream is = conn.getInputStream(); + StringBuilder sb = new StringBuilder(); + byte[] buf = new byte[1024]; + int len; + while ((len = is.read(buf)) != -1) { + sb.append(new String(buf, 0, len, java.nio.charset.StandardCharsets.UTF_8)); + } + is.close(); + return sb.toString(); + } + + + //时间转化,将int的时间戳转换为时间,这个方法用于订单详情里面的预约时间 + public static String timeStamp2Date(Order order) { + // 定义日期格式化模式 + String formattedDate=""; + SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");// 注意:Java的Date构 + if(order!=null){ + if (order.getMakeTime() != null){ + // 将时间戳转换为Date对象 + Date date = new Date((long) order.getMakeTime() * 1000); + formattedDate = sdf.format(date); + if (order.getMakeHour() != null){ + formattedDate += " " + order.getMakeHour(); + } + } + } + return formattedDate.replace("-", "~"); + } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/GaoDeMapUtil.java b/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/GaoDeMapUtil.java new file mode 100644 index 0000000..0d5a16c --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/GaoDeMapUtil.java @@ -0,0 +1,113 @@ +package com.ruoyi.system.ControllerUtil; + +import com.alibaba.fastjson2.JSONObject; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +/** + * 高德地图工具类 + * 提供常用的地图相关功能,如根据经纬度获取城市、逆地理编码等 + * 需要在高德开放平台注册并获取Web服务Key + */ +public class GaoDeMapUtil { + + // 请替换为你自己的高德Web服务Key + private static final String GAODE_KEY = "YOUR_GAODE_WEB_KEY"; + + /** + * 根据经纬度获取所在城市名称 + * @param longitude 经度 + * @param latitude 纬度 + * @return 城市名称(如"上海市"),失败返回null + */ + public static String getCityByLocation(double longitude, double latitude) { + try { + String url = String.format( + "https://restapi.amap.com/v3/geocode/regeo?location=%f,%f&key=%s&extensions=base", + longitude, latitude, GAODE_KEY + ); + HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); + conn.setRequestMethod("GET"); + conn.setDoInput(true); + InputStream is = conn.getInputStream(); + StringBuilder sb = new StringBuilder(); + byte[] buf = new byte[1024]; + int len; + while ((len = is.read(buf)) != -1) { + sb.append(new String(buf, 0, len, StandardCharsets.UTF_8)); + } + is.close(); + JSONObject json = JSONObject.parseObject(sb.toString()); + if (json.getIntValue("status") == 1) { + JSONObject regeocode = json.getJSONObject("regeocode"); + if (regeocode != null) { + JSONObject addressComponent = regeocode.getJSONObject("addressComponent"); + if (addressComponent != null) { + return addressComponent.getString("city"); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * 经纬度逆地理编码,获取详细地址 + * @param longitude 经度 + * @param latitude 纬度 + * @return 详细地址字符串,失败返回null + */ + public static String getAddressByLocation(double longitude, double latitude) { + try { + String url = String.format( + "https://restapi.amap.com/v3/geocode/regeo?location=%f,%f&key=%s&extensions=base", + longitude, latitude, GAODE_KEY + ); + HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); + conn.setRequestMethod("GET"); + conn.setDoInput(true); + InputStream is = conn.getInputStream(); + StringBuilder sb = new StringBuilder(); + byte[] buf = new byte[1024]; + int len; + while ((len = is.read(buf)) != -1) { + sb.append(new String(buf, 0, len, StandardCharsets.UTF_8)); + } + is.close(); + JSONObject json = JSONObject.parseObject(sb.toString()); + if (json.getIntValue("status") == 1) { + JSONObject regeocode = json.getJSONObject("regeocode"); + if (regeocode != null) { + return regeocode.getString("formatted_address"); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * 计算两个经纬度点之间的直线距离(单位:米) + * @param lng1 第一个点经度 + * @param lat1 第一个点纬度 + * @param lng2 第二个点经度 + * @param lat2 第二个点纬度 + * @return 距离(米) + */ + public static double getDistance(double lng1, double lat1, double lng2, double lat2) { + double radLat1 = Math.toRadians(lat1); + double radLat2 = Math.toRadians(lat2); + double a = radLat1 - radLat2; + double b = Math.toRadians(lng1) - Math.toRadians(lng2); + double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))); + s = s * 6378137.0; // 地球半径(米) + s = Math.round(s * 10000d) / 10000d; + return s; + } +} \ No newline at end of file diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/WXsendMsgUtil.java b/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/WXsendMsgUtil.java index 1fdfe4d..a336865 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/WXsendMsgUtil.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/WXsendMsgUtil.java @@ -5,11 +5,20 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.system.domain.AppleDoMain.TemplateData; +import com.ruoyi.system.domain.AppleDoMain.WxMssVo; +import com.ruoyi.system.domain.Order; +import com.ruoyi.system.domain.ServiceGoods; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; /** * 微信小程序通知消息推送工具类 @@ -19,8 +28,12 @@ import com.alibaba.fastjson2.JSONObject; public class WXsendMsgUtil { // 微信小程序的AppID和AppSecret(请替换为你自己的) - private static final String APPID = "YOUR_APPID"; - private static final String APPSECRET = "YOUR_APPSECRET"; + private static final String APPID = "wx73d0202b3c8a6d68"; + private static final String APPSECRET = "c0871da0ca140930420c695147f3694b"; + + private static final String PAY_SUCCESS="YKnuTCAD-oEEhNGoI3LUVkAqNsykOMTcyrf71S9vev8"; //下单成功通知 + private static final String ORDER_STATUS="5lA-snytEPl25fBS7rf6rQi8Y0i5HOSdG0JMVdUnMcU"; // ##订单状态通知 + private static final String PAY_GOODS="pv3cba-wPoinUbBZSskp0KpDNnJwrHqS0rvGBfDNQ1M"; //##商品支付通知 /** * 获取微信小程序全局唯一后台接口调用凭据 access_token @@ -44,57 +57,117 @@ public class WXsendMsgUtil { return json.getString("access_token"); } - /** - * 发送微信小程序订阅消息 - * @param accessToken access_token - * @param touser 接收者openid - * @param templateId 模板ID - * @param page 跳转页面(可选) - * @param data 模板内容(Map,如{"thing1":{"value":"内容"}}) - * @return 微信接口响应 - * @throws Exception 异常 - */ - public static String sendSubscribeMsg(String accessToken, String touser, String templateId, String page, Map data) throws Exception { - String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken; - Map param = new HashMap<>(); - param.put("touser", touser); - param.put("template_id", templateId); - if (page != null && !page.isEmpty()) { - param.put("page", page); - } - param.put("data", data); - String body = JSON.toJSONString(param); - HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); - conn.setRequestMethod("POST"); - conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); - conn.setDoOutput(true); - try (OutputStream os = conn.getOutputStream()) { - os.write(body.getBytes(StandardCharsets.UTF_8)); - } - InputStream is = conn.getInputStream(); - StringBuilder sb = new StringBuilder(); - byte[] buf = new byte[1024]; - int len; - while ((len = is.read(buf)) != -1) { - sb.append(new String(buf, 0, len, StandardCharsets.UTF_8)); - } - is.close(); - return sb.toString(); + + + //公共推送 + public static String PublicPush(WxMssVo wxMssVo) throws Exception { + RestTemplate restTemplate = new RestTemplate(); + // String accessToken = getAccessToken(); + //这里简单起见我们每次都获取最新的access_token + String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken(); + ResponseEntity responseEntity = + restTemplate.postForEntity(url, wxMssVo, String.class); + return responseEntity.getBody(); } - /** - * 示例:如何推送微信小程序订阅消息 - */ - public static void main(String[] args) throws Exception { - // 1. 获取access_token - String accessToken = getAccessToken(); - // 2. 组装消息内容 - Map data = new HashMap<>(); - Map thing1 = new HashMap<>(); - thing1.put("value", "测试内容"); - data.put("thing1", thing1); - // 3. 发送消息 - String result = sendSubscribeMsg(accessToken, "OPENID", "TEMPLATE_ID", "pages/index/index", data); - System.out.println("推送结果:" + result); + + + +// /** +// * 发送微信小程序订阅消息 +// * @param accessToken access_token +// * @param touser 接收者openid +// * @param templateId 模板ID +// * @param page 跳转页面(可选) +// * @param data 模板内容(Map,如{"thing1":{"value":"内容"}}) +// * @return 微信接口响应 +// * @throws Exception 异常 +// */ +// public static String sendSubscribeMsg(String accessToken, String touser, String templateId, String page, Map data) throws Exception { +// System.out.println("0001access_token:" + accessToken); +// String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken; +// Map param = new HashMap<>(); +// param.put("touser", touser); +// param.put("template_id", templateId); +// if (page != null && !page.isEmpty()) { +// param.put("page", page); +// } +// param.put("data", data); +// String body = JSON.toJSONString(param); +// HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); +// conn.setRequestMethod("POST"); +// conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); +// conn.setDoOutput(true); +// try (OutputStream os = conn.getOutputStream()) { +// os.write(body.getBytes(StandardCharsets.UTF_8)); +// } +// InputStream is = conn.getInputStream(); +// StringBuilder sb = new StringBuilder(); +// System.out.println("0001sb.toString():" + sb.toString()); +// byte[] buf = new byte[1024]; +// int len; +// while ((len = is.read(buf)) != -1) { +// sb.append(new String(buf, 0, len, StandardCharsets.UTF_8)); +// } +// is.close(); +// return sb.toString(); +// } + + + //微信推送第一步,通知用户预约成功 + public static String sendMsgForUserInfo(String openid, Order order, ServiceGoods serviceGoods) throws Exception { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + //拼接推送的模版 + WxMssVo wxMssVo = new WxMssVo(); + wxMssVo.setTouser(openid);//用户的openid(要发送给那个用户,通常这里应该动态传进来的) + wxMssVo.setTemplate_id(PAY_SUCCESS);//订阅消息模板id + wxMssVo.setPage("/pages/mine/serveOrder/index"); + String mark = order.getMark(); + if (StringUtils.isEmpty( mark)){ + mark="暂无备注"; + } + + Map m = new HashMap<>(3); + m.put("thing44", new TemplateData(serviceGoods.getTitle())); + m.put("character_string22", new TemplateData(order.getOrderId())); + m.put("time1", new TemplateData(AppletControllerUtil.timeStamp2Date(order))); + m.put("thing27", new TemplateData(mark)); + m.put("time10", new TemplateData(sdf.format(new Date()))); + System.out.println("thing44"+ serviceGoods.getTitle()); + System.out.println("character_string22"+ order.getOrderId()); + System.out.println("time1"+ AppletControllerUtil.timeStamp2Date(order)); + System.out.println("thing27"+ order.getMark()); + System.out.println("time10"+sdf.format(new Date())); + + System.out.println("0002订单号:" + m.toString()); + wxMssVo.setData(m); + return PublicPush(wxMssVo); + } + + + //微信推送第er步,给师傅通知派单情况 + public static String sendMsgForWorkerInfo(String openid, Order order, ServiceGoods serviceGoods) throws Exception { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + //拼接推送的模版 + WxMssVo wxMssVo = new WxMssVo(); + wxMssVo.setTouser(openid);//用户的openid(要发送给那个用户,通常这里应该动态传进来的) + wxMssVo.setTemplate_id(ORDER_STATUS);//订阅消息模板id + wxMssVo.setPage("/pages/tab/serve/index"); + String mark = order.getMark(); + if (StringUtils.isEmpty( mark)){ + mark="暂无备注"; + } + Map m = new HashMap<>(3); + m.put("thing15", new TemplateData(serviceGoods.getTitle())); + m.put("character_string5", new TemplateData(order.getOrderId())); + m.put("date3", new TemplateData(AppletControllerUtil.timeStamp2Date(order))); + m.put("thing1", new TemplateData(order.getAddress())); + m.put("thing9", new TemplateData(sdf.format(new Date()))); + + System.out.println("0002订单号:" + m.toString()); + wxMssVo.setData(m); + return PublicPush(wxMssVo); + } + } \ No newline at end of file diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/WechatPayUtil.java b/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/WechatPayUtil.java index 644714c..3bf259f 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/WechatPayUtil.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/controllerUtil/WechatPayUtil.java @@ -82,6 +82,8 @@ public class WechatPayUtil { */ private static final RestTemplate restTemplate = new RestTemplate(); + + /** * 小程序统一下单(JSAPI) * @param openid 用户openid @@ -92,7 +94,7 @@ public class WechatPayUtil { * @param attach 附加数据(可选) * @return 统一下单结果,包含小程序端调起支付的参数 */ - public Map unifiedOrder(String openid, String orderNo, int totalFee, String body, String notifyUrl, String attach) { + public Map unifiedOrder(String openid, String orderNo, int totalFee, String body, String notifyUrl, String attach) { Map result = new HashMap<>(); try { // 1. 参数校验 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/AppleDoMain/TemplateData.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/AppleDoMain/TemplateData.java new file mode 100644 index 0000000..b693035 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/AppleDoMain/TemplateData.java @@ -0,0 +1,19 @@ +package com.ruoyi.system.domain.AppleDoMain; + + +public class TemplateData { + + private String value;// + + public TemplateData(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/AppleDoMain/WxMssVo.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/AppleDoMain/WxMssVo.java new file mode 100644 index 0000000..05c68a2 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/AppleDoMain/WxMssVo.java @@ -0,0 +1,42 @@ +package com.ruoyi.system.domain.AppleDoMain; + +import java.util.Map; + +public class WxMssVo { + private String touser;//用户openid + private String template_id;//订阅消息模版id + private String page = "pages/index/index";//默认跳到小程序首页 + private Map data;//推送文字 + + public String getTouser() { + return touser; + } + + public void setTouser(String touser) { + this.touser = touser; + } + + public String getTemplate_id() { + return template_id; + } + + public void setTemplate_id(String template_id) { + this.template_id = template_id; + } + + public String getPage() { + return page; + } + + public void setPage(String page) { + this.page = page; + } + + public Map getData() { + return data; + } + + public void setData(Map data) { + this.data = data; + } +} \ No newline at end of file