You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.8 KiB

3 years ago
const app = getApp();
import store from '@/store'
3 years ago
3 years ago
const formatTime = date => {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`;
};
3 years ago
const formatNumber = n => {
3 years ago
n = n.toString();
return n[1] ? n : `0${n}`;
};
3 years ago
3 years ago
function chooseImages(callback) {
console.log('weixinupload')
3 years ago
uni.chooseImage({
3 years ago
count: 1,
sourceType: ['album', 'camera'],
success: res => {
3 years ago
const tempFilePaths = res.tempFilePaths;
for (let i = 0; i < tempFilePaths.length; i++) {
uni.getImageInfo({
src: tempFilePaths[i],
success: image => {
uni.showLoading({
title: '图片上传中',
3 years ago
mask: true
});
uni.uploadFile({
url: `${app.globalData.baseURL}api/upload`,
file: image,
filePath: image.path,
header: {
Authorization: 'Bearer ' + uni.getStorageSync('login_status'),
3 years ago
},
name: 'file',
success: res => {
if (callback) {
callback(JSON.parse(res.data).link);
}
},
fail: err => {
uni.showToast({
title: '上传图片失败',
icon: 'none',
duration: 2000
});
},
complete: res => {
uni.hideLoading();
}
});
},
fail: err => {
uni.showToast({
title: '获取图片信息失败',
icon: 'none',
duration: 2000
});
}
});
}
3 years ago
}
3 years ago
});
3 years ago
}
function chooseImages2(callback) {
3 years ago
uni.chooseImage({
3 years ago
count: 3,
sourceType: ['album', 'camera'],
success: res => {
3 years ago
const tempFilePaths = res.tempFilePaths;
for (let i = 0; i < tempFilePaths.length; i++) {
uni.getImageInfo({
src: tempFilePaths[i],
success: image => {
uni.showLoading({
title: '图片上传中',
mask: true
});
uni.uploadFile({
url: `${app.globalData.baseURL}api/upload`,
file: image,
filePath: image.path,
header: {
Authorization: 'Bearer ' + uni.getStorageSync('login_status')
3 years ago
},
name: 'file',
success: res => {
if (callback) {
callback(JSON.parse(res.data).link);
}
},
fail: err => {
uni.showToast({
title: '上传图片失败',
icon: 'none',
duration: 2000
});
},
complete: res => {
uni.hideLoading();
}
});
},
fail: err => {
uni.showToast({
title: '获取图片信息失败',
icon: 'none',
duration: 2000
});
}
});
}
3 years ago
}
3 years ago
});
3 years ago
}
3 years ago
const getWeek = date => {
let timeDate = new Date(date.replace(/-/g, "/")); //兼容IOS
let month = timeDate.getMonth() + 1,
3 years ago
day = timeDate.getDay();
3 years ago
return addZero(month) + '.' + addZero(day) + ' ' + '周' + '日一二三四五六'.charAt(day);
};
const addZero = function (num) {
3 years ago
num = num.toString();
3 years ago
return num[1] ? num : '0' + num;
};
3 years ago
3 years ago
const setTime = val => {
3 years ago
let date = val.split(' ')[0];
let times = val.split(' ')[1];
3 years ago
let day = date.split('-')[1] + '.' + date.split('-')[2];
let time = times.split(':')[0] + ':' + times.split(':')[1];
return day + ' ' + time;
};
3 years ago
const formatRichText = (html) => {
let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
match = match
.replace(/style="[^"]+"/gi, "")
.replace(/style='[^']+'/gi, "");
match = match
.replace(/width="[^"]+"/gi, "")
.replace(/width='[^']+'/gi, "");
match = match
.replace(/height="[^"]+"/gi, "")
.replace(/height='[^']+'/gi, "");
return match;
});
newContent = newContent.replace(
/style="[^"]+"/gi,
function (match, capture) {
match = match
.replace(/width:[^;]+;/gi, "max-width:100%;")
.replace(/width:[^;]+;/gi, "max-width:100%;");
return match;
}
);
newContent = newContent.replace(
/\<img/gi,
'<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'
);
return newContent;
};
3 years ago
module.exports = {
3 years ago
formatTime,
chooseImages,
3 years ago
chooseImages2,
getWeek,
setTime,
formatRichText
3 years ago
};