uniapp 开发微信小程序
大约 1 分钟
uniapp 开发微信小程序
格式化当前日期
async getDate() {
var t = new Date(),
e = (t.getFullYear(), t.getMonth() + 1),
o = t.getDate(),
a = t.getHours(),
n = t.getMinutes(),
i = (t.getSeconds(), e + "月" + o + "日"),
s = [a, n].map(this.formatNumber).join(":");
console.log(i), console.log(s),
this.setData({
date: i,
time: s
});
},
用时间来拼凑路径
var path = imgs[i].url;
var extension = path.substring(path.lastIndexOf('.') + 1)
var random = String(Math.random()*100000).split('.')[0]
var now = new Date()
var year = now.getFullYear()
var month = (now.getMonth() + 1) < 10?'0'+(now.getMonth() + 1):(now.getMonth() + 1)
var day = now.getDate() < 10?'0'+now.getDate():now.getDate()
var hours = now.getHours() < 10?'0'+now.getHours():now.getHours()
var minutes = now.getMinutes() < 10?'0'+now.getMinutes():now.getMinutes()
var seconds = now.getSeconds() < 10?'0'+now.getSeconds():now.getSeconds()
var cloudPath = 'wall'+'/'+year+'/'+month+'/'+day+'/'+hours+minutes+seconds+'_'+random+'.'+extension
格式化数字为最低报错两位数
formatNumber: function(t) {
return (t = t.toString())[1] ? t : "0" + t;
},
uniapp
上传图片到uniCloud
云存储
利用uview写的css部分(具体参数查看uview)
<u-upload
ref="uUpload"
action=""
max-count="100"
:file-list="''"
:auto-upload="false"
:uploadText="cover.mode == 0 ? '上传壁纸' : '上传图片'"
:width="cover.mode == 0 ? 203 : 203"
:height="cover.mode == 0 ? 325 : 203"
:maxSize="10 * 1024 * 1024"
:show-progress="false"
:size-type="['original']"
:source-type="['album']"
:multiple="true"
del-icon="close"
del-bg-color="#ff7c7c"
del-color="#FFFFFF"
:limitType="['png', 'jpg', 'jpeg']"
></u-upload>
JavaScript部分
async submit() {
var that = this;
var imgs = that.$refs.uUpload.lists;
await uniCloud.uploadFile({
filePath: path, // 文件路径
cloudPath: cloudPath // 存入云存储的路径
}).then(async res => {
console.log(res,'resUploadFile')
await uniCloud.getTempFileURL({ // 根据返回的id 来获取云存储上的路径
fileList: [res.fileID]
}).then(res => {
console.log(res,'getTempFileURL')
if(res.fileList.length > 0){
imgPaths.push(res.fileList[0].tempFileURL)
}else{
that.$u.toast("图片上传失败");
return;
}
});
})
}