admin
2019-08-16 83c71070bdc01f4cd4ae890025a978ff183cb34a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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.dto.push.PushContentDTO;
import com.yeshi.fanli.entity.bus.msg.MsgDeviceReadState;
import com.yeshi.fanli.entity.config.push.PushMsgFactory;
import com.yeshi.fanli.exception.PushException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.msg.MsgDeviceReadStateService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.util.StringUtil;
 
import net.sf.json.JSONObject;
 
/**
 * 
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("client/v1/callback")
public class CallBackController {
    @Resource
    private ConfigService configService;
 
    @Resource
    private PushService pushService;
 
    @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("设备标识");
            String uid = json.optJSONObject("customizedData").optString("用户ID");
            msgDeviceReadStateService.addUnreadDeviceMsg(MsgDeviceReadState.TYPE_KEFU, customizedId,
                    "android".equalsIgnoreCase(deviceOS) ? 1 : 2, 1, msg, new Date());
            if (!StringUtil.isNullOrEmpty(uid))// 推送客服消息
            {
                PushContentDTO dto = PushMsgFactory.createMsgKefu();
                try {
                    pushService.pushZNX(Long.parseLong(uid), dto.getTitle(), dto.getContent(), null, null);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                } catch (PushException e) {
                    e.printStackTrace();
                }
            }
        }
 
    }
 
    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");
    }
 
    @RequestMapping(value = "vip")
    public void VIP(PrintWriter out) {
        out.print("success");
    }
 
}