|
- import API from '@/api/config.js'
- import config from '@/utils/config.js'
- import { getClient, getNonce, getVersion, guid } from '@/utils/util.js'
-
- let BASE_URL = config.BASE_URL
- // #ifdef H5
- BASE_URL = '/api'
- // #endif
-
- export function uploadImage(filePath, fileInfo = {}) {
- const token = uni.getStorageSync('token')
- let mac = uni.getStorageSync('mac')
- if (!mac) {
- mac = uni.getSystemInfoSync().deviceId || guid()
- uni.setStorageSync('mac', mac)
- }
-
- const { timeStr, nonce } = getNonce()
- const { version, version_code } = getVersion()
-
- return new Promise((resolve, reject) => {
- uni.uploadFile({
- url: `${BASE_URL}${API.INTERFACE_FILE_UPLOAD}`,
- filePath,
- name: 'file',
- header: {
- token,
- timestr: timeStr,
- nonce
- },
- formData: {
- token,
- mac,
- client: getClient(),
- source: uni.getStorageSync('source') || '',
- version,
- version_code,
- upload_type: 1,
- file_name: fileInfo.name || ''
- },
- success: (res) => {
- let data = {}
- try {
- data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
- } catch (e) {
- reject({ code: 1, msg: '上传结果解析失败' })
- return
- }
- if (data.code === 0) {
- resolve(data)
- } else {
- reject(data)
- }
- },
- fail: (e) => reject({ code: 1, msg: e.errMsg || '上传失败' })
- })
- })
- }
-
- /**
- * 把原图作为 multipart 文件直接提交给某个业务接口(不先传 CDN 再由后端回源下载一次)。
- * 公共参数与鉴权头和 utils/request.js 保持一致。
- * @param {string} url 接口路径,取自 api/config.js
- * @param {string} filePath 本地临时文件路径
- * @param {object} params 业务参数
- * @param {string} failMsg 解析失败时的兜底文案
- */
- function uploadWithForm(url, filePath, params = {}, failMsg = '上传结果解析失败') {
- const token = uni.getStorageSync('token')
- const uid = uni.getStorageSync('open_id')
- let mac = uni.getStorageSync('mac')
- if (!mac) {
- mac = uni.getSystemInfoSync().deviceId || guid()
- uni.setStorageSync('mac', mac)
- }
-
- const { timeStr, nonce } = getNonce()
- const { version, version_code } = getVersion()
- const formData = {
- token,
- uid,
- mac,
- base_timestamp: parseInt(Date.now() / 1000),
- client: getClient(),
- source: uni.getStorageSync('source') || '',
- client_ios: uni.getSystemInfoSync().platform === 'ios' ? 1 : 0,
- version,
- version_code,
- ...params
- }
-
- Object.keys(formData).forEach((key) => {
- if (formData[key] === '' || formData[key] === undefined || formData[key] === null) {
- delete formData[key]
- } else if (typeof formData[key] !== 'string') {
- formData[key] = String(formData[key])
- }
- })
-
- return new Promise((resolve, reject) => {
- uni.uploadFile({
- url: `${BASE_URL}${url}`,
- filePath,
- name: 'file',
- header: {
- token,
- timestr: timeStr,
- nonce
- },
- formData,
- success: (res) => {
- let data = {}
- try {
- data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
- } catch (e) {
- reject({ code: 1, msg: failMsg })
- return
- }
- if (data.code === 0) {
- resolve(data)
- return
- }
- reject(data)
- },
- fail: (e) => reject({ code: 1, msg: e.errMsg || '照片上传失败,请重试' })
- })
- })
- }
-
- /**
- * 证件照制作
- */
- export function createIdPhoto(filePath, params = {}) {
- return uploadWithForm(API.INTERFACE_IDPHOTO_POST_CREATE, filePath, params, '制作结果解析失败')
- }
-
- /**
- * 老照片修复/上色:提交后立即返回任务,结果由 /oldphoto/getdetail 轮询
- */
- export function createOldPhoto(filePath, params = {}) {
- return uploadWithForm(API.INTERFACE_OLDPHOTO_POST_CREATE, filePath, params, '提交结果解析失败')
- }
-
- /**
- * 形象照/职业照/写真:提交后立即返回任务(含 1~4 个子任务),结果由 /portrait/getdetail 轮询
- */
- export function createPortrait(filePath, params = {}) {
- return uploadWithForm(API.INTERFACE_PORTRAIT_POST_CREATE, filePath, params, '提交结果解析失败')
- }
|