JImageUpload.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <div class="img">
  3. <a-upload
  4. name="file"
  5. listType="picture-card"
  6. :multiple="isMultiple"
  7. :action="uploadAction"
  8. :headers="headers"
  9. :data="{biz:bizPath}"
  10. :fileList="fileList"
  11. :beforeUpload="beforeUpload"
  12. :disabled="disabled"
  13. :isMultiple="isMultiple"
  14. @change="handleChange"
  15. @preview="handlePreview"
  16. :class="[!isMultiple?'imgupload':'', (!isMultiple && picUrl)?'image-upload-single-over':'' ]">
  17. <div>
  18. <!--<img v-if="!isMultiple && picUrl" :src="getAvatarView()" style="width:100%;height:100%"/>-->
  19. <div class="iconp">
  20. <a-icon :type="uploadLoading ? 'loading' : 'plus'" />
  21. <div class="ant-upload-text">{{ text }}</div>
  22. </div>
  23. </div>
  24. <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel()">
  25. <img alt="example" style="width: 100%" :src="previewImage"/>
  26. </a-modal>
  27. </a-upload>
  28. </div>
  29. </template>
  30. <script>
  31. import Vue from 'vue'
  32. import { ACCESS_TOKEN } from "@/store/mutation-types"
  33. import { getFileAccessHttpUrl } from '@/api/manage'
  34. const uidGenerator=()=>{
  35. return '-'+parseInt(Math.random()*10000+1,10);
  36. }
  37. const getFileName=(path)=>{
  38. if(path.lastIndexOf("\\")>=0){
  39. let reg=new RegExp("\\\\","g");
  40. path = path.replace(reg,"/");
  41. }
  42. return path.substring(path.lastIndexOf("/")+1);
  43. }
  44. export default {
  45. name: 'JImageUpload',
  46. data(){
  47. return {
  48. uploadAction:window._CONFIG['domianURL']+"/sys/common/upload",
  49. uploadLoading:false,
  50. picUrl:false,
  51. headers:{},
  52. fileList: [],
  53. previewImage:"",
  54. previewVisible: false,
  55. }
  56. },
  57. props:{
  58. text:{
  59. type:String,
  60. required:false,
  61. default:"上传"
  62. },
  63. /*这个属性用于控制文件上传的业务路径*/
  64. bizPath:{
  65. type:String,
  66. required:false,
  67. default:"temp"
  68. },
  69. value:{
  70. type:[String,Array],
  71. required:false
  72. },
  73. disabled:{
  74. type:Boolean,
  75. required:false,
  76. default: false
  77. },
  78. isMultiple:{
  79. type:Boolean,
  80. required:false,
  81. default: false
  82. },
  83. //update-begin-author:wangshuai date:20201021 for:LOWCOD-969 新增number属性,用于判断上传数量
  84. number:{
  85. type:Number,
  86. required:false,
  87. default:0
  88. }
  89. //update-end-author:wangshuai date:20201021 for:LOWCOD-969 新增number属性,用于判断上传数量
  90. },
  91. watch:{
  92. value: {
  93. handler(val,oldValue) {
  94. if (val instanceof Array) {
  95. this.initFileList(val.join(','))
  96. } else {
  97. this.initFileList(val)
  98. }
  99. if(!val || val.length==0){
  100. this.picUrl = false;
  101. }
  102. },
  103. //立刻执行handler
  104. immediate: true
  105. }
  106. },
  107. created(){
  108. const token = Vue.ls.get(ACCESS_TOKEN);
  109. this.headers = {"X-Access-Token":token}
  110. },
  111. methods:{
  112. initFileList(paths){
  113. if(!paths || paths.length==0){
  114. this.fileList = [];
  115. return;
  116. }
  117. this.picUrl = true;
  118. let fileList = [];
  119. let arr = paths.split(",")
  120. for(var a=0;a<arr.length;a++){
  121. let url = getFileAccessHttpUrl(arr[a]);
  122. fileList.push({
  123. uid: uidGenerator(),
  124. name: getFileName(arr[a]),
  125. status: 'done',
  126. url: url,
  127. response:{
  128. status:"history",
  129. message:arr[a]
  130. }
  131. })
  132. }
  133. this.fileList = fileList
  134. },
  135. beforeUpload: function(file){
  136. var fileType = file.type;
  137. if(fileType.indexOf('image')<0){
  138. this.$message.warning('请上传图片');
  139. return false;
  140. }
  141. },
  142. handleChange(info) {
  143. this.picUrl = false;
  144. let fileList = info.fileList
  145. //update-begin-author:wangshuai date:20201022 for:LOWCOD-969 判断number是否大于0和是否多选,返回选定的元素。
  146. if(this.number>0 && this.isMultiple){
  147. fileList = fileList.slice(-this.number);
  148. }
  149. //update-end-author:wangshuai date:20201022 for:LOWCOD-969 判断number是否大于0和是否多选,返回选定的元素。
  150. if(info.file.status==='done'){
  151. if(info.file.response.success){
  152. this.picUrl = true;
  153. fileList = fileList.map((file) => {
  154. if (file.response) {
  155. file.url = file.response.message;
  156. }
  157. return file;
  158. });
  159. }
  160. //this.$message.success(`${info.file.name} 上传成功!`);
  161. }else if (info.file.status === 'error') {
  162. this.$message.error(`${info.file.name} 上传失败.`);
  163. }else if(info.file.status === 'removed'){
  164. this.handleDelete(info.file)
  165. }
  166. this.fileList = fileList
  167. if(info.file.status==='done' || info.file.status === 'removed'){
  168. this.handlePathChange()
  169. }
  170. },
  171. // 预览
  172. handlePreview (file) {
  173. this.previewImage = file.url || file.thumbUrl
  174. this.previewVisible = true
  175. },
  176. getAvatarView(){
  177. if(this.fileList.length>0){
  178. let url = this.fileList[0].url
  179. return getFileAccessHttpUrl(url)
  180. }
  181. },
  182. handlePathChange(){
  183. let uploadFiles = this.fileList
  184. let path = ''
  185. if(!uploadFiles || uploadFiles.length==0){
  186. path = ''
  187. }
  188. let arr = [];
  189. if(!this.isMultiple && uploadFiles.length>0){
  190. arr.push(uploadFiles[uploadFiles.length-1].response.message)
  191. }else{
  192. for(let a=0;a<uploadFiles.length;a++){
  193. // update-begin-author:taoyan date:20200819 for:【开源问题z】上传图片组件 LOWCOD-783
  194. if(uploadFiles[a].status === 'done' ) {
  195. arr.push(uploadFiles[a].response.message)
  196. }else{
  197. return;
  198. }
  199. // update-end-author:taoyan date:20200819 for:【开源问题z】上传图片组件 LOWCOD-783
  200. }
  201. }
  202. if(arr.length>0){
  203. path = arr.join(",")
  204. }
  205. this.$emit('change', path);
  206. },
  207. handleDelete(file){
  208. //如有需要新增 删除逻辑
  209. console.log(file)
  210. },
  211. handleCancel() {
  212. this.close();
  213. this.previewVisible = false;
  214. },
  215. close () {
  216. },
  217. },
  218. model: {
  219. prop: 'value',
  220. event: 'change'
  221. }
  222. }
  223. </script>
  224. <style scoped>
  225. /* update--begin--autor:lvdandan-----date:20201016------for:j-image-upload图片组件单张图片详情回显空白
  226. * https://github.com/zhangdaiscott/jeecg-boot/issues/1810
  227. * https://github.com/zhangdaiscott/jeecg-boot/issues/1779
  228. */
  229. /deep/ .imgupload .iconp{padding:20px;}
  230. /* update--end--autor:lvdandan-----date:20201016------for:j-image-upload图片组件单张图片详情回显空白*/
  231. /deep/ .image-upload-single-over .ant-upload-select{display: none}
  232. </style>