uniapp利用Cookie实现微信小程序session登录以及验证码

         #登录返回
        if(res.cookies != undefined){
            //判断是否包含AGENTSESSIONID,安卓手机会随机的
            if(res.cookies[0].includes("AGENTSESSIONID")){
                uni.setStorageSync('loginCookie',res.cookies[0]);
            }else{
                uni.setStorageSync('loginCookie',res.cookies[1]);
            }
        }

        #接口请求    
        this.$myRequest({
             url:url,
             headers:{ 'Cookie':uni.getStorageSync('loginCookie')},
             methods:'POST',
             dataType:'json',
             data:data
         });

         #验证码验证
         uni.downloadFile({
                url:  BASE_URL+"/captcha/new?"+Math.round(Math.random()*100), // 这里是你接口地址 若要传参 直接 url拼接参数即可
                header:{
                     'Cookie':uni.getStorageSync('loginCookie')
                },
                methods: 'GET',
                success: (res) => {
                    this.model.codeimg = res.tempFilePath;
                    var tempFilePath = res.tempFilePath; // 这里拿到后端返回的图片路径
                },
                fail: () => {
                    uni.hideLoading();
                }
            });
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!