原文地址:http://callmesoul.cnreact
fly小程序文档git
配置,主要配置全局的host url和request拦截和request拦截github
var Fly=require("../lib/wx.js") //wx.js is your downloaded code var fly=new Fly(); //Create an instance of Fly // Add interceptors fly.interceptors.request.use((config,promise)=>{ wx.showLoading({ title: '加载中', mask:true }) // Add custom headers return config; }) fly.interceptors.response.use( (response,promise) => { if(typeof (response.data)=='string' && response.data!=''){ response.data=JSON.parse(response.data); } if(response.data.code=="C501"){ } wx.hideLoading() // response.data=Mock.mock(response.data) // Do something with response data . // Just return the data field of response }, (err,promise) => { // Do something with response error //promise.resolve("ssss") wx.hideLoading() } ) // Set the base url fly.config.baseURL="https:127.0.0.1" export default fly;
//引入配置文件 import fly from './config' export default { // 获取个人的阅读指导 get:function (params) { return fly.get("/user",params); }, delete:function (params) { return fly.delete("/user",params); }, }
api入口文件,引入全部模块的api,用的时候只须要由于index便可。小程序
import fly from './config' import book from './book' import bookList from './book-list' import classList from './class' import rank from './rank' import readPlan from './read-plan' import user from './user' import reaction from './reaction' import task from './task' import until from './until' import guide from './guide' import badge from './badge' import activity from './activity' let api= { book, bookList, classList, rank, readPlan, user, reaction, task, until, guide, badge, activity, apiHost:fly.config.baseURL//这属性输出当前http域名 } export default api;
建议直接在app中引入/api/index.js
而后其余页面再使用同过app使用api
xxx.api.user.get({userId:1}).then((res)=>{ console.log(res); })