MUI封装AJAX二次封装api接口
MUI封装AJAX二次封装api接口
皇上洗碗 发表于6个月前
MUI封装AJAX二次封装api接口
  • 发表于 6个月前
  • 阅读 107
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

var obj = {

    api: {

        login: 'http://api.jianai.org.cn/user/login',

    },

    ajaxPost: function(url, data, success) {

        if(!data.hasOwnProperty('access_token')) {

            //直接从本地存储中获取access_token

            data.access_token = plus.storage.getItem("login");

        }

        //该请求必须是在plus readyed 情况下

        mui.ajax(url, {

            data: data,

            dataType: 'json',

            type: 'post',

            timeout: 12000,

 

            success: function(data) {

                if(data.msg == "验证accessToken错误") {

                    //验证令牌错误时要求用户重新登陆

                    mui.toast('为了您的账号安全,您需要重新登陆');

                    mui.openWindow({

                        url: "../login.html",

                        id: 'login',

                        createNew: true,

                        show: {

                            aniShow: 'zoom-fade-out',

                            duration: 250

                        },

                        waiting: {

                            autoShow: false

                        }

                    })

 

                    return;

                }

 

                success(data);

 

            },

            error: function(xhr, type, errorThrown) {

                mui.toast('网络连接错误,请检查网络后重新启动', {

                    duration: 'long',

                    type: 'div'

                });

                setTimeout(function() {

                    plus.runtime.quit();

                }, 4000)

            }

        });

    },

}

下面是页面的用法,首先引入js

obj.ajaxPost(obj.api.login, {

    pwd: pwd_val,

    phone: phone_val,

    access_token: access_token//可以省略

}, function(data) {

    if(data.code == 1) {

        mui.openWindow({

            id: "index",

            url: "index.html",

            show: {

                aniShow: 'zoom-fade-out',

                duration: 200

            },

            waiting: {

                autoShow: false

            },

        });

    } else {

        mui.toast(data.msg);

    }

});

 

共有 人打赏支持
粉丝 4
博文 119
码字总数 45058
×
皇上洗碗
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: