f559da991ee918dd2836d2b242abd0ed85e2f7b9
企获客(SAAS)
1. 项目目录
apis : 统一的请求工厂处理
|--- interfaces //接口工厂
| |--- auth.js //授权接口模块
| |--- file.js //上传接口
|--- index.js //统一接口路由出口
|--- request.js //请求方法
|--- updateToken.hs //token更新
|--- err.js //错误处理
static : 静态资源目录
|--- imgs //图片
|--- icon //图标
components : 项目组件目录
pages : 页面路由
2. 使用apis接口方法
统一用法 : 在app.js 中引入api方法,使用wx.$方法挂载全局
//app.js
//全局引用apis
import apis from "./apis/index"
onLaunch(){
//挂载api方法
wx.$api = apis
}
//pages.js
function () {
wx.$api...().then(res=>{
//成功回调
}).cathc(err=>{
//失败回调
})
}
按需引用 : 按需要引用
//pages.js
import apis from "./apis/index"
function () {
apis...().then(res=>{
//成功回调
}).cathc(err=>{
//失败回调
})
}
多个接口同时回调 : 面需多个接口同时返回信息时可使用ES6 Promise All回调方法
let api1 = wx.$api.{moduleName}.{apiName}(obj)
api2 = wx.$api.{moduleName}.{apiName}(obj)
api3 = wx.$api.{moduleName}.{apiName}(obj)
Promise.all([api1, api2, api3]).then(res=>{
//请求回调成功
}).cathc(err=>{
//请求回调失败
})
3. 请求方法描述
api全局请求方法参数说明:
| 请求参数 | 是否必填 | 数据类型 | 数据说明 |
|---|---|---|---|
| method | 否 | String | 请求方式,默认为GET |
| url | 是 | String | 接口url地址 |
| data | 否 | Obj | 需要发送的请求数据 |
请求方法模块用法
import {req} from "../request"
const apiName = (obj) => req({
//请求参数
})
export default({
apiName
})
4. 上传方法描述
api全局请求方法参数说明:
| 请求参数 | 是否必填 | 数据类型 | 数据说明 |
|---|---|---|---|
| url | 是 | String | 上传接口url地址 |
| key | 是 | String | 文件对应的 key |
| path | 是 | String | 上传文件的临时路径,可通过wx.chooseImage获得图片临时路径 |
| data | 否 | Obj | 需要发送的请求数据 |
方法模块用法
import {upload} from "../request"
const apiName = (fileUrl, keyName, data) => upload({
url : "...", //api上传接口
key : keyName, //上传key
path: fileUrl, //上传路径
data: data //fromData
})
export default({
apiName
})
接口用法
wx.$api.file.uploadImg(fileUrl, keyName, {
//data
}).then(res=>{
//上传成功
}).cathc(err=>{
//上传失败
})
Description
Languages
JavaScript
100%