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.
 
 
 
 
 
 

217 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-if="!readonly && imgList.length < Number(number)" @click="chooseFile" class="solids">
  22. <l-icon type="file" />
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. /**
  30. * 附件上传组件
  31. * 使用相机拍摄 和 从相册选图
  32. *
  33. * 注意:可以选择图片;如果选择文档的话则是从手机的‘文件管理’里面去选择,有可能会找不到文档;
  34. */
  35. export default {
  36. props: {
  37. number: {
  38. default: 1
  39. },
  40. readonly: {},
  41. value: {
  42. default: () => []
  43. },
  44. folderId: {},
  45. title: {},
  46. required: false
  47. },
  48. data() {
  49. return {
  50. isShow: false,
  51. imgList: [],
  52. }
  53. },
  54. methods: {
  55. chooseFile() {
  56. uni.chooseImage({
  57. count: Number(this.number),
  58. sizeType: ['original', 'compressed'],
  59. sourceType: ['album', 'camera'],
  60. success: ({ tempFilePaths,tempFiles }) => {
  61. tempFilePaths.forEach((item, i) => {
  62. this.chooseChangeback(tempFilePaths[i],tempFiles[i])
  63. })
  64. }
  65. })
  66. },
  67. delImg(index) {
  68. this.CONFIRM("", "是否确认删除?", true).then(res => {
  69. if(!res)return
  70. this.LOADING('正在删除…');
  71. const newList = JSON.parse(JSON.stringify(this.imgList));
  72. this.HTTP_POST('learun/adms/annexes/wxdelete', this.imgList[index].id, "文件删除失败").then((data) => {
  73. this.HIDE_LOADING();
  74. if (data) {
  75. newList.splice(index, 1);
  76. this.imgList = newList
  77. this.$emit("update:value", newList);
  78. this.$emit("input", newList);
  79. this.$emit("change");
  80. this.$emit("del");
  81. }
  82. })
  83. })
  84. },
  85. showfile() {
  86. return true;
  87. },
  88. async chooseChangeback(tempFilePaths, tempFiles) {
  89. // console.log(tempFilePaths,22)
  90. let uploadImageRes = await this.uploadImage(tempFilePaths, tempFiles ? tempFiles.name : "")
  91. let newList = this.imgList || []
  92. if (uploadImageRes) {
  93. //请求‘获取附件列表’接口
  94. let data = await this.FETCH_FILEList(uploadImageRes);
  95. // console.log(data,33)
  96. if(data){
  97. newList = data.map(t=>({
  98. id: t.F_Id,
  99. name: t.F_FileName,
  100. url: t.F_FilePath.substring(t.F_FilePath.indexOf('Resource')),
  101. type: t.F_FileType,
  102. folderid:t.F_FolderId,
  103. }))
  104. }
  105. }
  106. this.imgList = newList
  107. this.$emit("update:value", newList);
  108. this.$emit("input", newList);
  109. this.$emit("change", newList);
  110. },
  111. uploadImage(url, name) {
  112. if (!url) return
  113. // 文件上传
  114. return new Promise(async (reslove, reject) => {
  115. this.LOADING('正在上传…');
  116. let params = name ? {
  117. folderId: this.folderId,
  118. name
  119. } : {
  120. folderId: this.folderId
  121. }
  122. this.HTTP_UPLOAD2('learun/adms/annexes/wxuploadinsingle', url, params).then((data) => {
  123. this.HIDE_LOADING();
  124. if (data) {
  125. reslove(data)
  126. } else {
  127. reject('上传失败!')
  128. }
  129. })
  130. })
  131. },
  132. validate(array) {
  133. // let type = array.every(item=>{
  134. // return item.type && item.type.substring(0,6) == "image/"
  135. // })
  136. // if(!type){
  137. // this.TOAST('文件类型错误');
  138. // return false
  139. // }
  140. let size = array.every(item => {
  141. return item.size && item.size <= 200 * 1024 * 1024
  142. })
  143. if (!size) {
  144. this.TOAST('文件大小不得超过200M');
  145. return false
  146. }
  147. return true
  148. },
  149. isImage(type) {
  150. if (type && type.length) {
  151. return ["png", "jpg"].includes(type.substring(type.length - 3, type.length))
  152. } else {
  153. return false
  154. }
  155. },
  156. viewImg(item) {
  157. if (!this.isImage(item.type)) {
  158. window.location.href = this.CONFIG("webHost") + item.url
  159. } else {
  160. uni.previewImage({
  161. urls: [this.CONFIG('webHost') + item.url],
  162. current: this.CONFIG('webHost') + item.url
  163. });
  164. }
  165. },
  166. },
  167. created() {
  168. // console.log(this.value)
  169. this.imgList = JSON.parse(JSON.stringify(this.value.map(item => {
  170. return {
  171. id: item.F_Id,
  172. name: item.F_FileName,
  173. url: item.F_FilePath.substring(item.F_FilePath.indexOf('Resource')),
  174. type: item.F_FileType,
  175. folderid:item.F_FolderId,
  176. }
  177. })))
  178. this.$nextTick(() => {
  179. this.isShow = true
  180. })
  181. }
  182. };
  183. </script>
  184. <style scoped>
  185. .file-name {
  186. position: absolute;
  187. bottom: 0;
  188. width: 100%;
  189. color: #606266;
  190. font-size: 12px;
  191. text-align: center;
  192. background-color: rgba(255, 255, 255, 0.6);
  193. text-overflow: ellipsis;
  194. overflow: hidden;
  195. white-space: nowrap;
  196. }
  197. </style>