You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

296 lines
7.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. ceshi() {
  121. function apiFn(params) {
  122. return new Promise((resolve, reject) => {
  123. this.LOADING('正在上传…');
  124. this.HTTP_UPLOAD2('StuInfoFresh/upload', params.url, {
  125. folderId: this.folderId
  126. }).then((data) => {
  127. this.HIDE_LOADING();
  128. if (data) {
  129. // this.HTTP_GET('StuInfoFresh/upload', {fileId:data})
  130. this.imgList[params.index] = [{
  131. id: data.F_Id,
  132. name: data.F_FileName,
  133. url: data.F_FilePath,
  134. type: data.F_FileType
  135. }]
  136. reslove(this.imgList[params.index])
  137. } else {
  138. reject('上传失败!')
  139. }
  140. })
  141. })
  142. }
  143. let array = this.imgList.map(item => {
  144. if (item.id) {
  145. return ""
  146. } else {
  147. return {
  148. url: "learun/adms/annexes/upload"
  149. }
  150. }
  151. })
  152. this.promiseAllLimit(2, [1, 2, 3, 4, 5, 6, 7, 8, 9], a).then(res => {
  153. console.log(res)
  154. })
  155. },
  156. /**
  157. * @description 控制promise.all并发数量
  158. * @param limit 并发数
  159. * @param array 参数列表
  160. * @param apiFn 执行函数
  161. * @returns {Promise<Awaited<unknown>[]>}
  162. */
  163. async promiseAllLimit(limit, array, apiFn) {
  164. const ret = [] // 用于存放所有的promise实例
  165. const executing = [] // 用于存放目前正在执行的promise
  166. for (const item of array) {
  167. const p = apiFn(item)
  168. ret.push(p)
  169. if (limit <= array.length) {
  170. // then回调中,当这个promise状态变为fulfilled后,将其从正在执行的promise列表executing中删除
  171. const e = p.then(() => executing.splice(executing.indexOf(e), 1))
  172. executing.push(e)
  173. if (executing.length >= limit) {
  174. // 一旦正在执行的promise列表数量等于限制数,就使用Promise.race等待某一个promise状态发生变更,
  175. // 状态变更后,就会执行上面then的回调,将该promise从executing中删除,
  176. // 然后再进入到下一次for循环,生成新的promise进行补充
  177. await Promise.race(executing)
  178. }
  179. }
  180. }
  181. return Promise.all(ret)
  182. },
  183. validate(array) {
  184. // let type = array.every(item=>{
  185. // return item.type && item.type.substring(0,6) == "image/"
  186. // })
  187. // if(!type){
  188. // this.TOAST('文件类型错误');
  189. // return false
  190. // }
  191. let size = array.every(item => {
  192. return item.size && item.size <= 200 * 1024 * 1024
  193. })
  194. if (!size) {
  195. this.TOAST('文件大小不得超过200M');
  196. return false
  197. }
  198. return true
  199. },
  200. isImage(type) {
  201. if (type && type.length) {
  202. return ["png", "jpg"].includes(type.substring(type.length - 3, type.length))
  203. } else {
  204. return false
  205. }
  206. },
  207. viewImg(item) {
  208. if (!this.isImage(item.type)) {
  209. window.location.href = this.CONFIG("webHost") + item.url
  210. } else {
  211. uni.previewImage({
  212. urls: [this.CONFIG('webHost') + item.url],
  213. current: this.CONFIG('webHost') + item.url
  214. });
  215. }
  216. },
  217. // previewFile() {
  218. // var file = document.querySelector('input[type=file]').files[0];
  219. // var reader = new FileReader();
  220. // // fileReader.readAsDataURL(blob);
  221. // // fileReader.onerror = () => {
  222. // // reject(new Error('blobToBase64 error'));
  223. // // };
  224. // // var encodedData = window.btoa("Hello, world");
  225. // reader.onloadend = function () {
  226. // //$('#PhotoImg').attr('src', reader.result);
  227. // var postData = {
  228. // Base64Url: reader.result
  229. // }
  230. // this.HTTP_POST(config.webapi + "StuInfoFresh/savePhoto", postData, (data) => {
  231. // if (data) {
  232. // $('#Photo').val(data.AnnexesFileId);
  233. // $('#PhotoImg').attr('src', config.web + data.Url);
  234. // } else {
  235. // learun.layer.toast('采集照片信息失败!');
  236. // }
  237. // });
  238. // }
  239. // if (file) {
  240. // reader.readAsDataURL(file);
  241. // }
  242. // },
  243. },
  244. created() {
  245. console.log(this.value)
  246. this.imgList = JSON.parse(JSON.stringify(this.value.map(item => {
  247. return {
  248. id: item.F_Id,
  249. name: item.F_FileName,
  250. url: item.F_FilePath,
  251. type: item.F_FileType
  252. }
  253. })))
  254. this.$nextTick(() => {
  255. this.isShow = true
  256. })
  257. }
  258. };
  259. </script>
  260. <style scoped>
  261. .file-name {
  262. position: absolute;
  263. bottom: 0;
  264. width: 100%;
  265. color: #606266;
  266. font-size: 13px;
  267. text-align: center;
  268. background-color: rgba(255, 255, 255, 0.6);
  269. text-overflow: ellipsis;
  270. overflow: hidden;
  271. white-space: nowrap;
  272. }
  273. </style>