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) } })