TraceCommentCreate.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <template>
  2. <div>
  3. <div>
  4. <Navbar title="创建评论" :is-go-back="true" />
  5. <div>
  6. <div class="form-group">
  7. <label for="exampleInputEmail2">评论内容</label>
  8. <div id="summernote" />
  9. <!-- <textarea style="width: 100%;"
  10. id="hcqk"
  11. class="form-control"
  12. rows="5"
  13. v-model="content"></textarea> -->
  14. </div>
  15. <div>
  16. <input autocomplete="off"
  17. ref="fileInput"
  18. type="file"
  19. class="form-control file-input"
  20. @change="onFileChanges"
  21. />
  22. <label for="attachment">
  23. <a
  24. role="button"
  25. class="btn btn-primary btn-sm"
  26. @click="clickButton"
  27. >
  28. <i class="fa fa-upload" />
  29. &nbsp; 上传附件
  30. </a>
  31. </label>
  32. </div>
  33. <div style="margin-top: 10px">
  34. <ul class="list-group">
  35. <li v-for="item in files" :key="item" class="list-group-item">
  36. <span class="badge" @click="download(item)">下载</span>
  37. <span class="badge" @click="removeFile(item)">删除</span>
  38. {{ item }}
  39. </li>
  40. </ul>
  41. </div>
  42. <div>
  43. <div style="margin-top: 10px">
  44. <button type="text" class="btn btn-default" @click="photograph">
  45. 确认
  46. </button>
  47. <button type="text" class="btn btn-default" @click="back">
  48. 取消
  49. </button>
  50. </div>
  51. </div>
  52. </div>
  53. </div>
  54. <Loading v-if="loading" />
  55. </div>
  56. </template>
  57. <script>
  58. var Uuid = require('pc-component-v3').default.Uuid;
  59. var Loading = require('pc-component-v3').default.Loading;
  60. var UpladFile = require('../widget/UpladFile.js');
  61. var Notify = require('pc-component-v3').default.Notify;
  62. var Common = require('../common/Common.js');
  63. var Navbar = require('pc-component-v3').default.Navbar;
  64. var DownloadService = require('pc-component-v3').default.DownloadService;
  65. export default {
  66. components: {
  67. Uuid,
  68. Loading,
  69. UpladFile,
  70. Notify,
  71. Common,
  72. Navbar,
  73. },
  74. data: function () {
  75. return {
  76. traceId: null,
  77. content: '',
  78. images: [],
  79. className: 'com.leanwo.prodog.trace.model.TraceComment',
  80. files: [],
  81. traceConfigDto: {},
  82. summernoteInitSuccess: false, // summernote初始化成功
  83. loading: false,
  84. };
  85. },
  86. mounted: function () {
  87. var _self = this;
  88. $('.input-tan').focus(function () {
  89. document.activeElement.blur();
  90. });
  91. this.initData();
  92. this.getTraceConfig();
  93. $('#summernote').summernote({
  94. toolbar: [
  95. [
  96. 'insert',
  97. [
  98. 'link',
  99. 'picture',
  100. 'video',
  101. 'audio',
  102. 'hr',
  103. 'table', //插件
  104. 'fontname',
  105. 'fontsize',
  106. 'color',
  107. 'bold',
  108. 'italic',
  109. 'underline',
  110. 'strikethrough',
  111. 'clear', //字体样式
  112. 'style',
  113. 'ol',
  114. 'ul',
  115. 'paragraph',
  116. 'height', //段落样式
  117. 'fullscreen',
  118. 'codeview',
  119. 'undo',
  120. 'redo',
  121. 'help', //Misc
  122. ],
  123. ],
  124. ],
  125. height: 400,
  126. minHeight: 300,
  127. maxHeight: 500,
  128. focus: true,
  129. lang: 'zh-CN',
  130. callbacks: {
  131. onImageUpload: function (files, editor, $editable) {
  132. _self.sendFile(files[0], editor, $editable);
  133. },
  134. },
  135. });
  136. _self.summernoteInitSuccess = true;
  137. },
  138. beforeUnmount: function () {
  139. if (this.summernoteInitSuccess == true) {
  140. $('#summernote').summernote('destroy');
  141. }
  142. },
  143. methods: {
  144. /**
  145. * 点击上传按钮事件
  146. * @return {[type]} [description]
  147. */
  148. clickButton: function () {
  149. $(this.$refs.fileInput).click();
  150. },
  151. /**
  152. * 初始化数据
  153. * @author GuoZhiBo 20171201
  154. */
  155. initData: function () {
  156. var _self = this;
  157. this.traceId = Number(this.$route.params.traceId);
  158. },
  159. /**
  160. * 监听文件改变事件
  161. * @author GuoZhiBo 20171201
  162. */
  163. onFileChange(e) {
  164. var _self = this;
  165. var files = e.target.files || e.dataTransfer.files;
  166. if (!files.length) return;
  167. _self.uploadImage(files[0]);
  168. },
  169. /**
  170. * 上传图片
  171. * @author GuoZhiBo 20171201
  172. */
  173. uploadImage: function (selectedFile) {
  174. var _self = this;
  175. if (selectedFile == undefined) {
  176. return;
  177. }
  178. if (!/image\/\w+/.test(selectedFile.type)) {
  179. Notify.error('提示', '请确保文件为图像类型!');
  180. return;
  181. }
  182. UpladFile.photoCompress(
  183. selectedFile,
  184. {
  185. quality: 0.2,
  186. },
  187. function (base64Codes) {
  188. var bl = UpladFile.convertBase64UrlToBlob(base64Codes);
  189. var rst = new FormData();
  190. rst.append('images', bl, 'file_' + Date.parse(new Date()) + '.jpg');
  191. rst.append('className', 'com.leanwo.prodog.trace.model.TraceComment');
  192. _self.loading=true;
  193. $.ajax({
  194. url: Common.getApiURL('file/imageUpload'),
  195. type: 'post',
  196. beforeSend: function (request) {
  197. Common.addTokenToRequest(request);
  198. },
  199. data: rst,
  200. contentType: false,
  201. processData: false,
  202. success: function (data) {
  203. _self.loading=false;
  204. if (data != 'error') {
  205. var imageName = data.substring(data.indexOf(':') + 1);
  206. _self.addImg(imageName);
  207. }
  208. },
  209. error: function (XMLHttpRequest, textStatus, errorThrown) {
  210. _self.loading=false;
  211. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  212. },
  213. });
  214. return rst.file;
  215. },
  216. );
  217. },
  218. /**
  219. * 添加图片
  220. * @author GuoZhiBo 20171201
  221. */
  222. addImg: function (imageName) {
  223. var _self = this;
  224. _self.images.push(imageName);
  225. },
  226. /**
  227. * 获取图片地址
  228. * @param {String} item 图片名称
  229. * @return {String} 图片URL地址
  230. */
  231. getImageSrc: function (item) {
  232. var _self = this;
  233. if (item != undefined && item != null) {
  234. return Common.getResourceUrl(
  235. 'image',
  236. 'com.leanwo.prodog.trace.model.TraceComment',
  237. item,
  238. );
  239. } else {
  240. return '';
  241. }
  242. },
  243. /**
  244. * 获取图片路径供外部调用
  245. * @return {Array} 图片地址
  246. */
  247. getImages: function () {
  248. return this.images;
  249. },
  250. /**
  251. * 删除图片
  252. *@author GuoZhiBo 20171201
  253. */
  254. deleteImg(index) {
  255. var _self = this;
  256. _self.images.splice(index, 1);
  257. },
  258. //----------文件------------
  259. /**
  260. * 删除文件
  261. * @param {String} item 文件Name
  262. */
  263. removeFile: function (index) {
  264. var _self = this;
  265. _self.files.splice(index, 1);
  266. },
  267. /**
  268. * 选择文件发生改变
  269. * @param {[type]} e [description]
  270. * @return {[type]} [description]
  271. */
  272. onFileChanges: function (e) {
  273. var files = e.target.files || e.dataTransfer.files;
  274. if (!files.length) return;
  275. this.uploadFile(files[0]);
  276. },
  277. /**
  278. * 上传文件
  279. * @param {File} selectedFile 选择的文件
  280. */
  281. uploadFile: function (selectedFile) {
  282. var _self = this;
  283. if (selectedFile == undefined) {
  284. return;
  285. }
  286. var size = undefined;
  287. if (
  288. _self.traceConfigDto.commentAttachmentSize != undefined &&
  289. _self.traceConfigDto.commentAttachmentSize != null
  290. ) {
  291. size = _self.traceConfigDto.commentAttachmentSize;
  292. }
  293. //当系统没有配置附件大小时默认8m
  294. if (size == undefined) {
  295. size = 8;
  296. }
  297. if (selectedFile.size != undefined && selectedFile.size != null) {
  298. if (selectedFile.size / 1024 <= 1024 * size) {
  299. var formData = new FormData();
  300. formData.append('files', selectedFile);
  301. formData.append('className', _self.className);
  302. _self.loading=true;
  303. $.ajax({
  304. url: Common.getApiURL('file/classFileUpload'),
  305. type: 'post',
  306. beforeSend: function (request) {
  307. Common.addTokenToRequest(request);
  308. },
  309. data: formData,
  310. contentType: false,
  311. processData: false,
  312. success: function (data) {
  313. _self.loading=false;
  314. if (data != 'error') {
  315. var fileName = data.substring(data.indexOf(':') + 1);
  316. _self.files.push(fileName);
  317. }
  318. },
  319. error: function (XMLHttpRequest, textStatus, errorThrown) {
  320. _self.loading=false;
  321. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  322. },
  323. });
  324. } else {
  325. Notify.error(
  326. '提示',
  327. '文件大小不能超过' + size + 'M,可在任务配置功能中配置!',
  328. );
  329. }
  330. } else {
  331. Notify.error('提示', '上传的文件为空!');
  332. }
  333. },
  334. /**
  335. * 获取任务管理配置
  336. */
  337. getTraceConfig: function () {
  338. var _self = this;
  339. $.ajax({
  340. url: Common.getApiURL('TraceConfigResource/queryTraceConfigDto'),
  341. type: 'get',
  342. dataType: 'json',
  343. beforeSend: function (request) {
  344. Common.addTokenToRequest(request);
  345. },
  346. success: function (data) {
  347. _self.traceConfigDto = data;
  348. },
  349. error: function (XMLHttpRequest, textStatus, errorThrown) {
  350. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  351. },
  352. });
  353. },
  354. /**
  355. * 获取文件地址
  356. * @param {String} item 图片名称
  357. * @return {String} 图片URL地址
  358. */
  359. getFileSrc: function (item) {
  360. var _self = this;
  361. if (item != undefined && item != null) {
  362. return Common.getResourceUrl('file', _self.className, item);
  363. } else {
  364. return '';
  365. }
  366. },
  367. /**
  368. * 下载文件
  369. * @param {String} fileName 文件名
  370. * @return {void}
  371. */
  372. download: function (fileName) {
  373. var _self = this;
  374. DownloadService.fileDownload(_self.className, fileName);
  375. },
  376. //内容图片上传
  377. sendFile: function (file, editor, $editable) {
  378. var _self = this;
  379. UpladFile.photoCompress(
  380. file,
  381. {
  382. quality: 1,
  383. },
  384. function (base64Codes) {
  385. var bl = UpladFile.convertBase64UrlToBlob(base64Codes);
  386. var rst = new FormData();
  387. rst.append('images', bl, 'file_' + Date.parse(new Date()) + '.jpg');
  388. rst.append('className', _self.className);
  389. $.ajax({
  390. url: Common.getApiURL('file/imageUpload'),
  391. type: 'post',
  392. data: rst,
  393. contentType: false,
  394. processData: false,
  395. beforeSend: function (request) {
  396. Common.addTokenToRequest(request);
  397. },
  398. success: function (data) {
  399. if (data != 'error') {
  400. var imageName = data.substring(data.indexOf(':') + 1);
  401. $('#summernote').summernote(
  402. 'insertImage',
  403. Common.getResourceUrl('image', _self.className, imageName),
  404. 'image name',
  405. );
  406. }
  407. },
  408. error: function () {
  409. Notify.error('提示', '上传失败!');
  410. return;
  411. },
  412. });
  413. return rst.file;
  414. },
  415. );
  416. },
  417. /**
  418. * 保存评论表数据
  419. * @author GuoZhiBo 20171201
  420. */
  421. photograph: function () {
  422. var _self = this;
  423. _self.content = $('#summernote').summernote('code');
  424. if (_self.content != '' && _self.content != null) {
  425. var traceComment = {
  426. traceId: _self.traceId,
  427. content: _self.content,
  428. attachments: _self.files.join(','),
  429. };
  430. _self.loading=true;
  431. $.ajax({
  432. url: Common.getApiURL('TraceCommentResource/saveTraceComment'),
  433. type: 'post',
  434. contentType: 'application/json',
  435. beforeSend: function (request) {
  436. Common.addTokenToRequest(request);
  437. },
  438. data: JSON.stringify(traceComment),
  439. success: function (data) {
  440. _self.loading=false;
  441. _self.back();
  442. },
  443. error: function (XMLHttpRequest, textStatus, errorThrown) {
  444. _self.loading=false;
  445. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  446. },
  447. });
  448. } else {
  449. Notify.error('提示', '请填写评论内容!');
  450. }
  451. },
  452. back: function () {
  453. history.back();
  454. },
  455. },
  456. };
  457. </script>
  458. <style scoped>
  459. .mui-divs3 {
  460. margin-top: 10px;
  461. }
  462. .file-input {
  463. width: 0.1px;
  464. height: 0.1px;
  465. opacity: 0;
  466. overflow: hidden;
  467. position: absolute;
  468. z-index: -1;
  469. }
  470. .badge {
  471. cursor: pointer;
  472. background-color: #428bca;
  473. color: white;
  474. }
  475. </style>