rentaiming
2024-07-16 2eb4e3b8ca10cc860fe0681f9737803cfb145e25
ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/controller/inner/MemberGoodsCollectionController.java
@@ -6,6 +6,8 @@
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
@@ -34,6 +36,8 @@
import com.ruoyi.system.api.feignClient.MemberClient;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.*;
@@ -107,8 +111,8 @@
                } catch (AlipayApiException e) {
                    throw new RuntimeException(e);
                }
            }if (data.getWxOpenid()!=null){
                push(data.getWxOpenid(),li.getFormId(),goodsSku.getSkuName(),goodsSeckill.getSeckillPrice(),goodsSeckill.getStartTime());
            }if (data.getMiniOpenid()!=null){
                push(data.getMiniOpenid(),li.getFormId(),goodsSku.getSkuName(),goodsSeckill.getSeckillPrice(),goodsSeckill.getStartTime());
            }
        }
@@ -150,11 +154,50 @@
            return null;
        }
    /**
     * 发送消息
     * @param template_id           所需下发的订阅模板id
     * @param page                  点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转
     * @param touser                接收者(用户)的 openid
     * @param data                  模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }的object
     * @param miniprogram_state     跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
     * @param lang                  进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
     * @return
     */
    public String push(String openid, String formid, String skuName, BigDecimal seckillPrice, LocalDateTime startTime) {
        //1,配置小程序信息
        WxMaInMemoryConfig wxConfig = new WxMaInMemoryConfig();
        String accessToken = null;
        try {
            accessToken = getAccessTokenByWX();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        JSONObject jsonObject = JSONObject.parseObject(accessToken);
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+jsonObject.get("access_token");
        Map<String, Object> params = new HashMap<>();
        params.put("template_id", "NHmHEbK8SjzafWgnErAxNR0b2XJzSehv2kPqbQefolU");
        params.put("page", null);
        params.put("touser", openid);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm");
        String data = "{\"thing1\":{\"value\":\""+skuName+"\"},\"amount3\":{\"value\":\""+seckillPrice.toString()+"\"}"+
                ",\"time4\":{\"value\":\""+startTime.format(formatter)+"\"}"+
                ",\"thing5\":{\"value\":\"优质老酒限时秒杀即将开始!点击参与秒杀哈\"}}";
        params.put("data", JSON.parseObject(data));
        params.put("miniprogram_state", "trial");
        params.put("lang", "zh_CN");
        try {
            Map<String, Object> payloadMapData = JSONObject.parseObject(HttpUtils.httpPost(url, JSONObject.toJSONString(params)));
            if (payloadMapData.containsKey("errCode")) {
                System.out.println("返回code码:"+payloadMapData.get("errCode").toString());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
      /*  WxMaInMemoryConfig wxConfig = new WxMaInMemoryConfig();
        wxConfig.setAppid("wx69e3ac6e13a889b7");//小程序appid
        wxConfig.setSecret("1b8bcfcb681524ac553e72054e5271ef");//小程序AppSecret
@@ -223,7 +266,7 @@
            return e.getMessage();
        }
*/
        return "推送成功";
    }