封装方法 api.js
utils文件下创建api.js
const apiRequest = (url, method, data) => {
const app = getApp()
//接收所需要的参数,如果不够还可以自己自定义参数
let promise = new Promise(function (resolve, reject) {
var api ='http://localhost:3000'
wx.request({
url: api+url,
data: data ? data : null,
method: method,
header: { 'authorization': wx.getStorageSync('eletoken') },
success: function (res) {
if (res.data.status == "401") {
wx.removeStorageSync('eletoken');
wx.request({
url: api+'/users/refreshToken',
//服务器的地址,现在微信小程序只支持https请求,所以调试的时候请勾选不校监安全域名
header: {
'content-type': 'application/json'
},
method: "GET",
success: function (res) {
wx.setStorageSync('e