小程序使用微信物流组件
接入前的准备工作
-
配置小程序的业务域名(开发管理->业务域名)
-
在物流服务中开通查询组建
-
服务器域名配置(需加入物流接口域名白名单)
在manifest.json中声明组建
"logisticsPlugin": {
"version": "2.3.0",
"provider": "插件的appId(账号设置->第三方设置->物流服务)"
}
在需要的页面中引入
// 引入插件
const plugin = requirePlugin("logisticsPlugin");
export default {
调用后端接口
async searchDetail(){
let options={
openId:uni.getStorageSync("userInfo").openId,
phone:uni.getStorageSync("userInfo").telephone,
waybillId:this.orderInfo.deliveryNo,
mallOrder:this.orderInfo
}
const {msg} = await api.post("/wechat/order/xxx",options);
console.log("获取物流轨迹token",msg);
plugin.openWaybillTracking({
waybillToken: msg
});
}
编写 java后端代码
- 封装物流API请求
/**
* 配合物流组件查询物流信息
* @return
*/
public String getLogistics(String openId, String phone, String waybillId, MallGoods mallGoods,String wxPayNo,String deliveryId){
String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
String replaceUrl = tokenUrl.replace("{0}",appId).replace("{1}",secret);
JSONObject tokenJson = JSONObject.parseObject(HttpUtils.sendGet(replaceUrl));
String token =tokenJson.get("access_token").toString();
String getUploadUrl = "https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/trace_waybill?access_token=" + token;
JSONObject json = new JSONObject();
json.put("openid",openId);
json.put("receiver_phone",phone);
json.put("trans_id",wxPayNo);
json.put("delivery_id",deliveryId);
json.put("waybill_id",waybillId);
JSONObject goodsInfo = new JSONObject();
JSONArray detailList = new JSONArray();
JSONObject detail = new JSONObject();
detail.put("goods_name",mallGoods.getName());
detail.put("goods_img_url","");
detailList.add(detail);
goodsInfo.put("detail_list",detailList);
json.put("goods_info",goodsInfo);
// System.out.println(json.toString());
JSONObject jsonObject = JSONObject.parseObject(HttpUtil.post(getUploadUrl,json.toString()));
// tradeOrder.setWaybillToken(jsonObject.getString("waybill_token"));
if (jsonObject.getString("errmsg").equals("ok")) {
String tokens = jsonObject.getString("waybill_token");
return tokens;
}
return null;
}