ImageListWidget.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <template>
  2. <div>
  3. <div :class="{'m-row' : hasImage}">
  4. <div class="row">
  5. <span class="col-md-12">
  6. <div
  7. v-for="item,index in newFieldValue.displayValue"
  8. :key="index"
  9. class="room_img"
  10. >
  11. <AuthImage
  12. class="m-small-img"
  13. :auth-src="getImageSrc(item)"
  14. @click="previewImg(getImageSrc(item))"
  15. />
  16. <span
  17. v-if="!readonly"
  18. class="glyphicon glyphicon-trash"
  19. @click="removeImage(item)"
  20. />
  21. </div>
  22. <div class="clearfix" />
  23. </span>
  24. </div>
  25. </div>
  26. <div id="app">
  27. <div>
  28. <input
  29. id="file"
  30. autocomplete="off"
  31. type="file"
  32. class="form-control"
  33. style="width: 100%;"
  34. :readonly="!readonly"
  35. multiple
  36. @change="onFileChange"
  37. />
  38. </div>
  39. <!-- <button class="btn btn-success" @click="toggleShow">选择图片</button>
  40. <my-upload field="img"
  41. v-model="show"
  42. :width="1024"
  43. :height="768"
  44. img-format="png"
  45. :className="className"
  46. @addImg="addImg"
  47. ></my-upload> -->
  48. </div>
  49. <Modal
  50. v-model:show="modal"
  51. :large="true"
  52. >
  53. <div class="modal-img-box">
  54. <AuthImage
  55. class="m-img"
  56. :auth-src="modalImgSrc"
  57. />
  58. </div>
  59. <template #header>图片查看器</template>
  60. </Modal>
  61. <Loading v-if="loading" />
  62. </div>
  63. </template>
  64. <script>
  65. import AuthImage from '../../widget/AuthImage.vue';
  66. import Common from '../../common/Common.js';
  67. import { Notify, Uuid } from 'pc-component-v3';
  68. export default {
  69. components: {
  70. AuthImage,
  71. },
  72. props: {
  73. readonly: {
  74. type: Boolean,
  75. default: null,
  76. },
  77. className: {
  78. type: String,
  79. default: null,
  80. },
  81. field: {
  82. type: Object,
  83. default : function(){
  84. return null;
  85. },
  86. },
  87. fieldValue: {
  88. type: Object,
  89. default : function(){
  90. return null;
  91. },
  92. },
  93. },
  94. emits: ['valueChanged'],
  95. data: function () {
  96. this.Common = Common;
  97. return {
  98. newFieldValue: {
  99. displayValue: [],
  100. displayType: 'String',
  101. },
  102. show: false,
  103. modalImgSrc: null,
  104. loading: false,
  105. modal: false,
  106. };
  107. },
  108. computed: {
  109. /**
  110. * 判断是否有图片
  111. * @return {Boolean} 是否有图片
  112. */
  113. hasImage: function () {
  114. if (this.newFieldValue != undefined &&
  115. this.newFieldValue.displayValue != undefined &&
  116. this.newFieldValue.displayValue[0] != undefined &&
  117. this.newFieldValue.displayValue[0].length > 0) {
  118. return true;
  119. } else {
  120. return false;
  121. }
  122. },
  123. },
  124. mounted: function () {
  125. var _self = this;
  126. var displayValue = _self.fieldValue.displayValue;
  127. if (displayValue != undefined &&
  128. displayValue.length > 0) {
  129. _self.newFieldValue.displayValue = displayValue[0].split(',');
  130. }
  131. },
  132. methods: {
  133. /**
  134. * 预览图片
  135. */
  136. previewImg: function (imgSrc) {
  137. this.modalImgSrc = imgSrc;
  138. this.modal = true;
  139. },
  140. /**
  141. * 删除图片
  142. * @param {String} item 图片Name
  143. */
  144. removeImage(item) {
  145. var _self = this;
  146. var index = _self.newFieldValue.displayValue.indexOf(item);
  147. if (index > -1) {
  148. _self.newFieldValue.displayValue.splice(index, 1);
  149. }
  150. var images = _self.newFieldValue.displayValue.join(',');
  151. _self.fieldValue.displayValue[0] = images;
  152. this.$emit('valueChanged', _self.fieldValue);
  153. },
  154. /**
  155. * 选择图片发生改变
  156. * @param {[type]} e [description]
  157. * @return {[type]} [description]
  158. */
  159. onFileChange(e) {
  160. const _self = this;
  161. var files = e.target.files || e.dataTransfer.files;
  162. if (!files.length)
  163. return;
  164. // 批量上传
  165. for (let i = 0; i < files.length; i++) {
  166. var reader = new FileReader();
  167. reader.onload = function (e) {
  168. _self.uploadImage(files[i],e.target.result);
  169. };
  170. reader.readAsDataURL(files[i]);
  171. }
  172. },
  173. /**
  174. * 上传图片
  175. * @param {File} selectedFile 选择的文件
  176. */
  177. uploadImage: function (selectedFile, element) {
  178. var _self = this;
  179. if (selectedFile == undefined) {
  180. return;
  181. }
  182. if (!/image\/\w+/.test(selectedFile.type)) {
  183. alert('请确保文件为图像类型');
  184. return;
  185. }
  186. if (selectedFile.size != null && selectedFile.size > 3000 * 1024) {
  187. Notify.error(_self.$t('lang.Notify.error'), _self.$t('lang.ImageListWidget.describe1'), true);
  188. element.value = '';
  189. return;
  190. }
  191. var formData = new FormData();
  192. formData.append('images', selectedFile);
  193. formData.append('className', _self.className);
  194. _self.loading=true;
  195. $.ajax({
  196. url: Common.getApiURL('file/imageUpload'),
  197. type: 'post',
  198. beforeSend: function (request) {
  199. Common.addTokenToRequest(request);
  200. },
  201. data: formData,
  202. contentType: false,
  203. processData: false,
  204. success: function (data) {
  205. _self.loading=false;
  206. console.log(data);
  207. if (data != 'error') {
  208. var imageName = data.substring(data.indexOf(':') + 1);
  209. _self.addImg(imageName);
  210. }
  211. },
  212. error: function (XMLHttpRequest, textStatus, errorThrown) {
  213. _self.loading=false;
  214. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  215. },
  216. });
  217. },
  218. /**
  219. * 添加图片
  220. * @param {String} imageName 添加的图片名称
  221. */
  222. addImg: function (imageName) {
  223. var _self = this;
  224. if (_self.newFieldValue.displayValue == undefined) {
  225. _self.newFieldValue.displayValue = new Array();
  226. }
  227. _self.newFieldValue.displayValue.push(imageName);
  228. var images = _self.newFieldValue.displayValue.join(',');
  229. _self.fieldValue.displayValue[0] = images;
  230. this.$emit('valueChanged', _self.fieldValue);
  231. _self.show = false;
  232. },
  233. /**
  234. * 获取图片地址
  235. * @param {String} item 图片名称
  236. * @return {String} 图片URL地址
  237. */
  238. getImageSrc: function (item) {
  239. var _self = this;
  240. if (item != undefined && item != null) {
  241. return Common.getImageSrc(_self.className, item);
  242. } else {
  243. return '';
  244. }
  245. },
  246. toggleShow() {
  247. this.show = !this.show;
  248. },
  249. },
  250. };
  251. </script>
  252. <style scoped>
  253. .inputfile {
  254. width: 0.1px;
  255. height: 0.1px;
  256. opacity: 0;
  257. overflow: hidden;
  258. position: absolute;
  259. z-index: -1;
  260. }
  261. .room_img {
  262. width: 150px;
  263. height: 100px;
  264. float: left;
  265. margin-right: 10px;
  266. }
  267. .room_img img {
  268. width: 100%;
  269. height: 100%;
  270. }
  271. .room_img span {
  272. position: relative;
  273. right: -125px;
  274. top: -95px;
  275. font-size: 20px;
  276. color: #43a51e;
  277. cursor: pointer;
  278. }
  279. .label-btn {
  280. color: white;
  281. background-color: #006699;
  282. display: inline-block;
  283. padding: 5px 15px;
  284. border-radius: 5px;
  285. }
  286. .label-btn:hover {
  287. color: white;
  288. background-color: #003399;
  289. display: inline-block;
  290. padding: 5px 15px;
  291. border-radius: 5px;
  292. cursor: pointer;
  293. }
  294. .modal-img-box {
  295. width: 100%;
  296. text-align: center;
  297. overflow: auto;
  298. }
  299. .m-small-img {
  300. cursor: pointer;
  301. }
  302. .m-img {
  303. width: 100%;
  304. }
  305. </style>