增加了小程序物流的两个接口

This commit is contained in:
Xu Chang 2025-03-06 18:10:22 +08:00
parent 9fb9aee154
commit 8f9271bdfd
8 changed files with 331 additions and 9 deletions

View File

@ -52,6 +52,7 @@ type UploadShippingInfo = {
}; };
}>; }>;
}; };
import { AddExpressOrderData, CancelExpressOrderData } from '../../types/Wechat';
export declare class WechatMpInstance { export declare class WechatMpInstance {
appId: string; appId: string;
appSecret?: string; appSecret?: string;
@ -177,5 +178,22 @@ export declare class WechatMpInstance {
* https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html#%E8%8E%B7%E5%8F%96%E8%BF%90%E5%8A%9Bid%E5%88%97%E8%A1%A8get-delivery-list * https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html#%E8%8E%B7%E5%8F%96%E8%BF%90%E5%8A%9Bid%E5%88%97%E8%A1%A8get-delivery-list
*/ */
getDeliveryList(): Promise<any>; getDeliveryList(): Promise<any>;
/**
*
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/express/express-by-business/addOrder.html
*/
addExpressOrder(data: AddExpressOrderData): Promise<{
waybill_id: string;
waybill_data: Array<{
key: string;
value: string;
}>;
}>;
/**
*
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/express/express-by-business/cancelOrder.html
* @param data
*/
cancelExpressOrder(data: CancelExpressOrderData): Promise<void>;
} }
export {}; export {};

View File

@ -350,7 +350,7 @@ export class WechatMpInstance {
const { delivery_mode, is_all_delivered, shipping_list } = info; const { delivery_mode, is_all_delivered, shipping_list } = info;
assert(delivery_mode === 1 || typeof is_all_delivered === 'boolean'); assert(delivery_mode === 1 || typeof is_all_delivered === 'boolean');
assert(shipping_list.length > 0 && shipping_list.length <= 10); assert(shipping_list.length > 0 && shipping_list.length <= 10);
const token = this.getAccessToken(); const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=${token}`, { const result = await this.access(`https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=${token}`, {
method: 'POST', method: 'POST',
body: JSON.stringify(info), body: JSON.stringify(info),
@ -363,7 +363,7 @@ export class WechatMpInstance {
* @param info * @param info
*/ */
async getOrderState(info) { async getOrderState(info) {
const token = this.getAccessToken(); const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/wxa/sec/order/get_order?access_token=${token}`, { const result = await this.access(`https://api.weixin.qq.com/wxa/sec/order/get_order?access_token=${token}`, {
method: 'POST', method: 'POST',
body: JSON.stringify(info), body: JSON.stringify(info),
@ -381,7 +381,7 @@ export class WechatMpInstance {
* https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html#%E8%8E%B7%E5%8F%96%E8%BF%90%E5%8A%9Bid%E5%88%97%E8%A1%A8get-delivery-list * https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html#%E8%8E%B7%E5%8F%96%E8%BF%90%E5%8A%9Bid%E5%88%97%E8%A1%A8get-delivery-list
*/ */
async getDeliveryList() { async getDeliveryList() {
const token = this.getAccessToken(); const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/get_delivery_list?access_token=${token}`, { const result = await this.access(`https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/get_delivery_list?access_token=${token}`, {
method: 'POST', method: 'POST',
headers: { headers: {
@ -392,4 +392,32 @@ export class WechatMpInstance {
// const { errcode, delivery_list, count } = result; // const { errcode, delivery_list, count } = result;
return result; return result;
} }
/**
* 生成物流运单
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/express/express-by-business/addOrder.html
*/
async addExpressOrder(data) {
const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/express/business/order/add?access_token=${token}`, {
method: 'POST',
body: JSON.stringify(data),
});
const { waybill_id, waybill_data } = result;
return {
waybill_id,
waybill_data,
};
}
/**
* 取消物流运单
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/express/express-by-business/cancelOrder.html
* @param data
*/
async cancelExpressOrder(data) {
const token = await this.getAccessToken();
await this.access(`https://api.weixin.qq.com/cgi-bin/express/business/order/cancel?access_token=${token}`, {
method: 'POST',
body: JSON.stringify(data),
});
}
} }

59
es/types/Wechat.d.ts vendored
View File

@ -52,3 +52,62 @@ export declare type WechatMpEventData = {
ThumbUrl?: string; ThumbUrl?: string;
ThumbMediaId?: string; ThumbMediaId?: string;
}; };
type ExpressUserData = {
name?: string;
tel?: string;
mobile?: string;
company?: string;
post_code?: string;
country?: string;
province?: string;
city?: string;
area?: string;
address?: string;
};
export type AddExpressOrderData = {
order_id: string;
openid?: string;
delivery_id: string;
biz_id: string;
custom_remark?: string;
tagid?: number;
add_source: 0 | 2;
wx_appid?: string;
sender: ExpressUserData;
receiver: ExpressUserData;
cargo: {
count: number;
weight: number;
space_x: number;
space_y: number;
space_z: number;
detail_list: Array<{
name: string;
count: number;
}>;
};
shop: {
wxa_path?: string;
img_url?: string;
goods_name?: string;
goods_count?: number;
detail_list?: Array<object>;
};
insured: {
use_insured: 0 | 1;
insured_value?: number;
};
service: {
service_type: number;
service_name: string;
};
expect_time?: number;
take_mode?: number;
};
export type CancelExpressOrderData = {
openid?: string;
delivery_id: string;
waybill_id: string;
order_id: string;
};
export {};

View File

@ -52,6 +52,7 @@ type UploadShippingInfo = {
}; };
}>; }>;
}; };
import { AddExpressOrderData, CancelExpressOrderData } from '../../types/Wechat';
export declare class WechatMpInstance { export declare class WechatMpInstance {
appId: string; appId: string;
appSecret?: string; appSecret?: string;
@ -177,5 +178,22 @@ export declare class WechatMpInstance {
* https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html#%E8%8E%B7%E5%8F%96%E8%BF%90%E5%8A%9Bid%E5%88%97%E8%A1%A8get-delivery-list * https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html#%E8%8E%B7%E5%8F%96%E8%BF%90%E5%8A%9Bid%E5%88%97%E8%A1%A8get-delivery-list
*/ */
getDeliveryList(): Promise<any>; getDeliveryList(): Promise<any>;
/**
*
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/express/express-by-business/addOrder.html
*/
addExpressOrder(data: AddExpressOrderData): Promise<{
waybill_id: string;
waybill_data: Array<{
key: string;
value: string;
}>;
}>;
/**
*
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/express/express-by-business/cancelOrder.html
* @param data
*/
cancelExpressOrder(data: CancelExpressOrderData): Promise<void>;
} }
export {}; export {};

View File

@ -354,7 +354,7 @@ class WechatMpInstance {
const { delivery_mode, is_all_delivered, shipping_list } = info; const { delivery_mode, is_all_delivered, shipping_list } = info;
(0, assert_1.assert)(delivery_mode === 1 || typeof is_all_delivered === 'boolean'); (0, assert_1.assert)(delivery_mode === 1 || typeof is_all_delivered === 'boolean');
(0, assert_1.assert)(shipping_list.length > 0 && shipping_list.length <= 10); (0, assert_1.assert)(shipping_list.length > 0 && shipping_list.length <= 10);
const token = this.getAccessToken(); const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=${token}`, { const result = await this.access(`https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=${token}`, {
method: 'POST', method: 'POST',
body: JSON.stringify(info), body: JSON.stringify(info),
@ -367,7 +367,7 @@ class WechatMpInstance {
* @param info * @param info
*/ */
async getOrderState(info) { async getOrderState(info) {
const token = this.getAccessToken(); const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/wxa/sec/order/get_order?access_token=${token}`, { const result = await this.access(`https://api.weixin.qq.com/wxa/sec/order/get_order?access_token=${token}`, {
method: 'POST', method: 'POST',
body: JSON.stringify(info), body: JSON.stringify(info),
@ -385,7 +385,7 @@ class WechatMpInstance {
* https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html#%E8%8E%B7%E5%8F%96%E8%BF%90%E5%8A%9Bid%E5%88%97%E8%A1%A8get-delivery-list * https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html#%E8%8E%B7%E5%8F%96%E8%BF%90%E5%8A%9Bid%E5%88%97%E8%A1%A8get-delivery-list
*/ */
async getDeliveryList() { async getDeliveryList() {
const token = this.getAccessToken(); const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/get_delivery_list?access_token=${token}`, { const result = await this.access(`https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/get_delivery_list?access_token=${token}`, {
method: 'POST', method: 'POST',
headers: { headers: {
@ -396,5 +396,33 @@ class WechatMpInstance {
// const { errcode, delivery_list, count } = result; // const { errcode, delivery_list, count } = result;
return result; return result;
} }
/**
* 生成物流运单
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/express/express-by-business/addOrder.html
*/
async addExpressOrder(data) {
const token = await this.getAccessToken();
const result = await this.access(`https://api.weixin.qq.com/cgi-bin/express/business/order/add?access_token=${token}`, {
method: 'POST',
body: JSON.stringify(data),
});
const { waybill_id, waybill_data } = result;
return {
waybill_id,
waybill_data,
};
}
/**
* 取消物流运单
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/express/express-by-business/cancelOrder.html
* @param data
*/
async cancelExpressOrder(data) {
const token = await this.getAccessToken();
await this.access(`https://api.weixin.qq.com/cgi-bin/express/business/order/cancel?access_token=${token}`, {
method: 'POST',
body: JSON.stringify(data),
});
}
} }
exports.WechatMpInstance = WechatMpInstance; exports.WechatMpInstance = WechatMpInstance;

59
lib/types/Wechat.d.ts vendored
View File

@ -52,3 +52,62 @@ export declare type WechatMpEventData = {
ThumbUrl?: string; ThumbUrl?: string;
ThumbMediaId?: string; ThumbMediaId?: string;
}; };
type ExpressUserData = {
name?: string;
tel?: string;
mobile?: string;
company?: string;
post_code?: string;
country?: string;
province?: string;
city?: string;
area?: string;
address?: string;
};
export type AddExpressOrderData = {
order_id: string;
openid?: string;
delivery_id: string;
biz_id: string;
custom_remark?: string;
tagid?: number;
add_source: 0 | 2;
wx_appid?: string;
sender: ExpressUserData;
receiver: ExpressUserData;
cargo: {
count: number;
weight: number;
space_x: number;
space_y: number;
space_z: number;
detail_list: Array<{
name: string;
count: number;
}>;
};
shop: {
wxa_path?: string;
img_url?: string;
goods_name?: string;
goods_count?: number;
detail_list?: Array<object>;
};
insured: {
use_insured: 0 | 1;
insured_value?: number;
};
service: {
service_type: number;
service_name: string;
};
expect_time?: number;
take_mode?: number;
};
export type CancelExpressOrderData = {
openid?: string;
delivery_id: string;
waybill_id: string;
order_id: string;
};
export {};

View File

@ -70,6 +70,8 @@ type UploadShippingInfo = {
}>; }>;
} }
import { AddExpressOrderData, CancelExpressOrderData } from '../../types/Wechat';
export class WechatMpInstance { export class WechatMpInstance {
appId: string; appId: string;
appSecret?: string; appSecret?: string;
@ -571,7 +573,7 @@ export class WechatMpInstance {
assert(shipping_list.length > 0 && shipping_list.length <= 10); assert(shipping_list.length > 0 && shipping_list.length <= 10);
const token = this.getAccessToken(); const token = await this.getAccessToken();
const result = await this.access( const result = await this.access(
`https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=${token}`, `https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=${token}`,
@ -594,7 +596,7 @@ export class WechatMpInstance {
sub_merchant_id?: string; sub_merchant_id?: string;
merchant_trade_no?: string; merchant_trade_no?: string;
}) { }) {
const token = this.getAccessToken(); const token = await this.getAccessToken();
const result = await this.access( const result = await this.access(
`https://api.weixin.qq.com/wxa/sec/order/get_order?access_token=${token}`, `https://api.weixin.qq.com/wxa/sec/order/get_order?access_token=${token}`,
@ -635,7 +637,7 @@ export class WechatMpInstance {
* https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html#%E8%8E%B7%E5%8F%96%E8%BF%90%E5%8A%9Bid%E5%88%97%E8%A1%A8get-delivery-list * https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html#%E8%8E%B7%E5%8F%96%E8%BF%90%E5%8A%9Bid%E5%88%97%E8%A1%A8get-delivery-list
*/ */
async getDeliveryList() { async getDeliveryList() {
const token = this.getAccessToken(); const token = await this.getAccessToken();
const result = await this.access( const result = await this.access(
`https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/get_delivery_list?access_token=${token}`, `https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/get_delivery_list?access_token=${token}`,
{ {
@ -649,4 +651,48 @@ export class WechatMpInstance {
// const { errcode, delivery_list, count } = result; // const { errcode, delivery_list, count } = result;
return result; return result;
} }
/**
*
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/express/express-by-business/addOrder.html
*/
async addExpressOrder(data: AddExpressOrderData) {
const token = await this.getAccessToken();
const result = await this.access(
`https://api.weixin.qq.com/cgi-bin/express/business/order/add?access_token=${token}`,
{
method: 'POST',
body: JSON.stringify(data),
}
);
const { waybill_id, waybill_data } = result;
return {
waybill_id,
waybill_data,
} as {
waybill_id: string;
waybill_data: Array<{
key: string;
value: string;
}>
};
}
/**
*
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/express/express-by-business/cancelOrder.html
* @param data
*/
async cancelExpressOrder(data: CancelExpressOrderData) {
const token = await this.getAccessToken();
await this.access(
`https://api.weixin.qq.com/cgi-bin/express/business/order/cancel?access_token=${token}`,
{
method: 'POST',
body: JSON.stringify(data),
}
);
}
} }

View File

@ -75,4 +75,70 @@ export declare type WechatMpEventData = {
PagePath?: string; // 小程序页面路径 PagePath?: string; // 小程序页面路径
ThumbUrl?: string; // 封面图片的临时cdn链接 ThumbUrl?: string; // 封面图片的临时cdn链接
ThumbMediaId?: string; // 封面图片的临时素材id ThumbMediaId?: string; // 封面图片的临时素材id
};
type ExpressUserData = {
name?: string;
tel?: string;
mobile?: string;
company?: string;
post_code?: string;
country?: string;
province?: string;
city?: string;
area?: string;
address?: string;
}
// 生成运单
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/express/express-by-business/addOrder.html
export type AddExpressOrderData = {
order_id: string;
openid?: string;
delivery_id: string;
biz_id: string;
custom_remark?: string;
tagid?: number;
add_source: 0 | 2;
wx_appid?: string;
sender: ExpressUserData;
receiver: ExpressUserData;
cargo: {
count: number;
weight: number;
space_x: number;
space_y: number;
space_z: number;
detail_list: Array<{
name: string;
count: number;
}>;
};
shop: {
wxa_path?: string;
img_url?: string;
goods_name?: string;
goods_count?: number;
detail_list?: Array<object>;
};
insured: {
use_insured: 0 | 1;
insured_value?: number;
};
service: {
service_type: number;
service_name: string;
};
expect_time?: number;
take_mode?: number;
};
// 取消运单
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/express/express-by-business/cancelOrder.html
export type CancelExpressOrderData = {
openid?: string;
delivery_id: string;
waybill_id: string;
order_id: string;
}; };