let timestamp = Date.parse(new Date())
module.exports = {
http(url, method, params, value = false) {
return new Promise((resolve, reject) => {
let host = 'https://xxxxx/';
let token = wx.getStorageSync('token');
let sign = ''
let data = {
sign
}
if (params.data) {
for (let key in params.data) {
if (params.data[key] == null || params.data[key] == 'null') {
delete params.data[key]
}
}
data = {
...data,
...params.data
}
}
wx.request({
url: host + url,
method: method == 'post' ? 'post' : 'get',
data,
header: {
'content-type': method == 'get' ? 'application/json' : 'application/x-www-form-urlencoded',
'os-type': 'miniapp',
'token': token,
timestamp: timestamp,
},
success(res) {
console.log('res',res)
resolve(params.success && params.success(res))
},
fail(err) {
console.log('err',err)
wx.showToast({
title: '服务器内部错误',
icon: 'none',
duration: 1500
})
reject(params.fail && params.fail(err))
},
complete() {
setTimeout(() => {
wx.hideLoading();
}, 1500)
}
})
})
}
}
import {http} from './http';
function 定义方法名(params) {
http('路径', 'post', params)
}
function 定义方法名(params) {
http('路径', 'get', params)
}
export default {
定义好的方法名
}
//xml页面中引用
import http from '../../utils/api.js' // 引入api接口管理文件
//get请求跟post请求都这么写
http.定义好的名字({ // 调用接口,传入参数
data: {
这里写需要传的参数
},
success: res => {
console.log('接口请求成功', res)
},
fail: err => {
console.log(err)
}
})