微信小程序 wxRequest封装

/Api/myRequest.js

const baseUrl = 'https://api.it120.cc';

const http = ({ url = '',param = {},...other } = {}) => {
wx.showLoading({
title: '请求中...'
});
let timeStart = Date.now();
return new Promise((resolve,reject) => {
wx.request({
url: url,data: param,header: {
"content-type": "application/x-www-form-urlencoded" // 默认值,另一种是 "content-type": "application/x-www-form-urlencoded"
},...other,complete: (res) => {
wx.hideLoading();
console.log(耗时${Date.now() - timeStart});
},success: (res) => {
resolve(res.data)
},fail: () => {
reject(res)
}
})
})
}

const getUrl = (url) => {
if (url.indexOf('://') == -1) {
url = baseUrl + url;
}
return url
}

// get方法
const _get = (url,param = {}) => {
return http({
url,param
})
}

const _post = (url,param,method: 'post'
})
}

const _put = (url,method: 'put'
})
}

const _delete = (url,method: 'put'
})
}
module.exports = {
baseUrl,_get,_post,_put,_delete
}

// Api/api.js

import api from './myRequest.js';
const baseUrl = 'https://api.it120.cc';

function getList(data) {
let url = baseUrl + '/34vu54u7vuiuvc546d' + '/banner/list';
return api._post(url,data)
}

module.exports = {
getList
}

// app.js 使用
//app.js
import api from '/Api/api.js';
App({
  onLaunch: function () {
    // 
    api.getList({ key: 'mallName'}).then(function(res) {
      console.log(res);
    })
  }
})

相关文章

概述 消息能力是小程序能力中的重要组成,我们为开发者提供了...
判断H5页面环境在微信中还是小程序中 用小程序提供的wx.mini...
wx.reLaunch和wx.navigateTo,wx.navigateTo的区别 2019-03-...
微信小程序如何从数组里取值_微信小程序 传值取值的几种方法...
H5项目接入微信授权登录,通过 UA 区分微信还是普通浏览器:...
微信小程序获取data-xx=""属性的值,自定...