更新记录
1.0.0(2021-03-17) 下载此版本
网络请求封装
平台兼容性
- main.js中全局引入
import Vue from 'vue' import App from './App' import Http from './common/http.js' Vue.config.productionTip = false Vue.prototype.Http = Http App.mpType = 'app' const app = new Vue({ Http, ...App }) app.$mount()
- index.vue中调用
<template> <view class="container"> <button :disabled="logining" @click="login()">登录测试示例</button> <button @click="getNum()">除登录外的业务网络请求示例1</button> <button @click="getList()">除登录外的业务网络请求示例2</button> </view> </template> <script> // import Http from '../../common/http.js' var _self; export default { data() { return { logining: false, mobile:"15638000030", password:"123456", } }, methods: { login:function(){ if(!this.Http.hasNetwork()){ return; } if(this.mobile == "" || this.password == ""){ uni.showToast({ title: "请输入完整信息", icon:'none' }); return; } this.logining = true; this.Http.sendLoginOrRegisterRequest({ url : "user/loginApp", data: { username : this.mobile, password : this.password }, success:function(res){ // uni.setStorageSync("sessionKey",res.token); _self.Http.setSession(res.token); uni.showToast({ title: '登录成功', icon:'none' }); }, fail:function(e){}, complete:function(){ _self.logining = false; } }) }, getNum:function(){ this.Http.sendRequest({ url : "index/index", success:function(res){ console.log("getNum:"+ JSON.stringify(res)); uni.showToast({ title: "success", icon:'none' }); }, fail:function(e){}, complete:function(){} }) }, getList:function() { const obj = { method: 'POST', url: 'machine/index', data: { token: true, keyword:'' } } this.Http.HttpRequest(obj).then(res => { console.log(JSON.stringify(res)) uni.showToast({ title: "success", icon:'none' }); }) }, }, onLoad:function(){ _self = this; } } </script> <style> </style>