效果图
数据:
依赖接口wx.upload、chooseImage与preview 数据请求通过LeanCloud完成
图片选择:
https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-picture.html#wxchooseimageobject
前端处理:
1.保存images数组为已选择图片 2.选择了更多图片后concat数组 3.预览图集 4.leancloud上传多图,目测顺序一致
js代码
const AV = require('../../../utils/av-weapp.js')
var that;
Page({
data: {
images: [],
uploadedImages: [],
imageWidth: getApp().screenWidth / 4 - 10
},
onLoad: function (options) {
that = this;
var objectId = options.objectId;
console.log(objectId);
},
chooseImage: function () {
wx.chooseImage({
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
var tempFilePaths = res.tempFilePaths;
console.log(tempFilePaths);
that.setData({
images: that.data.images.concat(tempFilePaths)
});
}
})
},
previewImage: function () {
wx.previewImage({
urls: that.data.images
});
},
submit: function () {
that.data.images.forEach(function (tempFilePath) {
new AV.File('file-name', {
blob: {
uri: tempFilePath,
},
}).save().then(
function(file) {
var uploadedImages = that.data.uploadedImages;
uploadedImages.push(file.url());
that.setData({
uploadedImages: uploadedImages
});
console.log(uploadedImages);
}
).catch(console.error);
});
wx.showToast({
title: '评价成功',
success: function () {
wx.navigateBack();
}
});
},
delete: function (e) {
var index = e.currentTarget.dataset.index;
var images = that.data.images;
images.splice(index, 1);
that.setData({
images: images
});
}
})
wxml代码
<view class="gallery">
<view class="item" wx:for="{{images}}" wx:key="">
<image style="width: {{imageWidth}}px; height: {{imageWidth}}px" src=" {{item}}" bindtap="previewImage" mode="aspectFill" />
<view class="delete" bindtap="delete" data-index="{{index}}"><image style="left: {{imageWidth / 2 - 10}}px;" src="/images/icon_delete.png" /></view>
</view>
<view class="item">
<image style="width: {{imageWidth}}px; height: {{imageWidth}}px" src="/images/icon_add.png" class="button-upload" bindtap="chooseImage" />
</view>
</view>
<button type="primary" bindtap="submit">提交</button>
wxss代码
.gallery {
display: flex;
justify-content: flex-start;
flex-wrap: wrap;
}
.item {
position: relative;
margin: 5px;
}
.delete {
position: absolute;
height: 20px;
bottom: 0;
width: 100%;
background: #ccc;
opacity: .8;
}
.delete image {
position: absolute;
width: 20px;
height: 20px;
}
源码下载:http://git.oschina.net/dotton/lendoo-wx,本文涉及代码存于/pages/member/evalute文件夹中。
|