微信程序开发之微信

一、 微信公众号

1、详情网址

微信开发平台(微信公众平台

微信官方文档(微信官方文档 | 微信开放文档

微信公众平台接口调试工具(微信公众平台接口调试工具

内网穿透工具(几款免费内网穿透工具测评使用 - 哔哩哔哩

2、使用测试号

①、微信公众平台

可以进行登录或注册:

 公众号分类

 使用微信测试号登陆

获取令牌access_token

access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。

 使用网页调试工具

使用网页调试工具调试该接口

生成的令牌不要存在数据里面因为它只会存在俩小时 

 生成菜单栏

请注意:

  1. 自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。
  2. 一级菜单最多4个汉字,二级菜单最多8个汉字,多出来的部分将会以“...”代替。
  3. 创建自定义菜单后,菜单的刷新策略是,在用户进入公众号会话页或公众号profile页时,如果发现上一次拉取菜单的请求在5分钟以前,就会拉取一下菜单,如果菜单有更新,就会刷新客户端的菜单。测试时可以尝试取消关注公众账号后再次关注,则可以看到创建后的效果。

内容body

{
    "button": [
        {
            "name": "扫码", 
            "sub_button": [
                {
                    "type": "scancode_waitmsg", 
                    "name": "扫码带提示", 
                    "key": "rselfmenu_0_0", 
                    "sub_button": [ ]
                }, 
                {
                    "type": "scancode_push", 
                    "name": "扫码推事件", 
                    "key": "rselfmenu_0_1", 
                    "sub_button": [ ]
                }
            ]
        }, 
        {
            "name": "发图", 
            "sub_button": [
                {
                    "type": "pic_sysphoto", 
                    "name": "系统拍照发图", 
                    "key": "rselfmenu_1_0", 
                    "sub_button": [ ]
                }, 
                {
                    "type": "pic_photo_or_album", 
                    "name": "拍照或者相册发图", 
                    "key": "rselfmenu_1_1", 
                    "sub_button": [ ]
                }, 
                {
                    "type": "pic_weixin", 
                    "name": "微信相册发图", 
                    "key": "rselfmenu_1_2", 
                    "sub_button": [ ]
                }
            ]
        }
    ]
}

 接口配置信息

URL:ip需要与域名绑定,并且需要到公安/网上备案

所以此处不支持IP的,将它变成域名,我使用了内网穿透工具:NATAPP-内网穿透 基于ngrok的国内高速内网映射工具

 

 注册一个账号  记得实名不然用不了 

 可以购买免费的隧道

 

 下载客户端然后解压

 需要找到兼容特殊编码的config.ini文件

#将本文件放置于natapp同级目录 程序将读取 [default] 段
#在命令行参数模式如 natapp -authtoken=xxx 等相同参数将会覆盖掉此配置
#命令行参数 -config= 可以指定任意config.ini文件
[default]
authtoken=2d435010b1baff62                     #对应一条隧道的authtoken
clienttoken=                    #对应客户端的clienttoken,将会忽略authtoken,若无请留空,
log=none                        #log 日志文件,可指定本地文件, none=不做记录,stdout=直接屏幕输出 ,默认为none
loglevel=ERROR                  #日志等级 DEBUG, INFO, WARNING, ERROR 默认为 DEBUG
http_proxy=                     #代理设置 如 http10.123.10.103128 非代理上网用户请务必留空

进入cmd 输入natapp

本地还没有开8080端口 

 

配置后台,开启8080端口

token要保持一致

 开启redis,在yml文件中配置

server:
  port: 8080
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/wx?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: root123
  redis:
    database: 0                           # Redis数据库索引(默认为0 redis有16个库)
    host: 47.100.191.44                      # Redis服务器地址
    port: 6379                            # Redis服务器连接端口
    password: xiaoli_redis                     # Redis服务器连接密码(默认为空)
    jedis:
      pool:
        max-active: 100                   # 连接池最大连接数(使用负值表示没有限制)
        max-idle: 10                      # 连接池中的最大空闲连接
        min-idle: 0                       # 连接池中的最小空闲连接
        max-wait: -1                      # 连接池最大阻塞等待时间(使用负值表示没有限制)
    timeout: 5000                         # 连接超时时间(毫秒),不能设置为0,时间太短连接不上会报错
  freemarker:
    allow-request-override: false
    request-context-attribute: req
    suffix: .ftl
    content-type: text/html;charset=UTF-8
    enabled: true
    cache: false
    template-loader-path: classpath:/templates/
    charset: UTF-8
  application:
    name: wx

导入依赖

<dependency>
            <groupId>org.weixin4j.spring.boot</groupId>
            <artifactId>weixin4j-spring-boot-starter</artifactId>
            <version>1.0.0</version>
        </dependency>

后台文件:

package com.yk.wx.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.weixin4j.spring.web.WeixinJieruController;
 
/**
 * 微信开发者接入
 */
@Controller
@RequestMapping("/jieru")
@SuppressWarnings("all")
public class JieruController extends WeixinJieruController {
}

在url后面加上jieru

 创建菜单:

package com.yk.wx.controller;
 
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.weixin4j.Configuration;
import org.weixin4j.Weixin;
import org.weixin4j.component.MenuComponent;
import org.weixin4j.model.base.Token;
import org.weixin4j.model.menu.Menu;
import org.weixin4j.model.menu.SingleButton;
import org.weixin4j.model.menu.ViewButton;
import org.weixin4j.spring.WeixinTemplate;
 
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Controller
@RequestMapping("/wx")
@Slf4j
@SuppressWarnings("all")
public class WeixinController {
 
    private WeixinTemplate weixinTemplate;
 
    @Autowired
    public WeixinController(WeixinTemplate weixinTemplate) {
        this.weixinTemplate = weixinTemplate;
    }
 
    public static void main(String[] args) throws Exception {
        Weixin weixin = new Weixin();
        weixin.base().token();
        Token token = weixin.getToken();
        System.out.println(token);
    }
 
    @RequestMapping("/createMenu")
    @ResponseBody
    public Map<String, Object> createMenu(Model model, HttpServletRequest request) {
        log.info("WeixinController.createMenu");
        String ctx = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
        log.info("ctx=" + ctx);
        Map<String, Object> jsonData = new HashMap<String, Object>();
        jsonData.put("code", 0);
        jsonData.put("message", "微信菜单创建成功,ts=" + System.currentTimeMillis());
        try {
            Menu menu = new Menu();
            //创建菜单按钮
            List<SingleButton> buttons = new ArrayList<SingleButton>();
            menu.setButton(buttons);
            SingleButton btn1 = new ViewButton("主界面", ctx + "/toMain");
            buttons.add(btn1);
            SingleButton btn2 = new ViewButton("更多", ctx + "/toHello");
            buttons.add(btn2);
            SingleButton btn3 = new ViewButton("还想要", ctx + "/toHello");
            buttons.add(btn3);
            //设置子菜单
            System.out.println(menu.toJSONObject().toString());
            //创建自定义菜单
            Weixin weixin = weixinTemplate.getWeixinFactory().getWeixin();
            MenuComponent menu1 = weixin.menu();
            menu1.create(menu);
            model.addAttribute("message", "微信菜单创建成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            jsonData.put("code", -1);
            jsonData.put("message", "微信菜单创建失败,原因:" + e.getMessage());
        }
        return jsonData;
    }
 
    @RequestMapping("/test1")
    @ResponseBody
    public Map<String, Object> test1() {
        log.info("WeixinController.test1");
        Map<String, Object> jsonData = new HashMap<String, Object>();
        System.out.println("test");
        String s1 = Configuration.getProperty("weixin4j.message.handler.normal");
        String s2 = Configuration.getProperty("weixin4j.message.handler.event");
        jsonData.put("weixin4j.message.handler.normal", s1);
        jsonData.put("weixin4j.message.handler.event", s2);
        return jsonData;
    }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值