在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Web服务/SOAP/RPC → 微信公共号配置案例源码(含小程序调用的发送模板消息接口)

微信公共号配置案例源码(含小程序调用的发送模板消息接口)

Web服务/SOAP/RPC

下载此实例
  • 开发语言:Java
  • 实例大小:0.07M
  • 下载次数:24
  • 浏览次数:259
  • 发布时间:2020-05-26
  • 实例类别:Web服务/SOAP/RPC
  • 发 布 人:woainiapple7351
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 公共号 配置 案例 源码 微信

实例介绍

【实例简介】

# 微信生态开发常用接口使用案例
#### 本项目是一个微信生态开发常用接口的案例集合,其中包括:解密小程序code获取openid,发送小程序模板消息,发送公众号模板消息,用户关注、取关公众号的事件处理等。

#### 扩展知识  
openid:一个小程序的每个用户都有一个唯一的openid,是一个小程序用户中用户的唯一身份标志,公众号也一样;  
unionid:用户的小程序、公众号可在微信开放平台进行关联,关联之后的小程序和公众号有一个相同的unionid,公众号始终有unionid,小程序如果没有和公众号关联是
没有unionid的。

### 1.0:微信小程序解密code得到openid([官方文档](https://developers.weixin.qq.com/miniprogram/dev/api/wx.login.html))
过程如下:  
1.前端传入code到后端;  
2.后端解密code得到openid、session_key、unionid(小程序和公众号关联后才能解密出unionid)。

### 1.1:发送小程序模板消息([官方文档](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html))
过程如下:  
必备知识:发送模板消息到指定用户需要用户的formid和openid,formid是用户进行表单提交时微信生成的一个序列号,有效期为一周,开发者每收集一个用户的
formid即可以向该用户发送一次模板消息。  
1.收集用户的formid;  
2.向指定用户发送模板消息。

### 1.2:对微信公众号进行服务器配置校验token([参考博客](https://blog.csdn.net/LONG_Yi_1994/article/details/90022307))
过程如下:  
如果进行微信公众号开发,进行服务器配置是必要的,通过配置,用户与公众号的事件交互消息将发送至开发者配置的URL上,开发者可在自己写的接口内进行事件处理。  
1.填写配置信息,确保URL可访问;  
2.开发token验证接口。

### 1.3:用户与公众号事件交互处理(包括用户关注、取关公众号事件)
过程如下:  
1:用户关注或取关公众号后,微信将把事件消息推送至开发者填写的URL上,开发者可在接口内进行处理,例如:保存用户openid,依据openid得到unionid等。

### 1.4:发送公众号模板消息([官方文档](https://mp.weixin.qq.com/advanced/tmplmsg?action=faq&token=611048160&lang=zh_CN))
过程如下:  
1:发送公众号模板消息和发送小程序模板消息类似,只是发送公众号模板消息是没有限制的,有用户的openid即可发送。



【实例截图】

【核心代码】


package com.longdatech.decryptcode.controller;

import com.alibaba.fastjson.JSONObject;
import com.longdatech.decryptcode.service.LdkjWxApiService;
import com.longdatech.decryptcode.utils.MyHttpRequestUtil;
import com.longdatech.decryptcode.utils.SignUtil;
import com.longdatech.decryptcode.utils.WxConstant;
import com.longdatech.decryptcode.utils.WxMessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Set;


@Slf4j
@Api(description = "微信接口相关控制器")
@RestController
@RequestMapping("/wxapi")
public class Ldkj_WxApiController {

    @Autowired
    private LdkjWxApiService ldkjWxApiService;

    /**
     * @description 解密code得到openid
     * @author: liyinlong
     * @date 2019-05-05 13:58
     * @param code
     * @return
     */
    @ApiOperation("1.0:解密code")
    @GetMapping("/decryptCode")
    public String decryptCode(@RequestParam String code){
        log.info("1.0:解密code===>code:"   code);
        String result = MyHttpRequestUtil.sendGet(WxConstant.getDesryptCodeUri(code));
        JSONObject ojb = JSONObject.parseObject(result);
        System.out.println("返回结果:"   ojb);
        Set<String> keys = ojb.keySet();
        keys.forEach(item->{
            System.out.println(item   ":"   ojb.get(item));
        });
        return ojb.getString("openid");
    }

    /**
     * @description 小程序发送模板消息
     * @author: liyinlong
     * @date 2019-05-05 13:58
     * @return
     */
    @ApiOperation("1.1:发送小程序模板消息")
    @GetMapping("/sendMiniTemplateMessage")
    public String sendMiniTemplateMessage(@RequestParam String formId,@RequestParam String openId){
        log.info("1.1:发送小程序模板消息===>formId:"   formId   ",openId:"   openId);
        ldkjWxApiService.sendMiniTemplate(formId,openId);
        return "success";
    }

    /**
     * @description 微信公众号服务器配置校验token
     * @author: liyinlong
     * @date 2019-05-09 9:38
     * @return
     */
    @ApiOperation("1.2:微信公众号服务器配置校验token")
    @RequestMapping("/checkToken")
    public void checkToken(HttpServletRequest request,HttpServletResponse response){
        log.info("1.2:微信公众号服务器配置校验token");
        //token验证代码段
        try{
            log.info("请求已到达,开始校验token");
            if (StringUtils.isNotBlank(request.getParameter("signature"))) {
                String signature = request.getParameter("signature");
                String timestamp = request.getParameter("timestamp");
                String nonce = request.getParameter("nonce");
                String echostr = request.getParameter("echostr");
                log.info("signature[{}], timestamp[{}], nonce[{}], echostr[{}]", signature, timestamp, nonce, echostr);
                if (SignUtil.checkSignature(signature, timestamp, nonce)) {
                    log.info("数据源为微信后台,将echostr[{}]返回!", echostr);
                    response.getOutputStream().println(echostr);
                }
            }
        }catch (IOException e){
            log.error("校验出错");
            e.printStackTrace();
        }
    }

    /**
     * @description 该接口url应与服务器配置中填写的URL保持一致,因校验token与处理用户校验事件都只
     * 能用一个URL,所以可以在token验证之后把1.2接口注释掉,把本接口名称改为1.2接口名称
     * @author: liyinlong
     * @date 2019-05-09 12:10
     * @param request
     * @param response
     * @return
     */
    @ApiOperation("1.3:用户与公众号交互事件处理")
    @RequestMapping("/handlePubFocus")
    public String handlePubFocus(HttpServletRequest request,HttpServletResponse response){
        log.info("1.3:用户与公众号交互事件处理");
        try{
            Map<String ,String > requestMap = WxMessageUtil.parseXml(request);
            Set<String> keys = requestMap.keySet();
            keys.forEach(item->{
                String value = requestMap.get(item);
                log.info(item   "===>"   value);
            });
            String messageType = requestMap.get("MsgType");
            String eventType = requestMap.get("Event");
            String openid = requestMap.get("FromUserName");
            if(messageType.equals("event")){
                //判断消息类型是否是事件消息类型
                log.info("公众号====>事件消息");
                log.info("openid:"   openid);
                log.info("Event:"   eventType);
                if(eventType.equals("subscribe")){
                    log.info("公众号====>新用户关注");
                }else if(eventType.equals("unsubscribe")){
                    log.info("公众号====>用户取消关注");
                }else{
                    log.info("公众号===>其他");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    /**
     * @description 公众号发送模板消息
     * @author: liyinlong
     * @date 2019-05-05 13:58
     * @return
     */
    @ApiOperation("1.4:发送公众号模板消息")
    @GetMapping("/sendPubTemplateMessage")
    public String sendPubTemplateMessage(@RequestParam String openId){
        log.info("1.4:发送公众号模板消息===>openId:"   openId);
        ldkjWxApiService.sendPubTemplateMessage(openId);
        return "success";
    }

    /**
     * @description 生成小程序二维码 官方文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html
     * @author: liyinlong
     * @date 2019-05-09 15:00
     * @param strinfo
     * @return
     */
    @ApiOperation("1.5:生成小程序二维码")
    @GetMapping("/createQrCode")
    public String createQrCode(String strinfo){
        ldkjWxApiService.createQrCode(strinfo);
        return "success";
    }




}


实例下载地址

微信公共号配置案例源码(含小程序调用的发送模板消息接口)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警