请求接口地址:https://doc.quickapp.cn/features/system/fetch.html?h=fetch
第一步,安装包:
npm install iconv-lite
async onInit() { var prompt = require(‘@system.prompt‘); { // 这是转换前的GBK var resultFetchOld = await fetch.fetch({ url: ‘http://ip.ws.126.net/ipquery‘, responseType: ‘text‘, method: ‘GET‘, headers: { ‘accept‘: ‘text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9‘, ‘cache-control‘: ‘no-cache‘, ‘pragma‘: ‘no-cache‘,}, }); console.log(‘显示转换前的结果: ‘, resultFetchOld.data.data, ‘4秒后显示转换后的结果‘); // 显示转换前的结果 setTimeout(function () { var htmlStr = ‘显示转换前的结果: ‘ + resultFetchOld.data.data + ‘,4秒后显示转换后的结果‘ prompt.showToast({ message: htmlStr }); }, 0); } var resultFetch = await fetch.fetch({ url: ‘http://ip.ws.126.net/ipquery‘, responseType: ‘arraybuffer‘, // responseType: ‘text‘, method: ‘GET‘, headers: { ‘accept‘: ‘text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9‘, ‘cache-control‘: ‘no-cache‘, ‘pragma‘: ‘no-cache‘, }, }); // console.log(‘resultFetch‘, resultFetch); // console.log(‘resultFetch.data.data‘, resultFetch.data.data) // console.log(‘resultFetch.data.data-stringify‘, JSON.stringify(resultFetch.data.data)); //这里是引入包 const Buffer = require(‘buffer‘).Buffer; // 这个包为 nodejs 自带,无需安装 const iconvLite = require(‘iconv-lite‘); // let b1 = new Uint8Array(ipHtmlResult.data.data); //Buffer.from(b1,‘hex‘)是把Uint8Array转化成Buffer类型数据 let htmlStr = iconvLite.decode(Buffer.from(resultFetch.data.data, ‘hex‘), ‘gbk‘); console.log(‘htmlStr‘, htmlStr); // 显示转换后的结果 setTimeout(function () { htmlStr = ‘显示转换后的结果: ‘ + htmlStr prompt.showToast({ message: htmlStr }); }, 4000); }
如果未开启 async 支持,请参考 https://doc.quickapp.cn/tutorial/framework/using-async.html?h=async