JVxeImageCell.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <div>
  3. <template v-if="hasFile" v-for="(file, fileKey) of [innerFile || {}]">
  4. <div :key="fileKey" style="position: relative;">
  5. <template v-if="!file || !(file['url'] || file['path'] || file['message'])">
  6. <a-tooltip :title="'请稍后: ' + JSON.stringify (file) + ((file['url'] || file['path'] || file['message']))">
  7. <a-icon type="loading"/>
  8. </a-tooltip>
  9. </template>
  10. <template v-else-if="file['path']">
  11. <img class="j-editable-image" :src="imgSrc" alt="无图片" @click="handleMoreOperation"/>
  12. </template>
  13. <a-tooltip v-else :title="file.message||'上传失败'" @click="handleClickShowImageError">
  14. <a-icon type="exclamation-circle" style="color:red;"/>
  15. </a-tooltip>
  16. <template style="width: 30px">
  17. <a-dropdown :trigger="['click']" placement="bottomRight" style="margin-left: 10px;">
  18. <a-tooltip title="操作">
  19. <a-icon
  20. v-if="file.status!=='uploading'"
  21. type="setting"
  22. style="cursor: pointer;"/>
  23. </a-tooltip>
  24. <a-menu slot="overlay">
  25. <a-menu-item v-if="originColumn.allowDownload !== false" @click="handleClickDownloadFile">
  26. <span><a-icon type="download"/>&nbsp;下载</span>
  27. </a-menu-item>
  28. <a-menu-item v-if="originColumn.allowRemove !== false" @click="handleClickDeleteFile">
  29. <span><a-icon type="delete"/>&nbsp;删除</span>
  30. </a-menu-item>
  31. <a-menu-item @click="handleMoreOperation(originColumn)">
  32. <span><a-icon type="bars"/> 更多</span>
  33. </a-menu-item>
  34. </a-menu>
  35. </a-dropdown>
  36. </template>
  37. </div>
  38. </template>
  39. <a-upload
  40. v-show="!hasFile"
  41. name="file"
  42. :data="{'isup': 1}"
  43. :multiple="false"
  44. :action="uploadAction"
  45. :headers="uploadHeaders"
  46. :showUploadList="false"
  47. v-bind="cellProps"
  48. @change="handleChangeUpload"
  49. >
  50. <a-button icon="upload">{{originColumn.btnText || '上传图片'}}</a-button>
  51. </a-upload>
  52. <j-file-pop ref="filePop" @ok="handleFileSuccess" :number="number"/>
  53. </div>
  54. </template>
  55. <script>
  56. import { getFileAccessHttpUrl } from '@api/manage'
  57. import JVxeCellMixins from '@/components/jeecg/JVxeTable/mixins/JVxeCellMixins'
  58. import { ACCESS_TOKEN } from '@/store/mutation-types'
  59. import JFilePop from '@/components/jeecg/minipop/JFilePop'
  60. import JVxeUploadCell from '@/components/jeecg/JVxeTable/components/cells/JVxeUploadCell'
  61. export default {
  62. name: 'JVxeImageCell',
  63. mixins: [JVxeCellMixins],
  64. components: {JFilePop},
  65. props: {},
  66. data() {
  67. return {
  68. innerFile: null,
  69. number:0
  70. }
  71. },
  72. computed: {
  73. /** upload headers */
  74. uploadHeaders() {
  75. let {originColumn: col} = this
  76. let headers = {}
  77. if (col.token === true) {
  78. headers['X-Access-Token'] = this.$ls.get(ACCESS_TOKEN)
  79. }
  80. return headers
  81. },
  82. /** 上传请求地址 */
  83. uploadAction() {
  84. if (!this.originColumn.action) {
  85. return window._CONFIG['domianURL'] + '/sys/common/upload'
  86. } else {
  87. return this.originColumn.action
  88. }
  89. },
  90. hasFile() {
  91. return this.innerFile != null
  92. },
  93. /** 预览图片地址 */
  94. imgSrc() {
  95. if (this.innerFile) {
  96. if (this.innerFile['url']) {
  97. return this.innerFile['url']
  98. } else if (this.innerFile['path']) {
  99. let path = this.innerFile['path'].split(',')[0]
  100. return getFileAccessHttpUrl(path)
  101. }
  102. }
  103. return ''
  104. },
  105. responseName() {
  106. if (this.originColumn.responseName) {
  107. return this.originColumn.responseName
  108. } else {
  109. return 'message'
  110. }
  111. },
  112. },
  113. watch: {
  114. innerValue: {
  115. immediate: true,
  116. handler() {
  117. if (this.innerValue) {
  118. this.innerFile = this.innerValue
  119. } else {
  120. this.innerFile = null
  121. }
  122. },
  123. },
  124. },
  125. methods: {
  126. // 点击更多按钮
  127. handleMoreOperation(originColumn) {
  128. //update-begin-author:wangshuai date:20201021 for:LOWCOD-969 判断传过来的字段是否存在number,用于控制上传文件
  129. if (originColumn.number) {
  130. this.number = originColumn.number
  131. } else {
  132. this.number = 0
  133. }
  134. //update-end-author:wangshuai date:20201021 for:LOWCOD-969 判断传过来的字段是否存在number,用于控制上传文件
  135. if(originColumn && originColumn.fieldExtendJson){
  136. let json = JSON.parse(originColumn.fieldExtendJson);
  137. this.number = json.uploadnum?json.uploadnum:0;
  138. }
  139. let path = ''
  140. if (this.innerFile) {
  141. path = this.innerFile.path
  142. }
  143. this.$refs.filePop.show('', path, 'img')
  144. },
  145. // 更多上传回调
  146. handleFileSuccess(file) {
  147. if (file) {
  148. this.innerFile.path = file.path
  149. this.handleChangeCommon(this.innerFile)
  150. }
  151. },
  152. // 弹出上传出错详细信息
  153. handleClickShowImageError() {
  154. let file = this.innerFile || null
  155. if (file && file['message']) {
  156. this.$error({title: '上传出错', content: '错误信息:' + file['message'], maskClosable: true})
  157. }
  158. },
  159. handleChangeUpload(info) {
  160. let {originColumn: col} = this
  161. let {file} = info
  162. let value = {
  163. name: file.name,
  164. type: file.type,
  165. size: file.size,
  166. status: file.status,
  167. percent: file.percent
  168. }
  169. if (file.response) {
  170. value['responseName'] = file.response[this.responseName]
  171. }
  172. if (file.status === 'done') {
  173. if (typeof file.response.success === 'boolean') {
  174. if (file.response.success) {
  175. value['path'] = file.response[this.responseName]
  176. this.handleChangeCommon(value)
  177. } else {
  178. value['status'] = 'error'
  179. value['message'] = file.response.message || '未知错误'
  180. }
  181. } else {
  182. // 考虑到如果设置action上传路径为非jeecg-boot后台,可能不会返回 success 属性的情况,就默认为成功
  183. value['path'] = file.response[this.responseName]
  184. this.handleChangeCommon(value)
  185. }
  186. } else if (file.status === 'error') {
  187. value['message'] = file.response.message || '未知错误'
  188. }
  189. this.innerFile = value
  190. },
  191. handleClickDownloadFile() {
  192. if (this.imgSrc) {
  193. window.open(this.imgSrc)
  194. }
  195. },
  196. handleClickDeleteFile() {
  197. this.handleChangeCommon(null)
  198. },
  199. },
  200. // 【组件增强】注释详见:JVxeCellMixins.js
  201. enhanced: {
  202. switches: {visible: true},
  203. getValue: value => JVxeUploadCell.enhanced.getValue(value),
  204. setValue: value => JVxeUploadCell.enhanced.setValue(value),
  205. }
  206. }
  207. </script>
  208. <style scoped lang="less">
  209. .j-editable-image {
  210. height: 32px;
  211. max-width: 100px !important;
  212. cursor: pointer;
  213. &:hover {
  214. opacity: 0.8;
  215. }
  216. &:active {
  217. opacity: 0.6;
  218. }
  219. }
  220. </style>