index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <template>
  2. <div class="app-container">
  3. <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
  4. <el-form-item label="分类" prop="categoryId">
  5. <el-select v-model="queryParams.categoryId" placeholder="请选择分类" clearable size="small">
  6. <el-option
  7. v-for="dict in categoryOptions"
  8. :key="dict.categoryId"
  9. :label="dict.categoryName"
  10. :value="dict.categoryId"
  11. />
  12. </el-select>
  13. </el-form-item>
  14. <el-form-item label="标题" prop="title">
  15. <el-input
  16. v-model="queryParams.title"
  17. placeholder="请输入标题"
  18. clearable
  19. size="small"
  20. @keyup.enter.native="handleQuery"
  21. />
  22. </el-form-item>
  23. <el-form-item label="发布状态" prop="publicStatus">
  24. <el-select v-model="queryParams.publicStatus" placeholder="请选择发布状态" clearable size="small">
  25. <el-option label="已发布" value="1" />
  26. <el-option label="草稿" value="2" />
  27. </el-select>
  28. </el-form-item>
  29. <el-form-item>
  30. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  31. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  32. </el-form-item>
  33. </el-form>
  34. <el-row :gutter="10" class="mb8">
  35. <el-col :span="1.5">
  36. <el-button
  37. type="primary"
  38. icon="el-icon-plus"
  39. size="mini"
  40. @click="handleAdd"
  41. v-hasPermi="['store:homeArticle:add']"
  42. >新增</el-button>
  43. </el-col>
  44. <el-col :span="1.5">
  45. <el-button
  46. type="success"
  47. icon="el-icon-edit"
  48. size="mini"
  49. :disabled="single"
  50. @click="handleUpdate"
  51. v-hasPermi="['store:homeArticle:edit']"
  52. >修改</el-button>
  53. </el-col>
  54. <el-col :span="1.5">
  55. <el-button
  56. type="danger"
  57. icon="el-icon-delete"
  58. size="mini"
  59. :disabled="multiple"
  60. @click="handleDelete"
  61. v-hasPermi="['store:homeArticle:remove']"
  62. >删除</el-button>
  63. </el-col>
  64. <!-- <el-col :span="1.5">-->
  65. <!-- <el-button-->
  66. <!-- type="warning"-->
  67. <!-- icon="el-icon-download"-->
  68. <!-- size="mini"-->
  69. <!-- @click="handleExport"-->
  70. <!-- v-hasPermi="['store:homeArticle:export']"-->
  71. <!-- >导出</el-button>-->
  72. <!-- </el-col>-->
  73. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  74. </el-row>
  75. <el-table v-loading="loading" :data="homeArticleList" @selection-change="handleSelectionChange" border>
  76. <el-table-column type="selection" width="55" align="center" />
  77. <el-table-column label="分类" align="center" prop="categoryName" />
  78. <el-table-column label="标题" align="center" prop="title" />
  79. <el-table-column label="封面图片" align="center" prop="imageUrl">
  80. <template slot-scope="scope">
  81. <el-image
  82. style="width: 80px; height: 50px"
  83. :src="scope.row.imageUrl"
  84. :preview-src-list="[scope.row.imageUrl]"
  85. fit="cover">
  86. </el-image>
  87. </template>
  88. </el-table-column>
  89. <el-table-column label="浏览数" align="center" prop="views" width="80" />
  90. <el-table-column label="发布时间" align="center" prop="publishTime" width="100">
  91. <template slot-scope="scope">
  92. <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
  93. </template>
  94. </el-table-column>
  95. <el-table-column label="创建时间" align="center" prop="createTime" width="160"/>
  96. <el-table-column label="发布状态" align="center" prop="publicStatus" width="100">
  97. <template slot-scope="scope">
  98. <el-tag type="success" v-if="scope.row.publicStatus == 1">已发布</el-tag>
  99. <el-tag type="info" v-else-if="scope.row.publicStatus == 2">草稿</el-tag>
  100. </template>
  101. </el-table-column>
  102. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  103. <template slot-scope="scope">
  104. <el-button
  105. size="mini"
  106. type="text"
  107. icon="el-icon-edit"
  108. @click="handleUpdate(scope.row)"
  109. v-hasPermi="['store:homeArticle:edit']"
  110. >修改</el-button>
  111. <el-button
  112. size="mini"
  113. type="text"
  114. icon="el-icon-delete"
  115. @click="handleDelete(scope.row)"
  116. v-hasPermi="['store:homeArticle:remove']"
  117. >删除</el-button>
  118. </template>
  119. </el-table-column>
  120. </el-table>
  121. <pagination
  122. v-show="total>0"
  123. :total="total"
  124. :page.sync="queryParams.pageNum"
  125. :limit.sync="queryParams.pageSize"
  126. @pagination="getList"
  127. />
  128. <!-- 添加或修改文章对话框 -->
  129. <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body :close-on-click-modal="false">
  130. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  131. <el-form-item label="分类" prop="categoryId">
  132. <el-select v-model="form.categoryId" placeholder="请选择分类" filterable>
  133. <el-option
  134. v-for="dict in categoryOptions"
  135. :key="dict.categoryId"
  136. :label="dict.categoryName"
  137. :value="dict.categoryId"
  138. />
  139. </el-select>
  140. </el-form-item>
  141. <el-form-item label="标题" prop="title">
  142. <el-input v-model="form.title" placeholder="请输入标题" maxlength="100"/>
  143. </el-form-item>
  144. <el-form-item label="描述" prop="description">
  145. <el-input v-model="form.description" placeholder="请输入描述内容" maxlength="200"/>
  146. </el-form-item>
  147. <el-form-item label="封面图片" prop="imageUrl">
  148. <el-upload
  149. class="avatar-uploader"
  150. :action="uploadUrl"
  151. :show-file-list="false"
  152. :on-success="handleImageUrlSuccess"
  153. :before-upload="beforeImageUrlUpload">
  154. <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar" width="200px">
  155. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  156. </el-upload>
  157. </el-form-item>
  158. <el-form-item label="是否推荐" prop="isTui">
  159. <el-radio-group v-model="form.isTui">
  160. <el-radio :label="1">是</el-radio>
  161. <el-radio :label="0">否</el-radio>
  162. </el-radio-group>
  163. </el-form-item>
  164. <el-form-item label="视频地址" prop="videoUrl">
  165. <div>
  166. <el-upload
  167. ref="upload"
  168. class="upload-demo"
  169. :action="uploadUrl"
  170. :on-success="handleVideoSuccess"
  171. :before-upload="beforeVideoUpload"
  172. :limit="1"
  173. :accept="videoAccept"
  174. >
  175. <el-button size="small" type="primary">点击上传视频</el-button>
  176. </el-upload>
  177. <video v-if="form.videoUrl" :src="form.videoUrl" controls style="max-width: 400px; max-height: 200px;"></video>
  178. </div>
  179. </el-form-item>
  180. <el-form-item label="内容" prop="content">
  181. <Editor ref="myeditor" @on-text-change="updateText"/>
  182. </el-form-item>
  183. <el-row>
  184. <el-col :span="12">
  185. <el-form-item label="浏览数" prop="views">
  186. <el-input-number v-model="form.views" :min="0" placeholder="请输入浏览数" />
  187. </el-form-item>
  188. </el-col>
  189. <el-col :span="12">
  190. <el-form-item label="排序" prop="sort">
  191. <el-input-number v-model="form.sort" :min="0" placeholder="请输入排序" />
  192. </el-form-item>
  193. </el-col>
  194. </el-row>
  195. <el-form-item label="发布状态" prop="publicStatus">
  196. <el-select v-model="form.publicStatus" placeholder="请选择发布状态">
  197. <el-option label="已发布" value="1" />
  198. <el-option label="草稿" value="2" />
  199. </el-select>
  200. </el-form-item>
  201. </el-form>
  202. <div slot="footer" class="dialog-footer">
  203. <el-button type="primary" @click="submitForm">确 定</el-button>
  204. <el-button @click="cancel">取 消</el-button>
  205. </div>
  206. </el-dialog>
  207. </div>
  208. </template>
  209. <script>
  210. import { listHomeArticle, getHomeArticle, delHomeArticle, addHomeArticle, updateHomeArticle, exportHomeArticle } from "@/api/his/homeArticle";
  211. import { listHomeCategory, allListHomeCategory } from "@/api/his/homeCategory";
  212. import Editor from '@/components/Editor/wang';
  213. export default {
  214. name: "HomeArticle",
  215. components: { Editor },
  216. data() {
  217. return {
  218. // 遮罩层
  219. loading: true,
  220. // 选中数组
  221. ids: [],
  222. // 非单个禁用
  223. single: true,
  224. // 非多个禁用
  225. multiple: true,
  226. // 显示搜索条件
  227. showSearch: true,
  228. // 总条数
  229. total: 0,
  230. // 文章表格数据
  231. homeArticleList: [],
  232. // 分类选项
  233. categoryOptions: [],
  234. // 上传URL
  235. uploadUrl: process.env.VUE_APP_BASE_API + "/common/uploadOSS",
  236. // 视频接受类型
  237. videoAccept: "video/*",
  238. // 弹出层标题
  239. title: "",
  240. // 是否显示弹出层
  241. open: false,
  242. // 查询参数
  243. queryParams: {
  244. pageNum: 1,
  245. pageSize: 10,
  246. categoryId: null,
  247. title: null,
  248. publicStatus: null
  249. },
  250. // 表单参数
  251. form: {},
  252. // 表单校验
  253. rules: {
  254. categoryId: [
  255. { required: true, message: "分类不能为空", trigger: "change" }
  256. ],
  257. title: [
  258. { required: true, message: "标题不能为空", trigger: "blur" }
  259. ],
  260. description: [
  261. { required: true, message: "发布状态不能为空", trigger: "change" }
  262. ],
  263. imageUrl: [
  264. { required: true, message: "封面图片不能为空", trigger: "change" }
  265. ],
  266. publicStatus: [
  267. { required: true, message: "发布状态不能为空", trigger: "change" }
  268. ]
  269. }
  270. };
  271. },
  272. created() {
  273. this.getList();
  274. this.getCategoryOptions();
  275. },
  276. methods: {
  277. /** 获取分类选项 */
  278. getCategoryOptions() {
  279. allListHomeCategory().then(response => {
  280. this.categoryOptions = response.rows;
  281. });
  282. },
  283. /** 查询文章列表 */
  284. getList() {
  285. this.loading = true;
  286. listHomeArticle(this.queryParams).then(response => {
  287. this.homeArticleList = response.rows.list;
  288. this.total = response.rows.total;
  289. this.loading = false;
  290. });
  291. },
  292. // 视频上传成功处理
  293. handleVideoSuccess(response, file) {
  294. if (response.code == 200) {
  295. this.form.videoUrl = response.url;
  296. this.$refs.upload.clearFiles();
  297. } else {
  298. this.msgError(response.msg);
  299. }
  300. },
  301. // 视频上传前的验证
  302. beforeVideoUpload(file) {
  303. const isLt200M = file.size / 1024 / 1024 < 200;
  304. if (!isLt200M) {
  305. this.$message.error('上传视频文件大小不能超过200MB!');
  306. return false;
  307. }
  308. return isLt200M;
  309. },
  310. // 更新富文本内容
  311. updateText(text) {
  312. this.form.content = text;
  313. },
  314. // 图片上传成功处理
  315. handleImageUrlSuccess(res, file) {
  316. if (res.code == 200) {
  317. this.form.imageUrl = res.url;
  318. this.$forceUpdate();
  319. } else {
  320. this.msgError(res.msg);
  321. }
  322. },
  323. // 图片上传前的验证
  324. beforeImageUrlUpload(file) {
  325. const isLt1M = file.size / 1024 / 1024 < 1;
  326. if (!isLt1M) {
  327. this.$message.error('上传图片大小不能超过1MB!');
  328. }
  329. return isLt1M;
  330. },
  331. // 取消按钮
  332. cancel() {
  333. this.open = false;
  334. this.reset();
  335. },
  336. // 表单重置
  337. reset() {
  338. this.form = {
  339. articleId: null,
  340. categoryId: null,
  341. title: null,
  342. description: null,
  343. imageUrl: null,
  344. isTui: 0,
  345. videoUrl: null,
  346. content: null,
  347. views: 0,
  348. sort: 0,
  349. publishTime: null,
  350. publicStatus: "1"
  351. };
  352. this.resetForm("form");
  353. if (this.$refs.myeditor) {
  354. this.$refs.myeditor.setText("");
  355. }
  356. },
  357. /** 搜索按钮操作 */
  358. handleQuery() {
  359. this.queryParams.pageNum = 1;
  360. this.getList();
  361. },
  362. /** 重置按钮操作 */
  363. resetQuery() {
  364. this.resetForm("queryForm");
  365. this.handleQuery();
  366. },
  367. // 多选框选中数据
  368. handleSelectionChange(selection) {
  369. this.ids = selection.map(item => item.articleId)
  370. this.single = selection.length!==1
  371. this.multiple = !selection.length
  372. },
  373. /** 新增按钮操作 */
  374. handleAdd() {
  375. this.reset();
  376. this.open = true;
  377. this.title = "添加文章";
  378. setTimeout(() => {
  379. if (this.$refs.myeditor) {
  380. this.$refs.myeditor.setText("");
  381. }
  382. }, 100);
  383. },
  384. /** 修改按钮操作 */
  385. handleUpdate(row) {
  386. this.reset();
  387. const articleId = row.articleId || this.ids
  388. getHomeArticle(articleId).then(response => {
  389. this.form = response.data;
  390. // 确保状态是字符串类型
  391. this.form.publicStatus = this.form.publicStatus ? this.form.publicStatus.toString() : "1";
  392. this.open = true;
  393. this.title = "修改文章";
  394. setTimeout(() => {
  395. if (this.$refs.myeditor) {
  396. if (this.form.content == null) {
  397. this.$refs.myeditor.setText("");
  398. } else {
  399. this.$refs.myeditor.setText(this.form.content);
  400. }
  401. }
  402. }, 100);
  403. });
  404. },
  405. /** 提交按钮 */
  406. submitForm() {
  407. this.$refs["form"].validate(valid => {
  408. if (valid) {
  409. if (this.form.articleId != null) {
  410. updateHomeArticle(this.form).then(response => {
  411. if (response.code === 200) {
  412. this.msgSuccess("修改成功");
  413. this.open = false;
  414. this.getList();
  415. }
  416. });
  417. } else {
  418. addHomeArticle(this.form).then(response => {
  419. if (response.code === 200) {
  420. this.msgSuccess("新增成功");
  421. this.open = false;
  422. this.getList();
  423. }
  424. });
  425. }
  426. }
  427. });
  428. },
  429. /** 删除按钮操作 */
  430. handleDelete(row) {
  431. const articleIds = row.articleId || this.ids;
  432. this.$confirm('是否确认删除该文章?', "警告", {
  433. confirmButtonText: "确定",
  434. cancelButtonText: "取消",
  435. type: "warning"
  436. }).then(function() {
  437. return delHomeArticle(articleIds);
  438. }).then(() => {
  439. this.getList();
  440. this.msgSuccess("删除成功");
  441. }).catch(function() {});
  442. },
  443. // /** 导出按钮操作 */
  444. // handleExport() {
  445. // const queryParams = this.queryParams;
  446. // this.$confirm('是否确认导出所有文章数据项?', "警告", {
  447. // confirmButtonText: "确定",
  448. // cancelButtonText: "取消",
  449. // type: "warning"
  450. // }).then(function() {
  451. // return exportHomeArticle(queryParams);
  452. // }).then(response => {
  453. // this.download(response.msg);
  454. // }).catch(function() {});
  455. // }
  456. }
  457. };
  458. </script>
  459. <style>
  460. .avatar-uploader .el-upload {
  461. border: 1px dashed #d9d9d9;
  462. border-radius: 6px;
  463. cursor: pointer;
  464. position: relative;
  465. overflow: hidden;
  466. }
  467. .avatar-uploader .el-upload:hover {
  468. border-color: #409EFF;
  469. }
  470. .avatar-uploader-icon {
  471. font-size: 28px;
  472. color: #8c939d;
  473. width: 150px;
  474. height: 150px;
  475. line-height: 150px;
  476. text-align: center;
  477. }
  478. .avatar {
  479. width: 150px;
  480. height: 150px;
  481. display: block;
  482. }
  483. </style>