| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <template>
- <div>
- <div
- v-for="item in newFieldValue.displayValue"
- :key="item"
- class="room_img"
- >
- <AuthImage
- :auth-src="getThumbnailImageSrc(item)"
- class="m-small-img"
- @click="previewImg(getImageSrc(item))"
- />
- </div>
- <Loading v-if="loading" />
- <Modal
- v-model:show="modal"
- :large="true"
- >
- <div class="modal-img-box">
- <AuthImage
- :auth-src="modalImgSrc"
- class="m-img"
- />
- </div>
- <template #header>图片查看器</template>
- </Modal>
- </div>
- </template>
- <script>
- import Common from '../../common/Common.js';
- import {notificationError} from '../../common/notification.js';
- import AuthImage from '../../widget/AuthImage.vue';
- export default {
- components: {
- AuthImage,
- },
-
- props: {
- className: {
- type: String,
- default: null,
- },
- field: {
- type: Object,
- default : function(){
- return null;
- },
- },
- fieldValue: {
- type: Object,
- default : function(){
- return null;
- },
- },
- },
-
- emits: ['valueChanged'],
- data: function () {
- this.Common = Common;
- return {
- newFieldValue: {
- displayValue: [],
- displayType: 'String',
- },
- modalImgSrc: null,
- loading: false,
- modal: false,
- };
- },
- mounted: function () {
- var _self = this;
- var displayValue = _self.fieldValue.displayValue;
- if (displayValue != undefined && displayValue.length > 0) {
- _self.newFieldValue.displayValue = displayValue[0].split(',');
- }
- },
- methods: {
- /**
- * 预览图片
- */
- previewImg: function (imgSrc) {
- this.modalImgSrc = imgSrc;
- this.modal = true;
- },
- /**
- * 删除图片
- * @param {String} item 图片Name
- */
- removeImage(item) {
- let _self = this;
- var index = _self.newFieldValue.displayValue.indexOf(item);
- if (index > -1) {
- _self.newFieldValue.displayValue.splice(index, 1);
- }
- var images = _self.newFieldValue.displayValue.join(',');
- _self.fieldValue.displayValue[0] = images;
- this.$emit('valueChanged', _self.fieldValue);
- },
- /**
- * 选择图片发生改变
- * @param {[type]} e [description]
- * @return {[type]} [description]
- */
- onFileChange(e) {
- var files = e.target.files || e.dataTransfer.files;
- if (!files.length)
- return;
- this.uploadImage(files[0]);
- },
- /**
- * 上传图片
- * @param {File} selectedFile 选择的文件
- */
- uploadImage: function (selectedFile) {
- var _self = this;
- if (selectedFile == undefined) {
- return;
- }
- if (!/image\/\w+/.test(selectedFile.type)) {
- alert('请确保文件为图像类型');
- return;
- }
- var formData = new FormData();
- formData.append('images', selectedFile);
- formData.append('className', _self.className);
- _self.loading=true;
- $.ajax({
- url: Common.getApiURL('file/imageUpload'),
- type: 'post',
- beforeSend: function (request) {
- Common.addTokenToRequest(request);
- },
- data: formData,
- contentType: false,
- processData: false,
- success: function (data) {
- _self.loading = false;
- if (data.errorCode == 0) {
- _self.addImgs(data.datas);
- }else{
- notificationError(data.errorMessage, '图片上传失败');
- }
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- _self.loading=false;
- Common.processException(XMLHttpRequest, textStatus, errorThrown);
- },
- });
- },
- /**
- * 添加图片
- * @param {String} imageName 添加的图片名称
- */
- addImgs: function (imageNames) {
- var _self = this;
- if (_self.newFieldValue.displayValue == undefined) {
- _self.newFieldValue.displayValue = new Array();
- }
- if(imageNames != null){
- imageNames.forEach(element => {
- _self.newFieldValue.displayValue.push(element);
- });
- }
-
- var images = _self.newFieldValue.displayValue.join(',');
- _self.fieldValue.displayValue[0] = images;
- this.$emit('valueChanged', _self.fieldValue);
- },
- /**
- * 获取图片地址
- * @param {String} item 图片名称
- * @return {String} 图片URL地址
- */
- getImageSrc: function (item) {
- var _self = this;
- if (item != undefined && item != null) {
- return Common.getImageSrc(_self.className, item);
- } else {
- return '';
- }
- },
- /**
- * 获取图片缩略图地址
- * @param {String} item 图片名称
- * @return {String} 图片URL地址
- */
- getThumbnailImageSrc: function(item){
- var _self = this;
- if (item != undefined && item != null) {
- return Common.getThumbnailImageSrc(_self.className, item);
- } else {
- return '';
- }
- },
- },
- };
- </script>
- <style scoped>
- .inputfile {
- width: 0.1px;
- height: 0.1px;
- opacity: 0;
- overflow: hidden;
- position: absolute;
- z-index: -1;
- }
- .room_img {
- width: 150px;
- height: 100px;
- float: left;
- margin-right: 10px;
- }
- .room_img img {
- width: 100%;
- height: 100%;
- }
- .room_img span {
- position: relative;
- right: -130px;
- top: -100px;
- font-size: 20px;
- color: red;
- }
- .label-btn {
- color: white;
- background-color: #006699;
- display: inline-block;
- padding: 5px 15px;
- border-radius: 5px;
- }
- .label-btn:hover {
- color: white;
- background-color: #003399;
- display: inline-block;
- padding: 5px 15px;
- border-radius: 5px;
- cursor: pointer;
- }
- .modal-img-box {
- width: 100%;
- text-align: center;
- overflow: auto;
- }
- .m-small-img {
- cursor: pointer;
- }
- .m-img {
- width: 100%;
- }
- </style>
|