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
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
JavaEE企业开发框架前端采用最受欢迎的HTML、CSS和JS框架——Bootstrap,所有开发者都能快速上手,所有设备都可以适配(PC设备、平板、手机等),所有项目都适用(网站、OA、ERP、CRM、APP后台等
资源推荐
资源详情
资源评论
收起资源包目录
【源码】oracle版本_spring4.0.rar_ERP源码_awaylxf_bootstrap_oracle_企业网站 (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
LoginHandlerInterceptor.class 2KB
ImageAnd64Binary.class 2KB
TextmsgService.class 2KB
CommandService.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
.classpath 1KB
org.eclipse.wst.common.component 563B
org.eclipse.wst.jsdt.ui.superType.container 49B
ext-neptune-debug.css 526KB
ext-neptune.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
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
林当时
- 粉丝: 113
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- coco.names 文件
- (源码)基于Spring Boot和Vue的房屋租赁管理系统.zip
- (源码)基于Android的饭店点菜系统.zip
- (源码)基于Android平台的权限管理系统.zip
- (源码)基于CC++和wxWidgets框架的LEGO模型火车控制系统.zip
- (源码)基于C语言的操作系统实验项目.zip
- (源码)基于C++的分布式设备配置文件管理系统.zip
- (源码)基于ESP8266和Arduino的HomeMatic水表读数系统.zip
- (源码)基于Django和OpenCV的智能车视频处理系统.zip
- (源码)基于ESP8266的WebDAV服务器与3D打印机管理系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功