查看接口信息
接口地址:http://192.168.1.68:8083/WebService/HRService.asmx?op=StaffBirthdayService
网址输入接口地址,获得如下接口信息:
HRService
单击此处,获取完整的操作列表。
StaffBirthdayService
获取当日生日员工工号[beginTime:年月日yyyy-mm-dd]
测试
测试窗体只能用于来自本地计算机的请求。SOAP 1.1
以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。
POST /WebService/HRService.asmx HTTP/1.1 Host: 192.168.1.68 Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://tempuri.org/StaffBirthdayService" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <StaffBirthdayService xmlns="http://tempuri.org/"> <beginTime>string</beginTime> </StaffBirthdayService> </soap:Body> </soap:Envelope>
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <StaffBirthdayServiceResponse xmlns="http://tempuri.org/"> <StaffBirthdayServiceResult>string</StaffBirthdayServiceResult> </StaffBirthdayServiceResponse> </soap:Body> </soap:Envelope>
SOAP 1.2
以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。
POST /WebService/HRService.asmx HTTP/1.1 Host: 192.168.1.68 Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <StaffBirthdayService xmlns="http://tempuri.org/"> <beginTime>string</beginTime> </StaffBirthdayService> </soap12:Body> </soap12:Envelope>
HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <StaffBirthdayServiceResponse xmlns="http://tempuri.org/"> <StaffBirthdayServiceResult>string</StaffBirthdayServiceResult> </StaffBirthdayServiceResponse> </soap12:Body> </soap12:Envelope>
以上包含 SOAP1.1 和 SOAP1.2,此次使用 SOAP1.2,注意在JAVA引用时,将传入参数<beginTime>string</beginTime>中的string换掉。
写接口
/wx-salary-api/src/main/java/com/aoyang/wxapp/service/salary/HrService.java 中写入接口,
/**
* Project Name: 澳洋信息系统微信台平台
* Date:2017年1月7日
* Copyright(c) 2016 All Rights Reserved
* @author gongkx
*
*/
package com.aoyang.wxapp.service.salary;
public interface HrService {
/**
* 获取当天生日的人员清单
* @return
*/
String getBirthdayStaffList(String beginDate);
}
写接口的实现类
1、写入请求代码
- 将接口中 SOAP1.2 的请求代码复制出来,并命名为字符串 soap,
- 注意将 <beginTime>string</beginTime> 改为 <beginTime>beginDate</beginTime>,因 string 容易出现重名字段,
- 将占位符替换为传入的参数,soap = soap.replace("beginDate", beginDate);
2、获取响应内容
String xmlContent = HTTPS.accessWebservice(BirthdayUrl, soap);
响应内容示例:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><StaffBirthdayServiceResponse xmlns="http://tempuri.org/"><StaffBirthdayServiceResult>[{"WORKERNAME":"邱德超","WORKERCODE":"AY057490"}]</StaffBirthdayServiceResult></StaffBirthdayServiceResponse></soap:Body></soap:Envelope>
3、提取响应内容的值
String value = Dom4jUtils.getElementStringValue(xmlContent , "StaffBirthdayServiceResult");
提取值示例:[{"WORKERNAME":"邱德超","WORKERCODE":"AY057490"}]
完整代码如下:
/**
* Project Name: 澳洋信息系统微信台平台
* Date:2017年1月7日
* Copyright(c) 2016 All Rights Reserved
* @author gongkx
*
*/
package com.aoyang.wxapp.service.salary.impl;
import org.springframework.stereotype.Service;
import com.aoyang.weixin.util.Dom4jUtils;
import com.aoyang.weixin.util.HTTPS;
import com.aoyang.wxapp.service.salary.HrService;
/**
* 工资服务接口的默认实现.
*
* <pre>
* 通过WebService连接工资系统,实现工资查询.
*
* @author gongkx
*
*/
@Service
public class HrServiceImpl implements HrService {
private final String BirthdayUrl = "http://192.168.1.68:8083/WebService/HRService.asmx?op=StaffBirthdayService";
/**
* 获取当天生日的人员清单
* @return
*/
@Override
public String getBirthdayStaffList(String beginDate) {
String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" +
" <soap12:Body>" +
" <StaffBirthdayService xmlns=\"http://tempuri.org/\">" +
" <beginTime>beginDate</beginTime>" +
" </StaffBirthdayService>" +
" </soap12:Body>" +
"</soap12:Envelope>";
soap = soap.replace("beginDate", beginDate); //当天日期
String xmlContent = HTTPS.accessWebservice(BirthdayUrl, soap);
if (xmlContent != null) {
String value = Dom4jUtils.getElementStringValue(xmlContent,"StaffBirthdayServiceResult");
return value;
}
return xmlContent;
}
}
调用方法
/wx-quartz-job/src/main/java/com/aoyang/wxapp/quartz/job/BirthdayBlessingDailyJob.java中调用方法
1、调用方法获取返回值
String birtydayStaff = hrService.getBirthdayStaffList(beginDate);
示例:[{"WORKERNAME":"邱德超","WORKERCODE":"AY057490"}]
2、将值装入List<Map>
List<Map<String,Object>> list = new Gson().fromJson(birtydayStaff, List.class);
3、提取需要的字段并拼接
String birtydayStaffId = "";
String birtydayStaffIdList = "";
for(Map<String,Object> map:list) {
birtydayStaffId = (String)map.get("WORKERCODE");
birtydayStaffIdList = birtydayStaffIdList.concat(birtydayStaffId+"|");
}//去掉最后一个多余的 |
birtydayStaffIdList = birtydayStaffIdList.substring(0,birtydayStaffIdList.length()-1);
完整代码如下:
/**
* Project Name: 澳洋信息系统微信台平台 Date:2019年8月20日 Copyright(c) 2019 All Rights Reserved
*
* @author qiudc.
*
*/
package com.aoyang.wxapp.quartz.job;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.aoyang.weixin.corp.model.msg.Article;
import com.aoyang.weixin.corp.model.msg.News;
import com.aoyang.weixin.corp.model.msg.resp.WxCorpNewsResponseMessage;
import com.aoyang.weixin.corp.service.WxCorpMessageSendService;
import com.aoyang.weixin.util.OAuth2Utils;
import com.aoyang.wxapp.service.fhoa.staff.StaffManager;
import com.aoyang.wxapp.service.salary.HrService;
import com.aoyang.wxapp.util.DateUtil;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.google.gson.Gson;
@Component("birthdayBlessingDailyJob")
public class BirthdayBlessingDailyJob implements SimpleJob {
private static final Logger LOGGER = LoggerFactory.getLogger(BirthdayBlessingDailyJob.class);
private static final String BIRTHDAY_BLESSING_URL = "weixin/hr/birthdayBlessing";
@Autowired
private HrService hrService;
@Resource(name="staffService")
private StaffManager staffService;
@Autowired
private WxCorpMessageSendService sendService;
@Value("${Hostname}")
private String hostName;
@Value("${agent.hr}")
private int hrAgentId;
public void syncVehicleReportDaily() throws Exception {
LOGGER.info("生日祝福日报推送开始......");
try {
String beginDate = DateUtil.getDay();
String birtydayStaff = hrService.getBirthdayStaffList(beginDate);
List<Map<String,Object>> list = new Gson().fromJson(birtydayStaff, List.class);
if(list == null || list.isEmpty()) {
LOGGER.info("未找到过生日的员工");
return;
}
LOGGER.info("获取的生日人员清单:{}",birtydayStaff);
String birtydayStaffId = "";
String birtydayStaffIdList = "";
for(Map<String,Object> map:list) {
birtydayStaffId = (String)map.get("WORKERCODE");
birtydayStaffIdList = birtydayStaffIdList.concat(birtydayStaffId+"|");
}
birtydayStaffIdList = birtydayStaffIdList.substring(0,birtydayStaffIdList.length()-1);
LOGGER.info("过生日人员清单拼接",birtydayStaffIdList);
if (birtydayStaffIdList != null) {
//推送的文字内容、图片url、跳转链接
String sendContent = "澳洋集团祝您生日快乐!";
String picurl = "http://weixin.aoyang.com/group1/M00/05/8D/wKgBxl1LeCCADe4-AAoVjP4jB4A736.jpg";
String url = OAuth2Utils.getOAuthUrl(hostName,BIRTHDAY_BLESSING_URL,hrAgentId);
List<Article> articles = new ArrayList<Article>();
articles.add(new Article("生日祝福", sendContent, picurl, url));
News news = new News();
news.setArticles(articles);
//推送微信通知给所有今天过生日的人
WxCorpNewsResponseMessage message = new WxCorpNewsResponseMessage();
message.setNews(news);
message.setTouser(birtydayStaffIdList);
//隶属于hr应用
message.setAgentid(hrAgentId);
message.setTotag("@all"); //@all表示忽略本参数
message.setToparty("@all");
String backMsg = sendService.sendMessage(message);
backMsg = (backMsg == null ? "" : backMsg);
LOGGER.info("[ITSM] 推送消息 : 生日祝福" + ";返回的数据为:" + backMsg);
}
} catch (Exception e) {
LOGGER.error("生日祝福日报推送时发生异常,{}", e);
}
LOGGER.info("生日祝福日报推送定时任务执行完毕");
}
@Override
public void execute(ShardingContext shardingContext) {
try {
LOGGER.info("分片值:" + shardingContext.getShardingItem());
syncVehicleReportDaily();
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}
}