1. 程式人生 > 實用技巧 >uni-app 登入Abp VNexe並獲取Token

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/roles/all', method: 'GET', header:{ 'Authorization': res.data }, success: res => { console.log(res) }, }); } })