UNPKG

mtl-js-sdk

Version:

ynf-fw-mtl-api

242 lines (226 loc) 6.33 kB
/* * @Author: wangyingliang@yonyou.com * @Date: 2024-02-02 09:33:10 * @LastEditors: wangyingliang wangyingliang@yonyou.com * @LastEditTime: 2024-07-22 16:51:31 * @FilePath: /mtl-api-project/src/platforms/h5/unique.js * @Description: H5平台通用api * Copyright (c) 2024 by Yonyou, All Rights Reserved. */ import unique from '../../common/unique.js' import xaxios from 'axios' const FAIL_CODE = 1; const axios = xaxios?.default || xaxios const upesn = { mdfCustomScanQRCode(obj = {}) { var person = window.prompt("Please enter the scan result:"); if (!!!person) { obj.fail && obj.fail({ code: FAIL_CODE, message: 'result is null!' }); obj.complete && obj.complete({ code: FAIL_CODE, message: 'result is null!' }); } else { obj.success && obj.success({ resultStr: person }); obj.complete && obj.complete({ resultStr: person }); } }, downloadFile(obj = {}) { let { url } = obj; if (unique.isEmpty('url', url, obj)) { obj?.fail && obj?.fail({ code: "-1006", message: "error: url is null!" }) return; } window.open(url); obj?.success && obj?.success({}); obj?.complete && obj?.complete({}); }, /** * 文件上传 * @param {string} filePath 文件路径 * @param {string} url 上传服务器地址, 默认上传到文件服务. * @param {object} formData 上传文件的其他参数 * @param {object} header 自定义请求 Header */ uploadFile(obj = {}) { // 参数校验 if (!obj.url) { obj?.fail && obj?.fail({ code: "-1006", message: "error: url is null!" }) return } if (!obj?.filePath) { obj?.fail && obj?.fail({ code: "-1006", message: "error: filePath is null!" }) return } let url = obj.url let formData = obj?.formData || {} let header = obj?.header || { 'Content-Type': 'application/json;' } var param = { type: 'file', ...formData } axios({ headers: header, method: 'post', url, params: param, data: [obj.filePath] }).then(res => { let { status: code, statusText: message, data } = res; if (code === 200) { if (data && data.code == 0) { obj?.success && obj?.success({ pictures: data.data.files }) } else { obj?.fail && obj?.fail(data) } } else { obj?.fail && obj?.fail({ code, message, data }); } }).catch(err => { obj?.fail && obj?.fail(err) }) }, /** * 文件多个上传 * @param {string} filePaths 文件路径 * @param {string} url 上传服务器地址, 默认上传到文件服务. * @param {object} formData 上传文件的其他参数 * @param {object} header 自定义请求 Header */ uploadFiles(obj = {}) { // 参数校验 if (!obj.url) { obj?.fail && obj?.fail({ code: "-1006", message: "error: url is null!" }) return } if (!obj?.filePaths) { obj?.fail && obj?.fail({ code: "-1006", message: "error: filePaths is null!" }) return } let url = obj.url let formData = obj?.formData || {} let header = obj?.header || { 'Content-Type': 'application/json;' } var param = { type: 'file', ...formData } axios({ headers: header, method: 'post', url, params: param, data: obj.filePaths }).then(res => { let { status: code, statusText: message, data } = res; if (code === 200) { if (data && data.code == 0) { obj?.success && obj?.success({ pictures: data.data.files }) } else { obj?.fail && obj?.fail(data) } } else { obj?.fail && obj?.fail({ code, message, data }); } }).catch(err => { obj?.fail && obj?.fail(err) }) }, /** * 标准文件上传接口, 6.2.14版本开始启用 * @param {string} domain 上传服务域名, 默认是 window.location.origin. * @param {string} url 上传服务器地址, 默认上传到文件服务. * @param {File} filePath 文件路径 * @param {string} name filePath 对应的 key, 非必传 * @param {string} fileName 文件名称, 带后缀, 非必传 * @param {object} formData 其他额外参数. * @param {string} formData.businessType 领域类型, 需要单独到文件服务注册, 必传. * @param {string} formData.businessId 领域ID, 必传. * @param {string} formData.usePrivateBucket 是否使用私有桶, 必传. * @param {object} header 自定义请求 Header */ uploadToFileService(obj) { // 参数校验 if (!obj?.filePath) { obj?.fail && obj?.fail({ code: "-1004", message: "error: filePath is null!" }) return } // 不传 url 参数, 走文件服务时, 需要手动传 businessType. if (!obj?.url && !obj.formData.businessType) { obj?.fail && obj?.fail({ code: "-1004", message: "error: businessType is null!" }) return } let domain = obj?.domain || window.location.origin // 处理 domain / 兼容性问题 let fileServiceUrl = "iuap-apcom-file/rest/mtl/file/upload" if (domain.endsWith('/')) { fileServiceUrl = domain + fileServiceUrl } else { fileServiceUrl = domain + '/' + fileServiceUrl } let url = obj?.url || fileServiceUrl let formData = obj?.formData || {} let header = obj?.header || { 'Content-Type': 'application/json;' } if (window?.tnsSdk && window?.tnsSdk?.readXscfToken) { header['X-XSRF-TOKEN'] = window.tnsSdk.readXscfToken() } var param = { type: 'file', ...formData } let fileData = new FormData() if (unique.isBase64File(obj.filePath) || unique.isBase64Image(obj.filePath)) { var file = unique.base64ToFile(obj.filePath, obj?.fileName || new Date().getTime() + '.png'); fileData.append(obj?.name || 'file', file) } else { fileData.append(obj?.name || 'file', obj?.filePath) } axios({ headers: header, method: 'post', url, params: param, data: fileData }).then(res => { let { status: code, statusText: message, data } = res; if (code === 200) { obj?.success && obj?.success({ code, message, data }) } else { obj?.fail && obj?.fail({ code, message, data }); } }).catch(err => { obj?.fail && obj?.fail(err) }) }, }; const uniqueObj = unique.upesn; const arrayUpesn = Object.keys(upesn); const arrayObj = Object.keys(uniqueObj); arrayObj.forEach(function (key) { if (!arrayUpesn.find(element => element == key)) { upesn[key] = uniqueObj[key] } }) let exports = { upesn }; export default exports;