service.js文件
const baseUrl = 'xxx.com'
const dfRequest = (params) => {
let token = wx.getStorageSync("token") || ''
return new Promise((resolve , reject) => {
wx.showLoading({
title: '加载中',
mask: true
})
wx.request({
url: params.url,
data: params.data,
method: params.method,
timeout: 300000,
header: {
'content-type': 'application/json',
'X-Access-Token': token
},
success: (res) => {
resolve(res.data)
wx.hideLoading()
},
fail: (err) => {
reject(err)
wx.hideLoading()
}
})
})
}
function packageParam(url, data, method) {
let param = {};
param.url = baseUrl + url;
param.data = data;
param.method = method;
return param;
}
const get = (param) => {
let params = packageParam(param.url, param.data, "GET")
return dfRequest(params)
}
const post = (param) => {
let params = packageParam(param.url, param.data, "POST")
return dfRequest(params)
}
module.exports = {
get,
post
}
api.js文件
const {post,get} = require('../../utils/service') // 引入
const api = {
login: '/xxx/xxx',
}
export const login= (params) => get({url: api.login,data: params})
login.wxml
import { login} from "./api" // 引入login
handleLogin() {
login(data)
.then(res => {
// 成功
})
.catch(err => {
// 失败
})
}