unVideoAndimg.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <view style="display: flex; flex-wrap: wrap; ">
  3. <!--图片-->
  4. <view style="display:flex; border-radius: 20rpx; ">
  5. <view v-for="(item,index) in imageList" :key="index">
  6. <view style="position:relative;width:250rpx;height:200rpx; " >
  7. <image style="width:250rpx;height:200rpx; border-radius: 20rpx; " :src="item" :data-src="image" @tap="previewImage"></image>
  8. <view style="position:absolute; right: 0rpx; top: -10rpx;" @tap="delect(index)">
  9. <view style="width:34rpx;height:34rpx;background-color: #EF3124; border-radius: 50%; line-height: 34rpx; text-align: center;font-size:10rpx !important; color: #FFFFFF;" class="iconfont" mode="">&#xe603;</view>
  10. <!--这个图标是指,图片或者视频上传成功了,点击右上角叉号(也就是这个图标)可以删除 这里自己找一个合适的替换掉就好-->
  11. </view>
  12. </view>
  13. </view>
  14. <!--视频-->
  15. <view v-for="(item1, index1) in srcVideo" :key="index1">
  16. <view style="position:relative;width:250rpx;height:200rpx; border-radius: 20rpx;">
  17. <video style="width:250rpx;height:200rpx;" :src="item1"></video>
  18. <view style="position:absolute; right: -10rpx; top: -10rpx;" @tap="delectVideo(index1)">
  19. <view style="width:34rpx;height:34rpx;background-color: #EF3124; border-radius: 50%; line-height: 34rpx; text-align: center;font-size:10rpx !important; color: #FFFFFF;" class="iconfont" mode="">&#xe603;</view>
  20. </view>
  21. </view>
  22. </view>
  23. <view v-if="VideoOfImagesShow" @tap="chooseVideoImage" style=" border-radius: 20rpx; width: 250rpx;height:200rpx;background-color: #F1F1F1;display:flex; justify-content: center; align-items: center; flex-direction: column;">
  24. <view class="iconfont " style="font-size: 70rpx;" mode="">&#xe686;</view>
  25. <view style="font-size:30rpx;">拍照/视频</view>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. var sourceType = [['camera'], ['album'], ['camera', 'album']];
  32. export default{
  33. data(){
  34. return {
  35. VideoOfImagesShow: true, // 页面图片或视频数量超出后,拍照按钮隐藏
  36. imageList: [], //存放图片的地址
  37. srcVideo: [],//视频存放的地址
  38. sourceType: ['拍摄', '相册', '拍摄或相册'],
  39. sourceTypeIndex: 2,
  40. cameraList: [{ value: 'back', name: '后置摄像头', checked: 'true' }, { value: 'front', name: '前置摄像头' }],
  41. cameraIndex: 0,//上传视频时的数量
  42. }
  43. },
  44. onUnload() {
  45. (this.imageList = []),(this.sourceTypeIndex = 2),(this.sourceType = ['拍摄','相册','拍摄或相册']);
  46. },
  47. methods:{
  48. //点击上传图片或视频
  49. chooseVideoImage(){
  50. uni.showActionSheet({
  51. title: '选择上传类型',
  52. itemList: ['图片', '视频'],
  53. success: res => {
  54. console.log(res);
  55. if (res.tapIndex == 0) {
  56. this.chooseImages();
  57. } else {
  58. this.chooseVideo();
  59. }
  60. }
  61. });
  62. },
  63. //上传图片
  64. chooseImages(){
  65. uni.chooseImage({
  66. count:4, //默认是9张
  67. sizeType:['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  68. sourceType: ['album', 'camera'], //从相册选择
  69. success:res=>{
  70. this.imageList = this.imageList.concat(res.tempFilePaths);
  71. //console.log(this.imageList)
  72. if(this.imageList.length == 4){
  73. this.VideoOfImagesShow = false; //图片上传数量和count一样时,让点击拍照按钮消失
  74. }
  75. }
  76. })
  77. },
  78. //上传视频
  79. chooseVideo(index){
  80. uni.chooseVideo({
  81. maxDuration: 10,//拍摄视频最长拍摄时间,单位秒。最长支持 60 秒
  82. count: 4,
  83. camera: this.cameraList[this.cameraIndex].value,//'front'、'back',默认'back'
  84. sourceType: sourceType[this.sourceTypeIndex],
  85. success:res =>{
  86. this.srcVideo = this.srcVideo.concat(res.tempFilePath);
  87. if (this.srcVideo.length == 4) {
  88. this.VideoOfImagesShow = false;
  89. }
  90. console.log(this.srcVideo);
  91. }
  92. })
  93. },
  94. //预览图片
  95. previewImage: function(e){
  96. var current = e.target.dataset.src;
  97. uni.previewImage({
  98. current: current,
  99. urls: this.imageList
  100. });
  101. },
  102. // 删除图片
  103. delect(index) {
  104. uni.showModal({
  105. title: '提示',
  106. content: '是否要删除该图片',
  107. success: res => {
  108. if (res.confirm) {
  109. this.imageList.splice(index, 1);
  110. }
  111. if (this.imageList.length == 4) {
  112. this.VideoOfImagesShow = false;
  113. } else {
  114. this.VideoOfImagesShow = true;
  115. }
  116. }
  117. });
  118. },
  119. // 删除视频
  120. delectVideo(index) {
  121. uni.showModal({
  122. title: '提示',
  123. content: '是否要删除此视频',
  124. success: res => {
  125. if (res.confirm) {
  126. this.srcVideo.splice(index, 1);
  127. }
  128. if (this.srcVideo.length == 4) {
  129. this.VideoOfImagesShow = false;
  130. } else {
  131. this.VideoOfImagesShow = true;
  132. }
  133. }
  134. });
  135. },
  136. }
  137. }
  138. </script>
  139. <style lang="scss" scoped>
  140. </style>