zoukankan      html  css  js  c++  java
  • 微信公众号(个人订阅号)

     公众号分类


    1.  订阅号(个人)

    2 服务号(公司-工商营业执照、个体户-工商营业执照)

    3 企业号(公司-工商营业执照)

    注册公众平台账号

    1 个人只能注册订阅号 :https://mp.weixin.qq.com

    2 订阅号 服务号 企业号 三者能使用的功能不相同

    3 注册的账号需要跟微信号绑定(微信号需要绑定银行卡)
        3.1 注册地址: https://mp.weixin.qq.com/cgi-bin/readtemplate?t=register/step1_tmpl&lang=zh_CN
      3.2 注册需要邮箱激活

    开通测试号

    登录后找到开发者工具:

    微信号、appID、appsecret这三个是开发项目中所用到的而且是唯一的

    因为微信不能用IP直接访问的,需要进行ip映射绑定的域名

     

     配置内网穿透工具 

    https://natapp.cn/

    注册账号(需要绑定身份证和支付宝)

    注册完点击购买隧道,选择免费隧道

     然后去首页下载客户端,选择自己的系统

     下载完解压后

     点击1分钟快速新手图文教程

     然后把config.ini放入下载客户端目录下:

    把token配进config.ini中

    运行exe文件

     

     配置项目的url地址 :

    项目配置:

    项目地址:https://github.com/chenjiahao12/wx01

    项目目录:

     

    application.yml项目相关的配置数据库连接,redis的配置

    weixin4j.properties就是配置ID就是登陆微信公众号的那三个

     IndexController

    package com.cjh.wx01.controller;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    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 java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    @Controller
    @Slf4j
    public class IndexController {
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
        @RequestMapping("/hello")
        @ResponseBody
        public Map<String, Object> hello() {
    //        log.info("hello");
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("code", 0);
            map.put("message", "xyz");
    
            return map;
        }
    
        @RequestMapping("/toMain")
        public String toMain(Model model) {
    //        log.info("toMain");
            Integer n = 1;
            String loginCount = stringRedisTemplate.opsForValue().get("loginCount");
            if (null != loginCount) {
                n = Integer.parseInt(loginCount) + 1;
            }
            stringRedisTemplate.opsForValue().set("loginCount", n.toString());
    
            model.addAttribute("now", new Date().toLocaleString()+",XXXXXYYYYYYzzzzAAAA");
            model.addAttribute("loginCount", n);
    
            return "main";
        }
    
        @RequestMapping("")
        public String toIndex() {
            return "index";
        }
    
        @RequestMapping("/toHello")
        public String toHello() {
            return "hello";
        }
    }

    启动项目:

    因为我们配置了ip映射域名所有直接用域名访问项目:

     配置weixin接入Controller 

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

    这样我们就可以进行开发了

     自定义菜单:

    weixinController

    package com.cjh.wx01.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.RequestBody;
    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.WeixinBuilder;
    import org.weixin4j.component.MenuComponent;
    import org.weixin4j.factory.WeixinFactory;
    import org.weixin4j.factory.defaults.DefaultWeixinFactory;
    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 sun.applet.Main;
    
    import javax.annotation.Resource;
    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
    public class WeixinController {
    
    
        @Autowired
        private WeixinTemplate weixinTemplate;
    
        @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", "微信菜单创建成功");
            try {
                Menu menu = new Menu();
                //创建菜单按钮
                List<SingleButton> buttons = new ArrayList<SingleButton>();
                menu.setButton(buttons);
    
                SingleButton btn1 = new ViewButton("主界面", ctx + "/");
                buttons.add(btn1);
    
                SingleButton btn2 = new ViewButton("界面Y", ctx + "/toHello");
                buttons.add(btn2);
    
                SingleButton btn3 = new ViewButton("界面X", ctx + "/toHello");
                buttons.add(btn2);
    
                //设置子菜单
                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;
        }
    
        public static void main(String[] args) throws Exception {
            Weixin weixin = new Weixin();
            weixin.base().token();
            Token token = weixin.getToken();
            System.out.println(token);
        }
    
    }

    需要接收weixin发送过来的token才能进行操作,而我们又是用redis来存储我们的token令牌的,所以我们需要开启vm,启动redis

    ./src/redis-service redis.conf

    然后运行方法创建菜单:

     可以看到redis也存储了一个token令牌

     

    进入我们自己的开发者中心,扫描关注一下,进入我们的订阅号就能看见我们的菜单了

     

    weixin这个包里面主要是我们对接收的各类消息进行处理,以及我们redis的工具类



  • 相关阅读:
    微信小程序代码大全
    【活动发布】捷微H5-微信新年砍价活动,开源发布了
    小程序官网CMS开源项目出炉,Weixin-App-CMS 1.0 版本正式发布
    jeecg开源项目的IDEA的部署
    1024程序员节宅男节日快乐 -- JAVA快速开发平台,JEECG 3.8宅男优化版本发布
    微信小程序商城开源项目,Weixin-App-Shop 1.0 版本正式发布!!!
    JEECG-Swagger UI的使用说明
    JEECG 上传插件升级-代码生成器
    Why 0.1 + 0.2 === 0.30000000000000004 ?
    linux (一)
  • 原文地址:https://www.cnblogs.com/chenjiahao9527/p/12102613.html
Copyright © 2011-2022 走看看