UNPKG

autobots-lib

Version:

汽车人基础库

237 lines (228 loc) 7.69 kB
import { Toast, Native, QCRNetworkModule } from 'autobots-commonlib'; const NetworkModule = QCRNetworkModule /** * fetch方式-消息体为form类型 * @param url * @param params * @param config * @returns {Promise<unknown | [any,null]>} */ const fetchForm = (url, params = {}, config = {}) => { const options = { method: 'post', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' }, timeout: 15000, ...config }; return NetworkModule.fetch(Native.getPostUrl(url), options, params).then((result) => { //console.log("***--fetch--*** request----res="+result); const res = typeof (result) == "string" ? JSON.parse(result) : result; if (String(res.status) === '1' || String(res.code) === '0') { // 网络请求成功和业务数据成功 return ([null, res]); } else { // Toast.Show(res.data.info); return ([new Error('status不为1'), res]); } }).catch(error => { return [error, null]; }) }; /** * fetch方式-消息体为json类型 * @param url * @param params * @param config * @returns {Promise<unknown | [any,null]>} */ const fetchJson = (url, params = {}, config = {}) => { const options = { method: 'post', headers: { Accept: 'application/json', 'Content-Type': 'application/json' }, timeout: 15000, ...config }; return NetworkModule.fetch(Native.getPostUrl(url), options, params).then((result) => { const res = typeof (result) == "string" ? JSON.parse(result) : result; if (res.status === 1) { // 网络请求成功和业务数据成功 return [null, res.returnObject]; } else { return [new Error('status不为1'), res.info]; } }).catch(error => { console.log(url) console.log(error); Toast.Show('网络请求失败'); return [error, null]; }) }; /** * post方式-消息体为form类型 * @param url * @param params * @param header * @returns {Promise<any>} */ const postUseForm = (url, params = {}, header = {}) => { const eventId = url.split('?')[0]; let queryParam = sliptParams(url); let param = Object.assign(params, queryParam); let json = JSON.stringify(param); return NetworkModule.postForm(Native.getPostUrl(url), header, params).then((result) => { const res = typeof (result) == "string" ? JSON.parse(result) : result; if (String(res.status) === '1' || String(res.code) === '0') { // 网络请求成功和业务数据成功 return ([null, res]); } else { // Toast.Show(res.data.info); return ([new Error('status不为1'), res]); } }).catch(error => { return [error, null]; }) }; /** * post方式-消息体为json类型 * @param url * @param params * @param header * @returns {Promise<any>} */ const postUseJson = (url, params = {}, header = {}) => { //console.log("***--fetch--*** postUseJson----url="+Native.getPostUrl(url)); //console.log("***--fetch--*** postUseJson----params="+JSON.stringify(params)); return NetworkModule.postJson(Native.getPostUrl(url), header, params).then((result) => { //console.log("***--fetch--*** postUseJson----result="+result); const res = typeof (result) == "string" ? JSON.parse(result) : result; if (res.status === 1) { // 网络请求成功和业务数据成功 return [null, res.returnObject]; } else { //Toast.Show(res.data.info); return [new Error('status不为1'), res.info]; } }).catch(error => { console.log(url); console.log(error); Toast.Show('网络请求失败'); return [error, null]; }) }; /** * get方式 * @param url * @param body * @param empId * @returns {any} */ const httpGet = function (url, body, empId) { //增加汽车人网页传递参数 url = addUrlParams(url); if (body != {}) { var queryString = toQueryString(body); var urlStr = url + (url.indexOf("?") < 0 ? "?" : "&") + queryString; return NetworkModule.get(urlStr, { "auto-eid": empId, "Accept": "application/json;charset=UTF-8" }, {}).then(result => { const res = typeof (result) == "string" ? JSON.parse(result) : result; return res }); } }; const addUrlParams = function (url) { var c = Config.get(); return c && c.urlParams ? url + (url.indexOf("?") < 0 ? "?" : "&") + c.urlParams : url; }; const toQueryString = function (obj) { var ret = []; for (var key in obj) { key = encodeURIComponent(key); var values = obj[key]; if (values && values.constructor == Array) { //数组 var queryValues = []; for (var i = 0, len = values.length, value; i < len; i++) { value = values[i]; queryValues.push(toQueryPair(key, value)); } ret = ret.concat(queryValues); } else { //字符串 ret.push(toQueryPair(key, values)); } } return ret.join("&"); }; const toQueryPair = function (key, value) { if (typeof value == "undefined") { return key; } return key + "=" + encodeURIComponent(value === null ? "" : String(value)); }; /** * 下载文件方式 * @param url * @param fileName * @returns {Promise<any>} */ const downloadFile = (url, fileName = '') => { if (fileName.length == 0) { let timestamp = (new Date()).getTime();//获取当前时间错 let random = String(((Math.random() * 1000000) | 0))//六位随机数 fileName = `${timestamp + random}.jpg`; } console.log('---downloadFile---url=' + url); console.log('---downloadFile---fileName=' + fileName); return NetworkModule.downFile(url, fileName).then(res => { //res=storage/emulated/0/Android/data/com.autohome.helper/files/371be190c42a0125fd3f5c223835fcbf/1712193802418823497.jpg return res }).catch(error => { return error }) } /** * 上传文件方式 * @param url * @param pictures 路径数组,支持多文件上传 * @param empId * @returns {Promise<any>} */ const uploadImage = (url, pictures) => { let formList = []; pictures.map((val, idx) => { const splits = val.split('.'); const suffix = splits[splits.length - 1]; const fileName = idx + '.' + suffix; //fileKey,文件标识 let file = { uri: val, type: 'multipart/form-data', name: fileName, fileKey: 'fileData' }; formList.push(file); }); return NetworkModule.postFile(Native.getPostUrl(url), {}, {}, formList).then(res => { if (typeof (res) == 'string') { res = JSON.parse(res) } if (res.status === 1) { // 网络请求成功和业务数据成功 return [null, res.returnObject]; } else { // Toast.Show(res.data.info); let errorMessage = res.info ? res.info.replace(/\s+/g, "") : '' if (errorMessage && errorMessage.length > 0) { Toast.Show(errorMessage); } return [new Error('status不为1'), null]; } }).catch(error => { console.log('url === ' + url) console.log(error); Toast.Show('网络请求失败'); return [error, null]; }); } module.exports = { fetchForm, fetchJson, postUseForm, postUseJson, httpGet, downloadFile, uploadFile };