admin
2021-01-27 92f1d85ddc449ce7452f9d52f8b081e6b69c720b
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.yeshi.fanli.controller.admin;
 
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
import javax.annotation.Resource;
 
import com.yeshi.fanli.entity.accept.AdminAcceptData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.JsonUtil;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.entity.bus.msg.UserSystemMsg;
import com.yeshi.fanli.entity.bus.msg.UserSystemMsgTypeEnum;
import com.yeshi.fanli.exception.push.PushException;
import com.yeshi.fanli.service.inter.msg.UserSystemMsgService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.IOSPushFactory;
 
import net.sf.json.JSONObject;
 
@Controller(value = "adminPushController")
@RequestMapping("admin/new/api/v1/push")
public class PushController {
 
    @Resource
    private PushService pushService;
 
    @Resource
    private UserSystemMsgService userSystemMsgService;
    
 
    /**
     * 短链接转换
     * 
     * @param url
     * @param out
     * @throws Exception
     */
    @RequestMapping(value = "convertLink")
    public void convertLink(AdminAcceptData acceptData,String callback, String url, PrintWriter out) {
 
        if (StringUtil.isNullOrEmpty(url)) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("转链数据不能为空"));
            return;
        }
 
        try {
            String shortLink = org.yeshi.utils.HttpUtil.getShortLink(url);
 
            JSONObject jsonData = new JSONObject();
            jsonData.put("url", url);
            jsonData.put("shortLink", shortLink);
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(jsonData));
 
        } catch (Exception e) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("转换失败"));
            e.printStackTrace();
        }
    }
 
 
    /**
     * 网页推送 -(新后台)
     * 
     * @param url
     * @param title
     * @param content
     * @param out
     * @throws Exception
     */
    @RequestMapping(value = "newPushUrlAll")
    public void newPushUrlAll(AdminAcceptData acceptData, String callback, String url, String title, String content, String uids,
                              String arrayIOS, String arrayAndroid, PrintWriter out) {
 
        if (StringUtil.isNullOrEmpty(url) || StringUtil.isNullOrEmpty(title) || StringUtil.isNullOrEmpty(content)) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请填写链接,标题与内容"));
            return;
        }
 
        JSONObject json = IOSPushFactory.createURLPush(HttpUtil.getShortLink(url), title, content);
        if (json.toString().getBytes().length > 256) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("标题或内容过长,请删减后再试"));
            return;
        }
 
        try {
 
            List<String> listuid = null;
            if (uids != null && uids.trim().length() > 0) {
                listuid = Arrays.asList(uids.split(","));
                if (listuid == null || listuid.size() == 0) {
                    JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("用户id格式不正确"));
                    return;
                }
            }
            
            if ((arrayIOS == null || arrayIOS.trim().length() == 0)
                    && (arrayAndroid == null || arrayAndroid.trim().length() == 0)) {
                JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("推送版本不能为空"));
                return;
            } 
            
            List<String> listIOS = convertList(arrayIOS);
            List<String> listAndroid = convertList(arrayAndroid);
            
            if (listuid == null) {
                // 全推
                pushService.pushUrl(null, title, content, url, listIOS, listAndroid,acceptData.getSystem());
            } else {
                // 部分推送
                for (String str_uid: listuid) {
                    if (str_uid != null && str_uid.trim().length() > 0) {
                        pushService.pushUrl(Long.parseLong(str_uid), title, content, url, listIOS, listAndroid,acceptData.getSystem());
                    }
                }
            }
            
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("推送成功"));
        } catch (PushException e) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMsg()));
        } catch (Exception e) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("推送失败"));
            e.printStackTrace();
        }
    }
 
    /**
     * 
     * 方法说明: 站内信推送 -(新后台)
     * 
     * @param title
     * @param content
     * @throws Exception
     */
    @RequestMapping(value = "newPushFanZNX")
    public void newPushFanZNX(AdminAcceptData acceptData,String callback, String uids, String title, String content, String arrayIOS,
            String arrayAndroid, PrintWriter out) {
 
        if (StringUtil.isNullOrEmpty(title) || StringUtil.isNullOrEmpty(content)) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请填写标题与内容"));
            return;
        }
 
        try {
 
            List<String> listuid = null;
            if (uids != null && uids.trim().length() > 0) {
                listuid = Arrays.asList(uids.split(","));
                if (listuid == null || listuid.size() == 0) {
                    JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("用户id格式不正确"));
                    return;
                }
            }
            
 
            if ((arrayIOS == null || arrayIOS.trim().length() == 0)
                    && (arrayAndroid == null || arrayAndroid.trim().length() == 0)) {
                JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("推送版本不能为空"));
                return;
            } 
            
            List<String> listIOS = convertList(arrayIOS);
            List<String> listAndroid = convertList(arrayAndroid);
            
            if (listuid == null) {
                // 全推
                pushService.pushZNX(null, title, content, listIOS, listAndroid,acceptData.getSystem());
            } else {
                // 部分推送
                for (String str_uid: listuid) {
                    if (str_uid != null && str_uid.trim().length() > 0) {
                        pushService.pushZNX(Long.parseLong(str_uid), title, content, listIOS, listAndroid,acceptData.getSystem());
 
                        userSystemMsgService.addUserSystemMsg(Long.parseLong(str_uid), UserSystemMsgTypeEnum.question,
                                title, content, UserSystemMsg.TIME_TAG_EMERGENT, null);
                    }
                }
            }
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("推送成功"));
        } catch (PushException e) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMsg()));
        } catch (Exception e) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("推送失败"));
            e.printStackTrace();
        }
    }
 
    /**
     * 
     * 方法说明: 百川推送 -(新后台)
     * 
     * @param title
     * @param content
     * @throws Exception
     */
    @RequestMapping(value = "pushBaiChuan")
    public void pushBaiChuan(AdminAcceptData acceptData,String callback, String uids, String title, String content, String url, String arrayIOS,
            String arrayAndroid, PrintWriter out) {
 
        if (StringUtil.isNullOrEmpty(title) || StringUtil.isNullOrEmpty(content)) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请填写标题与内容"));
            return;
        }
 
        try {
 
            List<String> listuid = null;
            if (uids != null && uids.trim().length() > 0) {
                listuid = Arrays.asList(uids.split(","));
                if (listuid == null || listuid.size() == 0) {
                    JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("用户id格式不正确"));
                    return;
                }
            }
            
            if ((arrayIOS == null || arrayIOS.trim().length() == 0)
                    && (arrayAndroid == null || arrayAndroid.trim().length() == 0)) {
                JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("推送版本不能为空"));
                return;
            } 
            
            List<String> listIOS = convertList(arrayIOS);
            List<String> listAndroid = convertList(arrayAndroid);
            
            if (listuid == null) {
                // 全推
                pushService.pushBaiChuanUrl(null, title, content, url, listIOS, listAndroid,acceptData.getSystem());
            } else {
                // 部分推送
                for (String str_uid: listuid) {
                    if (str_uid != null && str_uid.trim().length() > 0) {
                        pushService.pushBaiChuanUrl(Long.parseLong(str_uid), title, content, url, listIOS, listAndroid,acceptData.getSystem());
                    }
                }
            }
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("推送成功"));
 
        } catch (PushException e) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMsg()));
        } catch (Exception e) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("推送失败"));
            e.printStackTrace();
        }
    }
 
    
    /**
     * 版本处理
     * @param array
     * @return
     */
    public List<String> convertList (String array) {
        Gson gson = new Gson();
        List<String> list = null;
        if (array == null || array.trim().length() == 0) {
            list = new ArrayList<String>(); // 长度为0 不推送
        } else {
            list = gson.fromJson(array, new TypeToken<ArrayList<String>>() {}.getType());
            if (list != null && list.size() > 0 && list.contains("全推")) {
                list = null; // 全推
            }
        }
        return list;
    }
}