index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <template>
  2. <div class="admin-YouQingLianJie">
  3. <!-- 搜索 -->
  4. <div class="yqlj-search">
  5. <div>
  6. <el-input
  7. class="search-name"
  8. placeholder="请输入新闻分类名称"
  9. v-model="searchData"
  10. clearable>
  11. </el-input>
  12. <el-button size="mini" type="primary" @click="handleSearch">查询</el-button>
  13. </div>
  14. <div>
  15. <el-button size="mini" plain @click="handleDelete">删除</el-button>
  16. <el-button size="mini" type="primary" @click="handleOpenAdd">新增</el-button>
  17. </div>
  18. </div>
  19. <!-- table -->
  20. <myTable
  21. class="yqlj-table"
  22. @editor="handleOpenEditor"
  23. @delete="handleDelete"
  24. @select-change="handleSelectChange" />
  25. <!-- 分页 -->
  26. <myPagination
  27. class="yqlj-pagination"
  28. @size-change="handleSizeChange"
  29. @current-page="handleCurrentPage" />
  30. <!-- 弹窗 新增 -->
  31. <el-dialog
  32. title="新增新闻分类"
  33. :visible.sync="addDialogVisit"
  34. width="30%"
  35. @close="handleResetLinks('addForm')"
  36. center>
  37. <div class="add-dialog-content">
  38. <el-form :model="addForm" :rules="addRules" ref="addForm" label-width="80px" class="demo-ruleForm">
  39. <el-form-item label="名称:" prop="name">
  40. <el-input v-model="addForm.name"></el-input>
  41. </el-form-item>
  42. <el-form-item label="序号:" prop="number">
  43. <el-input v-model="addForm.number"></el-input>
  44. </el-form-item>
  45. </el-form>
  46. </div>
  47. <span slot="footer" class="dialog-footer">
  48. <el-button size="mini" @click="addDialogVisit = false">取 消</el-button>
  49. <el-button size="mini" type="primary" @click="handleAddLinks('addForm')">确 定</el-button>
  50. </span>
  51. </el-dialog>
  52. <!-- 弹窗 编辑 -->
  53. <el-dialog
  54. title="编辑新闻分类"
  55. :visible.sync="editorDialogVisit"
  56. width="30%"
  57. @close="handleEditorReset('editorForm')"
  58. @open="handleGetLinksInfo"
  59. center>
  60. <div class="add-dialog-content">
  61. <el-form :model="editorForm" :rules="editorRules" ref="editorForm" label-width="80px"
  62. class="demo-ruleForm">
  63. <el-form-item label="名称:" prop="name">
  64. <el-input v-model="editorForm.name"></el-input>
  65. </el-form-item>
  66. <el-form-item label="序号:" prop="number">
  67. <el-input v-model="editorForm.number"></el-input>
  68. </el-form-item>
  69. </el-form>
  70. </div>
  71. <span slot="footer" class="dialog-footer">
  72. <el-button size="mini" @click="editorDialogVisit = false">取 消</el-button>
  73. <el-button size="mini" type="primary" @click="handleEditorLinks('editorForm')">确 定</el-button>
  74. </span>
  75. </el-dialog>
  76. </div>
  77. </template>
  78. <script>
  79. import { tableServiceData } from './components/tableData';
  80. import { paginationServiceData } from './components/paginationData';
  81. import myPagination from './components/Pagination';
  82. import myTable from './components/Table';
  83. import * as NewsClassifyService from '@/api/newsClassify';
  84. import * as comminUtil from '@/utils/admin/tools';
  85. export default {
  86. name: 'YouQingLianJie',
  87. provide: {
  88. tableServiceData,
  89. paginationServiceData,
  90. },
  91. components: {
  92. myPagination,
  93. myTable,
  94. },
  95. data() {
  96. return {
  97. addDialogVisit: false,
  98. editorDialogVisit: false,
  99. searchData: '',
  100. tableSelectData: [],
  101. addForm: {
  102. name: '',
  103. number: 0,
  104. },
  105. addRules: {
  106. name: [
  107. { required: true, message: '友情链接名称不能为空', trigger: 'blur' },
  108. ],
  109. number: [
  110. { required: true, message: '序号不能为空', trigger: 'blur' },
  111. ],
  112. },
  113. editorForm: {
  114. name: '',
  115. number: 0,
  116. },
  117. editorRules: {
  118. name: [
  119. { required: true, message: '友情链接名称不能为空', trigger: 'blur' },
  120. ],
  121. number: [
  122. { required: true, message: '序号不能为空', trigger: 'blur' },
  123. ],
  124. },
  125. userId: null,
  126. selectRow: null,
  127. };
  128. },
  129. computed: {
  130. myPage() {
  131. return paginationServiceData.page;
  132. },
  133. mySize() {
  134. return paginationServiceData.size;
  135. },
  136. myTotal() {
  137. return paginationServiceData.total;
  138. },
  139. },
  140. methods: {
  141. handleResetLinks(formName) {
  142. this.$refs[formName].resetFields();
  143. this.addDialogVisit = false;
  144. },
  145. handleAddLinks(formName) {
  146. this.$refs[formName].validate((valid) => {
  147. if (valid) {
  148. this.addLinksFun();
  149. } else {
  150. return false;
  151. }
  152. });
  153. },
  154. async addLinksFun() {
  155. const loading = this.$loading({ background: 'rgba(0, 0, 0, 0.7)' });
  156. try {
  157. const opt = Object.assign({}, this.addForm, { userId: this.userId });
  158. const res = await NewsClassifyService.getNewsClassifyAdd(opt);
  159. if (res.code === 0) {
  160. this.addDialogVisit = false;
  161. const opt = this.getOption();
  162. const myData = await NewsClassifyService.getNewsClassifyList(opt);
  163. this.initPageData(myData.data);
  164. loading.close();
  165. }
  166. } catch (e) {
  167. loading.close();
  168. }
  169. this.addDialogVisit = false;
  170. },
  171. handleGetLinksInfo() {
  172. const opt = { newsClassifyId: this.selectRow.newsClassifyId };
  173. const loading = this.$loading({ background: 'rgba(0, 0, 0, 0.7)' });
  174. NewsClassifyService.getNewsClassifyInfo(opt)
  175. .then(res => {
  176. const { name, number } = res.data;
  177. this.editorForm.name = name;
  178. this.editorForm.number = number;
  179. loading.close();
  180. })
  181. .catch(err => {
  182. loading.close();
  183. });
  184. },
  185. handleEditorReset(formName) {
  186. this.$refs[formName].resetFields();
  187. this.addDialogVisit = false;
  188. },
  189. handleEditorLinks(formName) {
  190. this.$refs[formName].validate((valid) => {
  191. if (valid) {
  192. this.editorLinksFun();
  193. } else {
  194. return false;
  195. }
  196. });
  197. },
  198. async editorLinksFun() {
  199. const loading = this.$loading({ background: 'rgba(0, 0, 0, 0.7)' });
  200. try {
  201. const opt = Object.assign({}, this.editorForm, { userId: this.userId, newsClassifyId: this.selectRow.newsClassifyId });
  202. const res = await NewsClassifyService.getNewsClassifyUpdate(opt);
  203. if (res.code === 0) {
  204. this.editorDialogVisit = false;
  205. const opt = this.getOption();
  206. const myData = await NewsClassifyService.getNewsClassifyList(opt);
  207. this.initPageData(myData.data);
  208. loading.close();
  209. }
  210. loading.close();
  211. } catch (e) {
  212. loading.close();
  213. }
  214. this.addDialogVisit = false;
  215. },
  216. getOption() {
  217. const opt = {
  218. page: this.myPage,
  219. size: this.mySize,
  220. name: this.searchData,
  221. };
  222. return opt;
  223. },
  224. handleSearch() {
  225. paginationServiceData.page = 1;
  226. this.searchYouQingData();
  227. },
  228. handleOpenAdd() {
  229. this.addDialogVisit = true;
  230. },
  231. handleOpenEditor(data) {
  232. this.selectRow = data;
  233. this.editorDialogVisit = true;
  234. },
  235. async handleDelete() {
  236. if (!this.tableSelectData.length) {
  237. this.$message.warning('请选择至少一条需要删除的数据');
  238. return;
  239. }
  240. const opt = {
  241. newsClassifyIds: [...this.tableSelectData],
  242. };
  243. const loading = this.$loading({ background: 'rgba(0, 0, 0, 0.7)' });
  244. try {
  245. const res = await NewsClassifyService.getNewsClassifyDelete(opt);
  246. loading.close();
  247. if (res.code === 0 && res.data) {
  248. // 删除成功重置选择状态
  249. this.tableSelectData = [];
  250. // 刷新页面
  251. const newOpt = this.getOption();
  252. const res = await NewsClassifyService.getNewsClassifyList(newOpt);
  253. if (res.code === 0) {
  254. this.initPageData(res.data);
  255. }
  256. loading.close();
  257. }
  258. } catch (err) {
  259. loading.close();
  260. }
  261. },
  262. searchYouQingData() {
  263. const opt = this.getOption();
  264. const loading = this.$loading({ background: 'rgba(0, 0, 0, 0.7)' });
  265. NewsClassifyService.getNewsClassifyList(opt)
  266. .then(res => {
  267. this.initPageData(res.data);
  268. loading.close();
  269. })
  270. .catch(err => {
  271. loading.close();
  272. });
  273. },
  274. handleSizeChange(size) {
  275. paginationServiceData.size = size;
  276. this.searchYouQingData();
  277. },
  278. handleCurrentPage(page) {
  279. paginationServiceData.page = page;
  280. this.searchYouQingData();
  281. },
  282. handleSelectChange(data) {
  283. this.tableSelectData = data;
  284. },
  285. initPageData(listData) {
  286. tableServiceData.myData = listData.data;
  287. paginationServiceData.total = listData.total;
  288. },
  289. },
  290. created() {
  291. const userAuth = comminUtil.getUserAuth();
  292. this.userId = userAuth && userAuth.userId ? userAuth.userId : null;
  293. },
  294. mounted() {
  295. this.searchYouQingData();
  296. },
  297. };
  298. </script>
  299. <style lang="scss" scoped>
  300. .admin-YouQingLianJie {
  301. padding: 20px;
  302. .yqlj-search {
  303. display: flex;
  304. justify-content: space-between;
  305. margin-bottom: 20px;
  306. .search-name {
  307. width: 220px;
  308. margin-right: 10px;
  309. }
  310. }
  311. .yqlj-pagination {
  312. margin-top: 20px;
  313. }
  314. }
  315. </style>