request.js 公共脚本

request.js 定义了postget两个方法,方法对uni.request做了二次封装,增加全局异常处理和通用逻辑判断。

登录接口获取token

1、引入 request.js

import API from "@/common/request.js"

2、构建登录逻辑如下:

/*用户登录*/
login: function() {
    var that = this;
    if(!this.useraccount || !this.password){//验证用户名、密码
        uni.showToast({
            title: '用户名或密码不能为空',
            icon:'none',
            mask: false,
            duration: 3000
        });
        return false
    }
    that.loadModal = true;
    let statusMap = {
        "10001":"认证失败",
        "10002":"认证请求方法不支持.",
        "10003":"登录失败次数操作最大限制,请输入验证码",
        "10004":"验证码发送失败",
        "10005":"请输入验证码",
        "10006":"验证码已过期",
        "10007":"验证码不可用",
        "10008":"验证码错误",
        "10010":"用户名或密码错误",
        "10011":"用户未注册",
        "10012":"用户已注册",
        "10013":"用户帐号不存在",
        "10014":"用户帐号未启用",
        "10015":"用户帐号已过期",
        "10016":"用户帐号已被锁定",
        "10017":"没有为用户指定角色",
        "10021":"认证失败"
    }
    API.post({
        async        : false,
        type        : "POST",
        url            : this.config.apiList.getToken,
        contentType    : "application/json",
        dataType    : "json",
        data        : {
            "username"    : this.useraccount,
            "password"    : this.password,
            "captcha"    : this.captcha
        },
        //成功时回调
        success: function (data) {
            that.loadModal = false;
            if(statusMap[data.code]){
                uni.showToast({
                    title    : statusMap[data.code],
                    icon    :'none',
                    mask    : false,
                    duration: 1000
                })
                return;
            }

            uni.showToast({
                title    : '登录成功',
                icon    : 'success',
                mask    : false,
                duration: 1000
            });
            uni.setStorage({
                key: 'userlogin',//将用户登录状态储存到本地缓存中,重要的studentId及token
                data: data,
                success: function() {
                    uni.navigateBack({
                        delta:1
                    })
                    //that.getUserInfo(data.userkey);//将个人基本信息保存到本地存储里
                }
            });
            /*
             格式如: 
            {"msg":"认证成功",
             "code":"0", // 逻辑标记
             "initial":false, // 是否首次登陆
             "alias":"超级管理员", // 账户名,教师角色即教师名称,学生类型即学生名称
             "perms":[{"authority":"*"},{"authority":"ROLE_1"}], // 权限标记,与前端组件进行对应实现功能控制
             "usercode":"201909171", // 用户编码:教师角色即教工号,学生类型即学号
             "userid":"1",    // 用户表ID
             "userkey":"973628b4537962ac6f2cacbf9403397b", // 账户ID:教师角色即教工表ID,学生类型即学生表ID
             "token":"", // jwt授权
             "username":"admin" // 用户名
            * }
            */
        },
        // 接口调用结束的回调函数(调用成功、失败都会执行)
        complete : function(res){
            that.loadModal = false;
        }
    });
}

使用 API.post() 或者 API.get() 调用业务接口

1、post示例

API.post({
    type        : "POST",
    url            : this.config.apiList.signin_tch_initiate,
    contentType    : "application/json",
    dataType    : "json",
    data        : {
        "jsId"    : this.data.jsId,
        "kcId"    : this.data.kcId,
        "dkfs"    : this.dkfs,
        "dklx"    : this.data.dklx
    },
    //成功时回调
    success: function (res) {
        var data = res["data"] || {};
        // {"code":"10112","msg":"当前时间没有可打卡课程","data":null}
        if(data["code"] != "0"){
            uni.showToast({
                title    : data["msg"],
                mask    : false,
                duration: 3000
            })
            return;
        }
        that.initBind();
        uni.showToast({
            title    : data["msg"],
            icon    : 'success',
            mask    : false,
            duration: 3000
        })
        console.log(that.index,'aaa')
        if(that.index==1){
            that.modalName='DialogModal1'
            that.code = data["data"];
        }
        if(that.index==2){
            that.modalName='qrCode'
            that.code = data["data"];
            console.log(that.code,'111112')
        }
    }
});

2、get示例

// 查询教师当前课程信息
API.get({
    url            : this.config.apiList.signin_tch_now,
    //成功时回调
    success: function (res) {
        var data = res["data"] || {};
        // {"code":"10112","msg":"当前时间没有可打卡课程","data":null}
        if(data["code"] != "0"){
            that.tipText = data["msg"];
            return;
        }
        that.isShow = true;
        that.data = data["data"];
        that.dkfsList.forEach(function(item, index, array) {
            if(item.key == that.data.dkfs){
                that.index = index;
            }
        });
        that.dkfs = data["data"].dkfs;
        // 启动定时时间更新
        that.getTime();
        //that.Timer =
        //Timer = [{time:"09:00",},{time:"18:00"}],
        //res["data"]
        /*
         格式如: 
         {"code":"0",
          "msg":"请求成功",
          "data":{"id":null,"jsid":null,"jsmc":null,"kcid":null,"kcmc":"英语","xn":"2019-2020","xq":"1","kcrq":"2019-06-19","kssj":"19:00","jssj":"19:45","bj":"4fcd41a0eee249ad98e586837d087ae9,6be6d920a0434b1591972621202789ea","js":"8B2109EC87ECD969E050007F01007A0E","sksc":"120","dkzt":null,"bzxx":null,"dkzb":null,"dkdd":null,"dkfs":"1 ","dklx":null,"dksj":null,"dqsj":"2019-06-19 19:08:37"}}
        */
    }
});
作者:Jeebiz  创建时间:2019-07-14 00:05
 更新时间:2023-12-20 16:58