VideoListWidget.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <div>
  3. <div
  4. v-for="(item, index) in newFieldValue.displayValue"
  5. :key="index"
  6. class="room_img"
  7. >
  8. <video controls="true" :src="getVideoSrc(item)" />
  9. <span
  10. v-if="!readonly"
  11. class="glyphicon glyphicon-trash"
  12. @click="removeVideo(item)"
  13. />
  14. </div>
  15. <div>
  16. <div v-if="!readonly" enctype="multipart/form-data">
  17. <label :for="'file' + field.fieldId" class="label-btn">
  18. {{ $t("lang.VideoListWidget.selectVideo") }}
  19. </label>
  20. <input
  21. :id="'file' + field.fieldId"
  22. autocomplete="off"
  23. type="file"
  24. class="inputfile"
  25. @change="onFileChange"
  26. />
  27. </div>
  28. </div>
  29. <Loading v-if="loading" />
  30. </div>
  31. </template>
  32. <script>
  33. import AuthImage from '../../widget/AuthImage.vue';
  34. import Common from '../../common/Common.js';
  35. import { notificationError } from '../../common/notification.js';
  36. import { Notify, Uuid } from 'pc-component-v3';
  37. export default {
  38. components: {
  39. AuthImage,
  40. },
  41. props: {
  42. readonly: {
  43. type: Boolean,
  44. default: null,
  45. },
  46. className: {
  47. type: String,
  48. default: null,
  49. },
  50. field: {
  51. type: Object,
  52. default : function(){
  53. return null;
  54. },
  55. },
  56. fieldValue: {
  57. type: Object,
  58. default : function(){
  59. return null;
  60. },
  61. },
  62. },
  63. emits: ['valueChanged'],
  64. data: function () {
  65. this.Common = Common;
  66. return {
  67. newFieldValue: {
  68. displayValue: [],
  69. displayType: 'String',
  70. },
  71. loading: false,
  72. };
  73. },
  74. mounted: function () {
  75. var _self = this;
  76. var displayValue = _self.fieldValue.displayValue;
  77. if (displayValue != undefined && displayValue.length > 0) {
  78. _self.newFieldValue.displayValue = displayValue[0].split(',');
  79. }
  80. },
  81. methods: {
  82. /**
  83. * 删除视频
  84. * @param {String} item 图片Name
  85. */
  86. removeVideo(item) {
  87. let _self = this;
  88. var index = _self.newFieldValue.displayValue.indexOf(item);
  89. if (index > -1) {
  90. _self.newFieldValue.displayValue.splice(index, 1);
  91. }
  92. var videos = _self.newFieldValue.displayValue.join(',');
  93. _self.fieldValue.displayValue[0] = videos;
  94. this.$emit('valueChanged', _self.fieldValue);
  95. },
  96. /**
  97. * 选择视频发生改变
  98. * @param {[type]} e [description]
  99. * @return {[type]} [description]
  100. */
  101. onFileChange(e) {
  102. var files = e.target.files || e.dataTransfer.files;
  103. if (!files.length) return;
  104. this.uploadVideo(files[0]);
  105. },
  106. /**
  107. * 上传视频
  108. * @param {File} selectedFile 选择的文件
  109. */
  110. uploadVideo: function (selectedFile) {
  111. var _self = this;
  112. if (selectedFile == undefined) {
  113. return;
  114. }
  115. if (!/video\/\w+/.test(selectedFile.type)) {
  116. Notify.error('错误', '请确保文件为视频类型', true);
  117. return;
  118. }
  119. var formData = new FormData();
  120. formData.append('video', selectedFile);
  121. formData.append('className', _self.className);
  122. _self.loading=true;
  123. $.ajax({
  124. url: Common.getApiURL('file/videoUpload'),
  125. type: 'post',
  126. beforeSend: function (request) {
  127. Common.addTokenToRequest(request);
  128. },
  129. data: formData,
  130. contentType: false,
  131. processData: false,
  132. success: function (data) {
  133. _self.loading=false;
  134. console.log(data);
  135. if (data.errorCode == 0) {
  136. var videoNames = data.datas;
  137. _self.addVideos(videoNames);
  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} videoNames 添加的图片名称
  151. */
  152. addVideos: function (videoNames) {
  153. var _self = this;
  154. if (_self.newFieldValue.displayValue == undefined) {
  155. _self.newFieldValue.displayValue = new Array();
  156. }
  157. if(videoNames != null){
  158. videoNames.forEach(element => {
  159. _self.newFieldValue.displayValue.push(element);
  160. });
  161. }
  162. var videos = _self.newFieldValue.displayValue.join(',');
  163. _self.fieldValue.displayValue[0] = videos;
  164. this.$emit('valueChanged', _self.fieldValue);
  165. },
  166. /**
  167. * 获取视频地址
  168. * @param {String} item 图片名称
  169. * @return {String} 图片URL地址
  170. */
  171. getVideoSrc: function (item) {
  172. var _self = this;
  173. if (item != undefined && item != null) {
  174. return Common.getVideoSrc(_self.className, item);
  175. } else {
  176. return '';
  177. }
  178. },
  179. },
  180. };
  181. </script>
  182. <style scoped>
  183. .inputfile {
  184. width: 0.1px;
  185. height: 0.1px;
  186. opacity: 0;
  187. overflow: hidden;
  188. position: absolute;
  189. z-index: -1;
  190. }
  191. .room_img {
  192. width: 150px;
  193. height: 100px;
  194. float: left;
  195. margin-right: 10px;
  196. }
  197. .room_img video {
  198. width: 100%;
  199. height: 100%;
  200. }
  201. .room_img span {
  202. position: relative;
  203. right: -125px;
  204. top: -95px;
  205. font-size: 20px;
  206. color: red;
  207. cursor: pointer;
  208. }
  209. .label-btn {
  210. color: white;
  211. background-color: #006699;
  212. display: inline-block;
  213. padding: 5px 15px;
  214. border-radius: 5px;
  215. }
  216. .label-btn:hover {
  217. color: white;
  218. background-color: #003399;
  219. display: inline-block;
  220. padding: 5px 15px;
  221. border-radius: 5px;
  222. cursor: pointer;
  223. }
  224. </style>