提示:学习express,搭建管理系统
文章目录
- 前言
- 一、安装md5,axios
- 二、新建config/logistics.js
- 三、修改routes/order.js
- 四、查询物流信息
- 五、试错与误区
- 总结
前言
需求:主要学习express,所以先写service部分
快递100API文档
一、安装md5,axios
npm install axios md5 --save-dev
二、新建config/logistics.js
config/logistics.js
//快递100
const axios = require('axios');
const md5 = require('md5');
//查询物流信息url
let url= 'https://poll.kuaidi100.com/poll/query.do';
//customer
let customer = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
//授权key
let key = 'xxxxxxxxx';
const logistics = {
query:async (param)=>{
//一定要把param转换成JSON,不然一直报签名错误
let paramJson = JSON.stringify(param);
let sign = md5(paramJson+key+customer);
sign = sign.toUpperCase();
axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded';
const res = await axios.get(url+`?customer=${customer}&sign=${sign}¶m=${paramJson}`);
return res.data
}
}
module.exports = logistics;
customer,key通过 快递100企业信息获取
customer对应customer
key对应授权key
三、修改routes/order.js
routes/order.js
const OrderDao = require('../dao/order');
const logistics = require('../config/logistics');
const OrderRoutes = (router)=>{
router.post('/order/addGoodsToOrder',async (req,res)=>{
const result = await OrderDao.addGoodsToOrder(req.body);
res.json(result);
});
router.get('/order/queryGoodsByUserIdFromOrder',async (req,res)=>{
const result = await OrderDao.queryGoodsByUserIdFromOrder(req.query);
res.json(result);
});
router.post('/order/queryOrderLogistics',async (req,res)=>{
const result = await logistics.query(req.body);
res.json(result);
});
}
module.exports = OrderRoutes;
四、查询物流信息
url:http://localhost:1990/order/queryOrderLogistics
params:{
{
"com": "yuantong",
"num": "xxxxxxxxxxx",
"phone": "",
"from": "",
"to": "",
"resultv2": "4",
"show": "0",
"order": "desc"
}
}
免费版本试用100次
五、试错与误区
用于签名的param和get请求url上的param
必须是JSON.stringify(param)的JSON格式,
不能是Object格式,不然一直报签名错误
总结
踩坑路漫漫长@~@