journalism.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. <template>
  2. <div class="journalism">
  3. <!-- 头部细节部分 -->
  4. <div class="title">
  5. <el-row>
  6. <el-col :span="24">
  7. <div class="top">
  8. <el-input placeholder="新闻标题" class="input-demo" v-model="title"></el-input>
  9. <el-button type="success" style="margin-left: 1%;" @click="queryLook">查询</el-button>
  10. <el-button type="success" style="margin-left: 1%;" @click="addUserList = true">添加</el-button>
  11. </div>
  12. </el-col>
  13. </el-row>
  14. </div>
  15. <!-- 头部细节部分结束 -->
  16. <!-- 表格部分 -->
  17. <template>
  18. <el-table
  19. v-loading="loading"
  20. ref="multipleTable"
  21. :data="usertable"
  22. border
  23. tooltip-effect="dark">
  24. <el-table-column
  25. label="新闻标题"
  26. prop="title"
  27. show-overflow-tooltip>
  28. </el-table-column>
  29. <el-table-column
  30. prop="newsAbstract"
  31. label="新闻摘要"
  32. show-overflow-tooltip>
  33. </el-table-column>
  34. <el-table-column
  35. label="创建时间"
  36. show-overflow-tooltip
  37. width="180">
  38. <template slot-scope="scope" show-overflow-tooltip>
  39. <span>{{scope.row.createTime | fmtDate}}</span>
  40. </template>
  41. </el-table-column>
  42. <el-table-column
  43. label="操作"
  44. width="120">
  45. <template slot-scope="scope" show-overflow-tooltip>
  46. <span style="cursor:pointer;" @click="openChange(scope.row)">修改</span>
  47. <span style="margin-left: 10px; cursor:pointer; color: red;" @click="Delete(scope.row.id)">删除</span>
  48. </template>
  49. </el-table-column>
  50. </el-table>
  51. </template>
  52. <!-- 分页 -->
  53. <div class="block">
  54. <el-pagination
  55. @size-change="handleSizeChange"
  56. @current-change="handleCurrentChange"
  57. :current-page="current"
  58. :page-sizes="[6, 8, 10]"
  59. :page-size="pagesize"
  60. layout="total, sizes, prev, pager, next, jumper"
  61. :total="total">
  62. </el-pagination>
  63. </div>
  64. <!-- 新增新闻 -->
  65. <el-dialog
  66. @close="handleEditDialogClose"
  67. :visible.sync="addUserList">
  68. <div class="tou">新闻--添加</div>
  69. <div class="line"></div>
  70. <el-form
  71. label-position="right"
  72. label-width="80px"
  73. :rules="rules"
  74. ref="ruleForm"
  75. :model="formList">
  76. <el-form-item label="新闻标题" prop="title">
  77. <el-input v-model="formList.title" auto-complete="off" placeholder="请输入新闻标题"></el-input>
  78. </el-form-item>
  79. <el-form-item label="新闻摘要" prop="newsAbstract">
  80. <el-input v-model="formList.newsAbstract" auto-complete="off" placeholder="请输入新闻摘要"></el-input>
  81. </el-form-item>
  82. <el-form-item label="新闻封面">
  83. <el-upload
  84. ref='upload'
  85. action="http://192.168.1.199:18083/newsmess/save_news"
  86. list-type="picture-card"
  87. :on-preview="handlePreview"
  88. :on-remove="handleRemove"
  89. :on-change="handleSuccess"
  90. :auto-upload="false">
  91. <i class="el-icon-plus"></i>
  92. </el-upload>
  93. </el-form-item>
  94. <!-- <el-form-item label="新闻封面" prop="photoId">
  95. <el-upload
  96. class="anniu"
  97. ref="upload"
  98. action="http://192.168.1.199:18083/newsmess/save_news"
  99. :on-preview="handlePreview"
  100. :on-remove="handleRemove"
  101. :on-change="handleSuccess"
  102. :auto-upload="false">
  103. <el-button slot="trigger" size="small" type="primary">选取封面</el-button>
  104. </el-upload>
  105. </el-form-item> -->
  106. <el-form-item label="导航分类">
  107. <el-tree
  108. :data="menuList"
  109. show-checkbox
  110. default-expand-all
  111. check-strictly
  112. node-key="id"
  113. ref="tree"
  114. highlight-current
  115. :default-checked-keys="haveRight"
  116. :props="defaultProps"
  117. style="font-size: 0px;">
  118. </el-tree>
  119. </el-form-item>
  120. <el-form-item label="新闻内容" prop="content">
  121. <template>
  122. <!-- 图片上传组件辅助 -->
  123. <el-upload
  124. class="avatar-uploader"
  125. :action="serverUrl"
  126. name="photo"
  127. :headers="header"
  128. :show-file-list="false"
  129. :on-success="uploadSuccess"
  130. :on-error="uploadError"
  131. :before-upload="beforeUpload">
  132. </el-upload>
  133. <!-- 富文本编辑器组件 -->
  134. <el-row v-loading="quillUpdateImg">
  135. <quill-editor
  136. v-model="detailContent"
  137. ref="myQuillEditor"
  138. :options="editorOption"
  139. @change="onEditorChange($event)"
  140. @ready="onEditorReady($event)">
  141. </quill-editor>
  142. </el-row>
  143. </template>
  144. </el-form-item>
  145. </el-form>
  146. <div slot="footer" class="dialog-footer">
  147. <el-button @click="addUserList = false" type="info">取 消</el-button>
  148. <el-button type="primary" @click="addData('ruleForm')">新 增</el-button>
  149. </div>
  150. </el-dialog>
  151. <!-- 修改新闻 -->
  152. <el-dialog
  153. @close="handleEditDialogClose"
  154. :visible.sync="changeUser">
  155. <div class="tou">新闻--修改</div>
  156. <div class="line"></div>
  157. <el-form
  158. label-position="right"
  159. label-width="80px"
  160. :model="formChangeList">
  161. <el-form-item label="新闻标题">
  162. <el-input v-model="formChangeList.title" auto-complete="off" placeholder="请输入新闻标题"></el-input>
  163. </el-form-item>
  164. <el-form-item label="新闻摘要">
  165. <el-input v-model="formChangeList.newsAbstract" auto-complete="off" placeholder="请输入新闻摘要"></el-input>
  166. </el-form-item>
  167. <el-form-item label="新闻封面">
  168. <el-upload
  169. ref='upload'
  170. action="http://192.168.1.199:18083/app/photo"
  171. list-type="picture-card"
  172. :on-preview="handlePreview"
  173. :on-remove="handleRemove"
  174. :on-change="handleSuccess"
  175. :auto-upload="false">
  176. <i class="el-icon-plus"></i>
  177. </el-upload>
  178. <img style="width: 150px; height: 150px;" :src="dialogImageUrl" alt="">
  179. </el-form-item>
  180. <!-- <el-form-item label="新闻封面">
  181. <el-upload
  182. class="anniu"
  183. ref="upload"
  184. action="http://192.168.1.199:18083/newsmess/save_news"
  185. :on-preview="handlePreview"
  186. :on-remove="handleRemove"
  187. :on-change="handleSuccess"
  188. :auto-upload="false">
  189. <el-button slot="trigger" size="small" type="primary">选取封面</el-button>
  190. <img style="width: 150px; height: 150px;" :src= "dialogImageUrl" alt="">
  191. </el-upload>
  192. </el-form-item> -->
  193. <el-form-item label="导航分类">
  194. <el-tree
  195. :data="menuList"
  196. show-checkbox
  197. default-expand-all
  198. check-strictly
  199. node-key="id"
  200. ref="tree"
  201. highlight-current
  202. :default-checked-keys="haveRight"
  203. :props="defaultProps">
  204. </el-tree>
  205. </el-form-item>
  206. <el-form-item label="新闻内容">
  207. <template>
  208. <!-- 图片上传组件辅助 -->
  209. <el-upload
  210. class="avatar-uploader"
  211. :action="serverUrl"
  212. name="photo"
  213. :headers="header"
  214. :show-file-list="false"
  215. :on-success="uploadSuccessOne"
  216. :on-error="uploadErrorOne"
  217. :before-upload="beforeUploadOne">
  218. </el-upload>
  219. <!-- 富文本编辑器组件 -->
  220. <el-row v-loading="quillUpdateImg">
  221. <quill-editor
  222. v-model="detailContent"
  223. ref="myQuillEditor"
  224. :options="editorOption"
  225. @change="onEditorChange($event)"
  226. @ready="onEditorReady($event)">
  227. </quill-editor>
  228. </el-row>
  229. </template>
  230. </el-form-item>
  231. </el-form>
  232. <div slot="footer" class="dialog-footer">
  233. <el-button @click="changeUser = false" type="info">取 消</el-button>
  234. <el-button type="primary" @click="changeData">修 改</el-button>
  235. </div>
  236. </el-dialog>
  237. </div>
  238. </template>
  239. <script type="text/javascript">
  240. const toolbarOptions = [
  241. ['bold', 'italic', 'underline', 'strike'], // toggled buttons
  242. ['blockquote', 'code-block'],
  243. [{'header': 1}, {'header': 2}], // custom button values
  244. [{'list': 'ordered'}, {'list': 'bullet'}],
  245. [{'script': 'sub'}, {'script': 'super'}], // superscript/subscript
  246. [{'indent': '-1'}, {'indent': '+1'}], // outdent/indent
  247. [{'direction': 'rtl'}], // text direction
  248. [{'size': ['small', false, 'large', 'huge']}], // custom dropdown
  249. [{'header': [1, 2, 3, 4, 5, 6, false]}],
  250. [{'color': []}, {'background': []}], // dropdown with defaults from theme
  251. [{'font': []}],
  252. [{'align': []}],
  253. ['link', 'image', 'video'],
  254. ['clean'] // remove formatting button
  255. ]
  256. export default {
  257. data() {
  258. return {
  259. title: "",
  260. usertable: [],
  261. formList: {
  262. "urlList": [],
  263. "title": "",
  264. "newsAbstract": "",
  265. "photo": "",
  266. "navigationBarIds": [],
  267. "content": "",
  268. },
  269. defaultProps: {
  270. children: 'childList',
  271. label: 'name'
  272. },
  273. menuList: [],
  274. haveRight:[],
  275. treeIds:[],
  276. dialogImageUrl:"",
  277. flag: 0,
  278. formChangeList: {
  279. "id": "",
  280. "urlList": [],
  281. "title": "",
  282. "newsAbstract": "",
  283. "photo": "",
  284. "navigationBarIds": [],
  285. "content": "",
  286. },
  287. picList: [],
  288. photoList: [],
  289. current: 1,
  290. pagesize: 10,
  291. // 总共有多少条数据
  292. total: 0,
  293. addUserList: false,
  294. changeUser: false,
  295. photo: '',
  296. quillUpdateImg: false,
  297. // serverUrl: '',
  298. serverUrl: 'http://localhost:8088/api/app/upload', // 这里写你要上传的图片服务器地址
  299. header: {Authorization: 'Bearer ' + sessionStorage.token}, //有的图片服务器要求请求头需要有token之类的参数,写在这里
  300. detailContent: '', // 富文本内容
  301. editorOption: {
  302. placeholder: '',
  303. theme: 'snow', // or 'bubble'
  304. modules: {
  305. toolbar: {
  306. container: toolbarOptions, // 工具栏
  307. handlers: {
  308. 'image': function (value) {
  309. if (value) {
  310. // 触发input框选择图片文件
  311. document.querySelector('.avatar-uploader input').click()
  312. } else {
  313. this.quill.format('image', false);
  314. }
  315. }
  316. }
  317. }
  318. }
  319. }
  320. }
  321. },
  322. created() {
  323. this.loadData();
  324. this.loadTree();
  325. },
  326. methods: {
  327. // 列表展示
  328. async loadData() {
  329. const response = await this.$http.get(`app/page/newsArticle?&current=${this.current}&size=${this.pagesize}`);
  330. if (response.data.code === 1) {
  331. this.loading = false;
  332. this.usertable = response.data.responseData.records;
  333. // console.log(response.data.responseData);
  334. this.total = response.data.responseData.total;
  335. }
  336. },
  337. //展示树形导航
  338. async loadTree() {
  339. const response = await this.$http.get(`app/menu`);
  340. if (response.data.code === 1) {
  341. // console.log(response.data.responseData)
  342. this.menuList = response.data.responseData;
  343. this.loading = false;
  344. }
  345. },
  346. //查询
  347. async queryLook() {
  348. const response = await this.$http.get(`app/page/newsArticle?&current=${this.current}&size=${this.pagesize}&title=${this.title}`);
  349. if (response.data.code === 1) {
  350. this.loading = false;
  351. this.usertable = response.data.responseData.records;
  352. this.total = response.data.responseData.total;
  353. }
  354. },
  355. // 新增新闻
  356. addData(formName) {
  357. this.$refs[formName].validate(async (valid) => {
  358. if(valid) {
  359. const formData = new FormData();
  360. formData.append('photo', this.formList.photo);
  361. formData.append('title', this.formList.title);
  362. formData.append('newsAbstract', this.formList.newsAbstract);
  363. this.treeIds = this.$refs.tree.getCheckedNodes(false, true);
  364. for(var i = 0; i < this.treeIds.length; i++) {
  365. this.formList.navigationBarIds.push(this.treeIds[i].id);
  366. };
  367. formData.append('navigationBarIds', this.formList.navigationBarIds);
  368. formData.append('content', this.formList.content);
  369. formData.append('urlList', this.formList.urlList);
  370. const response = await this.$http.post(`app/newsArticle`, formData);
  371. if(response.data.code === 1) {
  372. this.loadData();
  373. this.addUserList = false;
  374. this.$message({
  375. type: 'success',
  376. message: '添加新闻成功'
  377. });
  378. }else {
  379. this.$message({
  380. type: 'error',
  381. message: '添加新闻失败'
  382. });
  383. }
  384. }else {
  385. this.$message.error('请查看是否有选项未填写或填错项');
  386. return false;
  387. }
  388. })
  389. },
  390. // 富文本图片上传前
  391. beforeUpload(file) {
  392. // 显示loading动画
  393. this.quillUpdateImg = true;
  394. },
  395. uploadSuccess(res, file) {
  396. // res为图片服务器返回的数据
  397. // 获取富文本组件实例
  398. let quill = this.$refs.myQuillEditor.quill;
  399. // console.log(quill)
  400. // 如果上传成功
  401. if (res.code == 1) {
  402. // 获取光标所在位置
  403. let length = quill.getSelection().index;
  404. // console.log(length)
  405. // 插入图片 res.info为服务器返回的图片地址
  406. quill.insertEmbed(length, 'image', res.responseData);
  407. // alert(res.responseData)
  408. this.formList.urlList.push(res.responseData);
  409. // console.log(this.formList.urlList);
  410. // 调整光标到最后
  411. quill.setSelection(length + 1);
  412. } else {
  413. this.$message.error('图片插入失败');
  414. }
  415. // loading动画消失
  416. this.quillUpdateImg = false;
  417. },
  418. // 富文本图片上传失败
  419. uploadError() {
  420. // loading动画消失
  421. this.quillUpdateImg = false;
  422. this.$message.error('图片插入失败');
  423. },
  424. //修改新闻内容时候上传的图片地址
  425. // 富文本图片上传前
  426. beforeUploadOne(file) {
  427. // 显示loading动画
  428. this.quillUpdateImg = true;
  429. },
  430. uploadSuccessOne(res, file) {
  431. // res为图片服务器返回的数据
  432. // 获取富文本组件实例
  433. let quill = this.$refs.myQuillEditor.quill;
  434. // console.log(quill)
  435. // 如果上传成功
  436. if (res.code == 1) {
  437. // 获取光标所在位置
  438. let length = quill.getSelection().index;
  439. // console.log(length)
  440. // 插入图片 res.info为服务器返回的图片地址
  441. quill.insertEmbed(length, 'image', res.responseData);
  442. // alert(res.responseData)
  443. this.formChangeList.urlList.push(res.responseData);
  444. // console.log(this.formList.urlList);
  445. // 调整光标到最后
  446. quill.setSelection(length + 1);
  447. } else {
  448. this.$message.error('图片插入失败');
  449. }
  450. // loading动画消失
  451. this.quillUpdateImg = false;
  452. },
  453. // 富文本图片上传失败
  454. uploadErrorOne() {
  455. // loading动画消失
  456. this.quillUpdateImg = false;
  457. this.$message.error('图片插入失败');
  458. },
  459. // 新增新闻内容
  460. onEditorReady(editor) { },// 准备编辑器
  461. onEditorBlur(){}, // 失去焦点事件
  462. onEditorFocus(){}, // 获得焦点事件
  463. onEditorChange(event){
  464. this.formList.content = event.html;
  465. var imgReg = /<img.*?(?:>|\/>)/gi;
  466. var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
  467. this.picList = event.html.match(imgReg);
  468. if( this.picList == []) {
  469. return;
  470. }else{
  471. for (var i = 0; i < this.picList.length; i++) {
  472. var src = this.picList[i].match(srcReg);
  473. //获取图片地址
  474. // console.log('图片地址'+(i+1)+':'+src[1]);
  475. // console.log(src[1]);
  476. }
  477. }
  478. // this.photoList.push(src);
  479. // console.log(this.photoList);
  480. // console.log(event.html)
  481. // console.log(document.getElementsByTagName('img'));
  482. // this.picList.push(document.getElementsByTagName('img').src);
  483. // console.log(this.picList)
  484. // console.log(this.formList.content)
  485. }, // 内容改变事件
  486. // 打开修改并赋予信息
  487. async openChange(user) {
  488. this.changeUser = true;
  489. this.formChangeList.id = user.id;
  490. this.formChangeList.title = user.title;
  491. this.formChangeList.newsAbstract = user.newsAbstract;
  492. this.dialogImageUrl = user.photoUrl;
  493. this.detailContent = user.content;
  494. const response = await this.$http.get(`app/newsArticle/${user.id}`);
  495. if(response.data.code === 1) {
  496. this.$refs.tree.setCheckedKeys([]);
  497. this.haveRight = response.data.responseData.navigationBarIds;
  498. this.formChangeList.urlList = response.data.responseData.urlList;
  499. console.log(this.formChangeList.urlList);
  500. // console.log(response.data.responseData)
  501. }
  502. },
  503. // 修改新闻
  504. async changeData() {
  505. const formData = new FormData();
  506. formData.append('id', this.formChangeList.id);
  507. if(this.flag == 1) {
  508. formData.append('photo', this.formChangeList.photo);
  509. }
  510. formData.append('title', this.formChangeList.title);
  511. formData.append('newsAbstract', this.formChangeList.newsAbstract);
  512. formData.append('urlList', this.formChangeList.urlList);
  513. console.log(this.formChangeList.urlList)
  514. this.treeIds = this.$refs.tree.getCheckedNodes(false, true);
  515. for(var i = 0; i < this.treeIds.length; i++) {
  516. this.formChangeList.navigationBarIds.push(this.treeIds[i].id);
  517. };
  518. formData.append('navigationBarIds', this.formChangeList.navigationBarIds);
  519. formData.append('content', this.detailContent);
  520. // const response = await this.$http.put(`app/newsArticle`, formData);
  521. // if(response.data.code === 1) {
  522. // this.loadData();
  523. // this.changeUser = false;
  524. // this.$message({
  525. // type: 'success',
  526. // message: '修改新闻成功'
  527. // });
  528. // }else {
  529. // this.$message({
  530. // type: 'error',
  531. // message: '修改新闻失败'
  532. // });
  533. // }
  534. },
  535. // 点击删除按钮
  536. Delete(id) {
  537. this.$confirm('是否删除该用户', '提示' ,{
  538. confirmButtonText: '确定',
  539. cancelButtonText: '取消',
  540. type: 'warning'
  541. }).then(async () => {
  542. const response = await this.$http.put(`app/newsArticle/${id}`);
  543. if(response.data.code === 1) {
  544. this.loadData();
  545. this.$message({
  546. type: 'success',
  547. message: '删除新闻成功'
  548. });
  549. }else {
  550. this.$message({
  551. type: 'error',
  552. message: '删除新闻失败'
  553. });
  554. }
  555. }).catch(() => {
  556. this.$message({ type: 'info', message: '已取消删除'});
  557. });
  558. },
  559. // 上传图片功能
  560. handleRemove(file, fileList) {
  561. console.log(file, fileList);
  562. },
  563. handlePreview(file) {
  564. console.log(file);
  565. },
  566. handleSuccess (a) {
  567. this.formList.photo = a.raw;
  568. if(a !== '') {
  569. this.flag = 1
  570. this.formChangeList.photo = a.raw;
  571. }else {
  572. this.flag = 0
  573. }
  574. },
  575. // 清空表单数据
  576. handleEditDialogClose() {
  577. for (var key in this.formList) {
  578. this.formList[key] = '';
  579. };
  580. this.formList.urlList = [];
  581. this.formList.navigationBarIds = [];
  582. //清除图片缓存
  583. this.$refs.upload.clearFiles();
  584. this.formList.content = '';
  585. this.$refs.tree.setCheckedKeys([]);
  586. this.haveRight = [];
  587. this.detailContent = '';
  588. this.formChangeList.navigationBarIds = [];
  589. },
  590. // 分页方法
  591. handleSizeChange(val) {
  592. this.pagesize = val;
  593. if(this.title !== '') {
  594. this.queryLook();
  595. }else{
  596. this.loadData();
  597. };
  598. console.log(`每页 ${val} 条`);
  599. },
  600. handleCurrentChange(val) {
  601. this.current = val;
  602. if(this.title !== '') {
  603. this.queryLook();
  604. }else{
  605. this.loadData();
  606. };
  607. console.log(`当前页: ${val}`);
  608. },
  609. }
  610. };
  611. </script>
  612. <style>
  613. .journalism {
  614. border: 1px solid #d9d9d9;
  615. border-radius: 10px;
  616. }
  617. .journalism .title {
  618. font-size: 5px;
  619. margin-bottom: 20px;
  620. }
  621. .journalism .top {
  622. padding-top: 20px;
  623. padding-left: 20px;
  624. }
  625. .journalism .text {
  626. display: inline-block;
  627. color: #000;
  628. font-size: 16px ;
  629. margin-left: 1%;
  630. }
  631. .journalism .input-demo {
  632. display: inline-block;
  633. width: 20%;
  634. margin-left: 1%;
  635. }
  636. .journalism .block {
  637. font-size: 5px;
  638. text-align: center;
  639. margin-top: 25px;
  640. margin-bottom: 25px;
  641. }
  642. .journalism .el-dialog {
  643. width: 60%;
  644. }
  645. .journalism .el-dialog__header, .el-dialog__body {
  646. padding: 0 20px;
  647. }
  648. .journalism .tou {
  649. font-size: 20px;
  650. height: 30px;
  651. line-height: 30px;
  652. padding-top: 15px;
  653. }
  654. .journalism .line {
  655. margin-top: 15px;
  656. margin-bottom: 15px;
  657. width: 100%;
  658. height: 2px;
  659. background-color: #d9d9d9;
  660. }
  661. .journalism .xinxi {
  662. text-align: center;
  663. margin: 15px auto;
  664. font-size: 18px;
  665. }
  666. </style>