package com.yeshi.fanli.controller;
|
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.PrintWriter;
|
import java.security.SignatureException;
|
import java.util.Date;
|
|
import javax.annotation.Resource;
|
import javax.crypto.Mac;
|
import javax.crypto.spec.SecretKeySpec;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import com.yeshi.fanli.entity.bus.msg.MsgDeviceReadState;
|
import com.yeshi.fanli.log.LogHelper;
|
import com.yeshi.fanli.service.inter.config.ConfigService;
|
import com.yeshi.fanli.service.inter.msg.MsgDeviceReadStateService;
|
|
import net.sf.json.JSONObject;
|
|
/**
|
*
|
*
|
* @author Administrator
|
*
|
*/
|
@Controller
|
@RequestMapping("client/v1/callback")
|
public class CallBackController {
|
@Resource
|
private ConfigService configService;
|
|
@Resource
|
private MsgDeviceReadStateService msgDeviceReadStateService;
|
|
/**
|
* 客服消息回调
|
*
|
* @param response
|
*/
|
@RequestMapping(value = "kefuMsg")
|
public void kefuMsg(HttpServletResponse response) {
|
|
}
|
|
/**
|
* 美洽消息回调
|
*
|
* @param response
|
*/
|
|
@RequestMapping(value = "meiQia")
|
public void meiQia(HttpServletRequest request, HttpServletResponse response) {
|
String auth = request.getHeader("Authorization");
|
String queryString = request.getQueryString();
|
LogHelper.test("美洽:queryString-" + queryString + "-auth:" + auth);
|
|
BufferedReader br = null;
|
StringBuilder sb = new StringBuilder("");
|
try {
|
br = request.getReader();
|
String str;
|
while ((str = br.readLine()) != null) {
|
sb.append(str);
|
}
|
br.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
LogHelper.test("美洽:body----" + sb.toString());
|
|
String sign = "";
|
try {
|
sign = sign(sb.toString());
|
} catch (SignatureException e) {
|
e.printStackTrace();
|
}
|
|
if (!auth.equalsIgnoreCase(sign)) {
|
LogHelper.test("美洽回调签名错误");
|
return;
|
}
|
JSONObject json = JSONObject.fromObject(sb.toString());
|
String msg = "";
|
if (json != null) {
|
String deviceOS = json.optString("deviceOS");
|
String contentType = json.optString("contentType");
|
if (contentType.equalsIgnoreCase("text"))
|
msg = json.optString("content");
|
else if (contentType.equalsIgnoreCase("photo"))
|
msg = "[图片]";
|
else if (contentType.equalsIgnoreCase("audio"))
|
msg = "[语音]";
|
String customizedId = json.optJSONObject("customizedData").optString("设备标识");
|
msgDeviceReadStateService.addUnreadDeviceMsg(MsgDeviceReadState.TYPE_KEFU, customizedId,
|
"android".equalsIgnoreCase(deviceOS) ? 1 : 2, 1, msg, new Date());
|
}
|
|
|
}
|
|
public String sign(String raw_body) throws java.security.SignatureException {
|
String key = "$2a$12$uC3EG/zSaSI37KKOgt1IgetDRHJY6Q2zEVDBr0DeWcwQbGNU7pewy";
|
String result = "";
|
try {
|
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1");
|
Mac mac = Mac.getInstance("HmacSHA1");
|
mac.init(signingKey);
|
byte[] rawHmac = mac.doFinal(raw_body.getBytes("UTF-8"));
|
byte[] hexBytes = new org.apache.commons.codec.binary.Hex().encode(rawHmac);
|
result = org.apache.commons.codec.binary.Base64.encodeBase64String(hexBytes).trim();
|
} catch (Exception e) {
|
throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
|
}
|
return "meiqia_sign:" + result;
|
}
|
|
@RequestMapping(value = "test")
|
public void test(PrintWriter out) {
|
configService.getConfig(1L);
|
out.print("success");
|
}
|
|
}
|