客户需求: 在系统录单时自动获取淘宝订单价格,在系统操作订单为已完成时,该订单在淘宝自动发货。
我这边使用到了阿奇索,这是一个比较好的对接淘宝的第三方平台,开发文档简单易懂,客服apple也比较好(扯远了)
进入开发之前,我们需要在阿齐索开放平台申请自己的账号,根据平台要求认证一些相关资料。最后创建属于自己的 Appid (自动生成对应的secret),让客户登录该淘宝授权地址https://alds.agiso.com/Open/Authorize.aspx 。授权完成之后将获取到的AccessToken拿到,就可以进入正式开发了(阿齐索也有自己的开发文档,讲的比较清楚,自己做个记录)
理一下思路:
1.一个淘宝商铺对应一个AccessToken,客户不止一个淘宝店铺
2.需要使用到的功能,获取淘宝订单详细信息,自动推送旺旺消息,自动发货功能等等
实现步骤:
将平台的Appid ,secret,淘宝授权的AccessToken 在数据库建立对应的字段,将这些数据保存到数据库,这样不同的淘宝店铺调用阿齐索的接口时,获取到对应的淘宝店铺授权的AccessToken就可以了。
写一个阿齐索调用接口的工具类,满足一个工具类调用不同的阿齐索接口。
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.cdchengqitech.business.pojo.TaobaoInterfaceBPo;
public class AQiSuoUtil {
/*inParm是调用方法时传入的参数,tid是淘宝的订单号,method是阿齐索对应的接口url
TaobaoInterfaceBPo 实体类是存放有平台的appid和secret,淘宝的授权码accessToken。
这样修改可以满足一个方法调用阿齐索很多的接口
*/
public static String temp(String inParam,String tid,String method,TaobaoInterfaceBPo tb){
String appSecret = tb.getAppSecret();//平台的appSecret
String accessToken = tb.getAccessToken();//淘宝授权码
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(method);//调用的接口
//设置头部
httpPost.addHeader("Authorization","Bearer "+ accessToken);
httpPost.addHeader("ApiVersion","1");
//业务参数
Map<String, String> data = new HashMap<String, String>();
data.put(inParam, tid);//调用接口对应的参数,有个tips分享,inParam在tid参数为
单个订单号的时候,这个值为tid,如果tid参数是多参,inParam为tids
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Long timestamp = System.currentTimeMillis() / 1000;
data.put("timestamp", timestamp.toString());
//参数签名
try {
data.put("sign", Sign(data,appSecret));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
for (Map.Entry<String, String> entry : data.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
//发起POST请求
try {
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse httpResponse = httpclient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return EntityUtils.toString(httpResponse.getEntity());
} else {
return ("doPost Error Response: " + httpResponse.getStatusLine().toString());
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//参数签名
public static String Sign(Map<String, String> params,String appSecret) throws NoSuchAlgorithmException, UnsupportedEncodingException
{
String[] keys = params.keySet().toArray(new String[0]);
Arrays.sort(keys);
StringBuilder query = new StringBuilder();
query.append(appSecret);
for (String key : keys) {
String value = params.get(key);
query.append(key).append(value);
}
query.append(appSecret);
byte[] md5byte = encryptMD5(query.toString());
return byte2hex(md5byte);
}
//byte数组转成16进制字符串
public static String byte2hex(byte[] bytes) {
StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
sign.append("0");
}
sign.append(hex.toLowerCase());
}
return sign.toString();
}
//Md5摘要
public static byte[] encryptMD5(String data) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
return md5.digest(data.getBytes("UTF-8"));
}
}
调用该方法的返回值在开放平台有介绍,写了一个类对应该返回值参数,然后使用json转化为对象
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@Accessors(chain = true)
public class TaoBaoResp {
private Boolean IsSuccess;
private TaoBaoPo Data;
public static Response success() {
return new Response();
}
public static Response success(TaoBaoPo Data) {
return new Response().setData(Data);
}
public static Response fail(String msg) {
return new Response().setCode(-1).setMsg(msg);
}
}
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class TaoBaoPo {
//参数有很多,我只取了自己需要的一部分
private String BuyerNick;
private BigDecimal Price;
private BigDecimal TotalFee;
private String ResultMsg;
private Integer ResultCode;
private String ErrorMsg;
private Integer ErrorCode;
}
最后就可以根据实际需求来调用阿齐索接口获取订单价格和完成自动发货功能了。