场景
利用React Native开发一个电商类的App,电商系统选择开源电子商城ECmall,ECmall移动端API利用UniConnector生成。调用前需要进行Token认证,认证通过可访问订单、发货单等。
第一步 安装React Native REST Client
npm install --save react-native-rest-client
第二步 在React Native应用中,创建ECmall RestFul Api实例,并继承RestClient class
import RestClient from 'react-native-rest-client';
export default class ECmallRestApi extends RestClient {
constructor () {
// 初始化base URL,由于API是UniConnector自动生成,填写UniConnector网关地址
super('http://d.uniconnector.com');
}
};
第三步 设置调用Ecmall RestFul Api的Token
import RestClient from 'react-native-rest-client';
export default class ECmallRestApi extends RestClient {
constructor () {
//初始化base URL,由于API是UniConnector自动生成,填写UniConnector网关地址
super('http://d.uniconnector.com', {
headers: {
//X-UniConnector-Api-Key为UniConnector授权Key名称,d3a124f1-bd1f-1ea0-aac0-2b658e1dcxxx为申请授权的ApiKey
X-UniConnector-Api-Key : 'd3a124f1-bd1f-1ea0-aac0-2b658e1dcxxx'
}
});
}
};
第四步 调用ECmall的RestFul Api
import RestClient from 'react-native-rest-client';
export default class ECmallRestApi extends RestClient {
constructor () {
//初始化base URL,由于API是UniConnector自动生成,填写UniConnector网关地址
super('http://d.uniconnector.com', {
headers: {
//X-UniConnector-Api-Key为UniConnector授权Key名称,d3a124f1-bd1f-1ea0-aac0-2b658e1dcxxx为申请授权的ApiKey
X-UniConnector-Api-Key : 'd3a124f1-bd1f-1ea0-aac0-2b658e1dcxxx'
}
});
}
// 获取订单信息
getOrderById (orderId) {
// 根据OrderId查询订单信息
return this.GET('/ecm_order', { orderId });
}
//获取指定商铺的商品信息
getStoreGoods (storeId) {
// 根据商铺Id,查询商铺下所有的商品
return this.GET('/ecm_goods', { storeId });
}
//发布一个商品
publishGood(storeId, goodsName, description, price, ...) {
return this.POST('/ecm_order', { storeId, goodsName, description, price, ...});
}
};