ImageListWidget.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <div>
  3. <div v-for="item in images" :key="item" class="room_img">
  4. <AuthImage :auth-src="getThumbnailImageSrc(item)" />
  5. <span class="glyphicon glyphicon-trash" @click="removeImage(item)" />
  6. </div>
  7. <div>
  8. <div>
  9. <label for="workflowFile" class="label-btn"> {{ $t("lang.ImageListWidget.selectPicture") }} </label>
  10. <input
  11. id="workflowFile"
  12. autocomplete="off"
  13. type="file"
  14. class="inputfile"
  15. @change="onFileChange"
  16. />
  17. </div>
  18. </div>
  19. <Loading v-if="loading" />
  20. </div>
  21. </template>
  22. <script>
  23. import AuthImage from '../widget/AuthImage.vue';
  24. import {notificationError} from '../common/notification.js';
  25. import Common from '../common/Common.js';
  26. export default {
  27. components: {
  28. AuthImage,
  29. },
  30. props: {
  31. className: {
  32. type: String,
  33. default: null,
  34. },
  35. },
  36. data: function () {
  37. this.Common = Common;
  38. return {
  39. images: [],
  40. loading: false,
  41. };
  42. },
  43. methods: {
  44. /**
  45. * 删除图片
  46. * @param {String} item 图片Name
  47. */
  48. removeImage(item) {
  49. var _self = this;
  50. var index = _self.images.indexOf(item);
  51. if (index > -1) {
  52. _self.images.splice(index, 1);
  53. }
  54. },
  55. /**
  56. * 选择图片发生改变
  57. * @param {[type]} e [description]
  58. * @return {[type]} [description]
  59. */
  60. onFileChange(e) {
  61. var files = e.target.files || e.dataTransfer.files;
  62. if (!files.length) return;
  63. this.uploadImage(files[0]);
  64. },
  65. /**
  66. * 上传图片
  67. * @param {File} selectedFile 选择的文件
  68. */
  69. uploadImage: function (selectedFile) {
  70. var _self = this;
  71. if (selectedFile == undefined) {
  72. return;
  73. }
  74. if (!/image\/\w+/.test(selectedFile.type)) {
  75. alert('请确保文件为图像类型');
  76. return;
  77. }
  78. var formData = new FormData();
  79. formData.append('images', selectedFile);
  80. formData.append('className', _self.className);
  81. _self.loading=true;
  82. $.ajax({
  83. url: Common.getApiURL('file/imageUpload'),
  84. type: 'post',
  85. beforeSend: function (request) {
  86. Common.addTokenToRequest(request);
  87. },
  88. data: formData,
  89. contentType: false,
  90. processData: false,
  91. success: function (data) {
  92. _self.loading=false;
  93. console.log(data);
  94. if (data.errorCode == 0) {
  95. _self.addImgs(data.datas);
  96. }else{
  97. notificationError(data.errorMessage, '图片上传失败');
  98. }
  99. },
  100. error: function (XMLHttpRequest, textStatus, errorThrown) {
  101. _self.loading=false;
  102. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  103. },
  104. });
  105. },
  106. /**
  107. * 添加图片
  108. * @param {String} imageName 添加的图片名称
  109. */
  110. addImgs: function (imageNames) {
  111. var _self = this;
  112. if(imageNames != null){
  113. imageNames.forEach(element => {
  114. _self.images.push(element);
  115. });
  116. }
  117. },
  118. /**
  119. * 获取图片地址
  120. * @param {String} item 图片名称
  121. * @return {String} 图片URL地址
  122. */
  123. getImageSrc: function (item) {
  124. var _self = this;
  125. if (item != undefined && item != null) {
  126. return Common.getImageSrc(_self.className, item);
  127. } else {
  128. return '';
  129. }
  130. },
  131. /**
  132. * 获取图片缩略图地址
  133. * @param {String} item 图片名称
  134. * @return {String} 图片URL地址
  135. */
  136. getThumbnailImageSrc: function(item){
  137. var _self = this;
  138. if (item != undefined && item != null) {
  139. return Common.getThumbnailImageSrc(_self.className, item);
  140. } else {
  141. return '';
  142. }
  143. },
  144. /**
  145. * 获取图片路径供外部调用
  146. * @return {Array} 图片地址
  147. */
  148. getImages: function () {
  149. return this.images;
  150. },
  151. },
  152. };
  153. </script>
  154. <style scoped>
  155. .inputfile {
  156. width: 0.1px;
  157. height: 0.1px;
  158. opacity: 0;
  159. overflow: hidden;
  160. position: absolute;
  161. z-index: -1;
  162. }
  163. .room_img {
  164. width: 150px;
  165. height: 100px;
  166. float: left;
  167. margin-right: 10px;
  168. }
  169. .room_img img {
  170. width: 100%;
  171. height: 100%;
  172. }
  173. .room_img span {
  174. position: relative;
  175. right: -125px;
  176. top: -95px;
  177. font-size: 20px;
  178. color: #43a51e;
  179. cursor: pointer;
  180. }
  181. .label-btn {
  182. color: white;
  183. background-color: #006699;
  184. display: inline-block;
  185. padding: 5px 15px;
  186. border-radius: 5px;
  187. }
  188. .label-btn:hover {
  189. color: white;
  190. background-color: #003399;
  191. display: inline-block;
  192. padding: 5px 15px;
  193. border-radius: 5px;
  194. cursor: pointer;
  195. }
  196. </style>