<%
//WeiXinHandler为内部类不能使用非final类型的对象
final String TOKEN="rongsoft";
final HttpServletRequest final_request=request;
final HttpServletResponse final_response=response;
%>
<%
class WeiXinHandler{
public void valid(){
String echostr=final_request.getParameter("echostr");
if(null==echostr||echostr.equals("")){
responseMsg();
}else{
if(this.checkSignature()){
this.print(echostr);
}else{
this.print("error");
}
}
}
//自动回复内容
public void responseMsg(){
String postStr=null;
try{
postStr=this.readStreamParameter(final_request.getInputStream());
}catch(Exception e){
e.printStackTrace();
}
System.out.println("postStr="+postStr);
if (null!=postStr&&!postStr.equals("")){
Document document=null;
try{
document = DocumentHelper.parseText(postStr);
}catch(Exception e){
e.printStackTrace();
}
if(null==document){
this.print("");
return;
}
Element root=document.getRootElement();
String fromUsername = root.elementText("FromUserName");
String toUsername = root.elementText("ToUserName");
String keyword = root.elementTextTrim("Content");
String eventkey = root.elementTextTrim("EventKey");
String time = new Date().getTime()+"";
String textTpl = "
"+
""+
""+
"%3$s"+
""+
""+
"0"+
"";
//创建日志
System.out.println("keyword="+keyword+",eventkey="+eventkey+",toUsername="+toUsername+",fromUsername="+fromUsername);
String shujukljc="jdbc:mysql://localhost:3306/rcms20_rongsoft?user=root&password=rongsoft&useUnicode=true&characterEncoding=utf8";//数据库连接
shujuk sjk=new shujuk(shujukljc);//数据库对象
String biaohao=UUID.randomUUID().toString().replaceAll("-","");
String biaoti=keyword;
String leixing="weixinxx_rizhisjlx";
String shuju1=toUsername;
String shuju2=fromUsername;
String neirong=postStr;
String tianjiasj=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
sjk.charugxjl("insert into rizhisj (biaohao,biaoti,leixing,shuju1,shuju2,neirong,tianjiasj) values ("+biaohao+","+biaoti+","+leixing+","+shuju1+","+shuju2+","+neirong+","+tianjiasj+" )");
//创建日志-结束
eventkey=eventkey.replace("qrscene_","");
String fubiaoh=sjk.dedaozdz("select biaohao from yonghu where biaohao="+eventkey+" ","");//父标号
if(!fubiaoh.equals(""))
{
biaohao=fromUsername;
String weixinoid=fromUsername;
String biaohao_lyh=sjk.dedaozdz("select biaohao from yonghu where weixinoid="+weixinoid+" ","");
if( biaohao_lyh.equals(""))
{
System.out.println("创建用户");
String lj[]=lujing.split(",");
for(int j=0;j
{
if(!lj[j].equals("") && !lj[j].equals( fubiaoh ) )
{
jifenzsbh="jianjietjyh_jifenzs";
jifen=sjk.dedaozdz("select jifen from xitongfz where biaohao="+jifenzsbh+" ","");//间接推荐用户
biaoti=sjk.dedaozdz("select biaoti from xitongfz where biaohao="+jifenzsbh+" ","")+biaohao;
sjk.charugxjl("insert into rizhisj (biaohao,leixing,biaoti,neirong,tianjiayhbh,tianjiasj,jifen,wangzhi) values ("+UUID.randomUUID().toString().replaceAll("-","")+",huodejf_rizhisjlx,"+biaoti+","+biaohao+","+lj[j]+","+tianjiasj+","+jifen+","+jifenzsbh+")");
sjk.charugxjl("update yonghu set jifen=jifen+"+jifen+" where biaohao=" + lj[j] + " ");//积分
}
}
}
else
{
System.out.println("老用户扫描"+biaohao_lyh);
}
}
if(null!=keyword&&keyword.equals("1"))
{
String msgType = "text";
String contentStr ="http://www.rongsoft.cn";
String resultStr = textTpl.format(textTpl, fromUsername, toUsername, time, msgType, contentStr);
this.print(resultStr);
}
else
{
String msgType = "text";
String contentStr ="vasttown.com";
String resultStr = textTpl.format(textTpl, fromUsername, toUsername, time, msgType, contentStr);
this.print(resultStr);
}
}else {
this.print("vasttown.com");
}
}
//微信接口验证
public boolean checkSignature(){
String signature = final_request.getParameter("signature");
String timestamp = final_request.getParameter("timestamp");
String nonce = final_request.getParameter("nonce");
String token=TOKEN;
String[] tmpArr={token,timestamp,nonce};
Arrays.sort(tmpArr);
String tmpStr=this.ArrayToString(tmpArr);
tmpStr=this.SHA1Encode(tmpStr);
if(tmpStr.equalsIgnoreCase(signature)){
return true;
}else{
return false;
}
}
//向请求端发送返回数据
public void print(String content){
try{
final_response.getWriter().print(content);
final_response.getWriter().flush();
final_response.getWriter().close();
}catch(Exception e){
}
}
//数组转字符串
public String ArrayToString(String [] arr){
StringBuffer bf = new StringBuffer();
for(int i = 0; i < arr.length; i++){
bf.append(arr[i]);
}
return bf.toString();
}
//sha1加密
public String SHA1Encode(String sourceString) {
String resultString = null;
try {
resultString = new String(sourceString);
MessageDigest md = MessageDigest.getInstance("SHA-1");
resultString = byte2hexString(md.digest(resultString.getBytes()));
} catch (Exception ex) {
}
return resultString;
}
public final String byte2hexString(byte[] bytes) {
StringBuffer buf = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
if (((int) bytes[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString((int) bytes[i] & 0xff, 16));
}
return buf.toString().toUpperCase();
}
//从输入流读取post参数
public String readStreamParameter(ServletInputStream in){
StringBuilder buffer = new StringBuilder();
BufferedReader reader=null;
try{
reader = new BufferedReader(new InputStreamReader(in));
String line=null;
while((line = reader.readLine())!=null){
buffer.append(line);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(null!=reader){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return buffer.toString();
}
}
%>
<%
WeiXinHandler handler=new WeiXinHandler();
handler.valid();
%>
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。