845d97a7416ec8aa33e355fe125dd627e2af9ce1.svn-base 7.0 KB

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