原文链接:http://callmesoul.cn/xiao-cheng-xu-kai-fa-zhi-yi-shi-yong-flyjin-xing-httpfeng-zhuang/
下载fly js文件
fly小程序文档
https://wendux.github.io/dist/#/doc/flyio/wx
/api/config.js
配置,主要配置全局的host url和request拦截和request拦截
var Fly=require("../lib/wx.js") //wx.js is your downloaded codevar fly=new Fly(); //Create an instance of Fly// Add interceptorsfly.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 urlfly.config.baseURL="https:127.0.0.1"export default fly;
生成对应模块api文件,比如user
//引入配置文件import fly from './config'export default { // 获取我的的阅读指导
get:function (params) { return fly.get("/user",params);
}, delete:function (params) { return fly.delete("/user",params);
},
}
/api/index.js
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使用
xxx.api.user.get({userId:1}).then((res)=>{ console.log(res);
})
你好, 原文没有了。 能在给提供一下吗? 谢谢了
不好意思,直接总博客复制过来的,平台不支持markdown,所以格式有出入。最上有原文地址,进入原文地址查看会好点
看着好难受。。。