ImageListViewWidget.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <div>
  3. <div
  4. v-for="item in newFieldValue.displayValue"
  5. :key="item"
  6. class="room_img"
  7. >
  8. <AuthImage
  9. :auth-src="getThumbnailImageSrc(item)"
  10. class="m-small-img"
  11. @click="previewImg(getImageSrc(item))"
  12. />
  13. </div>
  14. <Loading v-if="loading" />
  15. <Modal
  16. v-model:show="modal"
  17. :large="true"
  18. >
  19. <div class="modal-img-box">
  20. <AuthImage
  21. :auth-src="modalImgSrc"
  22. class="m-img"
  23. />
  24. </div>
  25. <template #header>图片查看器</template>
  26. </Modal>
  27. </div>
  28. </template>
  29. <script>
  30. import Common from '../../common/Common.js';
  31. import {notificationError} from '../../common/notification.js';
  32. import AuthImage from '../../widget/AuthImage.vue';
  33. export default {
  34. components: {
  35. AuthImage,
  36. },
  37. props: {
  38. className: {
  39. type: String,
  40. default: null,
  41. },
  42. field: {
  43. type: Object,
  44. default : function(){
  45. return null;
  46. },
  47. },
  48. fieldValue: {
  49. type: Object,
  50. default : function(){
  51. return null;
  52. },
  53. },
  54. },
  55. emits: ['valueChanged'],
  56. data: function () {
  57. this.Common = Common;
  58. return {
  59. newFieldValue: {
  60. displayValue: [],
  61. displayType: 'String',
  62. },
  63. modalImgSrc: null,
  64. loading: false,
  65. modal: false,
  66. };
  67. },
  68. mounted: function () {
  69. var _self = this;
  70. var displayValue = _self.fieldValue.displayValue;
  71. if (displayValue != undefined && displayValue.length > 0) {
  72. _self.newFieldValue.displayValue = displayValue[0].split(',');
  73. }
  74. },
  75. methods: {
  76. /**
  77. * 预览图片
  78. */
  79. previewImg: function (imgSrc) {
  80. this.modalImgSrc = imgSrc;
  81. this.modal = true;
  82. },
  83. /**
  84. * 删除图片
  85. * @param {String} item 图片Name
  86. */
  87. removeImage(item) {
  88. let _self = this;
  89. var index = _self.newFieldValue.displayValue.indexOf(item);
  90. if (index > -1) {
  91. _self.newFieldValue.displayValue.splice(index, 1);
  92. }
  93. var images = _self.newFieldValue.displayValue.join(',');
  94. _self.fieldValue.displayValue[0] = images;
  95. this.$emit('valueChanged', _self.fieldValue);
  96. },
  97. /**
  98. * 选择图片发生改变
  99. * @param {[type]} e [description]
  100. * @return {[type]} [description]
  101. */
  102. onFileChange(e) {
  103. var files = e.target.files || e.dataTransfer.files;
  104. if (!files.length)
  105. return;
  106. this.uploadImage(files[0]);
  107. },
  108. /**
  109. * 上传图片
  110. * @param {File} selectedFile 选择的文件
  111. */
  112. uploadImage: function (selectedFile) {
  113. var _self = this;
  114. if (selectedFile == undefined) {
  115. return;
  116. }
  117. if (!/image\/\w+/.test(selectedFile.type)) {
  118. alert('请确保文件为图像类型');
  119. return;
  120. }
  121. var formData = new FormData();
  122. formData.append('images', selectedFile);
  123. formData.append('className', _self.className);
  124. _self.loading=true;
  125. $.ajax({
  126. url: Common.getApiURL('file/imageUpload'),
  127. type: 'post',
  128. beforeSend: function (request) {
  129. Common.addTokenToRequest(request);
  130. },
  131. data: formData,
  132. contentType: false,
  133. processData: false,
  134. success: function (data) {
  135. _self.loading = false;
  136. if (data.errorCode == 0) {
  137. _self.addImgs(data.datas);
  138. }else{
  139. notificationError(data.errorMessage, '图片上传失败');
  140. }
  141. },
  142. error: function (XMLHttpRequest, textStatus, errorThrown) {
  143. _self.loading=false;
  144. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  145. },
  146. });
  147. },
  148. /**
  149. * 添加图片
  150. * @param {String} imageName 添加的图片名称
  151. */
  152. addImgs: function (imageNames) {
  153. var _self = this;
  154. if (_self.newFieldValue.displayValue == undefined) {
  155. _self.newFieldValue.displayValue = new Array();
  156. }
  157. if(imageNames != null){
  158. imageNames.forEach(element => {
  159. _self.newFieldValue.displayValue.push(element);
  160. });
  161. }
  162. var images = _self.newFieldValue.displayValue.join(',');
  163. _self.fieldValue.displayValue[0] = images;
  164. this.$emit('valueChanged', _self.fieldValue);
  165. },
  166. /**
  167. * 获取图片地址
  168. * @param {String} item 图片名称
  169. * @return {String} 图片URL地址
  170. */
  171. getImageSrc: function (item) {
  172. var _self = this;
  173. if (item != undefined && item != null) {
  174. return Common.getImageSrc(_self.className, item);
  175. } else {
  176. return '';
  177. }
  178. },
  179. /**
  180. * 获取图片缩略图地址
  181. * @param {String} item 图片名称
  182. * @return {String} 图片URL地址
  183. */
  184. getThumbnailImageSrc: function(item){
  185. var _self = this;
  186. if (item != undefined && item != null) {
  187. return Common.getThumbnailImageSrc(_self.className, item);
  188. } else {
  189. return '';
  190. }
  191. },
  192. },
  193. };
  194. </script>
  195. <style scoped>
  196. .inputfile {
  197. width: 0.1px;
  198. height: 0.1px;
  199. opacity: 0;
  200. overflow: hidden;
  201. position: absolute;
  202. z-index: -1;
  203. }
  204. .room_img {
  205. width: 150px;
  206. height: 100px;
  207. float: left;
  208. margin-right: 10px;
  209. }
  210. .room_img img {
  211. width: 100%;
  212. height: 100%;
  213. }
  214. .room_img span {
  215. position: relative;
  216. right: -130px;
  217. top: -100px;
  218. font-size: 20px;
  219. color: red;
  220. }
  221. .label-btn {
  222. color: white;
  223. background-color: #006699;
  224. display: inline-block;
  225. padding: 5px 15px;
  226. border-radius: 5px;
  227. }
  228. .label-btn:hover {
  229. color: white;
  230. background-color: #003399;
  231. display: inline-block;
  232. padding: 5px 15px;
  233. border-radius: 5px;
  234. cursor: pointer;
  235. }
  236. .modal-img-box {
  237. width: 100%;
  238. text-align: center;
  239. overflow: auto;
  240. }
  241. .m-small-img {
  242. cursor: pointer;
  243. }
  244. .m-img {
  245. width: 100%;
  246. }
  247. </style>