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.
 
 
 
 
 
 

218 lines
5.3 KiB

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