位置项目根目录index.js
定义了两种请求get和post
import baseconfig from "./common/baseconfig.js"
const httpClient = {
request:function(method,url,data){
var headers={
"Content-Type": "application/x-www-form-urlencoded",
// "Auth-Token": uni.getStorageSync("auth_token")
};
var nowDate = new Date().getTime();
return new Promise((resolve,reject)=>{
uni.request({
url:url,
header:headers,
data:data,
method:method,
dataType: 'json',
success:function(res){
uni.setStorageSync('closestatus', false);
console.log("接口获取原始数据:---->",res.data,nowDate)
},
fail: function(err){
reject(err)
}
})
})
},
Get: function(url,data){
let allurl = this.geturl(url);
return this.request('GET',allurl,data)
},
Post: function(url,data){
let allurl = this.geturl(url);
return this.request('POST',allurl,data)
},
geturl:function(url){
return baseconfig.server+url
}
};
module.exports = httpClient
位置common/baseconfig.js
var baseconfig={};
if(process.env.NODE_ENV === 'development'){
baseconfig = {
server:uni.getStorageSync('ip')+":"+uni.getStorageSync('port')
}
}else if(process.env.NODE_ENV === 'production'){
baseconfig = {
server:uni.getStorageSync('ip')+":"+uni.getStorageSync('port')
}
}
export default baseconfig;
根目录api.js
此js方法注册到全局vuex
import httpClient from './index.js'
import httpClient from './index.js'
export function get(url,params){
return httpClient.Get(url,params)
};
export function post(url,params){
return httpClient.Post(url,params)
};
根目录main.js修改
import {get,post} from ‘./api.js’
Vue.prototype.apiget = get
Vue.prototype.apipost = post
注册后在vue文件可以this.apiget直接调用
import Vue from 'vue'
import App from './App'
import store from './store'
import {get,post} from './api.js'
Vue.prototype.apiget = get
Vue.prototype.apipost = post
Vue.config.productionTip = false
Vue.prototype.$store = store
App.mpType = 'app'
const app = new Vue({
store,
...App
})
app.$mount()
demo.vue
methods:{
minfo() {
let that=this
that.apiget('/broker/my/info/dealer',{}).then(res => {
that.dealer = res.dealer
});
}
}