package com.fh.controller.weixin;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.marker.weixin.DefaultSession;
import org.marker.weixin.HandleMessageAdapter;
import org.marker.weixin.MySecurity;
import org.marker.weixin.msg.Data4Item;
import org.marker.weixin.msg.Msg4Event;
import org.marker.weixin.msg.Msg4Image;
import org.marker.weixin.msg.Msg4ImageText;
import org.marker.weixin.msg.Msg4Link;
import org.marker.weixin.msg.Msg4Location;
import org.marker.weixin.msg.Msg4Text;
import org.marker.weixin.msg.Msg4Video;
import org.marker.weixin.msg.Msg4Voice;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fh.controller.base.BaseController;
import com.fh.service.weixin.command.CommandService;
import com.fh.service.weixin.imgmsg.ImgmsgService;
import com.fh.service.weixin.textmsg.TextmsgService;
import com.fh.util.Const;
import com.fh.util.PageData;
import com.fh.util.Tools;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
/**
*
* 类名称:WeixinController.java
* 类描述: 微信公共平台开发
* @author FH
* 作者单位:
* 联系方式:
* 创建时间:2014年7月10日
* @version 1.0
*/
@Controller
@RequestMapping(value="/weixin")
public class WeixinController extends BaseController{
@Resource(name="textmsgService")
private TextmsgService textmsgService;
@Resource(name="commandService")
private CommandService commandService;
@Resource(name="imgmsgService")
private ImgmsgService imgmsgService;
/**
* 接口验证,总入口
* @param out
* @param request
* @param response
* @throws Exception
*/
@RequestMapping(value="/index")
public void index(
PrintWriter out,
HttpServletRequest request,
HttpServletResponse response
) throws Exception{
logBefore(logger, "微信接口");
PageData pd = new PageData();
try{
pd = this.getPageData();
String signature = pd.getString("signature"); //微信加密签名
String timestamp = pd.getString("timestamp"); //时间戳
String nonce = pd.getString("nonce"); //随机数
String echostr = pd.getString("echostr"); //字符串
if(null != signature && null != timestamp && null != nonce && null != echostr){/* 接口验证 */
logBefore(logger, "进入身份验证");
List<String> list = new ArrayList<String>(3) {
private static final long serialVersionUID = 2621444383666420433L;
public String toString() { // 重写toString方法,得到三个参数的拼接字符串
return this.get(0) + this.get(1) + this.get(2);
}
};
list.add(Tools.readTxtFile(Const.WEIXIN)); //读取Token(令牌)
list.add(timestamp);
list.add(nonce);
Collections.sort(list); // 排序
String tmpStr = new MySecurity().encode(list.toString(),
MySecurity.SHA_1); // SHA-1加密
if (signature.equals(tmpStr)) {
out.write(echostr); // 请求验证成功,返回随机码
}else{
out.write("");
}
out.flush();
out.close();
}else{/* 消息处理 */
logBefore(logger, "进入消息处理");
response.reset();
sendMsg(request,response);
}
} catch(Exception e){
logger.error(e.toString(), e);
}
}
/**
* 处理微信服务器发过来的各种消息,包括:文本、图片、地理位置、音乐等等
* @param request
* @param response
* @throws Exception
*/
public void sendMsg(HttpServletRequest request, HttpServletResponse response) throws Exception{
InputStream is = request.getInputStream();
OutputStream os = response.getOutputStream();
final DefaultSession session = DefaultSession.newInstance();
session.addOnHandleMessageListener(new HandleMessageAdapter(){
/**
* 事件
*/
@Override
public void onEventMsg(Msg4Event msg) {
/** msg.getEvent()
* unsubscribe:取消关注 ; subscribe:关注
*/
if("subscribe".equals(msg.getEvent())){
returnMSg(msg,null,"关注");
}
}
/**
* 收到的文本消息
*/
@Override
public void onTextMsg(Msg4Text msg) {
returnMSg(null,msg,msg.getContent().trim());
}
@Override
public void onImageMsg(Msg4Image msg) {
// TODO Auto-generated method stub
super.onImageMsg(msg);
}
@Override
public void onLocationMsg(Msg4Location msg) {
// TODO Auto-generated method stub
super.onLocationMsg(msg);
}
@Override
public void onLinkMsg(Msg4Link msg) {
// TODO Auto-generated method stub
super.onLinkMsg(msg);
}
@Override
public void onVideoMsg(Msg4Video msg) {
// TODO Auto-generated method stub
super.onVideoMsg(msg);
}
@Override
public void onVoiceMsg(Msg4Voice msg) {
// TODO Auto-generated method stub
super.onVoiceMsg(msg);
}
@Override
public void onErrorMsg(int errorCode) {
// TODO Auto-generated method stub
super.onErrorMsg(errorCode);
}
/**
* 返回消息
* @param emsg
* @param tmsg
* @param getmsg
*/
public void returnMSg(Msg4Event emsg, Msg4Text tmsg, String getmsg){
PageData msgpd;
PageData pd = new PageData();
String toUserName,fromUserName,createTime;
if(null == emsg){
toUserName = tmsg.getToUserName();
fromUserName = tmsg.getFromUserName();
createTime = tmsg.getCreateTime();
}else{
toUserName = emsg.getToUserName();
fromUserName = emsg.getFromUserName();
createTime = emsg.getCreateTime();
}
pd.put("KEYWORD", getmsg);
try {
msgpd = textmsgService.findByKw(pd);
if(null != msgpd){
Msg4Text rmsg = new Msg4Text();
rmsg.setFromUserName(toUserName);
rmsg.setToUserName(fromUserName);
//rmsg.setFuncFlag("0");
rmsg.setContent(msgpd.getString("CONTENT")); //回复文字消息
session.callback(rmsg);
}else{
msgpd = imgmsgService.findByKw(pd);
if(null != msgpd){
Msg4ImageText mit = new Msg4ImageText();
mit.setFromUserName(toUserName);
mit.setToUserName(fromUserName);
mit.setCreateTime(cre
没有合适的资源?快使用搜索试试~ 我知道了~
ssm+maven+shiro+bootstrap精美后台管理模板
共2000个文件
gif:1456个
png:246个
js:185个
3星 · 超过75%的资源 需积分: 10 88 下载量 177 浏览量
2018-08-28
11:02:16
上传
评论 7
收藏 45.12MB ZIP 举报
温馨提示
权限管理 按钮管理 菜单管理 数据字典 组织机构 在线管理 系统用户 会员管理 代码生成 性能监控 接口测试 发送邮件 置二维码 地图工具
资源推荐
资源详情
资源评论
收起资源包目录
ssm+maven+shiro+bootstrap精美后台管理模板 (2000个子文件)
UserController.class 13KB
RoleController.class 13KB
LoginController.class 12KB
PicturesController.class 11KB
HeadController.class 11KB
ImgmsgController.class 11KB
AppuserController.class 10KB
Uploader.class 10KB
WeixinController.class 10KB
TextmsgController.class 10KB
PagePlugin.class 9KB
CommandController.class 8KB
Page.class 8KB
Tools.class 7KB
WeixinController$2.class 7KB
SmsUtil.class 6KB
DictionariesController.class 6KB
MenuController.class 6KB
ToolController.class 6KB
TwoDimensionCode.class 5KB
FileUtil.class 5KB
SimpleMailSender.class 5KB
RoleService.class 5KB
ChatServer.class 5KB
ObjectExcelView.class 4KB
AppUtil.class 4KB
OnlineManagerController.class 4KB
CreateCodeController.class 4KB
Watermark.class 4KB
Jurisdiction.class 4KB
OnlineChatServer.class 4KB
SecCodeController.class 4KB
PageData.class 4KB
DateUtil.class 4KB
GetPinyin.class 4KB
JPushClientUtil.class 3KB
UserService.class 3KB
Freemarker.class 3KB
ObjectExcelRead.class 3KB
ChatServerPool.class 3KB
DaoSupport.class 3KB
OnlineChatServerPool.class 3KB
MapDistance.class 3KB
MenuService.class 3KB
PathUtil.class 3KB
MailSenderInfo.class 3KB
AppuserService.class 3KB
Menu.class 3KB
User.class 3KB
SortUtil.class 3KB
IntAppuserController.class 3KB
PicturesService.class 2KB
startFilter.class 2KB
Const.class 2KB
FileZip.class 2KB
ImageAnd64Binary.class 2KB
LoginHandlerInterceptor.class 2KB
CommandService.class 2KB
TextmsgService.class 2KB
ImgmsgService.class 2KB
Logger.class 2KB
FileUpload.class 2KB
Role.class 2KB
DictionariesService.class 2KB
BaseController.class 2KB
DelAllFile.class 2KB
ReflectHelper.class 2KB
LatLonUtil.class 2KB
FileDownload.class 2KB
RequestUtil.class 2KB
MD5.class 2KB
MyExceptionResolver.class 2KB
RightsHelper.class 2KB
ShiroRealm.class 2KB
PublicUtil.class 1KB
LoginFilter.class 1KB
WeixinController$1.class 1KB
ServiceHelper.class 1KB
Constants.class 1KB
StringUtil.class 1KB
WebAppContextListener.class 1KB
UuidUtil.class 906B
MyX509TrustManager.class 901B
TwoDimensionCodeImage.class 824B
MyAuthenticator.class 776B
startFilter$1.class 558B
DAO.class 466B
ext-neptune.css 526KB
ext-neptune-debug.css 526KB
ace.css 458KB
ext-all-scoped-debug.css 424KB
ext-ie-scoped-debug.css 392KB
ext-sandbox-debug.css 389KB
ext-all-debug.css 384KB
ext-all-gray-debug.css 374KB
ext-all-access-debug.css 364KB
ext-ie-debug.css 353KB
ext-standard-scoped-debug.css 310KB
ext-standard-debug.css 281KB
ext-all-scoped.css 257KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- qq_388394832020-08-22来个git地址 各种jar包各种缺
- vampirecc92019-05-20密码多少啊,登录不进去0.0lxy14202019-05-27你试试 w 时间太长忘记了,也可以自己走代码弄一个
- 蓝色风琴2019-03-22为什么我用eclipse导不进去lxy14202019-05-27导入maven工程
- 曾几何时灬2019-01-29运行一直报错,不要下,lxy14202019-03-20自己好好配置下
lxy1420
- 粉丝: 6
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功