更新记录

1.0.0(2021-03-17)

网络请求封装


平台兼容性

  1. 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()
  2. 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>

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议

暂无用户评论。

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问