9***@qq.com
9***@qq.com
  • 发布:2024-05-21 11:15
  • 更新:2024-05-21 12:11
  • 阅读:74

唤起一键登录后调用接口报错request:fail abort statusCode:-1 Expected URL scheme 'http' or 'https' but was 'file'

分类:HBuilderX

getPhoneNumber() { //一键登录
var that = this;
// uni.navigateTo({
// url: '/pages/index/index'
// })
uni.login({ //正式登录,弹出授权窗
provider: 'univerify',
univerifyStyle: { // 自定义登录框样式
"fullScreen": true, // 是否全屏显示,true表示全屏模式,false表示非全屏模式,默认值为false。
"backgroundColor": "#ffffff", // 授权页面背景颜色,默认值:#ffffff
"phoneNum": {
"color": "#2281F5", // 手机号文字颜色 默认值:#000000
},
"icon": {
"path": "static/images/loginLogo.png" // 自定义显示在授权框中的logo,仅支持本地图片 默认显示App logo
},

                    "authButton": {  
                        "normalColor": "#3479f5", // 授权按钮正常状态背景颜色 默认值:#3479f5  
                        "highlightColor": "#2861c5", // 授权按钮按下状态背景颜色 默认值:#2861c5(仅ios支持)  
                        "disabledColor": "#73aaf5", // 授权按钮不可点击时背景颜色 默认值:#73aaf5(仅ios支持)  
                        "textColor": "#ffffff", // 授权按钮文字颜色 默认值:#ffffff  
                        "title": "本机号码一键登录" // 授权按钮文案 默认值:“本机号码一键登录”  
                    }  
                },  
                success(res) { // 正式登录成功  
                console.log(res,'res正式登录成功!');  
                    let authResult = {  
                        accessToken: res.authResult.access_token,  
                        openid: res.authResult.openid  
                    }  
                    console.log(authResult, 'authResult');  
                    uni.request({  
                        method: 'POST',  
                        url: 'login/sign',  
                        data: authResult,  
                    }).then(res => {  
                        console.log(res,'res');  
                        uni.showToast({  
                            title: '登录成功'  
                        })  
                        uni.closeAuthView() //成功关闭授权页面  
                        setTimeout(() => {  
                            uni.switchTab({  
                                url: '/pages/index/index'  
                            })  
                            plus.runtime.restart();  
                        }, 500)  
                    }).catch(err => {  
                        console.error('Request failed:', err);  
                        // 处理请求失败的逻辑  
                    });  
                },  
                fail(err) { // 正式登录失败  
                console.log(err,'err');  
                    uni.closeAuthView() //关闭授权登录界面  
                }  
            })  
        },  

拿到了token和openid后带参调接口报错request:fail abort statusCode:-1 Expected URL scheme 'http' or 'https' but was 'file'后端说错误是JWT字符串参数不能为空或空字符串,后端配置文件里面jwt需要填写secret密钥,问一下这个secret密钥是前端在开发者中心的服务空间的密钥吗,现在新版本hbuilderX前端一键登录也不需要配置apiKey所以没找到 求大佬解答!!!

2024-05-21 11:15 负责人:无 分享
已邀请:
DCloud_云服务_LQ

DCloud_云服务_LQ

uni.request({
method: 'POST',
url: 'login/sign',
data: authResult,
})
url 需要写完整的服务器地址

  • 9***@qq.com (作者)

    修改后的代码是getPhoneNumber() { //一键登录

    let that=this

    uni.preLogin({ //预登陆检查是否符合一键登录的环境,可不用

    provider: 'univerify',

    success: () => {

    uni.login({

    provider: 'univerify',

    univerifyStyle: {

    "fullScreen": true, // 是否全屏显示,true表示全屏模式,false表示非全屏模式,默认值为false。

    "backgroundColor": "#ffffff", // 授权页面背景颜色,默认值:#ffffff

    "phoneNum": {

    "color": "#000000", // 手机号文字颜色 默认值:#000000

    },

    "slogan": {

    "color": "#8a8b90", // slogan 字体颜色 默认值:#8a8b90

    },

    "icon": {

    "path": "static/logo.png" // 自定义显示在授权框中的logo,仅支持本地图片 默认显示App logo

    },

    "authButton": {

    "normalColor": "#3479f5", // 授权按钮正常状态背景颜色 默认值:#3479f5

    "highlightColor": "#2861c5", // 授权按钮按下状态背景颜色 默认值:#2861c5(仅ios支持)

    "disabledColor": "#73aaf5", // 授权按钮不可点击时背景颜色 默认值:#73aaf5(仅ios支持)

    "textColor": "#ffffff", // 授权按钮文字颜色 默认值:#ffffff

    "title": "本机号码一键登录" // 授权按钮文案 默认值:“本机号码一键登录”

    },

    "otherLoginButton": {

    "visible": "false", // 是否显示其他登录按钮,默认值:true

    },

    },

    success: (res) => {

    console.log(res,'123');

    uniCloud.callFunction({

    name: 'getPhoneNumber', // 你的云函数名称

    data: {

    access_token: res.authResult.access_token, // 客户端一键登录接口返回的access_token

    openid: res.authResult.openid // 客户端一键登录接口返回的openid

    }

    }).then(dataRes => {

    console.log('云函数返回的参数', dataRes)

    // this.phone = dataRes.result.-res.phoneNumber //这里的dataRes.result.res就是前面云函数index中retrun的res(还包含其他信息,phoneNumber只是手机号)

    }).catch(err => {

    console.log('云函数报错', err)

    })

    uni.showToast({

    title: res.authResult,

    icon: "none"

    })

    uni.closeAuthView() //关闭一键登录弹出窗口

    },

    fail(res) { // 登录失败

    uni.closeAuthView() //关闭一键登录弹出窗口

    console.log('失败')

    },

    })

    },

    fail(res) {

    console.log('一键登录失败', res)

    if (res.errMsg != 'login:ok') {

    uni.showToast({

    title: res.metadata.msg,

    icon: "none"

    })

    }

    //如果手机没有插入有效的sim卡,或者手机蜂窝数据网络关闭,

    //都有可能造成预登录校验失败。

    }

    })

    },请问为什么token和openid打印都有值但是报错云函数报错, Error: [getPhoneNumber]: Cannot read property 'access_token' of undefined

    2024-05-22 10:36

要回复问题请先登录注册