1、先创建一个request.js文件,在文件中写内容如下:
const BASE_URL = 'http://www.ceshi.com'
const request = (url, data = {}, method = "GET", ContentType = 'application/json') => {
return new Promise(function(resolve, reject) {
let header = {}
if (uni.getStorageSync('token')) {
header = {
'Content-Type': ContentType,
'Authorization': uni.getStorageSync('token')
}
} else {
header = {
'Content-Type': ContentType
}
}
console.log(data, "data")
uni.request({
url: BASE_URL + url,
data,
method,
header,
success: function(res) {
console.log(res, 'res')
if (res.data.code !== 200) {
uni.showToast({
title: res.data.message,
icon: 'none',
duration: 2000
})
reject(res)
} else {
resolve(res.data)
}
},
fail: function(err) {
uni.showToast({
title: '加载失败,请稍后重试!',
icon: 'none',
duration: 2000
})
reject(err)
}
})
})
}
2、具体接口的请求js,内容如下:
import { request } from './request'
// 获取签到次数(当月)
const login = (params) => {
return request('qiye/teacher/signin/statistics', params, 'POST')
}
export {
login
}
3、页面中使用,内容如下:
import { login } from '@/api/login'
let obj = {
phone: 1375455555,
}
login(obj).then(res => {
console.log(res)
})