提价oo
parent
fc4a0032e8
commit
96e5c4ef08
|
|
@ -1,53 +0,0 @@
|
|||
{
|
||||
"name": "xffxkh-app",
|
||||
"version": "1.0.1",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"node_modules/@escook/request-miniprogram": {
|
||||
"version": "0.2.1",
|
||||
"resolved": "https://registry.npmmirror.com/@escook/request-miniprogram/-/request-miniprogram-0.2.1.tgz",
|
||||
"integrity": "sha512-ueWV5YsaEm/ycQZuEjMiA88GFMhfBQSjy9GrP9omy4xAQajkGTbYIlnhzsDfWzRPmRC1fKmAiKMrCVcgS+SHcQ=="
|
||||
},
|
||||
"node_modules/echarts": {
|
||||
"version": "4.9.0",
|
||||
"resolved": "https://registry.npmmirror.com/echarts/-/echarts-4.9.0.tgz",
|
||||
"integrity": "sha512-+ugizgtJ+KmsJyyDPxaw2Br5FqzuBnyOWwcxPKO6y0gc5caYcfnEUIlNStx02necw8jmKmTafmpHhGo4XDtEIA==",
|
||||
"dependencies": {
|
||||
"zrender": "4.3.2"
|
||||
}
|
||||
},
|
||||
"node_modules/es6-promise": {
|
||||
"version": "4.2.8",
|
||||
"resolved": "https://registry.npmmirror.com/es6-promise/-/es6-promise-4.2.8.tgz",
|
||||
"integrity": "sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w=="
|
||||
},
|
||||
"node_modules/flv.js": {
|
||||
"version": "1.6.2",
|
||||
"resolved": "https://registry.npmmirror.com/flv.js/-/flv.js-1.6.2.tgz",
|
||||
"integrity": "sha512-xre4gUbX1MPtgQRKj2pxJENp/RnaHaxYvy3YToVVCrSmAWUu85b9mug6pTXF6zakUjNP2lFWZ1rkSX7gxhB/2A==",
|
||||
"dependencies": {
|
||||
"es6-promise": "^4.2.8",
|
||||
"webworkify-webpack": "^2.1.5"
|
||||
}
|
||||
},
|
||||
"node_modules/moment": {
|
||||
"version": "2.30.1",
|
||||
"resolved": "https://registry.npmmirror.com/moment/-/moment-2.30.1.tgz",
|
||||
"integrity": "sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==",
|
||||
"engines": {
|
||||
"node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/webworkify-webpack": {
|
||||
"version": "2.1.5",
|
||||
"resolved": "https://registry.npmmirror.com/webworkify-webpack/-/webworkify-webpack-2.1.5.tgz",
|
||||
"integrity": "sha512-2akF8FIyUvbiBBdD+RoHpoTbHMQF2HwjcxfDvgztAX5YwbZNyrtfUMgvfgFVsgDhDPVTlkbb5vyasqDHfIDPQw=="
|
||||
},
|
||||
"node_modules/zrender": {
|
||||
"version": "4.3.2",
|
||||
"resolved": "https://registry.npmmirror.com/zrender/-/zrender-4.3.2.tgz",
|
||||
"integrity": "sha512-bIusJLS8c4DkIcdiK+s13HiQ/zjQQVgpNohtd8d94Y2DnJqgM1yjh/jpDb8DoL6hd7r8Awagw8e3qK/oLaWr3g=="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,99 +0,0 @@
|
|||
## 安装
|
||||
|
||||
```bash
|
||||
npm install @escook/request-miniprogram
|
||||
```
|
||||
|
||||
## 导入
|
||||
|
||||
```js
|
||||
// 按需导入 $http 对象
|
||||
import { $http } from '@escook/request-miniprogram'
|
||||
|
||||
// 将按需导入的 $http 挂载到 wx 顶级对象之上,方便全局调用
|
||||
wx.$http = $http
|
||||
|
||||
// 在 uni-app 项目中,可以把 $http 挂载到 uni 顶级对象之上,方便全局调用
|
||||
uni.$http = $http
|
||||
```
|
||||
|
||||
## 使用
|
||||
|
||||
### 支持的请求方法
|
||||
|
||||
```js
|
||||
// 发起 GET 请求,data 是可选的参数对象
|
||||
$http.get(url, data?)
|
||||
|
||||
// 发起 POST 请求,data 是可选的参数对象
|
||||
$http.post(url, data?)
|
||||
|
||||
// 发起 PUT 请求,data 是可选的参数对象
|
||||
$http.put(url, data?)
|
||||
|
||||
// 发起 DELETE 请求,data 是可选的参数对象
|
||||
$http.delete(url, data?)
|
||||
```
|
||||
|
||||
### 配置请求根路径
|
||||
|
||||
```js
|
||||
$http.baseUrl = 'https://www.example.com'
|
||||
```
|
||||
|
||||
### 请求拦截器
|
||||
|
||||
```js
|
||||
// 请求开始之前做一些事情
|
||||
$http.beforeRequest = function (options) {
|
||||
// do somethimg...
|
||||
}
|
||||
```
|
||||
|
||||
例 1,展示 loading 效果:
|
||||
|
||||
```js
|
||||
// 请求开始之前做一些事情
|
||||
$http.beforeRequest = function (options) {
|
||||
wx.showLoading({
|
||||
title: '数据加载中...',
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
例 2,自定义 header 请求头:
|
||||
|
||||
```js
|
||||
// 请求开始之前做一些事情
|
||||
$http.beforeRequest = function (options) {
|
||||
if (options.url.indexOf('/home/catitems') !== -1) {
|
||||
options.header = {
|
||||
'X-Test': 'AAA',
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 响应拦截器
|
||||
|
||||
```js
|
||||
// 请求完成之后做一些事情
|
||||
$http.afterRequest = function () {
|
||||
// do something...
|
||||
}
|
||||
```
|
||||
|
||||
例如,隐藏 loading 效果:
|
||||
|
||||
```js
|
||||
// 请求完成之后做一些事情
|
||||
$http.afterRequest = function () {
|
||||
wx.hideLoading()
|
||||
}
|
||||
```
|
||||
|
||||
## 开源协议
|
||||
|
||||

|
||||
|
||||
**enjoy!**
|
||||
|
|
@ -1,73 +0,0 @@
|
|||
class Request {
|
||||
constructor(options = {}) {
|
||||
// 请求的根路径
|
||||
this.baseUrl = options.baseUrl || ''
|
||||
// 请求的 url 地址
|
||||
this.url = options.url || ''
|
||||
// 请求方式
|
||||
this.method = 'GET'
|
||||
// 请求的参数对象
|
||||
this.data = null
|
||||
// header 请求头
|
||||
this.header = options.header || {}
|
||||
this.beforeRequest = null
|
||||
this.afterRequest = null
|
||||
}
|
||||
|
||||
get(url, data = {}) {
|
||||
this.method = 'GET'
|
||||
this.url = this.baseUrl + url
|
||||
this.data = data
|
||||
return this._()
|
||||
}
|
||||
|
||||
post(url, data = {}) {
|
||||
this.method = 'POST'
|
||||
this.url = this.baseUrl + url
|
||||
this.data = data
|
||||
return this._()
|
||||
}
|
||||
|
||||
put(url, data = {}) {
|
||||
this.method = 'PUT'
|
||||
this.url = this.baseUrl + url
|
||||
this.data = data
|
||||
return this._()
|
||||
}
|
||||
|
||||
delete(url, data = {}) {
|
||||
this.method = 'DELETE'
|
||||
this.url = this.baseUrl + url
|
||||
this.data = data
|
||||
return this._()
|
||||
}
|
||||
|
||||
_() {
|
||||
// 清空 header 对象
|
||||
this.header = {}
|
||||
// 请求之前做一些事
|
||||
this.beforeRequest && typeof this.beforeRequest === 'function' && this.beforeRequest(this)
|
||||
// 发起请求
|
||||
return new Promise((resolve, reject) => {
|
||||
let weixin = wx
|
||||
// 适配 uniapp
|
||||
if ('undefined' !== typeof uni) {
|
||||
weixin = uni
|
||||
}
|
||||
weixin.request({
|
||||
url: this.url,
|
||||
method: this.method,
|
||||
data: this.data,
|
||||
header: this.header,
|
||||
success: (res) => { resolve(res) },
|
||||
fail: (err) => { reject(err) },
|
||||
complete: (res) => {
|
||||
// 请求完成以后做一些事情
|
||||
this.afterRequest && typeof this.afterRequest === 'function' && this.afterRequest(res)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const $http = new Request()
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
{
|
||||
"name": "@escook/request-miniprogram",
|
||||
"version": "0.2.1",
|
||||
"description": "基于 Promise 的小程序网路请求库",
|
||||
"main": "miniprogram_dist/index.js",
|
||||
"miniprogram": "miniprogram_dist",
|
||||
"scripts": {},
|
||||
"keywords": [
|
||||
"request",
|
||||
"miniprogram",
|
||||
"wx",
|
||||
"weixin"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git@github.com:liulongbin1314/request-miniprogram.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/liulongbin1314/request-miniprogram/issues"
|
||||
},
|
||||
"author": "LiuLongBin",
|
||||
"license": "MIT"
|
||||
}
|
||||
Loading…
Reference in New Issue