更新记录
1.0.0(2021-03-17) 下载此版本
网络请求封装
平台兼容性
uni-app
app-vue | app-nvue | app-android | app-ios | app-harmony |
---|---|---|---|---|
? | ? | ? | ? | ? |
H5-Safari | Android Browser | 微信浏览器(Android) | QQ浏览器(Android) | Chrome | IE | Edge | Firefox | PC-Safari |
---|---|---|---|---|---|---|---|---|
? | ? | ? | ? | ? | ? | ? | ? | ? |
微信小程序 | 支付宝小程序 | 百度小程序 | 字节小程序 | QQ小程序 | 钉钉小程序 | 快手小程序 | 飞书小程序 | 京东小程序 |
---|---|---|---|---|---|---|---|---|
? | ? | ? | ? | ? | ? | ? | ? | ? |
快应用-华为 | 快应用-联盟 |
---|---|
? | ? |
uni-app x
app-android | app-ios |
---|---|
? | ? |
H5-Safari | Android Browser | 微信浏览器(Android) | QQ浏览器(Android) | Chrome | IE | Edge | Firefox | PC-Safari |
---|---|---|---|---|---|---|---|---|
? | ? | ? | ? | ? | ? | ? | ? | ? |
- 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>