With the rapid development of e-commerce, the demand for express delivery business is also increasing. Many companies take express delivery as their main business, and the realization of express mailing functions has become a necessary function for many companies. This article will introduce how to implement the express delivery function in the Vue project to help developers better complete related business needs.
1. Select Express API
To implement the express mailing function in the Vue project, you need to use a third-party express API. Currently, there are many optional express APIs on the market, such as aggregate data, Express 100, etc. Developers can choose the express API that suits them based on the actual situation of their projects.
2. Express Query Interface
The first step to realize the express mailing function is to query the express delivery information. It is necessary to provide users with an interface for querying the express delivery information. Generally, the selected express API will provide such an interface, and the returned data format is JSON format. To make interface calls in the Vue project, you can use network request libraries such as axios to obtain data. Simple interface example for querying express delivery information:
getData() { const url = "https://apis.juhe.cn/express/query"; // 接口地址 const params = { com: "shentong", // 快递公司的编码 no: "1234567890", // 快递单号 key: "*************" // 接口的key }; axios.get(url, {params}) .then(response => { console.log(response.data); // 打印接口返回的数据 }) .catch(error => { console.log(error); }); }
3. Express ordering interface
After querying express delivery information, the next function we need to provide users is to place express delivery orders. Compared with querying express delivery information, it is more difficult to place an express delivery order. The information that needs to be entered when placing an order generally includes sender information, recipient information, courier company, courier number, mobile phone number, etc. According to the information filled in by the user, the data is passed to the ordering interface of the Express API to complete the ordering operation.
The data format for placing orders is usually also in JSON format, and the data needs to be encapsulated into a JSON object. According to the order interface document provided by the express API, define an example of the order interface:
submitOrder() { const url = "https://apis.juhe.cn/express/order"; // 接口地址 const params = { sendMan: "张三", sendMobile: "13312345678", sendAddress: "广东省深圳市罗湖区", receiveMan: "李四", receiveMobile: "13512345678", receiveAddress: "湖北省武汉市洪山区", courierCode: "shentong", courierNumber: "1234567890", key: "*************" }; axios.post(url, params) .then(response => { console.log(response.data); // 打印接口返回的数据 }) .catch(error => { console.log(error); }); }
4. Express list display
In order to facilitate users’ subsequent express tracking and query, we need to use the Vue framework to Express delivery information is displayed. Before data display, the queried courier information and order information need to be stored. You can use VueX to share state between components. For smaller projects, you can directly use Vue's data binding to achieve it.
Express delivery list display is generally divided into two types: unsigned and signed. Classification display can be performed by judging the status of the express delivery, as follows:
Not signed for:
<ul> <li v-for="item in uncollected"> <p>快递公司:{{item.com}}</p> <p>快递单号:{{item.no}}</p> <p>状态:{{item.state}}</p> </li> </ul>
Signed for:
<ul> <li v-for="item in collected"> <p>快递公司:{{item.com}}</p> <p>快递单号:{{item.no}}</p> <p>状态:{{item.state}}</p> </li> </ul>
5. Express delivery status query
In order to facilitate users to track the status of express delivery at any time, we need to set up an entrance to query the status of express delivery. Similarly, when querying the express status, you need to call the express query interface provided by the express API, and the returned data format is also in JSON format. Example of the interface for querying express delivery status:
getExpressStatus() { const url = "https://apis.juhe.cn/express/query"; // 接口地址 const params = { com: "shentong", // 快递公司的编码 no: "1234567890", // 快递单号 key: "*************" // 接口的key }; axios.get(url, {params}) .then(response => { console.log(response.data); // 打印接口返回的数据 }) .catch(error => { console.log(error); }); }
6. Summary
This article introduces the method of implementing the express mailing function in the Vue project. Through third-party express delivery API calls and Vue framework data display, we can quickly and easily implement express delivery functions, making it convenient for users to use and track express delivery status.
The above is the detailed content of How to do express delivery in vue project. For more information, please follow other related articles on the PHP Chinese website!