uni-app 登录Abp VNexe并获取Token

uni.request方式登录abp关键代码如下,因abp获取token需要用formdata方式请求所以需要加上请求头
const baseUrl = 'http://127.0.0.1:44323';
uni.request({
    url: baseUrl + '/connect/token',
    method: 'POST',
    header: {
        'content-type': "application/x-www-form-urlencoded"
    },
    data: {
        grant_type: 'password',
        scope: 'shop',
        username: 'admin',
        password: '1q2w3E*',
        client_id: 'shop_App',
        client_secret: '1q2w3e*'
    },
    success: res => {
        if (res.statusCode === 200) {
            uni.setStorageSync('access_token', res.data.token_type + ' ' + res.data.access_token)
        } else {
            uni.showToast({
                icon: 'none',
                title: '登录失败!'
            })
        }
    }
});

测试获取数据,请求数据需要在请求头带上Token

const baseUrl = 'http://127.0.0.1';
uni.getStorage({
    key: 'access_token',
    success: res => {
        uni.request({
            url: baseUrl + '/api/identity/users',
            method: 'GET',
            header: {
                'Authorization': res.data
            },
            success: res => {
                console.log(res)
            },
        });
    },
    fail: res => {
        console.log(res)
    }
})

原文地址:https://www.cnblogs.com/liessay/p/14047276.html