1.在最外层的app.js中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | App({ globaldata: { serverurl: ‘实际地址‘ , // serverurl: ‘http://172.16.1.47:7001‘, authCode: ‘‘ , }, // 封装网络请求 https(httpstype, url, data) { dd.showLoading(); let endurl = encodeURI( this .globaldata.serverurl + url); return new Promise((resolve, reject) => { dd.httpRequest({ headers: { "Content-Type" : ‘application/json;charset=utf-8‘ , Authorization: ‘Bearer ‘ + ‘eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI1ZDVjOTkwODY1NmZmODAwYTE3ZTExNzUiLCJyYW5kb20iOiJjNjJhNWE2ZDk3OTliMTE1IiwidXNlcm5hbWUiOiLnlJjkuJzkuJwiLCJpYXQiOjE1NjY0NDE4OTR9.LZFdeYmNUCes-xF2HdFIGER4xVddDYn4RvtI3n1kLzs‘ }, url: endurl, method: httpstype, data: data, dataType: ‘json‘ , success: (res) => { resolve(res) }, fail: (res) => { reject(res) }, complete: (res) => { dd.hideLoading() } }) }) }, }) |
2.使用请求,在组件中请求数据
1 2 3 4 5 6 7 8 9 10 11 | let app = getApp() Component({ methods: { getData() { app.https( ‘GET‘ , ‘/api/v1/dailyTunneling?id=‘ + this .data.curTbmId + ‘&time=‘ + startdate).then(res => { console.log(res) }) }, }, }) |
原文:https://www.cnblogs.com/lonelyxmas/p/12423796.html