微信開發--客服接口--主動發消息(發圖片例子)

本次微信公衆號開發之客服消息用的是開發工具weixin-java-tools

1. 導入相關開發工具包

<dependency>
     <groupId>com.github.binarywang</groupId>
     <artifactId>weixin-java-mp</artifactId>
     <version>2.9.6.BETA</version>
</dependency>

2. 發送消息

內容是什麼不重要,就是很長的字符串,把字符串內容轉成一張圖片,選擇路徑存放

主要就是先將圖片上傳到微信後臺,然後再取出來發送

      SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMdd_HHmmss");
        Orders orders = this.ordersMapper.selectByPrimaryKey(orderId);
        ArrayList<OrderDetails> orderDetails = this.orderDetailMapper.selectByOrderId
                (orders.getOrderId());
        // 生成訂單小票
        OrderBillUtils orderBillUtils = new OrderBillUtils(0);
        orders.setGroupName("克拉瑪依新華書店");
        orders.setPayDate(new Date());
        String printInfo = orderBillUtils.getOrderBillText1(orders, orderDetails);
        // 根據str,font的樣式以及輸出文件目錄
        Integer width = 400;
        Font font = new Font("宋體",Font.BOLD, 20);
        logger.info(font.toString());
        String[] ss = printInfo.split("/");
        logger.info("小票:" + printInfo);
        logger.info("數組長度" + ss.length);
        Integer height = 35 * ss.length;
        // 創建圖片
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_BGR);
        Graphics g = image.getGraphics();
        g.setClip(0, 0, width, height);
        g.setColor(Color.white);
        g.fillRect(1, 1, width, height);// 先用黑色填充整張圖片,也就是背景
        g.setColor(Color.black);// 在換成黑色
        g.setFont(font);// 設置畫筆字體
        for (int j = 0; j < ss.length; j++) {
            g.drawString(ss[j], 20, 30 * (j + 2));// 畫出字符串
        }
        g.dispose();
        String path = System.getProperty("web.root") + "orderBills" + File.separator + "electronic_" + df1.format(new Date().getTime()) + ".png";
        File file = new File(path);
        try {
            ImageIO.write(image, "png", file);// 輸出png圖片
        } catch (IOException e) {
            e.printStackTrace();
        }


        logger.info("電子小票地址:" + path);

3. 發送圖片給微信用戶

  try {
            /*
             *給支付成功用戶發送電子小票二維碼
             */
            String openId = (String) session.getAttribute(CURRENT_OPEN_ID);
            logger.info("openId:" + openId);

            //1. 上傳圖片,返回結果
            WxMediaUploadResult result = wxMpService.getMaterialService().mediaUpload(WxConsts.MediaFileType.IMAGE, file);
            logger.info("上傳圖片結果:" + request);

            if (result != null) {
                String media_id = result.getMediaId();

                WxMpKefuMessage wxMpKefuMessage = new WxMpKefuMessage();
                wxMpKefuMessage.setToUser(openId);
                wxMpKefuMessage.setMsgType(WxConsts.KefuMsgType.IMAGE);
                wxMpKefuMessage.setMediaId(media_id);
                //  2. 發送圖片
                boolean result1 = wxMpService.getKefuService().sendKefuMessage(wxMpKefuMessage);
            }

            orders.setOrderBill(printInfo);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章