收藏
回答

关于微信小程序消息订阅数据封装问题,出现47003要怎么解决?

@Service
@Slf4j
public class WechatSubscriptionMessageImpl implements WeChatSubscriptionMessage {


    @Autowired
    OrdermanagementMapper ordermanagementMapper;

    @Override
    public AjaxResult SubscriptionMessage(SubscriptionMessageParam subscriptionMessageParam) {
        try {
            //创建Http连接
            CloseableHttpClient httpClient = HttpClients.createDefault();
            // 获取 AccessToken
            String accessToken = TheWeChatConfig.getAccessToken();

            // 创建一个HttpPost请求
            HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken);


            //构造json数据并开始封装

            //请求的Body参数
            JSONObject body = new JSONObject();

            //Body中的模板内容
            JSONObject date = new JSONObject();
            String orderId = subscriptionMessageParam.getOrderId();
            Ordermanagement ordermanagement = ordermanagementMapper.selectOrdermanagementByOrderid(orderId);
//            //设置消息内容
//            JSONObject value1 = new JSONObject();
//            value1.put("value",String.valueOf(ordermanagementMapper.selectPickupNumberByOrderId(orderId)));
//            date.put("character_string10",value1);
//
//            JSONObject value2 = new JSONObject();
//            value2.put("value", ordermanagement.getReservationname());
//            date.put("thing9", value2);
//
//            JSONObject value3 = new JSONObject();
//            value3.put("value", );
//            date.put("time4",value3);
//
//            JSONObject value4 = new JSONObject();
//            value4.put("value", ordermanagement.getTotalprice());
//            date.put("amount3", value4);
//
//            JSONObject value5 = new JSONObject();
//            value5.put("value", orderId);
//            date.put("character_string2", value5);




//            //设置消息内容
            JSONObject value1 = new JSONObject();
            value1.put("value","gghh1");
            date.put("character_string4",value1);

            JSONObject value2 = new JSONObject();
            value2.put("value", "测试数据");
            date.put("thing5", value2);



            JSONObject value3 = new JSONObject();
            Date localDateTime = ordermanagement.getOrdertime();
            SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy年M月d日 HH:mm");
            String formattedDate = outputFormat.format(localDateTime);
            value3.put("value", formattedDate);
            date.put("time21",value3);

            JSONObject value4 = new JSONObject();
            value4.put("value", "6");
            date.put("thing8", value4);


            JSONObject value5 = new JSONObject();
            value5.put("value", orderId);
            date.put("character_string1", value5);
//
            body.put("touser", subscriptionMessageParam.getTouser());
            body.put("template_id", subscriptionMessageParam.getTemplate_id());
            body.put("miniprogram_state", subscriptionMessageParam.getMiniprogram_state());
            body.put("lang", subscriptionMessageParam.getLang());
            body.put("date", date);


            
            StringEntity entity = new StringEntity(date.toString(), ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);

            System.out.println(body.toString());
            //发送请求
            CloseableHttpResponse response = httpClient.execute(httpPost);
            log.info("response:{}",response);
            JSONObject Res = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
            String errcode = Res.get("errcode").toString();
            String errmsg = Res.get("errmsg").toString();
            log.info("statusCode:{}",errcode);
            if (errcode.equals("0")){
                return AjaxResult.success("消息订阅成功");
            }else {
                log.error("errmsg:"+errmsg);
                throw new IOException("request failed");
                }
        }catch (Throwable e){
            log.error("Create we chat code url failed,orderNo:{}:", subscriptionMessageParam.getOrderId(), e);
            throw new CustomException("Create we chat code url failed", e);
        }

    }

}

回答关注问题邀请回答
收藏

1 个回答

  • Mr.Zhao
    Mr.Zhao
    05-13

    扇自己一巴掌吧,困了赶紧睡觉

    05-13
    有用
    回复 1
    • 可燃纯净水
      可燃纯净水
      05-13
      蚌埠住了,马上睡觉
      05-13
      回复
登录 后发表内容