微信第三方平台创建小程序

准备工作

第三方收集法人微信、法人姓名、企业名称、信用代码四个商户信息外加第三方客服电话,用以调用接口创建小程序;参考文档

发送请求

package com.litte.util;

import com.litte.entity.reception.TCreateApplets;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: litte
 * @description: 第三方创建小程序工具类
 * @author: Mr.Jkx
 * @create: 2019-10-14 11:11
 */
public class CreateApplets {
    private static final Logger LOGGER = LoggerFactory.getLogger(CreateApplets.class);
    // 创建小程序接口
    private static final String CREATE_APPLETS_URL = "https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=create&component_access_token=TOKEN";
    // 查询创建任务状态
    private static final String CREATE_APPLETS_STATUS_URL = "https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=search&component_access_token=TOKEN";

    /**
     * @Description: 第三方创建小程序
     * @Author: Mr.Jkx
     * @date: 2019/10/14 11:37
     */
    public static Map<String, Object> create(TCreateApplets tCreateApplets) throws IOException {
        Map<String, Object> map = new HashMap<>();
        JSONObject jsonData = new JSONObject();
        jsonData.accumulate("name", tCreateApplets.getCompanyName());
        jsonData.accumulate("code", tCreateApplets.getCreditCode());
        jsonData.accumulate("code_type", tCreateApplets.getCompanyCodeType());
        jsonData.accumulate("legal_persona_wechat", tCreateApplets.getFrWx());
        jsonData.accumulate("legal_persona_name", tCreateApplets.getFrName());
        jsonData.accumulate("component_phone", tCreateApplets.getKfTel());
        String url = CREATE_APPLETS_URL.replace("TOKEN", tCreateApplets.getComponentAccessToken());
        JSONObject retStr = WinxinUtil.doPostStr(url, jsonData.toString());
        LOGGER.info("==========第三方创建小程序=====回执信息{}", retStr);
        if (StringUtils.equals("0", retStr.getString("errcode"))) {
            map.put("statusCode", "200");
        }
        return map;
    }

    /**
     * @Description: 查询创建任务状态
     * @Author: Mr.Jkx
     * @date: 2019/10/14 11:53
     */
    public static Map<String, Object> createStatus(TCreateApplets tCreateApplets) throws IOException {
        Map<String, Object> map = new HashMap<>();
        JSONObject jsonData = new JSONObject();
        jsonData.accumulate("name", tCreateApplets.getCompanyName());
        jsonData.accumulate("legal_persona_wechat", tCreateApplets.getFrWx());
        jsonData.accumulate("legal_persona_name", tCreateApplets.getFrName());
        String url = CREATE_APPLETS_STATUS_URL.replace("TOKEN", tCreateApplets.getComponentAccessToken());
        JSONObject retStr = WinxinUtil.doPostStr(url, jsonData.toString());
        LOGGER.info("==========第三方创建小程序,查询创建任务状态=====回执信息{}", retStr);
        if (StringUtils.equals("0", retStr.getString("errcode"))) {
            map.put("msg", "创建成功!");
        } else if (StringUtils.equals("89249", retStr.getString("errcode"))) {
            map.put("msg", "该主体已有任务执行中,距创建任务 24h 后再试!");
        } else if (StringUtils.equals("86004", retStr.getString("errcode"))) {
            map.put("msg", "微信号无效!");
        } else if (StringUtils.equals("61070", retStr.getString("errcode"))) {
            map.put("msg", "法人姓名与微信号不一致!");
        } else if (StringUtils.equals("89248", retStr.getString("errcode"))) {
            map.put("msg", "企业代码类型无效,请选择正确类型填写!");
        } else if (StringUtils.equals("89250", retStr.getString("errcode"))) {
            map.put("msg", "未找到该任务!");
        } else if (StringUtils.equals("89251", retStr.getString("errcode"))) {
            map.put("msg", "待法人人脸核身校验!");
        } else if (StringUtils.equals("89252", retStr.getString("errcode"))) {
            map.put("msg", "法人或者企业信息一致性校验中!");
        } else {
            map.put("msg", "查询失败,请联系管理员!");
        }
        return map;
    }
}

接受微信事件推送

package com.litte.controller.warrant;

import com.litte.entity.reception.TCreateApplets;
import com.litte.service.createapplets.CreateAppletsService;
import com.litte.service.warrantinfo.TWarrantInfoService;
import com.litte.util.constant.Constants;
import com.litte.util.constant.StatusConstants;
import com.litte.util.warrant.AesException;
import com.litte.util.warrant.WXBizMsgCrypt;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * @Description: 小程序推送授权信息接收
 * @Author: Mr.Jkx
 * @date: 2020/7/21 10:32
 */
@RestController
@RequestMapping("/warrant")
public class WarrantController {
    private static final Logger LOGGER = LoggerFactory.getLogger(WarrantController.class);
    @Resource
    private TWarrantInfoService tWarrantInfoService;
    @Resource
    private CreateAppletsService createAppletsService;

    /**
     * 获取"component_verify_ticket"
     *
     * @Description:
     * @Author: Mr.Jkx
     * @Date: 2019/4/3 16:03
     * 参考链接:https://blog.csdn.net/liaoyundababe/article/details/53537417
     * https://blog.csdn.net/zhangdaiscott/article/details/48269837
     */
    @RequestMapping("/responseRequest")
    public void responseRequest(HttpServletRequest request, HttpServletResponse response) throws AesException, IOException, DocumentException {
        LOGGER.info("WeChat third-party platform --------- WeChat push Ticket message 10 minutes-----------");
        // 输出响应的内容。
        output(response, "success");
        processAuthorizeEvent(request);
    }

    /**
     * 处理授权事件的推送
     *
     * @param request
     * @throws IOException
     * @throws AesException
     * @throws DocumentException
     */
    public void processAuthorizeEvent(HttpServletRequest request)
            throws IOException, AesException, DocumentException {
        StringBuilder sb = new StringBuilder();
        BufferedReader in = request.getReader();
        String line;
        while ((line = in.readLine()) != null) {
            sb.append(line);
        }
        String xml = sb.toString();
        LOGGER.info("Third-party platform is released on the whole network-----------------------original, Xml:{}", xml);

        String nonce = request.getParameter("nonce");
        String timestamp = request.getParameter("timestamp");
        String msgSignature = request.getParameter("msg_signature");
        LOGGER.info("=====WeChat third-party platform======" + nonce + "  " + timestamp + "  " + msgSignature);
        WXBizMsgCrypt pc = new WXBizMsgCrypt(Constants.THIRD_PARTY_TOKEN, Constants.THIRD_PARTY_ENCODINGAESKEY, Constants.THIRD_PARTY_APPID);
        LOGGER.info("Third-party platform is released on the whole network-----------------------decryption WXBizMsgCrypt new 成功");
        String xml1 = pc.decryptMsg(msgSignature, timestamp, nonce, xml);
        LOGGER.info("Third-party platform is released on the whole network-----------------------After decryption, Xml:{}", xml1);

        // 解析xml
        Document doc = DocumentHelper.parseText(xml1);
        Element rootElt = doc.getRootElement();
        String infoType = rootElt.elementText("InfoType");
        LOGGER.info("------- 事件推送类型:{} -------", infoType);

        if (StringUtils.equals("notify_third_fasteregister", infoType)) {
            LOGGER.info("------- 创建小程序验证通过解析推送数据:{} -------", xml1);
            resolveCreateAppletsData(xml1);
        }
    }

    /**
     * @Description: 第三方创建小程序验证通过之后,解析微信推送的小程序信息
     * 参考链接:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Mini_Programs/Fast_Registration_Interface_document.html
     * <xml>
     * <AppId><![CDATA[第三方平台appid]]></AppId>
     * <CreateTime>1535442403</CreateTime>
     * <InfoType><![CDATA[notify_third_fasteregister]]></InfoType>
     * <appid>创建小程序appid<appid>
     * <status>0</status>
     * <auth_code>xxxxx第三方授权码</auth_code>
     * <msg>OK</msg>
     * <info>
     * <name><![CDATA[企业名称]]></name>
     * <code><![CDATA[企业代码]]></code>
     * <code_type>1</code_type>
     * <legal_persona_wechat><![CDATA[法人微信号]]></legal_persona_wechat>
     * <legal_persona_name><![CDATA[法人姓名]]></legal_persona_name>
     * <component_phone><![CDATA[第三方联系电话]]></component_phone>
     * </info>
     * </xml>
     * @Author: Mr.Jkx
     * @date: 2019/10/14 17:25
     */
    public void resolveCreateAppletsData(String xml) {
        LOGGER.info("------创建小程序推送消息------", xml);
        Document doc;
        try {
            doc = DocumentHelper.parseText(xml);
            Element rootElt = doc.getRootElement();
            String status = rootElt.elementText("status");
            // 判断时候验证成功
            if (StringUtils.equals("0", status)) {
                TCreateApplets tCreateApplets = new TCreateApplets();
                tCreateApplets.setaCompanyName(rootElt.elementText("name"));
                tCreateApplets.setaCode(rootElt.elementText("code"));
                tCreateApplets.setaLegalPersonaName(rootElt.elementText("legal_persona_wechat"));
                tCreateApplets.setaLegalPersonaName(rootElt.elementText("legal_persona_name"));
                tCreateApplets.setaThirdAppid(rootElt.elementText("AppId"));
                tCreateApplets.setaAppid(rootElt.elementText("appid"));
                tCreateApplets.setaAuthCode(rootElt.elementText("auth_code"));
                tCreateApplets.setaStatus(StatusConstants.CreaterAppletStatus.APPLETSTATUS1);
                tCreateApplets.setaUpdateTime(new Date());
                // 根据企业及法人信息修改信息
                int i = createAppletsService.updCreateApplets(tCreateApplets);
                if (i > 0) {
                    LOGGER.info("---创建小程序推动消息,修改数据库信息成功!---");
                } else {
                    LOGGER.info("---创建小程序推动消息,修改数据库信息失败!---");
                }
            } else {
                LOGGER.info("------创建小程序推送消息,验证未通过,原因:{}------", rootElt.elementText("msg"));
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 工具类:回复微信服务器"文本消息"
     *
     * @param response
     * @param returnvaleue
     */
    public void output(HttpServletResponse response, String returnvaleue) {
        try {
            PrintWriter pw = response.getWriter();
            pw.write(returnvaleue);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值