Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

231 righe
5.9 KiB

  1. <template>
  2. <view class="cu-form-group">
  3. <view class="grid col-4 grid-square flex-sub">
  4. <view v-for="(item, index) in imgList" @tap="viewImg(item)" :key="index" class="bg-img">
  5. <image v-if="showfile()&&isImage(item.type)" :src="CONFIG('webHost') + item.url" mode="aspectFill">
  6. </image>
  7. <l-icon v-if="showfile()&&!isImage(item.type)" type="text" />
  8. <text class="file-name">{{item.name}}</text>
  9. <view v-if="!readonly" @tap.stop="delImg(index)" class="cu-tag bg-red"
  10. style="width: 18px; height: 18px; font-size: 24px">
  11. <l-icon type="close" style="width: 18px; height: 18px; font-size: 12px" />
  12. </view>
  13. </view>
  14. <view v-show="!readonly && imgList.length < Number(number)&&isShow" class="solids">
  15. <!-- @tap="chooseImg" -->
  16. <l-icon type="file" />
  17. <lsj-upload ref="lsjUpload" height="80px" width="100%" :size="20" :option="{}" :count="1"
  18. @change="chooseChange" style="opacity: 0;"></lsj-upload>
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. props: {
  26. number: {
  27. default: 1
  28. },
  29. readonly: {},
  30. value: {
  31. default: () => []
  32. },
  33. folderId: {},
  34. },
  35. data() {
  36. return {
  37. isShow: false,
  38. imgList: [],
  39. }
  40. },
  41. methods: {
  42. chooseChange(files) {
  43. let array = Array.from(files);
  44. if (array.length) {
  45. this.$refs.lsjUpload.clear()
  46. }
  47. let tempFilePaths = [],
  48. tempFiles = [];
  49. array.forEach(item => {
  50. tempFilePaths.push(item[1].path)
  51. tempFiles.push(item[1].file)
  52. })
  53. this.chooseChangeback(tempFilePaths, tempFiles)
  54. },
  55. delImg(index) {
  56. this.CONFIRM("", "是否确认删除?", true).then(res => {
  57. if(!res)return
  58. this.LOADING('正在删除…');
  59. const newList = JSON.parse(JSON.stringify(this.imgList));
  60. this.HTTP_POST('StuInfoFresh/deleteFiles', {
  61. id: this.imgList[index].id
  62. }, "文件删除失败").then((data) => {
  63. this.HIDE_LOADING();
  64. if (data) {
  65. newList.splice(index, 1);
  66. this.imgList = newList
  67. this.$emit("update:value", newList);
  68. this.$emit("input", newList);
  69. this.$emit("change");
  70. this.$emit("del");
  71. }
  72. })
  73. })
  74. },
  75. showfile() {
  76. return true;
  77. },
  78. async chooseChangeback(tempFilePaths, tempFiles) {
  79. // let {tempFilePaths,tempFiles} = res
  80. // if(!this.validate(tempFiles))return
  81. let uploadImageRes = await this.uploadImage(tempFilePaths[0], tempFiles[0] ? tempFiles[0].name : "")
  82. let newList = this.imgList || []
  83. if (uploadImageRes) {
  84. newList = JSON.parse(JSON.stringify(newList)).concat(uploadImageRes);
  85. }
  86. this.imgList = newList
  87. this.$emit("update:value", newList);
  88. this.$emit("input", newList);
  89. this.$emit("change", newList);
  90. // this.$emit("add");
  91. },
  92. uploadImage(url, name) {
  93. if (!url) return
  94. // 文件上传
  95. return new Promise(async (reslove, reject) => {
  96. this.LOADING('正在上传…');
  97. let params = name ? {
  98. folderId: this.folderId,
  99. name
  100. } : {
  101. folderId: this.folderId
  102. }
  103. this.HTTP_UPLOAD2('StuInfoFresh/upload', url, params).then((data) => {
  104. this.HIDE_LOADING();
  105. this.$refs.lsjUpload.show()
  106. if (data) {
  107. // this.HTTP_GET('StuInfoFresh/upload', {fileId:data})
  108. reslove([{
  109. id: data.F_Id,
  110. name: data.F_FileName,
  111. url: data.F_FilePath,
  112. type: data.F_FileType,
  113. }])
  114. } else {
  115. reject('上传失败!')
  116. }
  117. })
  118. })
  119. },
  120. validate(array) {
  121. // let type = array.every(item=>{
  122. // return item.type && item.type.substring(0,6) == "image/"
  123. // })
  124. // if(!type){
  125. // this.TOAST('文件类型错误');
  126. // return false
  127. // }
  128. let size = array.every(item => {
  129. return item.size && item.size <= 200 * 1024 * 1024
  130. })
  131. if (!size) {
  132. this.TOAST('文件大小不得超过200M');
  133. return false
  134. }
  135. return true
  136. },
  137. isImage(type) {
  138. if (type && type.length) {
  139. return ["png", "jpg"].includes(type.substring(type.length - 3, type.length))
  140. } else {
  141. return false
  142. }
  143. },
  144. viewImg(item) {
  145. if (!this.isImage(item.type)) {
  146. window.location.href = this.CONFIG("webHost") + item.url
  147. } else {
  148. uni.previewImage({
  149. urls: [this.CONFIG('webHost') + item.url],
  150. current: this.CONFIG('webHost') + item.url
  151. });
  152. }
  153. },
  154. // previewFile() {
  155. // var file = document.querySelector('input[type=file]').files[0];
  156. // var reader = new FileReader();
  157. // // fileReader.readAsDataURL(blob);
  158. // // fileReader.onerror = () => {
  159. // // reject(new Error('blobToBase64 error'));
  160. // // };
  161. // // var encodedData = window.btoa("Hello, world");
  162. // reader.onloadend = function () {
  163. // //$('#PhotoImg').attr('src', reader.result);
  164. // var postData = {
  165. // Base64Url: reader.result
  166. // }
  167. // this.HTTP_POST(config.webapi + "StuInfoFresh/savePhoto", postData, (data) => {
  168. // if (data) {
  169. // $('#Photo').val(data.AnnexesFileId);
  170. // $('#PhotoImg').attr('src', config.web + data.Url);
  171. // } else {
  172. // learun.layer.toast('采集照片信息失败!');
  173. // }
  174. // });
  175. // }
  176. // if (file) {
  177. // reader.readAsDataURL(file);
  178. // }
  179. // },
  180. },
  181. created() {
  182. console.log(this.value)
  183. this.imgList = JSON.parse(JSON.stringify(this.value.map(item => {
  184. return {
  185. id: item.F_Id,
  186. name: item.F_FileName,
  187. url: item.F_FilePath,
  188. type: item.F_FileType
  189. }
  190. })))
  191. this.$nextTick(() => {
  192. this.isShow = true
  193. })
  194. }
  195. };
  196. </script>
  197. <style scoped>
  198. .file-name {
  199. position: absolute;
  200. bottom: 0;
  201. width: 100%;
  202. color: #606266;
  203. font-size: 13px;
  204. text-align: center;
  205. background-color: rgba(255, 255, 255, 0.6);
  206. text-overflow: ellipsis;
  207. overflow: hidden;
  208. white-space: nowrap;
  209. }
  210. </style>