Giter VIP home page Giter VIP logo

wechatofficialaccount's Introduction

WeChatOfficialAccount

基于SSM框架开发微信公众号 该项目主要是用来练习开发微信公众号的,使用SpringMVC、Spring、MyBatis框架构建项目,通过Maven管理jar包。

开发前你需要做的

  • 申请测试账号

  因为微信公众号的注册有一定的门槛,某些高级接口的权限需要微信认证后才可以获取,所以我申请的是测试号。申请成功后,在登录界面会有测试号的信息: image

  • 服务器接口配置

  公众号开发前需要对消息进行验证,是否来自于服务器,具体验证过程可以浏览微信公众号开发文档,官方也给出了代码示例。接下来就是接口信息配置,我使用的是一款内网穿透工具 ngrok,好处就是将本机地址映射到外网中,方便于测试使用: image
image
  token值应该与你代码中设置的token保持一致。填写好后提交,如果出现配置失败,多提交两三次,即可,如果还是失败,那就要检查你的代码了。到这里微信开发的基本配置算是弄好了。

  • 设置access_token值

  access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。这里我使用了SpringMVC里的一个定时器,每两小时获取一次access_token的值存入数据库:

    /**
     * 每两小时获取一次access_token值,并将access_token值存入数据库中
     */
    @Scheduled(cron = "0 0 */2 * * ?")
    public void getAccessToken() throws IOException {
        System.out.println("获取access_token值:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        AccessToken accessToken = CommonUtil.getAccessToken();
        System.out.println(accessToken.getAccess_token());
        System.out.println(accessToken.getExpires_in());

        // 将access_token存入数据库
        weChatService.insertAccessToken(accessToken);

    }

  以及手动获取access_token的值:

    /**
     * 手动获取access_token值: 如果当前时间与数据库里的最新access_token值的时间相差大于7200秒,则access_token过期,需要重新获取access_token值,并入库
     * @return
     * @throws IOException
     */
    public String queryLatestAccessToken() throws IOException {
        AccessToken accessToken = weChatService.queryLatestAccessToken();

        long curMillis = DateUtil.getCurrentMillis();
        long myCustomMillis = DateUtil.getCustomDateMillis(accessToken.getCreate_time());
        if ((curMillis - myCustomMillis) / 1000 >= 7200) {
            System.out.println("access_token值已过期,重新获取并入库......");
            accessToken = CommonUtil.getAccessToken();
            weChatService.insertAccessToken(accessToken);
        }

        return accessToken.getAccess_token();
    }

功能

  可以对自定义菜单进行创建、删除、事件推送,创建、删除、查询个性化菜单;可以接收普通消息,接收事件推送、被动回复用户消息。

优点

  使用SSM框架,对代码有很强的复用性和扩展性,并且很稳定。后续增加新的功能,可以直接在现有代码上进行二次开发。

交流一下

  image

wechatofficialaccount's People

Contributors

jenkinwang avatar

Stargazers

 avatar wanhaibing avatar  avatar Du Yanning avatar  avatar

Watchers

James Cloos avatar

wechatofficialaccount's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.