journalism.vue 22 KB

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