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.
 
 
 
 
 
 

212 rivejä
5.2 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: {}
  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. this.chooseChangeback(tempFilePaths,tempFiles)
  62. }
  63. })
  64. },
  65. delImg(index) {
  66. this.CONFIRM("", "是否确认删除?", true).then(res => {
  67. if(!res)return
  68. this.LOADING('正在删除…');
  69. const newList = JSON.parse(JSON.stringify(this.imgList));
  70. this.HTTP_POST('/learun/adms/annexes/wxdelete', this.imgList[index].id, "文件删除失败").then((data) => {
  71. this.HIDE_LOADING();
  72. if (data) {
  73. newList.splice(index, 1);
  74. this.imgList = newList
  75. this.$emit("update:value", newList);
  76. this.$emit("input", newList);
  77. this.$emit("change");
  78. this.$emit("del");
  79. }
  80. })
  81. })
  82. },
  83. showfile() {
  84. return true;
  85. },
  86. async chooseChangeback(tempFilePaths, tempFiles) {
  87. let uploadImageRes = await this.uploadImage(tempFilePaths[0], tempFiles[0] ? tempFiles[0].name : "")
  88. let newList = this.imgList || []
  89. if (uploadImageRes) {
  90. //请求‘获取附件列表’接口
  91. let data = await this.FETCH_FILEList(uploadImageRes);
  92. if(data){
  93. newList = data.map(t=>({
  94. id: t.F_Id,
  95. name: t.F_FileName,
  96. url: t.F_FilePath.substring(t.F_FilePath.indexOf('Resource')),
  97. type: t.F_FileType,
  98. folderid:t.F_FolderId,
  99. }))
  100. }
  101. }
  102. this.imgList = newList
  103. this.$emit("update:value", newList);
  104. this.$emit("input", newList);
  105. this.$emit("change", newList);
  106. },
  107. uploadImage(url, name) {
  108. if (!url) return
  109. // 文件上传
  110. return new Promise(async (reslove, reject) => {
  111. this.LOADING('正在上传…');
  112. let params = name ? {
  113. folderId: this.folderId,
  114. name
  115. } : {
  116. folderId: this.folderId
  117. }
  118. this.HTTP_UPLOAD2('/learun/adms/annexes/wxuploadinsingle', url, params).then((data) => {
  119. this.HIDE_LOADING();
  120. if (data) {
  121. reslove(data)
  122. } else {
  123. reject('上传失败!')
  124. }
  125. })
  126. })
  127. },
  128. validate(array) {
  129. // let type = array.every(item=>{
  130. // return item.type && item.type.substring(0,6) == "image/"
  131. // })
  132. // if(!type){
  133. // this.TOAST('文件类型错误');
  134. // return false
  135. // }
  136. let size = array.every(item => {
  137. return item.size && item.size <= 200 * 1024 * 1024
  138. })
  139. if (!size) {
  140. this.TOAST('文件大小不得超过200M');
  141. return false
  142. }
  143. return true
  144. },
  145. isImage(type) {
  146. if (type && type.length) {
  147. return ["png", "jpg"].includes(type.substring(type.length - 3, type.length))
  148. } else {
  149. return false
  150. }
  151. },
  152. viewImg(item) {
  153. if (!this.isImage(item.type)) {
  154. window.location.href = this.CONFIG("webHost") + item.url
  155. } else {
  156. uni.previewImage({
  157. urls: [this.CONFIG('webHost') + item.url],
  158. current: this.CONFIG('webHost') + item.url
  159. });
  160. }
  161. },
  162. },
  163. created() {
  164. // console.log(this.value)
  165. this.imgList = JSON.parse(JSON.stringify(this.value.map(item => {
  166. return {
  167. id: item.F_Id,
  168. name: item.F_FileName,
  169. url: item.F_FilePath.substring(item.F_FilePath.indexOf('Resource')),
  170. type: item.F_FileType,
  171. folderid:item.F_FolderId,
  172. }
  173. })))
  174. this.$nextTick(() => {
  175. this.isShow = true
  176. })
  177. }
  178. };
  179. </script>
  180. <style scoped>
  181. .file-name {
  182. position: absolute;
  183. bottom: 0;
  184. width: 100%;
  185. color: #606266;
  186. font-size: 12px;
  187. text-align: center;
  188. background-color: rgba(255, 255, 255, 0.6);
  189. text-overflow: ellipsis;
  190. overflow: hidden;
  191. white-space: nowrap;
  192. }
  193. </style>