原文地址: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); })