package com.hai.config; import cn.binarywang.wx.miniapp.api.WxMaMsgService; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage; import cn.binarywang.wx.miniapp.bean.WxMaTemplateData; import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage; import com.alibaba.fastjson.JSONObject; import com.hai.common.utils.DateUtil; import com.hai.common.utils.HttpsUtils; import com.hai.common.utils.ResponseMsgUtil; import com.hai.entity.HighChildOrder; import com.hai.entity.HighOrder; import com.hai.model.ResponseData; import com.hai.service.HighOrderService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Resource; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class WxMsgConfig { private static Logger log = LoggerFactory.getLogger(WxMsgConfig.class); @Resource private static HighOrderService highOrderService; public static ResponseData pushOneUser(Long orderId) { try { List list = new ArrayList<>(); HighChildOrder highChildOrder = highOrderService.getChildOrderByPresentation(orderId); HighOrder highOrder = highOrderService.getOrderById(orderId); Map m = new HashMap<>(); m.put("thing1", highChildOrder.getGoodsName()); m.put("amount2", highChildOrder.getGoodsActualPrice() + "元"); m.put("character_string3", highOrder.getOrderNo()); m.put("time4", DateUtil.date2String(highOrder.getPayTime() , "yyyy年MM月dd天 HH分mm秒")); m.put("thing6", "微信支付"); 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("oUGn_4lkgegcjTc9oR1qBGjtAm3A"); // 小程序openId subscribeMessage.setTemplateId("oUvaCPeeOg4wH6HTvCcSabU6FnzXUXOBXsqBYAPOV-U"); subscribeMessage.setData(list); subscribeMessage.setPage("pages/user/order_details/order_details?id=666"); subscribeMessage.setMiniprogramState("developer"); final WxMaService wxService = WxMaConfiguration.getMaService(); WxMaMsgService maMsgService = wxService.getMsgService(); maMsgService.sendSubscribeMsg(subscribeMessage); return ResponseMsgUtil.success("发送成功"); } catch (Exception e) { return ResponseMsgUtil.exception(e); } } public static JSONObject getWxMsgToken() { Map map = new HashMap<>(); map.put("appid", CommonSysConst.getSysConfig().getApiKey()); map.put("secret", CommonSysConst.getSysConfig().getApiSecret()); map.put("grant_type", "client_credential"); return HttpsUtils.doGet(CommonSysConst.getSysConfig().getTokenUrl() + "/cgi-bin/token", map); } }