小程序封装接口API
新建api文件,新建api.js 写一下代码调用接口
·
新建api文件,新建api.js 写一下代码
export const API_HOST = "接口前缀";
const app = getApp();
const ReqClient = (url, method, data) => {
return new Promise((resolve, reject) => {
wx.request({
url: API_HOST + url,
data,
method,
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: (res) => resolve(res),
fail: err => reject(err)
})
})
}
export const 接口名字= data => {
return ReqClient('接口', 'POST', {
...data
})
}
export const 接口名字= data => {
return ReqClient('接口', 'GET', {
...data
})
}
调用接口
// 先引入 然后调取即可
import {
wxLogin
} from '封装api的路径'
wxLogin(数据).then(res => {
if (res.data.code == 0) {
//成功获取
} else {
wx.showToast({
title: res.data.msg,
icon: 'none'
})
}
})
更多推荐
所有评论(0)