//建一个request.js
const BASE_URL = 'url' //后端给的url
// 传入请求参数
// method: method,
// // 配置请求类型
// header: method == 'get' ? {'token': token, 'X-Requested-With': 'XMLHttpRequest',"Accept": `application/json, text/plain, */*`,"Content-Type": "application/json; charset=UTF-8"} : {'token': token,'X-Requested-With': 'XMLHttpRequest','Content-Type': 'application/json; charset=UTF-8'},
export const myRequest = (options) => {
return new Promise((resolve,reject)=> {
let authtoken = uni.getStorageSync('authtoken')
uni.request({
url:BASE_URL + options.url,
data:options.data || {},
method:options.method || 'get',
header: options.method == 'get' ? {'token': uni.getStorageSync('authtoken'), 'X-Requested-With': 'XMLHttpRequest',"Accept": `application/json, text/plain, */*`,"Content-Type": "application/json; charset=UTF-8"} : {'token': uni.getStorageSync('authtoken'),'X-Requested-With': 'XMLHttpRequest','Content-Type': 'application/json; charset=UTF-8'},
// header:{
// 'Content-type': 'application/json',
// "X-Auth-Token": uni.getStorageSync('authtoken') //这里的token指的是微信授权登录的token,用来判断小程序是否属于登录状态
// },
dataType:'json',
success:res => {
if(res.data.code !== 0){
}
resolve(res)
},
fail:err => {
// uni.showToast({
// title:"请求接口失败",
// })
console.log('err',err)
reject(err)
}
})
})
}
main.js
import {myRequest} from "@/request/request.js"
Vue.prototype.$myRequest=myRequest
页面中
this.$myRequest({
url: `接口url`,
method:'get',
data:{}
}).then(data=>{
if(data.data.code == 200){
}
})