程序开发中都会调用后端工程师开发的API,小程序的开发文档提供了相对实用的APIwx.request(),但是在开发的过程中,又遇到了一些问题,在小程序的项目开发时,调用的API不止一个,同一个API调用不止一次。同时,对于调用的API的管理也十分复杂,这样的背景下,对wx.request()方法的封装变得尤为重要。
  本文的解决思路为:将API的路径和方法放在一个文件里面方便管理;封装小程序的request方法,并返回promise处理(ES6)。

一、units文件夹中新建request.js文件

// utils/request.js
//封装request let apiRequest = (url, method, data, header) => { //接收所需要的参数,如果不够还可以自己自定义参数
let promise = new Promise(function (resolve, reject) {
wx.showNavigationBarLoading() //在标题栏中显示加载
wx.request({
url: url,
data: data ? data : null,
method: method,
header: header ? header : { 'content-type': 'application/x-www-form-urlencoded' },
complete: function () {
wx.hideNavigationBarLoading(); //完成停止加载
wx.stopPullDownRefresh(); //停止下拉刷新
},
success: function (res) {
//接口调用成功
resolve(res.data); //根据业务需要resolve接口返回的json的数据
},
fail: function (res) {
wx.showModal({
showCancel: false,
confirmColor: '#1d8f59',
content: '数据加载失败,请检查您的网络,点击确定重新加载数据!',
success: function (res) {
if (res.confirm) {
apiRequest(url, method, data, header);
}
}
});
wx.hideLoading();
}
})
});
return promise; //注意,这里返回的是promise对象
} export default apiRequest;

二、units文件夹中新建api.js文件

import apiRequest from './request.js';
const HOST = 'http://www.mingmingym.com';
const API_LIST = {
all: {
method: 'POST',
url: '/e/extend/api/type.php'
},
} /*
多参数合并
*/
function MyHttp(defaultParams, API_LIST) {
let _build_url = HOST;
let resource = {};
for (let actionName in API_LIST) {
let _config = API_LIST[actionName];
resource[actionName] = (pdata) => {
let _params_data = pdata;
return apiRequest(_build_url + _config.url, _config.method, _params_data, {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8;Authorization;'
});
}
}
return resource;
}
const Api = new MyHttp({}, API_LIST);
export default Api;

三、业务中使用

import Api from '/../../utils/api.js';
.
.
.
getAll() {
Api.all({ id: 1 }).then(res => {
console.log(res);
})
}

通过对小程序网络请求方法的二次封装,使得我们的代码看上去更加的简洁,在接口的管理上也相对的便利,比如在后端修改API的路径和方法时,只需要在api.js文件中修改相应的API即可,也免去了修改时忽略了更多调用的麻烦。同时,也提高了代码的复用性,一劳永逸。
————————————————
原文链接:https://blog.csdn.net/mingcodes/article/details/103582581

小程序封装request请求,统一API的更多相关文章

  1. 小程序封装request请求

    //request.js var host = 'https://www.xxx.com';//请求域名 module.exports = function (type, params, method ...

  2. 监控微信小程序wx.request请求失败

    在微信小程序里,与后台服务器交互的主要接口函数是wx.request(),用于发起 HTTPS 网络请求.其重要性不言而喻.然而,却经常遇到请求失败的问题,笔者特意谷歌"wx.request ...

  3. 关于微信小程序的Request请求错误处理

    在学微信小程序的request请求的时候,一开始报“不在以下合法域名列表中,请参考文”的错误,后来又莫名其妙的报“400 Bad Request”错误,经过半天的研究,终于搞定了,把遇到的错误给大家分 ...

  4. 微信小程序wx.request请求用POST后台得不到传递数据

    微信小程序的wx.request请求,method设为POST并向后台传递数据,但从后台返回的信息来看后台并没有获得传递的数据 wx.request({              url: 'url' ...

  5. 微信小程序- wx.request请求不到数据

    小程序官方文档手册 https://mp.weixin.qq.com/debug/wxadoc/dev/ 小程序开发问答社区 http://www.henkuai.com/forum.php wx.r ...

  6. 微信小程序wx.request请求服务器json数据并渲染到页面

    [原文出自]: https://blog.csdn.net/weixin_39927850/article/details/79766259 微信小程序的数据总不能写死吧,肯定是要结合数据库来做数据更 ...

  7. 小程序发送 request请求失败 提示不在合法域名列表中的解决方法

    可以在小程序开发工具中设置不校验域名.

  8. 微信小程序测试request请求webapi

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...

  9. 小程序 wx.request请求

    1.wx.request相当于发送ajax请求 微信官方解释 参数 属性 类型 默认值 必填 说明 url string 是 开发者服务器接口地址 data string/object/ArrayBu ...

随机推荐

  1. 对于Python函数与方法,你可能存在些误解

    欢迎添加华为云小助手微信(微信号:HWCloud002 或 HWCloud003),输入关键字"加群",加入华为云线上技术讨论群:输入关键字"最新活动",获取华 ...

  2. sar命令,linux中最为全面的性能分析工具之一

    sar是System Activity Reporter(系统活动情况报告)的缩写.这个工具所需要的负载很小,也是目前linux中最为全面的性能分析工具之一.此款工具将对系统当前的状态就行取样,然后通 ...

  3. 华为参与《基于5G技术的医院网络建设标准》的制定

    [摘要] 5G 千兆网承载五地远程会诊,现场完成三例复杂性疑难重症远程病例讨论 [中国,北京,2019年9月4日] 金秋之际,在国家卫生健康委指导下,由中日友好医院•国家远程医疗与互联网医学中心•国家 ...

  4. Windows下利用IIS建立网站并实现局域网共享

    https://blog.csdn.net/qq_41485414/article/details/82754252 https://www.cnblogs.com/linuxprobe-sarah/ ...

  5. UVA11324 The Lagest Lique(SCC缩点+DP)

    Given a directed graph G, con- sider the following transformation. First, create a new graph T(G) to ...

  6. CodeForces1006C-Three Parts of the Array

    C. Three Parts of the Array time limit per test 1 second memory limit per test 256 megabytes input s ...

  7. SpringBoot学习【一】----- HelloWord

    springboot是什么 Spring Boot可以轻松创建可以运行的独立的,生产级的基于Spring的应用程序. 大多数Spring Boot应用程序只需要很少的Spring配置. 提供了一个运行 ...

  8. Django 11

    目录 功能配置设计 跨站请求伪造CSRF 什么是CSRF 如果实现CSRF 如何避免CSRF CSRF相关的两个装饰器 auth模块 常用方法 扩展auth_user表中的字段 功能配置设计 实现类似 ...

  9. java发送邮件基础方法(另附部分主流邮箱服务器地址、端口及设置方法)

    java发送邮件基础方法,可通过重载简化参数 import java.io.File; import java.io.UnsupportedEncodingException; import java ...

  10. 【docker】Dockerfile

    [docker]Dockerfile 转载: ============================================================= =============== ...