Public No. Classification
1. The subscription number (individual)
2 service number (company - the business license, the self-employed - business license)
3 Enterprise (company - business license)
Sign up public platform account
1 person can only register subscription number: https: //mp.weixin.qq.com
No. 2 subscription service number Enterprise can use three different functions
3 registered account required to bind with the micro signal (micro signal needs to bind bank card)
3.1 Registered address: https://mp.weixin.qq.com/cgi-bin/readtemplate?t=register/step1_tmpl&lang=zh_CN
3.2 Registration You need to activate the mailbox
The opening Test No.
After logging in to find the Developer Tools:
Micro Signal, appID, appsecret three development projects is being used and is the only
Because the micro-channel can not be directly accessed by IP, the need for mapping ip binding domain
Network penetration within the configuration tool
Registered account (identity card and to be bound Alipay)
Completed registration click to buy tunnel, select the tunnel free
Then go home to download the client, to choose their own system
After downloading unzip
Click one minute fast novice graphic tutorials
Then download the config.ini placed at the client directory:
With the token into the config.ini
Run exe files
Configuring the project url address:
Project Configuration:
Project Address: https://github.com/chenjiahao12/wx01
Project directory:
application.yml project-related configuration database connection, redis configuration
weixin4j.properties is to configure ID number is landing micro letter public that three
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的工具类