博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小程序开发之一(使用fly进行http封装)
阅读量:7048 次
发布时间:2019-06-28

本文共 1858 字,大约阅读时间需要 6 分钟。

原文地址:http://callmesoul.cn

下载fly js文件

/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);})

转载地址:http://vjkol.baihongyu.com/

你可能感兴趣的文章
shell编程 ---test
查看>>
Andriod 对话框 .
查看>>
程序员:面试挂了,面试官说我没有互联网气质,网友:那是啥气质
查看>>
有史以来,程序员被黑的最最最惨的一次!
查看>>
微信第三方平台全网发布检测
查看>>
企业WiFi解决方案 安全上网很重要
查看>>
lvs和nginx的区别
查看>>
java.lang.Comparable
查看>>
LINQ to Objects(1)
查看>>
RHCSA认证培训+考试七天实录(三)
查看>>
Golang的百科全书--the way to go
查看>>
JSP踩过的坑
查看>>
如何共享数据?- 每天5分钟玩转 Docker 容器技术(41)
查看>>
Linux tmp目录自动清理总结
查看>>
水晶报表工具栏按钮代码
查看>>
[Apache] lamp平台安装【注解版(整理后)】
查看>>
linux下nc检测端口 配置报警
查看>>
清理内存的命令
查看>>
网络知识
查看>>
Elasticsearch 5.4 Mapping详解
查看>>