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.
 
 
 
 
 
 

201 lines
4.8 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()&&item&&(typeof item == "string"||isImage(item.type))" :src="item.url?CONFIG('webHost') + item.url:item" 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 uploadImageRes = await this.uploadImage(tempFilePaths[0], tempFiles[0] ? tempFiles[0].name : "")
  80. let newList = this.imgList || []
  81. if (uploadImageRes) {
  82. newList = JSON.parse(JSON.stringify(newList)).concat(uploadImageRes);
  83. }
  84. this.imgList = newList
  85. this.$emit("update:value", newList);
  86. this.$emit("input", newList);
  87. this.$emit("change", newList);
  88. // this.$emit("add");
  89. },
  90. uploadImage(url, name) {
  91. if (!url) return
  92. // 文件上传
  93. return new Promise(async (reslove, reject) => {
  94. this.LOADING('正在上传…');
  95. let params = name ? {
  96. folderId: this.folderId,
  97. name
  98. } : {
  99. folderId: this.folderId
  100. }
  101. this.HTTP_UPLOAD2('StuInfoFresh/upload', url, params).then((data) => {
  102. this.HIDE_LOADING();
  103. this.$refs.lsjUpload.show()
  104. if (data) {
  105. // this.HTTP_GET('StuInfoFresh/upload', {fileId:data})
  106. reslove([{
  107. id: data.F_Id,
  108. name: data.F_FileName,
  109. url: data.F_FilePath,
  110. type: data.F_FileType,
  111. }])
  112. } else {
  113. reject('上传失败!')
  114. }
  115. })
  116. })
  117. },
  118. validate(array) {
  119. // let type = array.every(item=>{
  120. // return item.type && item.type.substring(0,6) == "image/"
  121. // })
  122. // if(!type){
  123. // this.TOAST('文件类型错误');
  124. // return false
  125. // }
  126. let size = array.every(item => {
  127. return item.size && item.size <= 200 * 1024 * 1024
  128. })
  129. if (!size) {
  130. this.TOAST('文件大小不得超过200M');
  131. return false
  132. }
  133. return true
  134. },
  135. isImage(type) {
  136. if (type && type.length) {
  137. return ["png", "jpg"].includes(type.substring(type.length - 3, type.length))
  138. } else {
  139. return false
  140. }
  141. },
  142. viewImg(item) {
  143. if (!this.isImage(item.type)) {
  144. window.location.href = this.CONFIG("webHost") + item.url
  145. } else {
  146. uni.previewImage({
  147. urls: [this.CONFIG('webHost') + item.url],
  148. current: this.CONFIG('webHost') + item.url
  149. });
  150. }
  151. },
  152. },
  153. created() {
  154. console.log(this.value)
  155. this.imgList = JSON.parse(JSON.stringify(this.value.map(item => {
  156. return {
  157. id: item.F_Id,
  158. name: item.F_FileName,
  159. url: item.F_FilePath,
  160. type: item.F_FileType
  161. }
  162. })))
  163. this.$nextTick(() => {
  164. this.isShow = true
  165. })
  166. }
  167. };
  168. </script>
  169. <style scoped>
  170. .file-name {
  171. position: absolute;
  172. bottom: 0;
  173. width: 100%;
  174. color: #606266;
  175. font-size: 13px;
  176. text-align: center;
  177. background-color: rgba(255, 255, 255, 0.6);
  178. text-overflow: ellipsis;
  179. overflow: hidden;
  180. white-space: nowrap;
  181. }
  182. </style>