gallery.gif數(shù)據(jù):依賴接口wx.upload、chooseImage與preview數(shù)據(jù)請(qǐng)求通過(guò)LeanCloud完成圖片選擇:https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-picture.html#wxchooseimageobject前端處理:1.保存images數(shù) ...

gallery.gif
數(shù)據(jù):
依賴接口wx.upload、chooseImage與preview
數(shù)據(jù)請(qǐng)求通過(guò)LeanCloud完成
圖片選擇:
https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-picture.html#wxchooseimageobject
前端處理:
1.保存images數(shù)組為已選擇圖片
2.選擇了更多圖片后concat數(shù)組
3.預(yù)覽圖集
4.leancloud上傳多圖,目測(cè)順序一致
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'], // 可以指定來(lái)源是相冊(cè)還是相機(jī),默認(rèn)二者都有success: function (res) { // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標(biāo)簽的src屬性顯示圖片var tempFilePaths = res.tempFilePaths; console.log(tempFilePaths);that.setData({ images: that.data.images.concat(tempFilePaths)});}})}, previewImage: function () { // 預(yù)覽圖集wx.previewImage({ urls: that.data.images});}, submit: function () { // 提交圖片,事先遍歷圖集數(shù)組that.data.images.forEach(function (tempFilePath) { new AV.File('file-name', { blob: { uri: tempFilePath,},}).save().then( // file => console.log(file.url())function(file) { // 先讀取var uploadedImages = that.data.uploadedImages;uploadedImages.push(file.url()); // 再寫(xiě)入that.setData({ uploadedImages: uploadedImages}); console.log(uploadedImages);}).catch(console.error);});wx.showToast({ title: '評(píng)價(jià)成功', 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代碼
/*畫(huà)廊*/.gallery { /*margin-bottom: 100rpx;*/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文件夾中。