category.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <template>
  2. <div class="category-box" v-loading="loading > 0">
  3. <Header :title="title"></Header>
  4. <div class="container">
  5. <div class="content">
  6. <!-- 表格 -->
  7. <div class="btn" @click="categoryModal('add')">+ 添加{{name}}分类</div>
  8. <el-table
  9. :data="categoryList"
  10. class="table no-border-table"
  11. ref="newsTable"
  12. :header-cell-style="{
  13. border: 'none',
  14. color: '#333',
  15. background: '#f6f6f6',
  16. borderBottom: '1px solid #E8E8E8',
  17. height: '54px',
  18. }"
  19. :row-style="{
  20. height: '54px',
  21. }"
  22. >
  23. <el-table-column
  24. prop="name"
  25. align="center"
  26. label="名称"
  27. width="120"
  28. ></el-table-column>
  29. <el-table-column
  30. prop="sort"
  31. align="left"
  32. label="排序"
  33. ></el-table-column>
  34. <el-table-column
  35. prop="created"
  36. align="left"
  37. label="编辑日期"
  38. ></el-table-column>
  39. <el-table-column prop="created" align="left" label="是否显示">
  40. <div slot-scope="scope">
  41. <i
  42. v-if="scope.row.status === 1"
  43. class="iconfont icon-checkbox checked-icon"
  44. ></i>
  45. <i v-else class="iconfont icon-checkbox no-checked-icon"></i>
  46. </div>
  47. </el-table-column>
  48. <el-table-column label="操作" align="left" width="240">
  49. <div slot-scope="scope">
  50. <div>
  51. <button
  52. size="mini"
  53. class="table-btn"
  54. @click="categoryModal('edit', scope.row)"
  55. >
  56. 编辑
  57. </button>
  58. <button
  59. size="mini"
  60. class="table-btn"
  61. @click="deleteCategory(scope.row.id)"
  62. >
  63. 删除
  64. </button>
  65. </div>
  66. </div>
  67. </el-table-column>
  68. </el-table>
  69. </div>
  70. <!-- 分页按钮 -->
  71. <div class="footer">
  72. <el-pagination
  73. @current-change="handleCurrentChange"
  74. :current-page.sync="currentPage"
  75. background
  76. layout="total, prev, pager, next, jumper"
  77. :page-size="size"
  78. :total="total"
  79. ></el-pagination>
  80. </div>
  81. </div>
  82. <!-- 发布分类弹窗 -->
  83. <el-dialog width="456px" :visible.sync="categoryShow">
  84. <div slot="title">
  85. <p class="dialog-title">
  86. {{ type === "add" ? "添加" : "编辑" }}{{name}}分类
  87. </p>
  88. </div>
  89. <div class="dialog-content">
  90. <el-form
  91. :rules="rules"
  92. :model="form"
  93. ref="categoryForm"
  94. class="form"
  95. label-width="180px"
  96. >
  97. <el-form-item label="分类名称:" prop="name">
  98. <el-input
  99. class="category-input"
  100. size="small"
  101. style="width: 150px"
  102. v-model="form.name"
  103. placeholder="请输入分类名称"
  104. ></el-input>
  105. </el-form-item>
  106. <el-form-item label="输入排序:">
  107. <el-input
  108. class="category-input"
  109. size="small"
  110. style="width: 60px"
  111. type="number"
  112. onkeypress="return( /[\d]/.test(String.fromCharCode(event.keyCode)))"
  113. v-model.number="form.sort"
  114. placeholder=""
  115. ></el-input>
  116. </el-form-item>
  117. <el-form-item label="展示:">
  118. <el-checkbox v-model="form.status">是否展示该分类</el-checkbox>
  119. </el-form-item>
  120. </el-form>
  121. </div>
  122. <div slot="footer" class="dialog-footer">
  123. <button
  124. class="btn cancel-btn"
  125. size="small"
  126. @click="categoryShow = false"
  127. >
  128. 取 消
  129. </button>
  130. <button class="btn confirm-btn" size="small" @click="handleCategory">
  131. 确 定
  132. </button>
  133. </div>
  134. </el-dialog>
  135. </div>
  136. </template>
  137. <script type="text/ecmascript-6">
  138. import dayjs from 'dayjs';
  139. import Header from '../../components/common/header';
  140. export default {
  141. components: {
  142. Header
  143. },
  144. name: 'category',
  145. data() {
  146. return {
  147. currentPage: 1, // 当前页码
  148. size: 20, // 每页条数
  149. total: 0, // 总页数
  150. loading: 0, // 加载中
  151. type: '', // edit:修改 add:添加
  152. categoryInfo: {}, // 分类信息
  153. categoryList: [], // 分类列表
  154. categoryShow: false, // 分类弹窗
  155. form: {
  156. // 表单数据
  157. name: '',
  158. class: null,
  159. label: 0,
  160. sort: 0,
  161. status: false
  162. },
  163. rules: {
  164. // 表单验证规则
  165. name: [{ required: true, message: '请输入内容', trigger: 'blur' }]
  166. }
  167. };
  168. },
  169. props: {
  170. name: {
  171. type: String,
  172. value: ''
  173. },
  174. classNum: {
  175. type: Number
  176. },
  177. title: {
  178. type: Object,
  179. value: {
  180. // 页面标题
  181. firstTitile: '',
  182. secondTitle: ''
  183. }
  184. }
  185. },
  186. mounted() {
  187. // 获取列表
  188. this.getCategoryList();
  189. },
  190. filters: {
  191. // 时间格式化
  192. dateFormat(date) {
  193. if (!date) return '请选择时间';
  194. let day = dayjs(date);
  195. return day.format('YYYY-MM-DD HH:mm:ss');
  196. }
  197. },
  198. computed: {},
  199. methods: {
  200. // 切换页码
  201. handleCurrentChange(page) {
  202. this.currentPage = page;
  203. this.getCategoryList();
  204. },
  205. // 获取分类列表
  206. getCategoryList() {
  207. this.loading++;
  208. this.httpGet(this.$root.getCategoryList, {
  209. page: this.currentPage,
  210. page_size: this.size,
  211. class: this.classNum
  212. }).then(
  213. (res) => {
  214. this.loading--;
  215. this.categoryList = res.list;
  216. this.total = res.total;
  217. },
  218. (res) => {
  219. this.loading--;
  220. this.$message.error(res);
  221. }
  222. );
  223. },
  224. // 分类弹窗
  225. categoryModal(type, rowData) {
  226. this.$refs.categoryForm && this.$refs.categoryForm.clearValidate();
  227. this.type = type;
  228. if (type === 'edit') {
  229. this.categoryInfo = rowData;
  230. this.form = {
  231. name: rowData.name,
  232. class: this.classNum,
  233. sort: rowData.sort,
  234. status: rowData.status === 1,
  235. label: 0
  236. };
  237. } else if (type === 'add') {
  238. this.form = {
  239. name: '',
  240. class: this.classNum,
  241. label: 0,
  242. sort: 0,
  243. status: false
  244. };
  245. }
  246. this.categoryShow = true;
  247. },
  248. // 确认添加/修改分类
  249. handleCategory() {
  250. this.$refs.categoryForm.validate((valid) => {
  251. if (valid) {
  252. this.loading++;
  253. let inputData = {};
  254. let url = '';
  255. if (this.type === 'add') {
  256. url = this.$root.addCategory;
  257. inputData = { ...this.form };
  258. } else if (this.type === 'edit') {
  259. url = this.$root.updateCategory;
  260. inputData = {
  261. id: this.categoryInfo.id,
  262. ...this.form
  263. };
  264. }
  265. this.httpPost(url, {
  266. ...inputData,
  267. status: inputData.status ? 1 : 2
  268. }).then(
  269. (res) => {
  270. this.$message({
  271. message: '操作成功',
  272. type: 'success'
  273. });
  274. this.loading--;
  275. this.categoryShow = false;
  276. this.getCategoryList();
  277. },
  278. (res) => {
  279. this.loading--;
  280. this.$message.error(res);
  281. }
  282. );
  283. } else {
  284. return false;
  285. }
  286. });
  287. },
  288. // 删除分类
  289. deleteCategory(id) {
  290. this.$confirm(
  291. `<p class='title'><i class='icon el-icon-question'></i>确认要删除这条${this.name}分类吗?</p><p class='text'>此操作将永久删除该条信息,要继续完成该删除动作吗?</p>`,
  292. {
  293. confirmButtonText: '确定',
  294. cancelButtonText: '取消',
  295. cancelButtonClass: 'cancel-btn',
  296. confirmButtonClass: 'confirm-btn',
  297. customClass: 'confirm-box',
  298. dangerouslyUseHTMLString: true,
  299. showClose: false,
  300. type: 'none'
  301. }
  302. ).then(() => {
  303. this.loading++;
  304. this.httpPost(this.$root.deleteCategory, {
  305. class: 1,
  306. id
  307. }).then(
  308. (res) => {
  309. this.$message({
  310. message: '操作成功',
  311. type: 'success'
  312. });
  313. this.loading--;
  314. this.getCategoryList();
  315. },
  316. (res) => {
  317. this.loading--;
  318. this.$message.error(res);
  319. }
  320. );
  321. });
  322. }
  323. }
  324. };
  325. </script>
  326. <style lang="stylus" rel="stylesheet/stylus">
  327. @import '~assets/public.styl';
  328. </style>
  329. <style lang="stylus" rel="stylesheet/stylus" scoped>
  330. @import '~assets/main.styl';
  331. .category-box {
  332. height: 100%;
  333. width: 100%;
  334. flex-y(flex-start, flex-start);
  335. overflow-y: hidden;
  336. background: bg-color;
  337. .container {
  338. box-sizing: border-box;
  339. height: 100%;
  340. width: 100%;
  341. overflow-y: auto;
  342. flex-y(flex-start, flex-start);
  343. padding: 24px;
  344. // content 表格
  345. .content {
  346. flex: 1;
  347. width: 100%;
  348. font-size: 14px;
  349. background: white;
  350. box-sizing: border-box;
  351. padding: 24px 32px;
  352. border-radius: 2px;
  353. .checked-icon {
  354. color: btn-color;
  355. font-size: 16px;
  356. }
  357. .no-checked-icon {
  358. color: #aaa;
  359. }
  360. // 按钮
  361. .btn {
  362. // width: 100px;
  363. display: inline-block;
  364. padding: 0 10px;
  365. height: 32px;
  366. line-height: 32px;
  367. confirm-btn();
  368. font-size: 14px;
  369. margin-bottom: 16px;
  370. }
  371. // 表格按钮
  372. .table-btn {
  373. width: 65px;
  374. height: 24px;
  375. line-height: 24px;
  376. color: gray3;
  377. font-size: 12px;
  378. margin-right: 8px;
  379. cancel-btn();
  380. }
  381. }
  382. }
  383. .footer {
  384. width: 100%;
  385. padding: 24px;
  386. box-sizing: border-box;
  387. flex-x(flex-end);
  388. background: white;
  389. }
  390. .dialog-content {
  391. flex-y();
  392. .form {
  393. width: 100%;
  394. // flex-y();
  395. }
  396. }
  397. }
  398. </style>