welcome to xlongwei.com

欢迎大家一起学习、交流、分享


QQ:9167702333 邮箱:admin@xlongwei.com

ShowApiUtil 秀派聚合接口工具


分类 Java   关键字 分享   标签 java   web   发布 hongwei  1442279346436
注意 转载须保留原文链接,译文链接,作者译者等信息。  
showapi是一个接口聚合平台,接入showapi有许多好多:很多复杂的接口不必自己费劲去实现了(比如分词、文本相似度、图形验证码、OCR识别 等),很多收费的接口不必自己去联系各家接口提供商(比如短信接口,word转pdf接口等),还有很多常用的接口使用很方便(比如常见的数据校验接口 等)。ShowApiUtil封装了调用showapi接口的通用方法。

package com.itecheast.ite.front.util;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itecheast.ite.domain.util.DateUtil;
import com.itecheast.ite.domain.util.DateUtil.FormatType;
import com.itecheast.ite.domain.util.GlobalConfig;
import com.itecheast.ite.domain.util.ServiceRequester;
import com.itecheast.ite.domain.util.StringUtil;

public class ShowApiUtil {
private static String myapp = GlobalConfig.getProperty("showapi.myapp");
private static String mysecret = GlobalConfig.getProperty("showapi.mysecret");
private static Logger logger = LoggerFactory.getLogger(ShowApiUtil.class);

/** 发送短信到手机号,返回接口调用成功或失败 */
public static boolean sendSms(String mobile, String tNum, Map<String, String> content) {
//使用tNum作为name,提取出实际的tNum短信模板编号,短信模板使用ShowApiUtilTester管理,然后再配置文件设置
tNum = StringUtil.getPatternString(GlobalConfig.getProperty("showapi.sms.tNums"), tNum+":(T\\d+)");
if(StringUtils.isBlank(tNum)) return false;//需要指定短信模板编号

Map<String, String> params = new HashMap<>();
params.put("mobile", mobile);
params.put("tNum", tNum);
params.put("content", buildContent(content));
String post = post("http://route.showapi.com/28-1", params);

if(StringUtils.isBlank(post)) return false;

JSONObject json = JSON.parseObject(post);
int showapi_res_code = json.getIntValue("showapi_res_code");
String showapi_res_error = json.getString("showapi_res_error");
if(showapi_res_code==0) {
logger.info("ShowApiUtil.sendCheckcode success");
return true;
}else {
logger.info("ShowApiUtil.sendCheckcode failed: "+showapi_res_error);
return false;
}
}

/** 转换doc至pdf,返回下载链接,支持doc和docx类型文档 */
public static String doc2pdf(File doc) {
if(StringUtils.isBlank(myapp) || StringUtils.isBlank(mysecret) || doc==null || !doc.exists()) return null;
Map<String, String> params = new HashMap<>();
params.put("showapi_appid", myapp);
params.put("showapi_timestamp", DateUtil.format(new Date(), FormatType.SECONDS));
sign(params);

try {
String post = ServiceRequester.post("http://route.showapi.com/770-1", params, "doc", doc);
if(StringUtils.isBlank(post)) return null;

JSONObject json = JSON.parseObject(post);
int showapi_res_code = json.getIntValue("showapi_res_code");
String showapi_res_error = json.getString("showapi_res_error");
if(showapi_res_code==0) {
JSONObject showapi_res_body = json.getJSONObject("showapi_res_body");
String domain = showapi_res_body.getString("domain");
String path = showapi_res_body.getString("path");
if(StringUtils.isNotBlank(domain) && StringUtils.isNotBlank(path)) {
String url = domain+path;
logger.info("ShowApiUtil.doc2pdf success: "+url);
return url;
}else {
logger.info("ShowApiUtil.doc2pdf failed: "+showapi_res_body.toJSONString());
}
}else {
logger.info("ShowApiUtil.doc2pdf failed: "+showapi_res_error);
}
}catch(Exception e) {
logger.warn("ShowApiUtil.doc2pdf failed: "+e.getMessage(), e);
}
return null;
}

/** 校验数据类型
* type 基础校验:numbers全数字 identifier字母或下划线开头的用户名 chinese全中文 email邮箱 mobile手机号 url网址
* 高级校验:idNumber身份证号 organizationCode组织机构代码 taxRegistrationNo税务登记号 /regex/正则匹配
 */
public static boolean validate(String type, String value) {
Map<String, String> params = new HashMap<>();
params.put("type", type);
params.put("value", value);
String post = post("http://route.showapi.com/794-1", params);

if(StringUtils.isBlank(post)) return false;

JSONObject json = JSON.parseObject(post);
int showapi_res_code = json.getIntValue("showapi_res_code");
String showapi_res_error = json.getString("showapi_res_error");
if(showapi_res_code==0) {
boolean valid = json.getBooleanValue("valid");
logger.info("ShowApiUtil.validate success, valid: "+valid);
return valid;
}else {
logger.info("ShowApiUtil.sendCheckcode failed: "+showapi_res_error);
return false;
}
}

/** 加密并提交请求至showapi */
public static String post(String service, Map<String, String> params) {
if(StringUtils.isBlank(myapp) || StringUtils.isBlank(mysecret)) return null;
params.put("showapi_appid", myapp);
params.put("showapi_timestamp", DateUtil.format(new Date(), FormatType.SECONDS));
sign(params);
return ServiceRequester.post(service, params);
}

/** 构建短信模板的content参数 */
public static String buildContent(Map<String, String> content) {
StringBuilder sb = new StringBuilder("{");
if(content!=null && content.size()>0) {
for(String name : content.keySet()) {
String value = content.get(name);
sb.append(name+"'"+value+"',");
}
}
sb.setCharAt(sb.length()-1, '}');
return sb.toString();
}

private static void sign(Map<String, String> params) {
//1.去除file字段 ,同时排序
TreeSet<String> keys=new TreeSet<String>(params.keySet());
//2.把所有参数名和参数值串在一起
StringBuilder query = new StringBuilder();
for(String key : keys) {
String value = params.get(key);
if(StringUtils.isNotBlank(value)) {
query.append(key).append(value);
}
}
query.append(mysecret);
// 3.使用MD5/HMAC加密
byte[] bytes = DigestUtils.md5(query.toString());
// 4.把二进制转化为大写的十六进制
String sign = Hex.encodeHexString(bytes);
params.put("showapi_sign", sign);
}
}

属性文件配置:
showapi.myapp=7827
showapi.mysecret=
showapi.sms.tNums=checkcode:T150909000214 #格式name:tNum,name:tNum

测试类,以及短信模板创建
public class ShowApiUtilTester {
String smsTemplateRetrieve = "http://route.showapi.com/28-3";
String smsTemplateCreate = "http://route.showapi.com/28-2";
String smsSend = "http://route.showapi.com/28-1";
String tNum = "T150909000214";

@Test public void retrive() {
Map<String, String> params = new HashMap<>();
String post = ShowApiUtil.post(smsTemplateRetrieve, params);
System.out.println(post);
}
@Test public void create() {
Map<String, String> params = new HashMap<>();
params.put("title", "倍赢财富箱");//要求格式名称不让审核不过
params.put("content", "尊敬的客户:[name],您好!您本次操作的验证码是[code],请在[minute]分钟内使用,过期无效!");
String post = ShowApiUtil.post(smsTemplateCreate, params);
System.out.println(post);
}
@Test public void send() {
Map<String, String> params = new HashMap<>();
params.put("mobile", "18810761776");
params.put("content", "{name:'xlongwei',code:'87653',minute:'3'}");
params.put("tNum", tNum);
String post = ShowApiUtil.post(smsSend, params);
System.out.println(post);
}
@Test public void doc2pdf() {
File doc = new File("src/test/resources/test.docx");
String url = ShowApiUtil.doc2pdf(doc);
System.out.println(url);
}
@Test public void validate() {
System.out.println(ShowApiUtil.validate("organizationCode", "30527275-2"));
System.out.println(ShowApiUtil.validate("taxRegistrationNo", "500108095339225"));
System.out.println(ShowApiUtil.validate("mobile", "18810761776"));
}
}

ServiceRequester参考:https://www.xlongwei.com/detail/15091515



评论列表