接下来开始写写上传图片的公用组件,可以自定义上传几张图片。

chooseImage文件夹里面的index.wxml和index.js,涉及图片上传,删除,预览。

<view class="img-v clearfix">
<view class="img-chooseImage" wx:for="{{imgs}}" wx:for-item="item" wx:key="*this">
<image src="{{item}}" data-index="{{index}}" mode="aspectFill" bindtap="previewImg" class="list-img"></image>
<view class="delete-btn" data-index="{{index}}" catchtap="deleteImg"><image src="../../../image/close.png"></image></view>
</view>
<view class="upload-img-btn" bindtap="chooseImg" hidden='{{ishide}}'><image class="add-img" src="../../../image/add-img.jpg"></image></view>
</view>
Component({
options: {
multipleSlots: true // 在组件定义时的选项中启用多slot支持
},
properties: {
count: String //父子传参
},
/**
* 页面的初始数据
*/
data: {
imgs: [],
count:,
ishide:false
},
// 上传图片
methods: {
chooseImg: function (e) {
var that = this;
var imgs = this.data.imgs;
if (imgs.length >= ) {
this.setData({
lenMore:
});
setTimeout(function () {
that.setData({
lenMore:
});
}, );
return false;
}
wx.chooseImage({
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
var tempFilePaths = res.tempFilePaths;
var imgs = that.data.imgs;
// console.log(tempFilePaths + '----');
console.log(that.data.count);
for (var i = ; i < tempFilePaths.length; i++) {
if (imgs.length > that.data.count-) {
that.setData({
imgs: imgs
});
return false;
} else {
imgs.push(tempFilePaths[i]);
if (imgs.length > that.data.count-){
that.setData({
ishide: true
});
}
}
}
// console.log(imgs);
that.setData({
imgs: imgs
});
that.triggerEvent("chooseImg");//触发回调
}
});
},
// 删除图片
deleteImg: function (e) {
var imgs = this.data.imgs;
var index = e.currentTarget.dataset.index;
imgs.splice(index, );
this.setData({
imgs: imgs
});
if (imgs.length < this.data.count) {
// console.log(imgs.length);
this.setData({
ishide: false
});
}
this.triggerEvent("deleteImg");//触发回调
},
// 预览图片
previewImg: function (e) {
//获取当前图片的下标
var index = e.currentTarget.dataset.index;
//所有图片
var imgs = this.data.imgs;
wx.previewImage({
//当前显示图片
current: imgs[index],
//所有图片
urls: imgs
})
}
}
})

index文件夹的index.json和index.wxml和index.js

{
"navigationBarTitleText": "图片上传",
"usingComponents": {
"pop": "../common/chooseImage/index",
"popnum": "../common/chooseImage/index"
}
}
 <view class="perfect-title">图片上传公用组件</view>
<view class="pop-box">
<view class="upload-title">营业执照原件(1张)</view>
<view style="margin:30rpx;">
<pop id="pop" bind:chooseImg="chooseImg" bind:deleteImg="deleteImg" bind:previewImg="previewImg" count="{{count}}"></pop>
</view>
</view>
<view class="pop-box">
<view class="upload-title">身份证原件正反面(2张)</view>
<view style="margin:30rpx;">
<popnum id="popnum" bind:chooseImg="chooseImgnum" bind:deleteImg="deleteImgnum" bind:previewImg="previewImg" count="{{countnum}}"></popnum>
</view>
</view>
<view class="btn-area" id="buttonContainer2">
<button type="primary" bindtap="submitBtn">确认</button>
</view>
Page({

  /**
* 页面的初始数据
*/
data: {
imgs:[],
imgsnum: [],
count:,
countnum:
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
//获得pop组件
this.pop = this.selectComponent("#pop");
this.popnum = this.selectComponent("#popnum");
},
chooseImg: function (e) {
// console.log(this.pop.data.imgs);
this.setData({
imgs: this.pop.data.imgs
})
},
deleteImg: function (e) {
// console.log(this.pop.data.imgs);
this.setData({
imgs: this.pop.data.imgs
})
},
chooseImgnum: function (e) {
// console.log(this.pop.data.imgs);
this.setData({
imgsnum: this.popnum.data.imgs
})
},
deleteImgnum: function (e) {
// console.log(this.pop.data.imgs);
this.setData({
imgsnum: this.popnum.data.imgs
})
},
submitBtn: function(){
console.log(this.data.imgs);
console.log(this.data.imgsnum);
}, /**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () { }
})

最新文章

  1. Python 【第六章】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
  2. CSS3动画几个平时没注意的属性
  3. 在Win Server 2012中安装.NET Framework 3.5的问题
  4. 页断裂(partial write)与doublewrite技术
  5. eclipse安装ADT
  6. Web应用程序项目以配置使用IIS。未找到Web服务器
  7. [IOS Delegate和协议]
  8. jquery ui和jquery easy ui的区别
  9. IE7中line-height垂直居中问题
  10. Metasploit命令大全
  11. 创建一个基本的 Win32 窗口
  12. canvas标签的使用
  13. 使用Django的时候,页面请求正常,也没有报任何错误,甚至连警告都没有的情况下,页面却还是原地不动或者闪一下或者无限显示加载动画的情况下的解决办法
  14. 动态规划-LIS1
  15. spring MVC处理请求过程
  16. python: 爬取[博海拾贝]图片脚本
  17. P1601 A+B Problem(高精)
  18. vue select的change事件,将点击过的城市名存在数组中,下次调用不需要再调用接口
  19. Plant Simulation常用命令
  20. 反编译APK文件的三种方法(转)

热门文章

  1. Check the port occupy on Mac OSX
  2. c++11 默认函数的控制
  3. Linux系统下安装jenkins使用
  4. linux运维、架构之路-HTTP服务
  5. 在使用mybatis中指定字段查询
  6. CSS/CSS3常用的样式
  7. Number theory
  8. HDU6415 Rikka with Nash Equilibrium
  9. JS-DOM Event
  10. Java常用工具——java包装类