网站后台怎么做,怎么做网站导航栏,wordpress自动评论软件,江都建设网站一、背景
最近公司让我对接顺丰同城急送的API#xff0c;讲讲里面我遇到的坑
官方的API文档给我的感觉是不怎么规范的#xff0c;很多细节要靠猜#xff0c;示例代码也不全#xff0c;具体细节不多说#xff0c;如果你现在也需要对接他们API#xff0c;可以参考本篇博客…一、背景
最近公司让我对接顺丰同城急送的API讲讲里面我遇到的坑
官方的API文档给我的感觉是不怎么规范的很多细节要靠猜示例代码也不全具体细节不多说如果你现在也需要对接他们API可以参考本篇博客再配合官方文档结合起来看可以让您再开发的时候少掉两根头发对您会有一定帮助的
官网api文档
首先你们要对接他们产品之前需要得到账号账号这边是同事给我的 开始对接之前必须要搞清楚你们对接的是店铺还是企业版的区别就是企业版本的顺丰官方会给你一个卡号是月结卡你公司本月下的单扣费就在这个卡里面扣款然后月底结算费用店铺的是没有的其他的后续我再补充 二、代码 用他这个签名是没毛病的但是如果你传入的数据只有一层对象那么是可以的但是如果你传入的数据是二维map甚至更多层级这里根据他的接口参数决定那这样再调用他的sign签名会出问题因为他这个postData参数必须是json格式的字符串而且还要排序的postData里面是你这个接口所有的参数具体代码还是看我的这个更实在sign具体看generateOpenSign方法
package com.admin.business.controller.sfsamecity;import com.admin.util.HttpUtils;
import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;import java.io.IOException;
import java.security.MessageDigest;
import java.util.*;import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;Component
public class SFHapper {protected Logger logger LoggerFactory.getLogger(this.getClass());Value(${sf.same.city.devId})private String devId;Value(${sf.same.city.secretKey})private String secretKey;/*** 预创建订单店铺* https://openic.sf-express.com/open/api/docs/index#/apidoc*/public String precreateorder(MapString, Object param){try {param.put(dev_id,Integer.parseInt(devId));String jsonString formatAndSortMap(param,0);String sign generateOpenSign(jsonString,Integer.parseInt(devId),secretKey);String resultStr null;try {resultStr HttpUtils.postJson(https://openic.sf-express.com/open/api/external/precreateorder?signsign, jsonString);}catch (Exception e){e.printStackTrace();}return resultStr;} catch (Exception e) {logger.error(e.getMessage());}return null;}/*** https://openic.sf-express.com/open/api/docs/index#/apidoc* 创建订单店铺**/public String createorder(MapString, Object param){try {param.put(dev_id,Integer.parseInt(devId));String jsonString formatAndSortMap(param,0);String sign generateOpenSign(jsonString,Integer.parseInt(devId),secretKey);String resultStr null;try {resultStr HttpUtils.postJson(https://openic.sf-express.com/open/api/external/createorder?signsign, jsonString);}catch (Exception e){e.printStackTrace();}return resultStr;} catch (Exception e) {logger.error(e.getMessage());}return null;}/*** 预创建订单企业* https://openic.sf-express.com/open/api/docs/index#/apidoc*/public String precreateorderEnterprise(MapString, Object param){try {param.put(dev_id,Integer.parseInt(devId));String jsonString formatAndSortMap(param,0);String sign generateOpenSign(jsonString,Integer.parseInt(devId),secretKey);String resultStr null;try {resultStr HttpUtils.postJson(https://openic.sf-express.com/open/api/external/precreateorder4c?signsign, jsonString);}catch (Exception e){e.printStackTrace();}return resultStr;} catch (Exception e) {logger.error(e.getMessage());}return null;}/*** https://openic.sf-express.com/open/api/docs/index#/apidoc* 创建订单企业**/public String createorderEnterprise(MapString, Object param){try {param.put(dev_id,Integer.parseInt(devId));String jsonString formatAndSortMap(param,0);String sign generateOpenSign(jsonString,Integer.parseInt(devId),secretKey);String resultStr null;try {resultStr HttpUtils.postJson(https://openic.sf-express.com/open/api/external/createorder4c?signsign, jsonString);}catch (Exception e){e.printStackTrace();}return resultStr;} catch (Exception e) {logger.error(e.getMessage());}return null;}/*** https://openic.sf-express.com/open/api/docs/index#/apidoc* 预取消订单*/public String precancelorder(MapString, Object param){try {param.put(dev_id,Integer.parseInt(devId));String jsonString formatAndSortMap(param,0);String sign generateOpenSign(jsonString,Integer.parseInt(devId),secretKey);String resultStr null;try {resultStr HttpUtils.postJson(https://openic.sf-express.com/open/api/external/precancelorder?signsign, jsonString);}catch (Exception e){e.printStackTrace();}return resultStr;} catch (Exception e) {logger.error(e.getMessage());}return null;}/*** https://openic.sf-express.com/open/api/docs/index#/apidoc* 取消订单店铺*/public String cancelorder(MapString, Object param){try {param.put(dev_id,Integer.parseInt(devId));String jsonString formatAndSortMap(param,0);String sign generateOpenSign(jsonString,Integer.parseInt(devId),secretKey);String resultStr null;try {resultStr HttpUtils.postJson(https://openic.sf-express.com/open/api/external/cancelorder?signsign, jsonString);}catch (Exception e){e.printStackTrace();}return resultStr;} catch (Exception e) {logger.error(e.getMessage());}return null;}/*** https://openic.sf-express.com/open/api/docs/index#/apidoc* 获取配送员轨迹H5店铺*/public String riderviewv2(MapString, Object param){try {param.put(dev_id,Integer.parseInt(devId));String jsonString formatAndSortMap(param,0);String sign generateOpenSign(jsonString,Integer.parseInt(devId),secretKey);String resultStr null;try {resultStr HttpUtils.postJson(https://openic.sf-express.com/open/api/external/riderviewv2?signsign, jsonString);}catch (Exception e){e.printStackTrace();}return resultStr;} catch (Exception e) {logger.error(e.getMessage());}return null;}public static void main(String[] args) throws IOException {MapString, Object param new HashMap();param.put(shop_id,3243xxx93);param.put(dev_id,1691xxx52);
// param.put(shop_type,1);param.put(user_lng,1xx.16427833749388);param.put(user_lat,2xx.558482814127863);param.put(user_address,广东省深圳市罗湖区中xxxxxx);param.put(weight,20);param.put(product_type,18);// 转换为秒级时间戳long timestampInMillis System.currentTimeMillis();long timestampInSeconds timestampInMillis / 1000;param.put(push_time,timestampInSeconds);System.out.println(timestampInSeconds);
// param.put(total_price,);
// param.put(is_appoint,);
// param.put(appoint_type,);
// param.put(expect_time,);
// param.put(expect_pickup_time,);
// param.put(lbs_type,);
// param.put(is_insured,);
// param.put(is_person_direct,);
// param.put(vehicle,);
// param.put(four_wheeler_type,);
// param.put(declared_value,);
// param.put(gratuity_fee,);
// param.put(rider_pick_method,);
// param.put(return_flag,);String jsonString formatAndSortMap(param,0);String sign generateOpenSign(jsonString,169xxx52,3c58cb1exxxxxx867);System.out.println(sign);}/*** 生成签名* param jsonString* param devId* param appKey* return**/public static String generateOpenSign(String jsonString, Integer devId, String appKey) throws IOException {String sb jsonString devId appKey;MessageDigest md null;String ret null;try {md MessageDigest.getInstance(MD5);byte[] md5 md.digest(sb.toString().getBytes(utf-8));int i;StringBuffer buf new StringBuffer();for (int offset 0; offset md5.length; offset) {i md5[offset];if (i 0) {i 256;}if (i 16) {buf.append(0);}buf.append(Integer.toHexString(i));}ret Base64.encodeBase64String(buf.toString().getBytes(utf-8));} catch (Exception e) {throw new RuntimeException(e);}return ret;}// 将 Map 转换成指定格式的字符串并排序键值对public static String formatAndSortMap(MapString, Object map, int indentLevel) {// 将 Map 的键值对转换成 ListListMap.EntryString, Object entryList new ArrayList(map.entrySet());// 对 List 中的键值对按照键进行排序Collections.sort(entryList, Comparator.comparing(Map.Entry::getKey));// 构建格式化后的字符串StringBuilder sb new StringBuilder();String indent getIndent(indentLevel);sb.append({\n);for (Map.EntryString, Object entry : entryList) {sb.append(indent).append( \).append(entry.getKey()).append(\: );Object value entry.getValue();if (value instanceof Map) {// 如果值是 Map则递归处理sb.append(formatAndSortMap((MapString, Object) value, indentLevel 1));} else if (value instanceof List) {// 如果值是 List则递归处理sb.append(formatList((List?) value, indentLevel 1));} else if (value instanceof String) {sb.append(\).append(value).append(\);} else {sb.append(value);}sb.append(,\n);}sb.deleteCharAt(sb.length() - 2); // 删除最后一个逗号sb.append(indent).append(});return sb.toString();}// 将 List 转换成指定格式的字符串private static String formatList(List? list, int indentLevel) {StringBuilder sb new StringBuilder();String indent getIndent(indentLevel);sb.append([\n);for (Object value : list) {sb.append(indent).append( );if (value instanceof Map) {// 如果值是 Map则递归处理sb.append(formatAndSortMap((MapString, Object) value, indentLevel 1));} else if (value instanceof String) {sb.append(\).append(value).append(\);} else {sb.append(value);}sb.append(,\n);}sb.deleteCharAt(sb.length() - 2); // 删除最后一个逗号sb.append(indent).append(]);return sb.toString();}// 根据缩进级别生成缩进字符串private static String getIndent(int indentLevel) {StringBuilder sb new StringBuilder();for (int i 0; i indentLevel; i) {sb.append( ); // 使用两个空格作为缩进}return sb.toString();}
}三、回调
前面的工作其实还好回调这里特别鸡肋因为他们提供的测试环境是不可以回调的也就是触发不了回调只能下生产订单当然这样是要你们给真实的钱出去的然后真实的骑手过来取货然后联系他们的人员把你下的这订单指派给这位骑手小哥我这里还的感谢那位骑手小哥他坐在我旁边配合我测试配合我联调非常谢谢他
联调的话你要再后台配置你的回调URL 以上动作配置好了之后就可以下面操作
package com.admin.business.interfaces.service.sfsamecity;import cn.hutool.json.JSONObject;
import com.admin.frame.base.ConfigMapper;
import com.admin.util.BeanUtils;
import com.google.common.collect.ImmutableMap;
import com.itextpdf.text.log.Logger;
import com.itextpdf.text.log.LoggerFactory;
import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;import static javax.crypto.Cipher.SECRET_KEY;RestController
RequestMapping(/sfsamecity)
public class SFSameCityBackController {protected Logger logger LoggerFactory.getLogger(this.getClass());Value(${sf.same.city.devId})private String devId;Value(${sf.same.city.secretKey})private String secretKey;/*** https://openic.sf-express.com/open/api/docs/index#/apidoc* 配送状态更改回调*** shop_id string(64) 空 是 店铺ID* sf_order_id string64 0 是 顺丰订单ID* shop_order_id string64 0 是 商家订单ID* url_index string 空 是 回调url前缀 rider_status* operator_name string 空 是 配送员姓名* operator_phone string 空 是 配送员电话* rider_lng string 空 是 配送员位置经度* rider_lat string 空 是 配送员位置纬度* order_status int 空 是 订单状态 10-配送员接单/改派;12:配送员到店;15:配送员配送中* status_desc string 空 是 状态描述 文案见上个字段的注释* push_time int 空 是 状态变更时间**/RequestMapping(/getSFCallbackOrderStatus)public MapString,Object getSFCallbackOrderStatus(HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException {MapString,Object resMap new HashMap();System.out.println(----------------------------顺丰同城配送状态更改-----------------------------------------);StringBuilder requestBody new StringBuilder();try (BufferedReader reader request.getReader()) {String line;while ((line reader.readLine()) ! null) {requestBody.append(line);}}// 解析JSON数据JSONObject json new JSONObject(requestBody.toString());// 从JSON对象中获取参数String sign request.getParameter(sign);Integer order_status json.getInt(order_status);String sf_order_id json.getStr(sf_order_id);String sign2 validateSignature(jsonObject.toString());if (!sign2.equals(sign)) {System.out.println(..................sign签名错误..................);resMap.put(error_code,500);resMap.put(error_msg,error sign签名错误);return resMap;}try {if(order_status10){//10-配送员接单delivery.setSfSameCityStatusExpress(2);}else if(order_status12){//12:配送员到店delivery.setSfSameCityStatusExpress(3);}else if(order_status15){//15:配送员配送中resMap.put(error_code,0);resMap.put(error_msg,success);return resMap;}}catch (Exception e){System.out.println(顺丰同城配送状态更改异常);e.printStackTrace();}resMap.put(error_code,500);resMap.put(error_msg,error);return resMap;}private String validateSignature(String jsonString) throws IOException {return SFHapper.generateOpenSign(jsonString,Integer.parseInt(devId),secretKey);}}四、上线
前面的开发联调测试阶段结束了终于等到上线了这个时候还会卡你一星期因为他们还要审核撒的是邮箱审核反正很慢要等一个星期如果很急的话可以提前跟他们讲把材料给他们把事情讲清楚然后提前审批不然等开发完了上线还得再等一星期 这个是目前我对接他们的现状希望他们做的越来越好规范化标准化 如果小伙伴们有什么疑问欢迎下面评论。欢迎指正。如还有什么不懂的加我 QQ517861659
如果没有及时回复可以点我先问问AI机器人编辑https://chatgpt.byabstudio.com/login?code202307011314