const BASE_URL = 'url'
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'},dataType:'json',success:res => {if(res.data.code !== 0){}resolve(res)},fail:err => {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){}})