Blame view

src/main/java/com/jfinal/wxaapp/api/WxaMessageApi.java 1.93 KB
e80df919   atao   init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  /**
   * Copyright (c) 2011-2014, L.cm 卢春梦 (qq596392912@gmail.com).
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   */
  
  package com.jfinal.wxaapp.api;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import com.jfinal.weixin.sdk.api.ApiResult;
  import com.jfinal.weixin.sdk.utils.HttpUtils;
  import com.jfinal.weixin.sdk.utils.JsonUtils;
  
  /**
   * 客服接口-发消息
   * @author L.cm
   *
   */
  public class WxaMessageApi {
      private static String customMessageUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";
  
      /**
       * 发送客服消息
       * @param message 消息封装
       * @return ApiResult
       */
      private ApiResult sendMsg(Map<String, Object> message) {
          String accessToken = WxaAccessTokenApi.getAccessTokenStr();
          String jsonResult = HttpUtils.post(customMessageUrl + accessToken, JsonUtils.toJson(message));
          return new ApiResult(jsonResult);
      }
      
      /**
       * 发送文本客服消息
       * @param openId openId
       * @param text 文本消息
       * @return ApiResult
       */
      public ApiResult sendText(String openId, String text) {
          Map<String, Object> json = new HashMap<String, Object>();
          json.put("touser", openId);
          json.put("msgtype", "text");
  
          Map<String, Object> textObj = new HashMap<String, Object>();
          textObj.put("content", text);
  
          json.put("text", textObj);
          return sendMsg(json);
      }
  
      /**
       * 发送图片消息
       * @param openId openId
       * @param mediaId 图片媒体id
       * @return ApiResult
       */
      public ApiResult sendImage(String openId, String mediaId) {
          Map<String, Object> json = new HashMap<String, Object>();
          json.put("touser", openId);
          json.put("msgtype", "image");
  
          Map<String, Object> image = new HashMap<String, Object>();
          image.put("media_id", mediaId);
  
          json.put("image", image);
          return sendMsg(json);
      }
  }