| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- package com.xcgl.weixin;
- import com.aliyuncs.exceptions.ClientException;
- import com.aliyuncs.exceptions.ServerException;
- import com.xcgl.utils.AgentInfo;
- import com.xcgl.utils.XcglConstant;
- import com.xcgl.weixin.entity.WXAjaxJson;
- import net.sf.json.JSONObject;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.params.CoreConnectionPNames;
- import org.apache.http.util.EntityUtils;
- import org.apache.log4j.Logger;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.ResponseBody;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.PrintWriter;
- import java.net.URLEncoder;
- import java.util.ArrayList;
- import java.util.List;
- @Controller
- @RequestMapping(value = "/api/wx")
- public class WXControllerAgent {
- private static final Logger logger = Logger.getLogger(WXControllerAgent.class);
- /**
- * 访问地址:http://localhost:8080/xcgl/api/wx
- * @return
- */
- @RequestMapping(method = RequestMethod.GET)
- @ResponseBody
- public WXAjaxJson checkNet() {
- WXAjaxJson json = new WXAjaxJson();
- return json;
- }
-
- @RequestMapping(value = "user/get_user_info")
- @ResponseBody
- public void getUserInfo(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("openid");
- lsParamNames.add("timestamp");
- handdleRequest(request, response, lsParamNames, "user/get_user_info",XcglConstant.SYSTEM_HR);
- }
-
- @RequestMapping(value = "user/getProjectList4Mine")
- @ResponseBody
- public void getProjectList4Mine(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("openid");
- lsParamNames.add("timestamp");
- handdleRequest(request, response, lsParamNames, "user/getProjectList4Mine",XcglConstant.SYSTEM_HR);
- }
- @RequestMapping(value = "user/getProjectList")
- @ResponseBody
- public void getProjectList(HttpServletRequest request, HttpServletResponse response) {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("openid");
- lsParamNames.add("timestamp");
- handdleRequest(request, response, lsParamNames, "user/getProjectList",XcglConstant.SYSTEM_HR);
- }
- /**
- * 微信绑定手机号
- * */
- @RequestMapping(value = "user/bindTel")
- @ResponseBody
- public void bindTel(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("openid");
- lsParamNames.add("phonenum");
- lsParamNames.add("timestamp");
- handdleRequest(request, response, lsParamNames, "user/bindTel",XcglConstant.SYSTEM_HR);
- }
-
- /**
- * 上传附件
- * */
- @RequestMapping(value = "user/uploadAttachment")
- @ResponseBody
- public void uploadAttachment(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("userid");
- lsParamNames.add("isup");
- lsParamNames.add("isdel");
- lsParamNames.add("picture0id");
- lsParamNames.add("picture1id");
- lsParamNames.add("picture2id");
- lsParamNames.add("picture3id");
- lsParamNames.add("picture4id");
- lsParamNames.add("picture5id");
- lsParamNames.add("picture6id");
- lsParamNames.add("accessToken");
- handdleRequest(request, response, lsParamNames, "user/uploadAttachment",XcglConstant.SYSTEM_HR);
- }
-
- /**
- * 从服务器获取用户功能权限
- * */
- @RequestMapping(value = "user/qryAuth")
- @ResponseBody
- public void qryAuth(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("openid");
- handdleRequest(request, response, lsParamNames, "user/qryAuth",XcglConstant.SYSTEM_HR);
- }
- /**
- * 变更工作状态
- * */
- @RequestMapping(value = "task/changeWorkStatus")
- @ResponseBody
- public void changeWorkStatus(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("userid");
- lsParamNames.add("openid");
- lsParamNames.add("taskid");
- lsParamNames.add("oldStatus");
- lsParamNames.add("newStatus");
- lsParamNames.add("closeReason");
- handdleRequest(request, response, lsParamNames, "task/changeWorkStatus",XcglConstant.SYSTEM_HR);
- }
- /**
- * 提交工作执行结果
- * */
- @RequestMapping(value = "task/sendWorkResult")
- @ResponseBody
- public void sendWorkResult(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("openid");
- lsParamNames.add("pictureids");
- lsParamNames.add("taskid");
- lsParamNames.add("texts");
- lsParamNames.add("accessToken");
- handdleRequest(request, response, lsParamNames, "task/sendWorkResult",XcglConstant.SYSTEM_HR);
- }
- /**
- * 上报或分配工作
- * */
- @RequestMapping(value = "task/dispatchWork")
- @ResponseBody
- public void dispatchWork(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("openid");
- lsParamNames.add("taskid");
- lsParamNames.add("dispatchType");
- lsParamNames.add("newOwner");
- handdleRequest(request, response, lsParamNames, "task/dispatchWork",XcglConstant.SYSTEM_HR);
- }
- /**
- * 查询用户工作
- * | openid | true | string | 用户openid |
- | qrydate | false | string | 查询条件:日期,dateformater: yyyy-MM-dd hh:mm:ss ,参数中有taskid时,此参数可空|
- | taskid | false | string | 工作id。进入任务详情时使用此参数,其他场景可空 |
- * */
- @RequestMapping(value = "task/qryUserTasks")
- @ResponseBody
- public void qryUserTasks(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("openid");
- lsParamNames.add("taskid");
- lsParamNames.add("qrydate");
- lsParamNames.add("status");
- lsParamNames.add("projectid");
- handdleRequest(request, response, lsParamNames, "task/qryUserTasks",XcglConstant.SYSTEM_HR);
- }
- /**
- * 查询用户工作执行结果
- * | openid | true | string | 用户openid |
- | taskid | false | string | 工作id。进入任务详情时使用此参数,其他场景可空 |
- * */
- @RequestMapping(value = "task/qryTaskResult")
- @ResponseBody
- public void qryTaskResult(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("openid");
- lsParamNames.add("taskid");
- handdleRequest(request, response, lsParamNames, "task/qryTaskResult",XcglConstant.SYSTEM_HR);
- }
- /**
- * 查询项目工作
- * | projectid | true | string | 项目id |
- | openid | true | string | 用户openid |
- | qrydate | true | string | 查询条件:日期,dateformater: yyyy-MM-dd hh:mm:ss |
- | taskid | false | string | 工作id。进入任务详情时使用此参数,其他场景可空 |
- * */
- @RequestMapping(value = "task/qryProjectTasks")
- @ResponseBody
- public void qryProjectTasks(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("projectid");
- lsParamNames.add("taskid");
- lsParamNames.add("qrydate");
- lsParamNames.add("status");
- handdleRequest(request, response, lsParamNames, "task/qryProjectTasks",XcglConstant.SYSTEM_HR);
- }
- /**
- * 查询项目本日工作完成率
- * | projectid | true | string | 项目id |
- * */
- @RequestMapping(value = "task/qryTodayWorkStati")
- @ResponseBody
- public void qryTodayWorkStati(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("projectid");
- handdleRequest(request, response, lsParamNames, "task/qryTodayWorkStati",XcglConstant.SYSTEM_HR);
- }
-
- /**
- * 查询项目七日内工作完成率
- * | projectid | true | string | 项目id |
- * */
- @RequestMapping(value = "task/qryWeekWorkStati")
- @ResponseBody
- public void qryWeekWorkStati(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("projectid");
- handdleRequest(request, response, lsParamNames, "task/qryWeekWorkStati",XcglConstant.SYSTEM_HR);
- }
-
- /**
- * 查询项目设备状态统计
- * | projectid | true | string | 项目id |
- * */
- @RequestMapping(value = "device/qryDeviceStatusSumData")
- @ResponseBody
- public void qryDeviceStatusSumData(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("projectid");
- handdleRequest(request, response, lsParamNames, "device/qryDeviceStatusSumData",XcglConstant.SYSTEM_DEVICE);
- }
-
- /**
- * 查询项目设备监测数据
- * | projectid | true | string | 项目id |
- | openid | true | string | 用户openid |
- * */
- @RequestMapping(value = "device/qryProjectDevicesData")
- @ResponseBody
- public void qryProjectDevicesData(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("projectid");
- handdleRequest(request, response, lsParamNames, "device/qryProjectDevicesData",XcglConstant.SYSTEM_DEVICE);
- }
-
- /**
- * 查询项目设备本日能耗
- * */
- @RequestMapping(value = "device/getNenghao")
- @ResponseBody
- public void getNenghao(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("projectid");
- handdleRequest(request, response, lsParamNames, "device/getNenghao",XcglConstant.SYSTEM_DEVICE);
- }
-
- /**
- * 查询项目人员信息
- * | projectid | true | string | 项目id |
- | openid | true | string | 用户openid |
- * */
- @RequestMapping(value = "employee/qryProjectEmployee")
- @ResponseBody
- public void qryProjectEmployee(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("projectid");
- handdleRequest(request, response, lsParamNames, "employee/qryProjectEmployee",XcglConstant.SYSTEM_HR);
- }
-
- /**
- * 查询员工附件信息
- * | openid | true | String | openid |
- | userid | true | String | 员工id |
- * */
- @RequestMapping(value = "employee/qryEmployeeAttc")
- @ResponseBody
- public void qryEmployeeAttc(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("openid");
- lsParamNames.add("userid");
- handdleRequest(request, response, lsParamNames, "employee/qryEmployeeAttc",XcglConstant.SYSTEM_HR);
- }
-
- /**
- * 发送验证码
- * | phoneno | true | String | 手机号
- | openid | true | String | openid
- * @throws ClientException
- * @throws ServerException
- * */
- @RequestMapping(value = "employee/sendSms")
- @ResponseBody
- public void sendSms(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("phoneno");
- handdleRequest(request, response, lsParamNames, "employee/sendSms",XcglConstant.SYSTEM_HR);
- }
-
- /**
- * 查询(用户or项目)工作今日本周本月统计数据
- * */
- @RequestMapping(value = "task/qryTaskStati")
- @ResponseBody
- public void qryTaskStati(HttpServletRequest request, HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("projectid");
- lsParamNames.add("openid");
- handdleRequest(request, response, lsParamNames, "task/qryTaskStati",XcglConstant.SYSTEM_HR);
- }
-
- /**
- * paramNames:参数名称列表,与API中协议一致
- * requestMapping:访问地址,一般与原访问地址一致
- * targetSystem:需要转发访问的系统
- * */
- private void handdleRequest(HttpServletRequest request, HttpServletResponse response,List<String> paramNames,String requestMapping,String targetSystem)
- {
- JSONObject json = new JSONObject();
- String serverIP = AgentInfo.getInstance().getServer(targetSystem);
- StringBuffer url = new StringBuffer();
- url.append(serverIP);
- url.append(requestMapping).append("?access=agent");
- for(String paramName : paramNames) {
- url.append(request.getParameterMap().containsKey(paramName)
- ?("&"+paramName+"="+request.getParameter(paramName))
- :"");
- }
- HttpClient httpclient = new DefaultHttpClient();
- HttpEntity entity = null;
- String jsonContent = "";
- HttpGet httpGet = null;
- try {
- // 设置超时时间
- httpclient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
- httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000);
- // 客户端的请求方法类型
- httpGet = new HttpGet(url.toString().replaceAll(" ", "%20"));
- HttpResponse responseForword = httpclient.execute(httpGet);
- // 检验返回码
- int statusCode = responseForword.getStatusLine().getStatusCode();
- if (statusCode != HttpStatus.SC_OK) {
- json.put("code", XcglConstant.RETCODE_SERVICE_ERROR);
- json.put("msg", XcglConstant.RETMSG_SERVICEERROR);
- response.setContentType("text/html;charset=utf-8");
- PrintWriter out = response.getWriter();
- out.println(json.toString());
- out.flush();
- out.close();
- }else {
- // 获取服务器返回页面的值
- entity = responseForword.getEntity();
- jsonContent = EntityUtils.toString(entity);
- httpGet.abort();
- json = JSONObject.fromObject(jsonContent);
- response.setContentType("text/html;charset=utf-8");
- PrintWriter out = response.getWriter();
- out.println(jsonContent);
- out.flush();
- out.close();
- }
-
- } catch(java.lang.IllegalArgumentException illega) {
- logger.error("catch:"+illega);
- logger.error("catch转码后:"+URLEncoder.encode(url.toString()));
- }
- catch (Exception e) {
- logger.error(e.getMessage());
- }finally {
- if (httpGet != null) {
- httpGet.releaseConnection();
- }
- }
- }
- /**
- * 上传附件
- * */
- @RequestMapping(value = "user/uploadPersonalImage")
- @ResponseBody
- public void uploadPersonalImage(HttpServletRequest request,HttpServletResponse response)
- {
- List<String> lsParamNames = new ArrayList<>();
- lsParamNames.add("userid");
- lsParamNames.add("isup");
- lsParamNames.add("isdel");
- lsParamNames.add("pictureid");
- lsParamNames.add("accessToken");
- handdleRequest(request, response, lsParamNames, "user/uploadPersonalImage",XcglConstant.SYSTEM_HR);
- }
- }
|