新建utils文件夹,新建service.js文件
const baseUrl = 'xxxx'
const token = wx.getStorageSync('token')
export const req = {
get:(parmas) => {
console.log(parmas,'parmas');
return new Promise((resolve , reject) => {
wx.request({
url: baseUrl + parmas.url,
data: parmas.data,
method: 'GET',
header: {
'content-type': 'application/json',
'X-Access-Token':token
},
success: (res) => {
resolve(res)
},
fail: (err) => {
reject(err)
}
})
})
},
post:(parmas) => {
console.log(parmas,'parmas');
return new Promise((resolve , reject) => {
wx.request({
url: baseUrl + parmas.url,
data: parmas.data,
method: 'POST',
header: {
'content-type': 'application/json',
'X-Access-Token':token
},
success: (res) => {
resolve(res)
},
fail: (err) => {
reject(err)
}
})
})
},
}
api.js文件
import { req } from "../../utils/service"
const api = {
login: '/wx/login'
}
//export const loginApi = (params) => req.get({url: api.login,data:params})
export const loginApi = (params) => req.post({url: api.login,data:params})
login.js文件应用
import { loginApi } from "./api"
Page({
login() {
loginApi(params)
.then(res=> {
// 成功回调
})
.catch(err=> {
// 失败回调
})
},
})