package com.engine.web.aixinaction; import com.alibaba.fastjson.JSON; import com.api.aixinchina.utils.HttpUtils; import com.api.aixinchina.utils.R; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import weaver.general.BaseBean; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.core.Context; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author:CL * @date:2023/1/29 8:34 */ public class AixinAction { private static final Logger LOGGER = LoggerFactory.getLogger("爱信中国"); @POST @Path("/rate") public R getRate(@Context HttpServletRequest request, @Context HttpServletResponse response) throws ParseException { LOGGER.info("com.engine.web.aixinaction.AixinAction 已进入 rate接口"); // 读取AppCode BaseBean bb=new BaseBean(); String AppCode = bb.getPropValue("aixinChina", "AppCode"); if(AppCode==""){ return R.error("请填写正确的AppCode"); } // 获取币种名称 String codeName = request.getParameter("code"); if ("RMB".equals(codeName)) { return R.success("100.00"); } // 或许要查询汇率的日期 String date = request.getParameter("date"); // 因为阿里云只能按月查询且格式必须yyyyMM 对日期进行格式转换 Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(date); String result = new SimpleDateFormat("yyyyMM").format(date1); // 访问第三方阿里云接口 String host = "http://ali-waihui.showapi.com"; String path = "/bankhis"; String method = "GET"; String appcode = AppCode; Map headers = new HashMap(); //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 headers.put("Authorization", "APPCODE " + appcode); Map querys = new HashMap(); // 货币编号 querys.put("code", codeName); querys.put("month", result); try { HttpResponse rageResponse = HttpUtils.doGet(host, path, method, headers, querys); // 获取查询的结果 转为map String mes = EntityUtils.toString(rageResponse.getEntity()); // 第一次结果的map HashMap resultMap = JSON.parseObject(mes, HashMap.class); LOGGER.info("com.engine.web.aixinaction.AixinAction 阿里云调用结果[{}]",resultMap); // 筛选第一次结果获取body HashMap bodyMap = JSON.parseObject(resultMap.get("showapi_res_body").toString(), HashMap.class); // 获取返回的状态码 code Integer retCode = (Integer) bodyMap.get("ret_code"); //判断阿里云api是否访问成功 ret_code为0查询成功 否则 查询失败 不扣费 if (retCode == 0) { // 获取到最终需要的list List> rateList = (List>) bodyMap.get("list"); for (Map map : rateList) { // 发布日期 String publish_time = map.get("publish_time"); // 判断日期是否为需要的日期 返回对应的中行折算价 if ( date.equals(publish_time)) { //中行折算价 String middle_rate = map.get("middle_rate"); return R.success(middle_rate); } } return R.error("该天未查询到汇率,请您重新输入"); } else { // 阿里云code调用失败的return 返回他的提示信息 String remark = (String) bodyMap.get("remark"); return R.error(remark); } } catch (Exception e) { LOGGER.info("com.engine.web.aixinaction.AixinAction catch捕获",e); return R.error("出现异常,请联系管理员"); } } }