WXControllerAgent.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. package com.xcgl.weixin;
  2. import com.aliyuncs.exceptions.ClientException;
  3. import com.aliyuncs.exceptions.ServerException;
  4. import com.xcgl.utils.AgentInfo;
  5. import com.xcgl.utils.XcglConstant;
  6. import com.xcgl.weixin.entity.WXAjaxJson;
  7. import net.sf.json.JSONObject;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.HttpStatus;
  11. import org.apache.http.client.HttpClient;
  12. import org.apache.http.client.methods.HttpGet;
  13. import org.apache.http.impl.client.DefaultHttpClient;
  14. import org.apache.http.params.CoreConnectionPNames;
  15. import org.apache.http.util.EntityUtils;
  16. import org.apache.log4j.Logger;
  17. import org.springframework.stereotype.Controller;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RequestMethod;
  20. import org.springframework.web.bind.annotation.ResponseBody;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import java.io.PrintWriter;
  24. import java.net.URLEncoder;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. @Controller
  28. @RequestMapping(value = "/api/wx")
  29. public class WXControllerAgent {
  30. private static final Logger logger = Logger.getLogger(WXControllerAgent.class);
  31. /**
  32. * 访问地址:http://localhost:8080/xcgl/api/wx
  33. * @return
  34. */
  35. @RequestMapping(method = RequestMethod.GET)
  36. @ResponseBody
  37. public WXAjaxJson checkNet() {
  38. WXAjaxJson json = new WXAjaxJson();
  39. return json;
  40. }
  41. @RequestMapping(value = "user/get_user_info")
  42. @ResponseBody
  43. public void getUserInfo(HttpServletRequest request, HttpServletResponse response)
  44. {
  45. List<String> lsParamNames = new ArrayList<>();
  46. lsParamNames.add("openid");
  47. lsParamNames.add("timestamp");
  48. handdleRequest(request, response, lsParamNames, "user/get_user_info",XcglConstant.SYSTEM_HR);
  49. }
  50. @RequestMapping(value = "user/getProjectList4Mine")
  51. @ResponseBody
  52. public void getProjectList4Mine(HttpServletRequest request, HttpServletResponse response)
  53. {
  54. List<String> lsParamNames = new ArrayList<>();
  55. lsParamNames.add("openid");
  56. lsParamNames.add("timestamp");
  57. handdleRequest(request, response, lsParamNames, "user/getProjectList4Mine",XcglConstant.SYSTEM_HR);
  58. }
  59. @RequestMapping(value = "user/getProjectList")
  60. @ResponseBody
  61. public void getProjectList(HttpServletRequest request, HttpServletResponse response) {
  62. List<String> lsParamNames = new ArrayList<>();
  63. lsParamNames.add("openid");
  64. lsParamNames.add("timestamp");
  65. handdleRequest(request, response, lsParamNames, "user/getProjectList",XcglConstant.SYSTEM_HR);
  66. }
  67. /**
  68. * 微信绑定手机号
  69. * */
  70. @RequestMapping(value = "user/bindTel")
  71. @ResponseBody
  72. public void bindTel(HttpServletRequest request, HttpServletResponse response)
  73. {
  74. List<String> lsParamNames = new ArrayList<>();
  75. lsParamNames.add("openid");
  76. lsParamNames.add("phonenum");
  77. lsParamNames.add("timestamp");
  78. handdleRequest(request, response, lsParamNames, "user/bindTel",XcglConstant.SYSTEM_HR);
  79. }
  80. /**
  81. * 上传附件
  82. * */
  83. @RequestMapping(value = "user/uploadAttachment")
  84. @ResponseBody
  85. public void uploadAttachment(HttpServletRequest request, HttpServletResponse response)
  86. {
  87. List<String> lsParamNames = new ArrayList<>();
  88. lsParamNames.add("userid");
  89. lsParamNames.add("isup");
  90. lsParamNames.add("isdel");
  91. lsParamNames.add("picture0id");
  92. lsParamNames.add("picture1id");
  93. lsParamNames.add("picture2id");
  94. lsParamNames.add("picture3id");
  95. lsParamNames.add("picture4id");
  96. lsParamNames.add("picture5id");
  97. lsParamNames.add("picture6id");
  98. lsParamNames.add("accessToken");
  99. handdleRequest(request, response, lsParamNames, "user/uploadAttachment",XcglConstant.SYSTEM_HR);
  100. }
  101. /**
  102. * 从服务器获取用户功能权限
  103. * */
  104. @RequestMapping(value = "user/qryAuth")
  105. @ResponseBody
  106. public void qryAuth(HttpServletRequest request, HttpServletResponse response)
  107. {
  108. List<String> lsParamNames = new ArrayList<>();
  109. lsParamNames.add("openid");
  110. handdleRequest(request, response, lsParamNames, "user/qryAuth",XcglConstant.SYSTEM_HR);
  111. }
  112. /**
  113. * 变更工作状态
  114. * */
  115. @RequestMapping(value = "task/changeWorkStatus")
  116. @ResponseBody
  117. public void changeWorkStatus(HttpServletRequest request, HttpServletResponse response)
  118. {
  119. List<String> lsParamNames = new ArrayList<>();
  120. lsParamNames.add("userid");
  121. lsParamNames.add("openid");
  122. lsParamNames.add("taskid");
  123. lsParamNames.add("oldStatus");
  124. lsParamNames.add("newStatus");
  125. lsParamNames.add("closeReason");
  126. handdleRequest(request, response, lsParamNames, "task/changeWorkStatus",XcglConstant.SYSTEM_HR);
  127. }
  128. /**
  129. * 提交工作执行结果
  130. * */
  131. @RequestMapping(value = "task/sendWorkResult")
  132. @ResponseBody
  133. public void sendWorkResult(HttpServletRequest request, HttpServletResponse response)
  134. {
  135. List<String> lsParamNames = new ArrayList<>();
  136. lsParamNames.add("openid");
  137. lsParamNames.add("pictureids");
  138. lsParamNames.add("taskid");
  139. lsParamNames.add("texts");
  140. lsParamNames.add("accessToken");
  141. handdleRequest(request, response, lsParamNames, "task/sendWorkResult",XcglConstant.SYSTEM_HR);
  142. }
  143. /**
  144. * 上报或分配工作
  145. * */
  146. @RequestMapping(value = "task/dispatchWork")
  147. @ResponseBody
  148. public void dispatchWork(HttpServletRequest request, HttpServletResponse response)
  149. {
  150. List<String> lsParamNames = new ArrayList<>();
  151. lsParamNames.add("openid");
  152. lsParamNames.add("taskid");
  153. lsParamNames.add("dispatchType");
  154. lsParamNames.add("newOwner");
  155. handdleRequest(request, response, lsParamNames, "task/dispatchWork",XcglConstant.SYSTEM_HR);
  156. }
  157. /**
  158. * 查询用户工作
  159. * | openid | true | string | 用户openid |
  160. | qrydate | false | string | 查询条件:日期,dateformater: yyyy-MM-dd hh:mm:ss ,参数中有taskid时,此参数可空|
  161. | taskid | false | string | 工作id。进入任务详情时使用此参数,其他场景可空 |
  162. * */
  163. @RequestMapping(value = "task/qryUserTasks")
  164. @ResponseBody
  165. public void qryUserTasks(HttpServletRequest request, HttpServletResponse response)
  166. {
  167. List<String> lsParamNames = new ArrayList<>();
  168. lsParamNames.add("openid");
  169. lsParamNames.add("taskid");
  170. lsParamNames.add("qrydate");
  171. lsParamNames.add("status");
  172. lsParamNames.add("projectid");
  173. handdleRequest(request, response, lsParamNames, "task/qryUserTasks",XcglConstant.SYSTEM_HR);
  174. }
  175. /**
  176. * 查询用户工作执行结果
  177. * | openid | true | string | 用户openid |
  178. | taskid | false | string | 工作id。进入任务详情时使用此参数,其他场景可空 |
  179. * */
  180. @RequestMapping(value = "task/qryTaskResult")
  181. @ResponseBody
  182. public void qryTaskResult(HttpServletRequest request, HttpServletResponse response)
  183. {
  184. List<String> lsParamNames = new ArrayList<>();
  185. lsParamNames.add("openid");
  186. lsParamNames.add("taskid");
  187. handdleRequest(request, response, lsParamNames, "task/qryTaskResult",XcglConstant.SYSTEM_HR);
  188. }
  189. /**
  190. * 查询项目工作
  191. * | projectid | true | string | 项目id |
  192. | openid | true | string | 用户openid |
  193. | qrydate | true | string | 查询条件:日期,dateformater: yyyy-MM-dd hh:mm:ss |
  194. | taskid | false | string | 工作id。进入任务详情时使用此参数,其他场景可空 |
  195. * */
  196. @RequestMapping(value = "task/qryProjectTasks")
  197. @ResponseBody
  198. public void qryProjectTasks(HttpServletRequest request, HttpServletResponse response)
  199. {
  200. List<String> lsParamNames = new ArrayList<>();
  201. lsParamNames.add("projectid");
  202. lsParamNames.add("taskid");
  203. lsParamNames.add("qrydate");
  204. lsParamNames.add("status");
  205. handdleRequest(request, response, lsParamNames, "task/qryProjectTasks",XcglConstant.SYSTEM_HR);
  206. }
  207. /**
  208. * 查询项目本日工作完成率
  209. * | projectid | true | string | 项目id |
  210. * */
  211. @RequestMapping(value = "task/qryTodayWorkStati")
  212. @ResponseBody
  213. public void qryTodayWorkStati(HttpServletRequest request, HttpServletResponse response)
  214. {
  215. List<String> lsParamNames = new ArrayList<>();
  216. lsParamNames.add("projectid");
  217. handdleRequest(request, response, lsParamNames, "task/qryTodayWorkStati",XcglConstant.SYSTEM_HR);
  218. }
  219. /**
  220. * 查询项目七日内工作完成率
  221. * | projectid | true | string | 项目id |
  222. * */
  223. @RequestMapping(value = "task/qryWeekWorkStati")
  224. @ResponseBody
  225. public void qryWeekWorkStati(HttpServletRequest request, HttpServletResponse response)
  226. {
  227. List<String> lsParamNames = new ArrayList<>();
  228. lsParamNames.add("projectid");
  229. handdleRequest(request, response, lsParamNames, "task/qryWeekWorkStati",XcglConstant.SYSTEM_HR);
  230. }
  231. /**
  232. * 查询项目设备状态统计
  233. * | projectid | true | string | 项目id |
  234. * */
  235. @RequestMapping(value = "device/qryDeviceStatusSumData")
  236. @ResponseBody
  237. public void qryDeviceStatusSumData(HttpServletRequest request, HttpServletResponse response)
  238. {
  239. List<String> lsParamNames = new ArrayList<>();
  240. lsParamNames.add("projectid");
  241. handdleRequest(request, response, lsParamNames, "device/qryDeviceStatusSumData",XcglConstant.SYSTEM_DEVICE);
  242. }
  243. /**
  244. * 查询项目设备监测数据
  245. * | projectid | true | string | 项目id |
  246. | openid | true | string | 用户openid |
  247. * */
  248. @RequestMapping(value = "device/qryProjectDevicesData")
  249. @ResponseBody
  250. public void qryProjectDevicesData(HttpServletRequest request, HttpServletResponse response)
  251. {
  252. List<String> lsParamNames = new ArrayList<>();
  253. lsParamNames.add("projectid");
  254. handdleRequest(request, response, lsParamNames, "device/qryProjectDevicesData",XcglConstant.SYSTEM_DEVICE);
  255. }
  256. /**
  257. * 查询项目设备本日能耗
  258. * */
  259. @RequestMapping(value = "device/getNenghao")
  260. @ResponseBody
  261. public void getNenghao(HttpServletRequest request, HttpServletResponse response)
  262. {
  263. List<String> lsParamNames = new ArrayList<>();
  264. lsParamNames.add("projectid");
  265. handdleRequest(request, response, lsParamNames, "device/getNenghao",XcglConstant.SYSTEM_DEVICE);
  266. }
  267. /**
  268. * 查询项目人员信息
  269. * | projectid | true | string | 项目id |
  270. | openid | true | string | 用户openid |
  271. * */
  272. @RequestMapping(value = "employee/qryProjectEmployee")
  273. @ResponseBody
  274. public void qryProjectEmployee(HttpServletRequest request, HttpServletResponse response)
  275. {
  276. List<String> lsParamNames = new ArrayList<>();
  277. lsParamNames.add("projectid");
  278. handdleRequest(request, response, lsParamNames, "employee/qryProjectEmployee",XcglConstant.SYSTEM_HR);
  279. }
  280. /**
  281. * 查询员工附件信息
  282. * | openid | true | String | openid |
  283. | userid | true | String | 员工id |
  284. * */
  285. @RequestMapping(value = "employee/qryEmployeeAttc")
  286. @ResponseBody
  287. public void qryEmployeeAttc(HttpServletRequest request, HttpServletResponse response)
  288. {
  289. List<String> lsParamNames = new ArrayList<>();
  290. lsParamNames.add("openid");
  291. lsParamNames.add("userid");
  292. handdleRequest(request, response, lsParamNames, "employee/qryEmployeeAttc",XcglConstant.SYSTEM_HR);
  293. }
  294. /**
  295. * 发送验证码
  296. * | phoneno | true | String | 手机号
  297. | openid | true | String | openid
  298. * @throws ClientException
  299. * @throws ServerException
  300. * */
  301. @RequestMapping(value = "employee/sendSms")
  302. @ResponseBody
  303. public void sendSms(HttpServletRequest request, HttpServletResponse response)
  304. {
  305. List<String> lsParamNames = new ArrayList<>();
  306. lsParamNames.add("phoneno");
  307. handdleRequest(request, response, lsParamNames, "employee/sendSms",XcglConstant.SYSTEM_HR);
  308. }
  309. /**
  310. * 查询(用户or项目)工作今日本周本月统计数据
  311. * */
  312. @RequestMapping(value = "task/qryTaskStati")
  313. @ResponseBody
  314. public void qryTaskStati(HttpServletRequest request, HttpServletResponse response)
  315. {
  316. List<String> lsParamNames = new ArrayList<>();
  317. lsParamNames.add("projectid");
  318. lsParamNames.add("openid");
  319. handdleRequest(request, response, lsParamNames, "task/qryTaskStati",XcglConstant.SYSTEM_HR);
  320. }
  321. /**
  322. * paramNames:参数名称列表,与API中协议一致
  323. * requestMapping:访问地址,一般与原访问地址一致
  324. * targetSystem:需要转发访问的系统
  325. * */
  326. private void handdleRequest(HttpServletRequest request, HttpServletResponse response,List<String> paramNames,String requestMapping,String targetSystem)
  327. {
  328. JSONObject json = new JSONObject();
  329. String serverIP = AgentInfo.getInstance().getServer(targetSystem);
  330. StringBuffer url = new StringBuffer();
  331. url.append(serverIP);
  332. url.append(requestMapping).append("?access=agent");
  333. for(String paramName : paramNames) {
  334. url.append(request.getParameterMap().containsKey(paramName)
  335. ?("&"+paramName+"="+request.getParameter(paramName))
  336. :"");
  337. }
  338. HttpClient httpclient = new DefaultHttpClient();
  339. HttpEntity entity = null;
  340. String jsonContent = "";
  341. HttpGet httpGet = null;
  342. try {
  343. // 设置超时时间
  344. httpclient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
  345. httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000);
  346. // 客户端的请求方法类型
  347. httpGet = new HttpGet(url.toString().replaceAll(" ", "%20"));
  348. HttpResponse responseForword = httpclient.execute(httpGet);
  349. // 检验返回码
  350. int statusCode = responseForword.getStatusLine().getStatusCode();
  351. if (statusCode != HttpStatus.SC_OK) {
  352. json.put("code", XcglConstant.RETCODE_SERVICE_ERROR);
  353. json.put("msg", XcglConstant.RETMSG_SERVICEERROR);
  354. response.setContentType("text/html;charset=utf-8");
  355. PrintWriter out = response.getWriter();
  356. out.println(json.toString());
  357. out.flush();
  358. out.close();
  359. }else {
  360. // 获取服务器返回页面的值
  361. entity = responseForword.getEntity();
  362. jsonContent = EntityUtils.toString(entity);
  363. httpGet.abort();
  364. json = JSONObject.fromObject(jsonContent);
  365. response.setContentType("text/html;charset=utf-8");
  366. PrintWriter out = response.getWriter();
  367. out.println(jsonContent);
  368. out.flush();
  369. out.close();
  370. }
  371. } catch(java.lang.IllegalArgumentException illega) {
  372. logger.error("catch:"+illega);
  373. logger.error("catch转码后:"+URLEncoder.encode(url.toString()));
  374. }
  375. catch (Exception e) {
  376. logger.error(e.getMessage());
  377. }finally {
  378. if (httpGet != null) {
  379. httpGet.releaseConnection();
  380. }
  381. }
  382. }
  383. /**
  384. * 上传附件
  385. * */
  386. @RequestMapping(value = "user/uploadPersonalImage")
  387. @ResponseBody
  388. public void uploadPersonalImage(HttpServletRequest request,HttpServletResponse response)
  389. {
  390. List<String> lsParamNames = new ArrayList<>();
  391. lsParamNames.add("userid");
  392. lsParamNames.add("isup");
  393. lsParamNames.add("isdel");
  394. lsParamNames.add("pictureid");
  395. lsParamNames.add("accessToken");
  396. handdleRequest(request, response, lsParamNames, "user/uploadPersonalImage",XcglConstant.SYSTEM_HR);
  397. }
  398. }