ECJiaWiki:新到家门店小程序API的使用方法
跳到导航
跳到搜索
目录
简介
新到家门店(v2.0.0)及之后版本在请求api上进行了重构,相较于之前版本的api请求方式,新版本的api请求有很多优势:
1、可预定义请求参数(比如token),减少重复代码。
2、可自定义缓存,提升性能。
3、可自定义提示,处理提示的操作流程。
4、可自定义报错处理等。。。
扩展API类
新到家的API扩展文件在 /miniprogram/utils/requests
目录下。
其中EcjiaBaseRequest.js
为主API类,其他API类均继承此类。
API接口定义
新到家的API接口定位文件为`miniprogram/utils/constants/ecjia-api-const.js
。
只需要在类里面,新增一行定义即可,例:
```
static HOME_DATA = 'home/data'; //HOME数据
```
接口配置
如何新建一个接口文件?
在miniprogram/utils/requests
目录下新建一个文件,比如新建一个AddressListRequest.js
。
新建一个类,继承EcjiaBaseRequest
类,例如:
import {EcjiaBaseRequest} from './EcjiaBaseRequest';
import {EcjiaApiConst} from '../../utils/constants/ecjia-api-const';
import {EcjiaToken} from "../ecjia-token";
export class AddressListRequest extends EcjiaBaseRequest {
/**
*
* @type {{seller_id: null, token: null}}
*/
default_args = {
token: EcjiaToken.token(),
seller_id: null,
};
constructor(data = {}, options = {}) {
super(data, options); //call the parent method with super
this.name = 'AddressListRequest';
this.url += EcjiaApiConst.ADDRESS_LIST;
this.setData(data, this.default_args);
}
/**
* 启动缓存
* @returns {boolean}
*/
cacheableByEnabled() {
return true;
}
/**
* 缓存KEY,默认类名
* @returns {string}
*/
cacheableByKey() {
return this.cacheableByMd5Key();
}
}
方法介绍:
- default_args:默认传参
- constructor:构造函数
- cacheableByEnabled:启动缓存
- cacheableByKey:缓存key
- setData:发送数据
- justApiStatus:API状态判断
如何调用接口
1、引用接口文件
通过import引用:
import {GoodsDetailRequest} from "../../utils/requests/GoodsDetailRequest";
2、new创建对象,请求接口
(new GoodsDetailRequest({
goods_id: this.goods_id, //传参goods_id
product_id: this.product_id //传参product_id
})).handle().then(function (request) { //接口请求后的流程
let data = request.getData(); //获取返回数据
let status = request.getStatus(); //获取返回状态
wx.hideLoading();
});
注意:
1、可以通过getData
获取请求返回的数据
2、可以通过getStatus
获取返回状态
3、then
后处理接口请求后的流程