package cn.haiwang.controller;
import cn.haiwang.cons.WxMpConstant;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.service.WxOAuth2Service;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
@RestController
@RequestMapping("/weixin")
@Slf4j
public class WxjavaTestController {
@Autowired
private WxMpService wxMpService;
@Autowired
private WxMpConfigStorage wxMpConfigStorage;
@Autowired
private WxMpMessageRouter wxMpMessageRouter;
/**
* 接口配置信息 中配置的url ,接收微信推送消息
* @param requestBody
* @param signature
* @param timestamp
* @param nonce
* @return
*/
@RequestMapping(value = "/portal", produces = "application/xml; charset=UTF-8")
public String handleMessage(@RequestBody String requestBody,
@RequestParam("signature") String signature,
@RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce) {
log.info("handleMessage调用");
System.out.println("wxMpService = " + wxMpService);
System.out.println("wxMpConfigStorage = " + wxMpConfigStorage);
System.out.println("wxMpConfigStorage.getAccessToken() = " + wxMpConfigStorage.getAccessToken());
// 校验消息是否来自微信
if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
}
// 解析消息体,封装为对象
WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
WxMpXmlOutMessage outMessage;
try {
// 将消息路由给对应的处理器,获取响应
outMessage = wxMpMessageRouter.route(inMessage);
} catch (Exception e) {
log.error("微信消息路由异常", e);
outMessage = null;
}
// 将响应消息转换为xml格式返回
return outMessage == null ? "" : outMessage.toXml();
}
/**
* 自定义公众号菜单
* @return
* @throws WxErrorException
*/
@GetMapping("/createMenu")
public String createMenu() throws WxErrorException {
// 创建菜单对象
WxMenu menu = new WxMenu();
// 创建按钮1
WxMenuButton button1 = new WxMenuButton();
button1.setType(WxConsts.MenuButtonType.CLICK);
button1.setName("今日歌曲");
button1.setKey(WxMpConstant.CLICK_MENU_KEY);
// 创建按钮2
WxMenuButton button2 = new WxMenuButton();
button2.setName("菜单");
// 创建按钮2的子按钮1
WxMenuButton button21 = new WxMenuButton();
button21.setType(WxConsts.MenuButtonType.VIEW);
button21.setName("搜索");
button21.setUrl("https://www.baidu.com/");
// 创建按钮2的子按钮2
WxMenuButton button22 = new WxMenuButton();
button22.setType(WxConsts.MenuButtonType.VIEW);
button22.setName("视频");
button22.setUrl("https://v.qq.com/");
// 创建按钮2的子按钮3
WxMenuButton button23 = new WxMenuButton();
button23.setType(WxConsts.MenuButtonType.CLICK);
button23.setName("赞一下我们");
button23.setKey("V1001_GOOD");
// 将子按钮添加到按钮2
button2.getSubButtons().add(button21);
button2.getSubButtons().add(button22);
button2.getSubButtons().add(button23);
// 将按钮1和你按钮2添加到菜单
menu.getButtons().add(button1);
menu.getButtons().add(button2);
// 创建按钮
return wxMpService.getMenuService().menuCreate(menu);
}
/**
* 发送模板消息
* @return
* @throws WxErrorException
*/
@GetMapping("/sendTemplateMessage")
public String sendTemplateMessage() throws WxErrorException {
System.out.println("wxMpConfigStorage.getAccessToken() = " + wxMpConfigStorage.getAccessToken());
// 创建模板消息,设置模板id、指定模板消息要发送的目标用户
WxMpTemplateMessage wxMpTemplateMessage = WxMpTemplateMessage.builder()
.templateId("9lK80vWuf7SUIuZD7cwfqeEN74RLxpEHAk8Hku6uvYw")
.toUser("o_m8e64x4ndYwy6cYcrxEbh0Wedc")
.build();
// 填充模板消息中的变量
wxMpTemplateMessage.addData(new WxMpTemplateData("goodsName", "华为mate40pro"));
wxMpTemplateMessage.addData(new WxMpTemplateData("time", "2020-10-25"));
wxMpTemplateMessage.addData(new WxMpTemplateData("price", "5854"));
wxMpTemplateMessage.addData(new WxMpTemplateData("beizhu", "麒麟9000牛逼"));
// 发送模板消息,返回消息id
System.out.println("wxMpConfigStorage.getAccessToken() = " + wxMpConfigStorage.getAccessToken());
return wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
}
/**
* 生成带参数的二维码
* @return
* @throws WxErrorException
*/
@RequestMapping("/qrcode")
public Object qrcode() throws WxErrorException {
// 获取二维码的ticket
// 获取临时二维码的ticket,其中scene为场景值,expire_seconds为二维码超时时间
WxMpQrCodeTicket ticket = wxMpService.getQrcodeService().qrCodeCreateTmpTicket("scene", 2592000);
// 获取永久二维码的ticket,其中scene为场景值
// WxMpQrCodeTicket ticket = wxMpService.getQrcodeService().qrCodeCreateLastTicket(scene);
// 用ticket换取二维码图片
// 获得在系统临时目录下的文件,需要自己保存使用,注意:临时文件夹下存放的文件不可靠,不要直接使用
File file = wxMpService.getQrcodeService().qrCodePicture(ticket);
return file;
}
/**
* 网页授权 ---- 第一步:构建授权页面url
* @param response
* @throws IOException
*/
@GetMapping("/buildAuthPage")
public void auth(HttpServletResponse response) throws IOException {
WxOAuth2Service oAuth2Service = wxMpService.getOAuth2Service();
// 构建授权url
String s = oAuth2Service.buildAuthorizationUrl("http://lingling.mynatapp.cc/weixin/callback",
WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
response.setContentType("text/html;charset=UTF-8");
response.sendRedirect(s);
}
/**
* 网页授权 ---- 第二步:
* 用户确认授权后的回调处理,利用code获取accessToken,再利用accessToken获取用户信息
* @param code
* @return
* @throws WxErrorException
*/
@GetMapping("/callback")
public WxOAuth2UserInfo callback(String code) throws WxErrorException {
没有合适的资源?快使用搜索试试~ 我知道了~
微信开发框架com.github.binarywang - WxJava 开发微信公众号程序
共39个文件
xml:11个
java:11个
class:11个
需积分: 5 0 下载量 144 浏览量
2024-02-15
15:16:13
上传
评论
收藏 30KB RAR 举报
温馨提示
使用 com.github.binarywang -- wx-java-mp-spring-boot-starter 开发微信公众号程序
资源推荐
资源详情
资源评论
收起资源包目录
微信开发框架com.github.binarywang - WxJava 开发微信公众号程序.rar (39个子文件)
weixin_tttt
pom.xml 2KB
src
main
resources
application.yml 157B
java
cn
haiwang
handle
SubscribeHandler.java 981B
TextHandler.java 1KB
ImageHandler.java 944B
EventHandler.java 1KB
UnSubscribeHandler.java 1KB
controller
WxjavaTestController.java 8KB
Weixin_ttt_Application.java 325B
cons
WxMpConstant.java 220B
config
WxMpConfiguration.java 2KB
WxJavaConfig.java 2KB
WxMpProperties.java 633B
weixin_tttt.iml 263B
.idea
jarRepositories.xml 879B
restkit
RESTKit_CommonSetting.xml 225B
RESTKit_Environment.xml 182B
uiDesigner.xml 9KB
qaplug_profiles.xml 422B
workspace.xml 5KB
misc.xml 572B
inspectionProfiles
Project_Default.xml 155B
compiler.xml 783B
.gitignore 184B
encodings.xml 191B
$PROJECT_FILE$ 364B
target
classes
cn
haiwang
handle
TextHandler.class 2KB
EventHandler.class 2KB
SubscribeHandler.class 2KB
UnSubscribeHandler.class 2KB
ImageHandler.class 2KB
controller
WxjavaTestController.class 9KB
Weixin_ttt_Application.class 742B
cons
WxMpConstant.class 386B
config
WxMpConfiguration.class 2KB
WxJavaConfig.class 2KB
WxMpProperties.class 3KB
application.yml 157B
generated-sources
annotations
.gitignore 395B
共 39 条
- 1
资源评论
小哇666
- 粉丝: 287
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功