后端集成微信公众号查询天气预报功能

 

1、准备外网穿透,外网映射工具有NatApp、花生壳、ngrok

以下本人使用NatApp,进入NatApp官网下载软件:https://natapp.cn/并注册登录进去

2、配置NatApp隧道

2、启动NatApp的使用 

NatApp软件中输入以下命令启动:13fe978af964cf43 authtoken

natapp -authtoken= 13fe978ag964cf43 

出现以下界面说明启动成功

二、微信公众号开发平台

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login&token=579271486&lang=zh_CN

微信扫描登录后会出现以下界面

get接口是为测试验证接口

相关技术文档

https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html

微服务服务整合微服务开发框架

Maven依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

核心代码

@Component
public class MsgHandler extends AbstractHandler {
    @Autowired
    private KeywordMapper keywordMapper;
    @Value("${cyb.wx.defaultMsg}")
    private String defaultMsg;

    /**
     * 第三方天气接口
     */
    @Value("${cyb.wx.weatherUrl}")
    private String weatherUrl;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {

        if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
            //TODO 可以选择将消息保存到本地
        }
        String msgContent = wxMessage.getContent();
        // 根据msgContent查询关键字表
        WechatKeyword wechatKeyword = keywordMapper.findByKeyword(msgContent);
        String resultMsg = null;
        if (wechatKeyword != null) {
            resultMsg = wechatKeyword.getKeywordValue();
            return new TextBuilder().build(resultMsg, wxMessage, weixinService);
        }
        // 2.查询第三方天气接口
        String rpcWeatherUrl = weatherUrl.replaceFirst("#location#", msgContent);
        JSONObject resultJsonObject = HttpClientUtils.httpGet(rpcWeatherUrl);
        if (resultJsonObject != null) {
            JSONArray results = resultJsonObject.getJSONArray("results");
            JSONObject resultsZeroJSONObject = results.getJSONObject(0);
            JSONObject locationJSONObject = resultsZeroJSONObject.getJSONObject("location");
            // 地址
            String path = locationJSONObject.getString("path");
            JSONObject nowJSONObject = resultsZeroJSONObject.getJSONObject("now");

            String text = nowJSONObject.getString("text");
            String temperature = nowJSONObject.getString("temperature");
            String lastUpdate = resultsZeroJSONObject.getString("last_update");
            resultMsg = "您当前查询的城市" + msgContent + ",天气为" + text + "天、实时温度为:" + temperature + "℃," +
                    "最后更新的时间为:" +lastUpdate;
            return new TextBuilder().build(resultMsg, wxMessage, weixinService);
        }
        // 查询默认的
        return new TextBuilder().build(defaultMsg, wxMessage, weixinService);
    }

}

 数据库

import com.cyb.weixin.entity.WechatKeyword;
import org.apache.ibatis.annotations.Select;


public interface KeywordMapper {
    @Select("SELECT  id as id ,keyword_name as keywordname,\n" +
            "keyword_value as keywordvalue,create_time as createtime,\n" +
            "update_time as updatetime ,version  as version\n" +
            "  FROM wechat_keywords where keyword_name=#{keyword};")
    WechatKeyword findByKeyword(String keyword);
}

@Data
public class WechatKeyword {
    private Long id;
    private String keywordName;
    private String keywordValue;
    private Date createTime;
    private Date updateTime;
    private Long version;
}

  数据库表结构

CREATE TABLE `wechat_keywords` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `keyword_name` varchar(255) DEFAULT NULL,
  `keyword_value` varchar(255) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  `version` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of wechat_keywords

  新增配置文件

logging:
  level:
    org.springframework.web: INFO
    com.github.binarywang.demo.wx.mp: DEBUG
    me.chanjar.weixin: DEBUG
wx:
  mp:
    configs:
      - appId: wx5c43fde3c9733d9e
        secret: b8b217126c33a5fb7074927d5e72a81a
        token: mayikt

cyb:
   wx:
     defaultMsg: 您的消息我已经收到了,后期会给您回复的噢!
     weatherUrl: http

  整合第三方天气预报接口

心知天气接口网站:https://www.seniverse.com/ 登录进入该网站

  

  

 

 注意:在启动的时候要在启动类中加入mapper扫包范围

具体代码请查看本人百度网盘:对应文件链接:链接:https://pan.baidu.com/s/1KJxGXxlLtJKatT99FUwZ0Q 提取码:8yge 

说明:本人该篇博客主要用作于本人笔记及提供给在此阶段需要参考的博友,本人以下内容参考:余胜军,请大家在转载的时候说明出处

 

 

 

 

 

 

 

 

posted @ 2020-03-14 16:34  陈远波  阅读(745)  评论(0编辑  收藏  举报