123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- 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
- };
|