java企业号回调模式,【跟着博弈走,微信轻松有】企业号第三方Java环境搭建之二: 回调模式...

前节【项目搭建】 讲述了如何搭建一个SpringMVC的Maven项目,这节我们将关注回调模式的相关事宜。

开启应用的回调模式

当你开启应用的回调模式时,企业号会要求你填写应用的URL、Token、EncodingAESKey三个参数。

URL是企业应用接收企业号推送请求的访问协议和地址,支持http或https协议。

Token可由企业任意填写,用于生成签名。

EncodingAESKey用于消息体的加密,是AES密钥的Base64编码。

登陆企业服务号之后,管理员可以添加企业应用:

00e5ddd01a5948e4260fc2cf268ce799.png

点击添加按钮之后,按照提示创建一个WeChat的应用

573b45b8be79239bbe4ad4f9b343f08b.png

点击进入WeChat配置

a3a952a2d0719662814075919949b374.png

选中回调模式下面的进入按钮,

c7d7aab163e54af5a5f826295eb7e32e.png

选择右上角的开启按钮,将会弹出对话框,让你输入接口信息:

2eb5acbcdb1e0a3783623a50b1a59bd4.png

URL 是第三方平台用来接收微信转发消息的地址,需要提供GET和POST两种方法,GET主要是用来验证URL有效性, POST主要用来接收消息。

Token 和 AESKey可以随便定义,也可以用他后面的随机获取。

我们这里定义 Token = “A8888888888888888888A”, EncodingAESKey = "A88888888888888888888888888888888888888888A"。

URL我们要填写SpringMVC的crontroller的地址,比如 http://www.xxx.com/msg.

这样我们就可以开始来设计Spring MVC中的Controller类。

创建包: com.boyi.wechat.controller

创建类: com.boyi.wechat.controller.MsgController

package com.boyi.wechat.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import com.boyi.wechat.utils.CommonUtils;

import com.boyi.wechat.wx.msg.in.model.InMsg;

import com.boyi.wechat.wx.msg.in.model.ReplyMsg;

import com.boyi.wechat.wx.msg.model.MsgType;

import com.boyi.wechat.wx.msg.parser.MsgParser;

import com.boyi.wechat.wx.utils.WXBizMsgCrypt;

import com.boyi.wechat.wx.utils.WXBizMsgCryptManager;

@Controller

@RequestMapping("/msg")

public class MsgController {

@RequestMapping(method = RequestMethod.GET)

public @ResponseBody

String valid(@RequestParam(value = "msg_signature") String signature,

@RequestParam(value = "timestamp") String timestamp,

@RequestParam(value = "nonce") String nonce,

@RequestParam(value = "echostr") String echostr) throws Exception {

String sEchoStr = "";

WXBizMsgCrypt wxcpt = WXBizMsgCryptManager.getChatWXBizMsgCrypt();

try {

sEchoStr = wxcpt.VerifyURL(signature, timestamp, nonce, echostr);

System.out.println("sEchoStr : " + sEchoStr);

} catch (Exception e) {

e.printStackTrace();

}

return sEchoStr;

}

@RequestMapping(method = RequestMethod.POST)

public @ResponseBody String message(

@RequestParam(value = "msg_signature") String signature,

@RequestParam(value = "timestamp") String timestamp,

@RequestParam(value = "nonce") String nonce,

@RequestBody String encryptMsg) throws Exception {

String sEncryptMsg = null;

WXBizMsgCrypt wxcpt = WXBizMsgCryptManager.getChatWXBizMsgCrypt();

try {

String msg = wxcpt.DecryptMsg(signature, timestamp, nonce, encryptMsg);

System.out.println("decode msg = " + msg);

//parse the xml message to InMsg object

InMsg inMsg = MsgParser.parse(msg);

if (inMsg != null) {

System.out.println("msg id = " + inMsg.getMsgId());

System.out.println("msg type = " + inMsg.getMsgType().name());

String content = "你刚才发的是[" + inMsg.getMsgType().name() + "]";

//create the reply message

ReplyMsg replyMsg = new ReplyMsg();

replyMsg.setToUserName(inMsg.getFromUserName());

replyMsg.setFromUserName(inMsg.getToUserName());

replyMsg.setCreateTime(CommonUtils.getCurrentSecond());

replyMsg.setMsgType(MsgType.text);

replyMsg.setContent(content);

//convert the reply msg object to xml

String xmlString = MsgParser.convert(replyMsg);

System.out.println("reply message = " + xmlString);

//Encrypt the xml message

sEncryptMsg = wxcpt.EncryptMsg(xmlString, CommonUtils.getCurrentSecond(), nonce);

System.out.println("encrypt message = " + sEncryptMsg);

}

} catch (Exception e) {

e.printStackTrace();

}

return sEncryptMsg;

}

}

在这个Controller里我们定义了2个方法,一个是RequestMethod 是GET的方法valid, 另外一个是POST的方法message, 由于我们在配置文件里面加入了JackSon的配置,所以就不需要去写其他的方法去获取POST发过来的数据了,直接通过@RequestBody就可以获取微信发过来的加密的消息。

WXBizMsgCrypt 这个类可以从微信官方下载到,http://qydev.weixin.qq.com/java.zip

package com.boyi.wechat.wx.utils;

import java.util.HashMap;

import java.util.Map;

public class WXBizMsgCryptManager {

public static final String sToken = "A8888888888888888888A";

/**

* replace the sCorpID to your corp id

*/

public static final String sCorpID = "corpId";

public static final String sEncodingAESKey = "A88888888888888888888888888888888888888888A";

private static Map crypts = new HashMap();

public static WXBizMsgCrypt getChatWXBizMsgCrypt(){

return getWXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID);

}

public static WXBizMsgCrypt getWXBizMsgCrypt(String token, String encodingAesKey, String corpId) {

WXBizMsgCrypt wxcpt = null;

String key = corpId + "-" + token;

if (crypts.containsKey(key)) {

wxcpt = crypts.get(key);

} else {

try {

wxcpt = new WXBizMsgCrypt(token, encodingAesKey, corpId);

} catch (Exception e) {

e.printStackTrace();

}

}

return wxcpt;

}

}

为了测试回调模式,可以先把message方法注释掉。 然后打个包,部署到你们的云主机上面,再回到回调模式页面,测试一下.

f2e025b92795b90bb781f28ddb670529.png

同时把下面几个选项都打开,特别是用户消息上报,如果关闭的话第三方将收不到任何消息。

本节结束,下节讲述消息的解析和发送。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,以下是一个简单的Java五子棋人机博弈的示例代码: ```java import java.util.Scanner; public class GomokuGame { private static final int SIZE = 15; private static final char EMPTY = '-'; private static final char PLAYER = 'X'; private static final char COMPUTER = 'O'; private char[][] board; public GomokuGame() { board = new char[SIZE][SIZE]; initializeBoard(); } private void initializeBoard() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { board[i][j] = EMPTY; } } } private void printBoard() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } private boolean isBoardFull() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (board[i][j] == EMPTY) { return false; } } } return true; } private boolean isWinningMove(int row, int col, char player) { // 检查行 int count = 0; for (int i = Math.max(0, col - 4); i <= Math.min(SIZE - 1, col + 4); i++) { if (board[row][i] == player) { count++; if (count == 5) { return true; } } else { count = 0; } } // 检查列 count = 0; for (int i = Math.max(0, row - 4); i <= Math.min(SIZE - 1, row + 4); i++) { if (board[i][col] == player) { count++; if (count == 5) { return true; } } else { count = 0; } } // 检查主对角线 count = 0; int startRow = Math.max(0, row - 4); int startCol = Math.max(0, col - 4); while (startRow <= Math.min(SIZE - 1, row + 4) && startCol <= Math.min(SIZE - 1, col + 4)) { if (board[startRow][startCol] == player) { count++; if (count == 5) { return true; } } else { count = 0; } startRow++; startCol++; } // 检查副对角线 count = 0; startRow = Math.min(SIZE - 1, row + 4); startCol = Math.max(0, col - 4); while (startRow >= Math.max(0, row - 4) && startCol <= Math.min(SIZE - 1, col + 4)) { if (board[startRow][startCol] == player) { count++; if (count == 5) { return true; } } else { count = 0; } startRow--; startCol++; } return false; } private boolean isValidMove(int row, int col) { return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] == EMPTY; } private void makeMove(int row, int col, char player) { board[row][col] = player; } private void playGame() { Scanner scanner = new Scanner(System.in); boolean isPlayerTurn = true; while (true) { System.out.println("当前棋盘:"); printBoard(); if (isPlayerTurn) { System.out.println("玩家,请输入您的下棋位置(行 列,以空格分隔):"); int row = scanner.nextInt(); int col = scanner.nextInt(); if (isValidMove(row, col)) { makeMove(row, col, PLAYER); if (isWinningMove(row, col, PLAYER)) { System.out.println("玩家获胜!"); break; } isPlayerTurn = false; } else { System.out.println("无效的位置,请重新输入。"); } } else { System.out.println("电脑正在思考下棋位置..."); // 在这里实现电脑的下棋逻辑 // ... int row = 0; // 电脑下棋的行 int col = 0; // 电脑下棋的列 if (isValidMove(row, col)) { makeMove(row, col, COMPUTER); if (isWinningMove(row, col, COMPUTER)) { System.out.println("电脑获胜!"); break; } isPlayerTurn = true; } } if (isBoardFull()) { System.out.println("平局!"); break; } } scanner.close(); } public static void main(String[] args) { GomokuGame game = new GomokuGame(); game.playGame(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值