zhtx-uploadImg.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <view>
  3. <view class="zhtx-imgs">
  4. <view class="zhtx-single" v-for="(item, index) in list" :key="index" >
  5. <image :src="item" :data-src="item" mode="aspectFit" @tap="previewImg" />
  6. <view class="zhtx-del" @tap="deleteItem(index)">x</view>
  7. </view>
  8. <view v-if="limit>list.length" class="zhtx-single zhtx-addNew" @tap="chooseFile">
  9. <text class="zhtx-add">+</text>
  10. </view>
  11. </view>
  12. <view class="num">
  13. <text style="color: #007AFF;font-size: 14px;">{{list.length}}</text>
  14. /{{limit}}
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. let toast= msg=>{
  20. uni.showToast({
  21. title: msg,
  22. icon:'none'
  23. });
  24. }
  25. export default {
  26. props: {
  27. uImgList: {
  28. type: Array, //附件列表
  29. default () {
  30. return {}
  31. }
  32. },
  33. uploadFileUrl: {
  34. type: String,
  35. dafault: '#' // 上传文件的服务器url
  36. },
  37. header: {
  38. type: Object, //上传图片到服务器时,HTTP 请求 Header
  39. default () {
  40. return {}
  41. }
  42. },
  43. limit: {
  44. type: Number, //限制可上传的图片数量
  45. default: 9 //这里有问题???
  46. },
  47. fileKeyName: {
  48. type: String,
  49. default: 'file' //用于在服务端通过自定义key值获取该文件数据
  50. },
  51. canUploadFile: { //是否更新
  52. type: Boolean,
  53. default: true
  54. }
  55. },
  56. computed: {
  57. list: {
  58. get(){
  59. return this.uImgList
  60. }
  61. }
  62. },
  63. data() {
  64. return {
  65. imageList: [],
  66. };
  67. },
  68. methods: {
  69. //预览
  70. previewImg(e) {
  71. console.log(...this.list);
  72. uni.previewImage({
  73. current: e.target.dataset.src,
  74. loop: true,
  75. longPressActions: {
  76. itemList: ['发送给朋友', '保存图片', '收藏'],
  77. success:(data)=> {
  78. //可以自定义分享操作
  79. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  80. },
  81. fail: function(err) {
  82. console.log(err.errMsg);
  83. }
  84. },
  85. urls: this.list //this.imageList,保持删除了的不在
  86. });
  87. },
  88. //删除
  89. deleteItem(index) {
  90. uni.showModal({
  91. title: '提示',
  92. content: '确定要删除此项吗?',
  93. success: res => {
  94. if (res.confirm) {
  95. this.imageList.splice(index,1)
  96. this.list.splice(index, 1); //已经达到了数据更新的状态
  97. // this.$forceUpdate(); //强制更新
  98. this.$emit('update:uImgList', this.list); //类似双向数据绑定
  99. }
  100. }
  101. });
  102. },
  103. chooseFile() {
  104. //双重保证
  105. // console.log(this.list);
  106. if (this.list.length >= this.limit) {
  107. toast('已达到最大上传数量')
  108. return;
  109. }
  110. let canUploadFile = this.canUploadFile;
  111. let tempFiles;
  112. if (canUploadFile) {
  113. uni.chooseImage({
  114. count: this.limit - this.list.length,
  115. sizeType: ['original', 'compressed'],
  116. sourceType: ['album', 'camera'],
  117. success: (res) => {
  118. // console.log(res.tempFilePaths);
  119. tempFiles = res.tempFilePaths;
  120. this.imageList = this.imageList.concat(tempFiles)
  121. console.log(this.imageList);
  122. this.list.push(...tempFiles)//如果图片一次性就超过这个值怎么使他赋的值回退
  123. // #ifdef H5
  124. if (this.list.length >= this.limit) {
  125. this.list.splice(this.limit)
  126. toast('已达到最大上传数量')
  127. return;
  128. }
  129. // #endif
  130. this.$emit('update:uImgList', this.list); //类似双向数据绑定,更新数据, 使用.sync修饰
  131. this.$forceUpdate();
  132. console.log(this.list);
  133. },
  134. fail:err=>{
  135. console.log(err);
  136. }
  137. });
  138. }
  139. },
  140. upload(){
  141. let files=[];
  142. uni.showLoading({
  143. title: '上传中...',
  144. mask: false
  145. });
  146. //这里改成异步上传会不会更好(对于跨端请求,只能重复调用api)
  147. for(let i=0; i<this.list.length;i++){
  148. let path=this.list[i]
  149. let index=i.toString()
  150. let files={name:index,uri:path}
  151. console.log(path);
  152. uni.uploadFile({
  153. url: this.uploadFileUrl,
  154. name: this.fileKeyName,
  155. filePath: path, // 使用files上传数组列表,上面两者都会失效
  156. files:[ //使用files仅支持app与H5
  157. {name:index,uri:path}
  158. ],
  159. success:res=>{
  160. uni.hideLoading()
  161. console.log(res);
  162. this.$emit('uploadSuccess', res);
  163. if (res.statusCode == 200) {
  164. //上传成功将原信息,直接删除,
  165. this.list.splice(i,1)
  166. console.log(this.list);
  167. console.log(res);
  168. this.$forceUpdate();
  169. } else {
  170. uni.hideLoading()
  171. toast('上传失败,请稍后重试')
  172. }
  173. },
  174. fail:err=>{
  175. uni.hideLoading()
  176. toast(err.errMsg)
  177. console.log(err);
  178. }
  179. })
  180. }
  181. },
  182. }
  183. };
  184. </script>
  185. <style >
  186. .zhtx-imgs {
  187. display: flex;
  188. flex-wrap: wrap;
  189. justify-content: flex-start;
  190. align-items: center;
  191. }
  192. .zhtx-del {
  193. position: absolute;
  194. width: 35rpx;
  195. height: 35rpx;
  196. background: #f56c6c;
  197. color: #fff;
  198. top: 0;
  199. text-align: center;
  200. right: 0;
  201. line-height: 28rpx;
  202. font-size: 30rpx;
  203. z-index: 100;
  204. }
  205. .zhtx-single {
  206. position: relative;
  207. width: 180rpx;
  208. height: 180rpx;
  209. border: 1px solid #ccc;
  210. margin: 10rpx;
  211. }
  212. .zhtx-addNew {
  213. display: flex;
  214. justify-content: center;
  215. align-items: center;
  216. }
  217. text {
  218. font-size: 50rpx;
  219. color: #999;
  220. }
  221. image {
  222. width: 100%;
  223. height: 100%;
  224. display: block;
  225. }
  226. .num{
  227. float: right;
  228. color: #ccc;
  229. font-size: 12px;
  230. }
  231. .num::after{
  232. clear: both;
  233. }
  234. </style>