123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package com.ruoyi.web.controller.wechat;
- import com.ruoyi.common.core.controller.BaseController;
- import com.ruoyi.common.core.domain.AjaxResult;
- import com.ruoyi.common.core.domain.model.LoginBody;
- import com.ruoyi.common.utils.wechat.MessageUtil;
- import com.ruoyi.common.utils.wechat.xml.XMLUtil;
- import com.ruoyi.framework.web.service.WechatService;
- import com.ruoyi.system.wechatUtils.UserQueue;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.bind.annotation.RequestBody;
- import javax.servlet.http.HttpServletRequest;
- import java.io.IOException;
- import java.util.Map;
- /**
- * <p>
- * 与微信产生交换的控制器
- * </p>
- *
- * @author ruoyi
- * @date 2020-12-11
- */
- @RestController
- @RequestMapping("/wechat")
- public class WechatController extends BaseController {
- @Autowired
- private WechatService wechatService;
- /**
- * 公众号与微信服务器进行认证的API接口
- *
- * @param signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
- * @param timestamp 时间戳
- * @param nonce 随机数
- * @param echostr 随机字符串
- * @return 结果
- */
- @GetMapping(value = "/wechat")
- public Object check(String signature,
- String timestamp,
- String nonce,
- String echostr) {
- return echostr;
- }
- /**
- * 微信授权信息时返回token
- *
- * @param loginBody code
- * @return 结果
- */
- @PostMapping(value = "/programLogin")
- public AjaxResult weChatProgramLogin(@RequestBody LoginBody loginBody) {
- // 生成令牌
- AjaxResult result = wechatService.login(loginBody);
- System.out.println("生成的令牌:" + result.get("data"));
- return result;
- }
- /**
- * 公众号接收请求
- *
- * @param request 请求
- * @return 接口
- */
- @PostMapping(value = "/wechat", produces = {"application/xml;charset=utf-8"})
- public Object toRequest(HttpServletRequest request) throws IOException {
- Map<String, String> map = XMLUtil.getMap(request.getInputStream());
- String msgType = map.get("MsgType");
- String toUser = map.get("ToUserName");
- String fromUser = map.get("FromUserName");
- String createTime = map.get("CreateTime");
- String reply = "";
- if ("event".equals(msgType)) {
- String event = map.get("Event");
- if ("subscribe".equals(event)) {
- reply = "欢迎关注";
- UserQueue.ATTENTIONQUEUE.push(fromUser);
- } else if ("unsubscribe".equals(event)) {
- UserQueue.CHECKOFFQUEUE.push(fromUser);
- }
- }
- return MessageUtil.setMessage(fromUser, toUser, 45860, "text", reply);
- }
- }
|