|
- 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 || '上传失败' })
- })
- })
- }
|