uniapp接口封装

//网络请求
    networkRequest(param) {
        let that = this;
        const token = uni.getStorageSync('token');
        const header = param.header || {
            appId: appId,
            'content-type': 'application/json'
        }
        if (token != '') {
            header['token'] = token;
        }

        if (param.method) {
            param.method = param.method.toUpperCase(); //小写改为大写
        }
        //网络请求
        uni.request({
            url: apiUrl + param.url,
            method: param.method || "GET",
            header: header,
            data: param.data || {},
            success: res => {
                uni.hideLoading();
                if (res.data.code == 401) {
                    uni.removeStorageSync('token');
                    //token失效
                    if (that.isGoLogin) {
                        // #ifdef MP-WEIXIN
                        uni.showLoading({
                            title: '重新登录...',
                            mask: true
                        });
                        

                        that._mp_wx_login('/account/getwxphone', function(res) {
                            that.showToast("登录成功");
                            uni.setStorage({
                                key: 'token',
                                data: res.token
                            });
                            that.isGoLogin = true;
                            let page = getCurrentPages().pop();
                            if (!page) return;
                            page.onLoad(page.options); //如果页面存在,则重新刷新页面
                            page.onShow(); //如果页面存在,则重新刷新页面
                        });
                        // #endif
                        // #ifdef H5
                        uni.navigateTo({
                            url: "/pages/me/login/login?url=" + encodeURIComponent(window.location.href)
                        });
                        // #endif
                    }
                    that.isGoLogin = false;
                    return false;
                }
                if (res.data.code != 200) {
                    this.showToast(res.data.msg);
                    typeof param.error == "function" && param.error(res.data);
                    return false;
                }
                typeof param.success == "function" && param.success(res.data.data);
            },
            fail: (err) => {
                uni.hideLoading();
                this.showToast(err.errMsg);
            },
            complete: (res) => {
                typeof param.complete == "function" && param.complete(res.data);
            }
        });
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值