Home Reference Source
import WeappBackendApi from 'weapp-backend-api'
public class | source

WeappBackendApi

Extends:

BackendApi → WeappBackendApi

统一封装微信小程序平台后端接口的调用

Example:

```javascript
import BackendApi from 'weapp-backend-api';

var backendApi = new BackendApi({
    'getList': {
        method: 'GET',
        url: 'https://domain.com/list'
    }
});
backendApi.sendRequest('getList').then(function([data]) {
    console.log(data);
}, function(requestResult) {
    console.log(requestResult);
});
```

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public

Method Summary

Public Methods
public

$sendHttpRequest(requestOptions: object): *

发送 HTTP 请求

public

afterSend(requestOptions: *, requestResult: *)

内置如下功能

  • 清理请求队列
  • 关闭 loading 提示
public

beforeSend(requestOptions: *): undefined | Promise

内置如下功能

  • 查询请求队列拦截重复请求(不发送请求)
  • 获取接口缓存数据的机制, 存在缓存数据则直接读取缓存数据(不发送请求)
  • 显示 loading 提示
public

commonFailStatusHandler(requestOptions: object, requestResult: object): Promise

当接口处理失败时通用的错误状态处理

public

commonFailTip(requestOptions: object, requestResult: object)

接口出错时统一弹出错误提示信息

public abstract

failStatusHandler(requestOptions: object, requestResult: object)

对错误状态的处理

public

getFailTipMessage(requestOptions: object, requestResult: object): string

获取给用户的错误提示

Private Methods
private

_addToSending(requestOptions: object)

将请求放入到发送中的队列中

private

_failHandler(requestOptions: object, requestResult: object): *

接口调用失败时的默认处理方法

private

_getErrorCode(标准的接口数据: object): string

获取错误码

private

_getRequestInfoHash(requestOptions: object): string

获取一个请求的关键信息

private

_hideLoading(requestOptions: *)

private

_ifApiSuccess(requestOptions: object, requestResult: object): boolean

判断接口请求调用是否成功

private

拦截重复请求

private

_isAnySending(excludeNoLoading: boolean): boolean

是不是有正在发送中的请求

private

_isSending(requestOptions: object): boolean

某个请求是否正在发送中

private

_normalizeRequestResult(requestOptions: object, requestResult: object)

标准化接口的返回数据, 会改写 requestResult.data 的内容

private

_removeFromSending(requestOptions: object)

将请求从发送中的队列中移除出来

private

_showLoading(requestOptions: *)

private

_successHandler(requestOptions: object, requestResult: object): object | Promise

接口调用成功时的默认处理方法

Inherited Summary

From class BackendApi
public
public
public

logger: *

public

sending: {}

public abstract

$sendHttpRequest(requestOptions: object): Promise

发送 HTTP 请求的具体实现

public abstract

afterSend(requestOptions: object, requestResult: object)

请求结束后的统一处理

public abstract

beforeSend(requestOptions: object): undefined | Promise

发送请求前的统一处理

public

normalizeRequestResult(requestOptions: any, requestResult: any): any

标准化接口返回的数据格式, 方便适配各种接口返回数据格式不同的情况

public

sendRequest(name: string, options: object): Promise

统一发送(接口)请求的方法

private

获取请求的参数

Public Constructors

public constructor() source

Override:

BackendApi#constructor

Public Members

public simpleStorage: * source

Public Methods

public $sendHttpRequest(requestOptions: object): * source

发送 HTTP 请求

Override:

BackendApi#$sendHttpRequest

Params:

NameTypeAttributeDescription
requestOptions object

wx.requesst options requestOptions._showLoading {boolean} 是否显示 loading 提示 requestOptions._showLoadingMask {boolean} 是否显示 loading 提示的 mask requestOptions._interceptDuplicateRequest {boolean} 是否拦截重复请求 requestOptions._showFailTip {boolean} 接口调用出错时是否给用户提示错误消息 requestOptions._showFailTipDuration {number} 接口调用出错时错误信息的显示多长时间(ms) requestOptions._cacheTtl {number} 缓存的存活时间(ms) requestOptions._normalizeRequestResult {function} 标准化接口返回的数据格式

Return:

*

public afterSend(requestOptions: *, requestResult: *) source

内置如下功能

  • 清理请求队列
  • 关闭 loading 提示

Override:

BackendApi#afterSend

Params:

NameTypeAttributeDescription
requestOptions *
requestResult *

public beforeSend(requestOptions: *): undefined | Promise source

内置如下功能

  • 查询请求队列拦截重复请求(不发送请求)
  • 获取接口缓存数据的机制, 存在缓存数据则直接读取缓存数据(不发送请求)
  • 显示 loading 提示

Override:

BackendApi#beforeSend

Params:

NameTypeAttributeDescription
requestOptions *

Return:

undefined | Promise

如果返回 Promise 则不会去发送请求

public commonFailStatusHandler(requestOptions: object, requestResult: object): Promise source

当接口处理失败时通用的错误状态处理

例如:

  • 接口出错时统一弹出错误提示信息
  • 接口出错时根据 status 做通用的错误处理(例如用户 session 超时, 引到用户重新登录)

Params:

NameTypeAttributeDescription
requestOptions object

wx.request options

requestResult object

wx.request success 或者 fail 返回的结果

Return:

Promise

public commonFailTip(requestOptions: object, requestResult: object) source

接口出错时统一弹出错误提示信息

Params:

NameTypeAttributeDescription
requestOptions object

wx.request options

requestResult object

wx.request success 或者 fail 返回的结果

public abstract failStatusHandler(requestOptions: object, requestResult: object) source

对错误状态的处理

Params:

NameTypeAttributeDescription
requestOptions object

wx.request options

requestResult object

wx.request success 或者 fail 返回的结果

public getFailTipMessage(requestOptions: object, requestResult: object): string source

获取给用户的错误提示

例如: 提供给用户看的消息格式参考 QQ 的错误提示消息 提示消息 (错误码: xxx)灰色字

Params:

NameTypeAttributeDescription
requestOptions object
requestResult object

Return:

string

Private Methods

private _addToSending(requestOptions: object) source

将请求放入到发送中的队列中

Params:

NameTypeAttributeDescription
requestOptions object

private _failHandler(requestOptions: object, requestResult: object): * source

接口调用失败时的默认处理方法

接口错误码规范

根据发送请求的最终状态定义错误的分类

  • 发送失败(即请求根本就没有发送出去)
  • 发送成功
    • HTTP 异常状态(例如 404/500...)
    • HTTP 正常状态(例如 200)
      • 接口调用成功
      • 接口调用失败(业务错误, 即接口规范中 status 非 0 的情况)

错误码可不固定长度, 整体格式为: 字母+数字 字母作为错误类型, 可扩展性更好, 数字建议划分区间来细分错误 例如:

  • A for API: API 调用失败(请求发送失败)的错误, 例如 A100 表示 URL 非法
  • H for HTTP, HTTP 异常状态的错误, 例如 H404 表示 HTTP 请求404错误
  • B for backend or business, 接口调用失败的错误, 例如 B100 业务A错误, B200 业务B错误
  • C for Client: 客户端错误, 例如 C100 表示解析 JSON 失败

Params:

NameTypeAttributeDescription
requestOptions object

wx.request options

requestResult object

wx.request success 或者 fail 返回的结果

Return:

*

private _getErrorCode(标准的接口数据: object): string source

获取错误码

Params:

NameTypeAttributeDescription
标准的接口数据 object

Return:

string

private _getRequestInfoHash(requestOptions: object): string source

获取一个请求的关键信息

  • method
  • url
  • data

Params:

NameTypeAttributeDescription
requestOptions object

Return:

string

请求关键信息组合的 MD5 值

private _hideLoading(requestOptions: *) source

Params:

NameTypeAttributeDescription
requestOptions *

private _ifApiSuccess(requestOptions: object, requestResult: object): boolean source

判断接口请求调用是否成功

Params:

NameTypeAttributeDescription
requestOptions object

wx.request options

requestResult object

wx.request success 返回的结果

Return:

boolean

private _interceptDuplicateRequest(requestOptions: object): Promise source

拦截重复请求

Params:

NameTypeAttributeDescription
requestOptions object

Return:

Promise

private _isAnySending(excludeNoLoading: boolean): boolean source

是不是有正在发送中的请求

Params:

NameTypeAttributeDescription
excludeNoLoading boolean

排除队列中没有开启 loading 的请求, 即 _showLoading 参数为 false 的请求

Return:

boolean

private _isSending(requestOptions: object): boolean source

某个请求是否正在发送中

Params:

NameTypeAttributeDescription
requestOptions object

Return:

boolean

private _normalizeRequestResult(requestOptions: object, requestResult: object) source

标准化接口的返回数据, 会改写 requestResult.data 的内容

Params:

NameTypeAttributeDescription
requestOptions object
requestResult object

private _removeFromSending(requestOptions: object) source

将请求从发送中的队列中移除出来

Params:

NameTypeAttributeDescription
requestOptions object

private _showLoading(requestOptions: *) source

Params:

NameTypeAttributeDescription
requestOptions *

private _successHandler(requestOptions: object, requestResult: object): object | Promise source

接口调用成功时的默认处理方法

Params:

NameTypeAttributeDescription
requestOptions object

wx.request options

requestResult object

wx.request success 返回的结果

Return:

object | Promise