java对接微信小程序客服(超详细)

1.https://developers.weixin.qq.com,先注册登录微信小程序,然后把认证信息填写了。
2.找到 开发-开发管理-开发设置,获取小程序的appID和生成小程序密钥AppSecret。
java对接微信小程序客服(超详细)_第1张图片

3.把你的服务器配置一个域名,可以通过http或者https访问的地址。
4.在开发设置下面开通消息推送服务。URL就是可以访问消息推送的接口地址(后续贴出来),令牌自己填的,要在接口校验的。(具体流程就是通过这个接口URL,微信会发一个get请求,然后返回信息,微信就让你填写令牌,然后接下来的发送消息,是通过post请求,校验令牌,就可以给客服人员微信发送信息)
java对接微信小程序客服(超详细)_第2张图片
5.在功能-客服-小程序客服,添加客服人员的微信号

前面都是准备,下面是详细代码和说明。

import com.donwait.common.annotation.AccessControl;
import com.donwait.common.service.applet.AutoResponseService;
import com.donwait.common.utils.MessageUtils;
import com.donwait.common.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.util.*;

/**
 * @Description: 用户
 * @Author: zjh
 * @Date: 2021/6/7 14:49
 **/
@Api(tags = "用户信息")
@RequestMapping("/api/applet/member")
@RestController
@Slf4j
public class MemberApi {


    private final AutoResponseService autoResponseService;

    private final String token = "你的令牌";

    public MemberApi(AutoResponseService autoResponseService) {
        this.autoResponseService = autoResponseService;
    }


    @ApiOperation("微信消息通知-GET请求校验(确认请求来自微信服务器)")
    @RequestMapping(value = "/getSignature", method = {RequestMethod.GET, RequestMethod.POST})
    @AccessControl
    public String signature(HttpServletRequest request, HttpServletResponse response) {
        //微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数
        boolean isGet = request.getMethod().toLowerCase().equals("get");
        if (isGet) {
            String signature = request.getParameter("signature");
            // 时间戳
            String timestamp = request.getParameter("timestamp");
            // 随机数
            String nonce = request.getParameter("nonce");
            // 随机字符串
            String echostr = request.getParameter("echostr");
            log.info("echostr>>>>>>>>>>" + echostr);
            //通过检验signature对请求进行校验,若校验成功则原样返回echostr,否则接入失败
            if (checkSignature(signature, timestamp, nonce)) {
                log.info("echostr>>>>>>>>>>>>" + echostr);
                return echostr;
            }
            log.info("客服消息验证url验证结果:{}", echostr);
        } else {
            response(request, response);
        }
        return null;
    }

    /*@ApiOperation("微信消息通知-POST请求校验(确认请求来自微信服务器)")
    @PostMapping("/postSignature")
    @AccessControl*/
    public String response(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 接收消息并返回消息
            String result = acceptMessage(request, response);
            log.info("接受消息并返回消息result>>>>>>" + result);
            // 响应消息
            PrintWriter out = response.getWriter();
            log.info("out>>>>>>>>" + out);
            out.print(result);
            out.close();
        } catch (Exception ex) {
            log.error("微信帐号接口配置失败!" + ex.getMessage());
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * 校验(确认请求来自微信服务器)
     *
     * @param signature
     * @param timestamp
     * @param nonce
     * @return
     */
    public boolean checkSignature(String signature, String timestamp, String nonce) {
        String[] params = {"dw818502", timestamp, nonce};
        log.info("params>>>>>>>>>>>>>" + Arrays.toString(params));
        //1、将token、timestamp、nonce三个参数进行字典序排序
        Arrays.sort(params);
        //2、将三个参数字符串拼接成一个字符串
        StringBuilder sb = new StringBuilder();
        for (String param : params) {
            sb.append(param);
        }
        log.info("字符串拼接>>>>>>>>>>>>>>>>>" + sb);
        MessageDigest messageDigest = null;
        String tpmStr = null;
        try {
            messageDigest = MessageDigest.getInstance("SHA-1");
            log.info("md>>>>>>>>>>>>" + messageDigest);
            //3、将三个参数字符串拼接成一个字符串进行sha1加密
            byte[] digest = messageDigest.digest(sb.toString().getBytes());
            log.info("digest>>>>>>>>>>>" + Arrays.toString(digest));
            tpmStr = StringUtil.byteToHex(digest);
            log.info("加密串>>>>>>>>>>>" + tpmStr);
        } catch (Exception e) {
            log.info("错误信息>>>>>>>>>>>>" + e.getMessage());
            e.printStackTrace();
        }
        //4、将sha1加密后的字符串可与signature对比,标识该请求来源于微信
        return tpmStr != null && tpmStr.equals(signature);
    }

    public String acceptMessage(HttpServletRequest request, HttpServletResponse response) {
        //返回值
        String result = "success";
        try {
            // 将请求、响应的编码均设置为UTF-8(防止中文乱码)
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            Map<String, String> requestMap = MessageUtils.parseXml(request);
            log.info("requestMap>>>>>>>>>>" + requestMap);
            // 发送者的openid
            String fromUserName = requestMap.get("FromUserName");
            // 小程序的原始ID
            String toUserName = requestMap.get("ToUserName");
            // 消息类型
            String msgType = requestMap.get("MsgType");
            // 文本消息内容
            String content = requestMap.get("Content");
            // 事件类型
            String event = requestMap.get("Event");
            log.info("fromUserName=" + fromUserName + ",toUserName=" + toUserName + ",msgType=" + msgType + ",content=" + content + ",event=" + event);
            StringBuffer contentMessage = new StringBuffer();
            autoResponseService.sendCustomerTextMessage(fromUserName, contentMessage.toString(), token);
        } catch (Exception e) {
            log.info("错误信息打印>>>>>>>>>>>" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }


}

/**
 * 客服功能 - 消息发送请求工具类
 * Created by Lance on 2020/10/10 17:52
 */
public class HttpUtils {

    /**
     * Get 发送的请求
     * @param url
     * @return
     */
    public static String sendGetRequest(String url) {
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(url, String.class);
    }

    public static String sendPostRequest(String url, HttpMethod method, HttpEntity<Map<String, Object>> httpEntity) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.exchange(url, method, httpEntity, String.class);
        return response.getBody();
    }
}

/**
 * 客服功能 - 消息工具类
 * Created by Lance on 2020/10/10 17:52
 */
public class MessageUtils {

    public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
        // 将解析结果存储在HashMap 中
        Map<String, String> map = new HashMap<String, String>();
        // 从request 中取得输入流
        InputStream inputStream = request.getInputStream();
        // 读取输入流
        SAXReader reader = new SAXReader();
        Document document = reader.read(inputStream);
        // 得到xml 根元素
        Element root = document.getRootElement();
        // 得到根元素的所有子节点
        List<Element> elementList = root.elements();
        // 遍历所有子节点
        for (Element e : elementList) {
            map.put(e.getName(), e.getText());
        }
        // 释放资源
        inputStream.close();
        return map;
    }
}
/**
 * 客服功能 - 字符串转换
 * Created by Lance on 2020/10/12 10:52
 */
@Slf4j
public class StringUtil {
    /**
     * 十六进制字节数组转为字符串
     *
     * @param hash
     * @return
     */
    public static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }
}
import java.security.MessageDigest;

public final class SHA1 {

    private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    /**
     * Takes the raw bytes from the digest and formats them correct.
     *
     * @param bytes the raw bytes from the digest.
     * @return the formatted bytes.
     */
    private static String getFormattedText(byte[] bytes) {
        int len = bytes.length;
        StringBuilder buf = new StringBuilder(len * 2);
        // 把密文转换成十六进制的字符串形式
        for (int j = 0; j < len; j++) {
            buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
        }
        return buf.toString();
    }

    public static String encode(String str) {
        if (str == null) {
            return null;
        }
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
            messageDigest.update(str.getBytes());
            return getFormattedText(messageDigest.digest());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
/**
 * 客服功能 - 自动回复小程序APP链接
 * Created by Lance on 2020/10/10 11:03
 */
@Slf4j
@Service
public class AutoResponseService {

    /**
     * 人工服务
     *
     * @param fromUserName
     * @param toUserName
     * @param requestMap
     * @return
     */
    public String switchCustomerService(String fromUserName, String toUserName) {
        String returnXml = "\n" +
                "     + fromUserName + "]]>\n" +
                "     + toUserName + "]]>\n" +
                "    " + System.currentTimeMillis() / 1000 + "\n" +
                "    \n" +
                "";
        log.info("人工服务result>>>>>>>" + returnXml);
        return returnXml;
    }
    /**
     * 文本消息
     *
     * @param openid
     * @param text
     * @param accessToken
     * @return
     */
    private static String SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send";//固定发送地址

    public String sendCustomerTextMessage(String openid, String text, String accessToken) throws IOException {
        HashMap<String, Object> map_content = new HashMap<>();
        map_content.put("content", text);
        HashMap<String, Object> map = new HashMap<>();
        map.put("touser", openid);
        map.put("msgtype", "text");
        map.put("text", map_content);
        String content = JSONObject.toJSONString(map);
        log.info("文本消息content" + content);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity = new HttpEntity(map, headers);
        String str = HttpUtils.sendPostRequest(SEND_URL + "?access_token=" + accessToken, HttpMethod.POST, httpEntity);
        log.info("文本消息str>>>>>>>>>>"+str);
        return str;
    }
}

到这里就已经可以了,然后看一下效果
java对接微信小程序客服(超详细)_第3张图片
java对接微信小程序客服(超详细)_第4张图片

你可能感兴趣的:(开发随手记,java,微信小程序,notepad++)