import api from '@/common/http/' import { axios } from '@/common/http/' import store from '@/store/index.js' // 获取用户信息 export const getUserInfo = () => { return new Promise((resolve, reject) => { api.get('/wechat/user/info').then((response) => { const { data } = response; uni.setStorageSync('recycle_mobile_user', data); resolve(data); }) .catch((error) => { reject(error); }); }); } const getUUID = function() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16) }) } const createName = function(name) { const date = Date.now() const uuid = getUUID() const fileSuffix = name.substring(name.lastIndexOf('.') + 1) return `${date}${uuid}.${fileSuffix}` } // 图片上传 export const uploadImg = async function(file) { uni.showLoading({ mask: true, }) //获取阿里oss上传参数 const par = await api.get('/common/oss/config') .then(res => { return res.data }).catch(err => { uni.hideLoading(); }) const fileKey = par.dir + createName(file.name); return new Promise((resolve, reject) => { const uploadTask = uni.uploadFile({ url: par.host, // header: { // "Content-Type": 'multipart/form-data', // }, name: 'file', formData: { ...par, name: file.name, key: fileKey }, filePath: file.path, success(res) { resolve({ data: { name: file.name, url: fileKey } }) }, fail(err) { reject(err) }, complete(res) { uni.hideLoading(); } }) uploadTask.onProgressUpdate((res) => { console.log('上传进度' + res.progress); uni.showLoading({ title: `已上传${res.progress}%` }); // console.log('已经上传的数据长度' + res.totalBytesSent); // console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend); // return res.progress // 测试条件,取消上传任务。 if (res.progress == 100) { // uploadTask.abort(); uni.hideLoading(); } }); }) } export const getArea = function(str) { let area = {} let index11 = 0 let index1 = str.indexOf("省") if (index1 == -1) { index11 = str.indexOf("自治区") if (index11 != -1) { area.Province = str.substring(0, index11 + 3) } else { area.Province = str.substring(0, 0) } } else { area.Province = str.substring(0, index1 + 1) } let index2 = str.indexOf("市") if (index11 == -1) { area.City = str.substring(index11 + 1, index2 + 1) } else { if (index11 == 0) { area.City = str.substring(index1 + 1, index2 + 1) } else { area.City = str.substring(index11 + 3, index2 + 1) } } let index3 = str.lastIndexOf("区") if (index3 == -1) { index3 = str.indexOf("县") area.Country = str.substring(index2 + 1, index3 + 1) } else { area.Country = str.substring(index2 + 1, index3 + 1) } return area; } // 图片上传 export const uploadImgs = async function(file) { uni.showLoading({ title: '上传中', mask: true, }); // 获取oss配置 const par = await axios({ url: '/common/oss/config', method: 'get', }) .then((res) => { return res.data; }) .catch((err) => { uni.hideLoading(); }); const fileKey = par.dir + createName(file.name); return new Promise((resolve, reject) => { uni.uploadFile({ url: par.host, // header: { // "Content-Type": 'multipart/form-data', // }, name: 'file', formData: { ...par, name: file.name, key: fileKey, }, filePath: file.path, success(res) { resolve({ url: fileKey, }); }, fail(err) { reject(err); }, complete(res) { uni.hideLoading(); }, }); }); }; export default { getUserInfo, uploadImg, getArea, uploadImgs };