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

1.https://developers.weixin.qq.com,先注册登录微信小程序,然后把认证信息填写了。
2.找到 开发-开发管理-开发设置,获取小程序的appID和生成小程序密钥AppSecret。
在这里插入图片描述

3.把你的服务器配置一个域名,可以通过http或者https访问的地址。
4.在开发设置下面开通消息推送服务。URL就是可以访问消息推送的接口地址(后续贴出来),令牌自己填的,要在接口校验的。(具体流程就是通过这个接口URL,微信会发一个get请求,然后返回信息,微信就让你填写令牌,然后接下来的发送消息,是通过post请求,校验令牌,就可以给客服人员微信发送信息)
在这里插入图片描述
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 = "<xml>\n" +
                "    <ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>\n" +
                "    <FromUserName><![CDATA[" + toUserName + "]]></FromUserName>\n" +
                "    <CreateTime>" + System.currentTimeMillis() / 1000 + "</CreateTime>\n" +
                "    <MsgType><![CDATA[transfer_customer_service]]></MsgType>\n" +
                "</xml>";
        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;
    }
}

到这里就已经可以了,然后看一下效果
在这里插入图片描述
在这里插入图片描述

<think>好的,我现在需要帮助用户了解Java对接微信小程序的整体开发流程。首先,我需要整合用户提供的引用内容,特别是引用1到4,这些引用提到了前后端通信、开发入门教程、Spring Boot整合小程序以及获取openID和手机号的示例。 首先,用户的主要问题是询问Java开发微信小程序的整体流程,并希望参考微信官方文档和示例教程。根据引用2,微信小程序的开发文档链接已经给出,我需要确保流程中涵盖前端和后端的配置,尤其是Java后端如何与小程序通信。 接下来,引用1提到使用GET方法将数据传到Java Servlet,处理并返回数据。这可能涉及到后端的控制器编写,比如使用Spring Boot的@RestController处理请求。引用3提到了基于Spring Boot的小程序商城案例,说明Spring Boot是常用的Java框架,需要整合到流程中。引用4则详细说明了如何获取openID和用户手机号,这涉及到微信的API调用,如wx.login和wx.getPhoneNumber,以及后端如何通过appid、secret和code换取session_key和openid。 我需要按照步骤来组织流程,从前端配置开始,到后端环境搭建,再到具体功能的实现,比如用户登录、数据通信等。同时,要确保每个步骤都有对应的文档或示例支持,比如引用2的官方文档链接,引用4的代码示例。 可能需要注意的地方包括微信小程序的appid和secret的安全性,后端接口的跨域问题,以及微信API的调用流程是否正确。此外,需要指导用户如何测试和调试,比如使用微信开发者工具和Postman进行接口测试。 最后,生成相关问题时要围绕用户可能遇到的后续问题,比如如何保证通信安全、处理用户数据等,参考已有的引用内容,确保问题具有针对性。</think>### Java对接微信小程序开发流程详解 #### 1. 环境准备阶段 - **小程序前端配置**:注册微信小程序账号并获取AppID[^2],安装微信开发者工具,创建项目时填入AppID - **Java后端环境**:建议使用Spring Boot框架搭建,Maven依赖需包含`spring-boot-starter-web`[^3],JDK版本推荐1.8+ #### 2. 通信基础搭建 - **接口定义**:使用`@RestController`创建RESTful接口 ```java @RestController @RequestMapping("/api") public class WxController { @GetMapping("/test") public String testConnection(@RequestParam String data) { System.out.println("收到小程序数据:" + data); return "服务端响应数据"; } } ``` - **跨域配置**:添加CORS配置类解决跨域问题[^1] #### 3. 核心功能开发 - **用户身份认证**: (1) 小程序端调用`wx.login()`获取code[^4] (2) Java后端通过code换取openid: ```java String url = "https://api.weixin.qq.com/sns/jscode2session?" + "appid=" + appId + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code"; ``` - **数据加解密**:使用`WxMaCryptUtils`处理加密数据(如获取手机号) #### 4. 部署与联调 - **本地测试**: - 后端使用`spring-boot-maven-plugin`打包运行 - 小程序端设置`request`域名白名单 ```javascript wx.request({ url: 'http://localhost:8080/api/test', method: 'GET', success: res => console.log(res.data) }) ``` - **生产部署**: - 服务器需备案域名 - 配置HTTPS证书(微信强制要求) #### 5. 官方资源指引 - 接口文档:微信官方开发文档[^2] - 加解密工具:微信提供Java版加解密库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随风奔跑的十八岁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值