news.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import language from "./common/language.js";
  2. import Service from "./common/service.js";
  3. new Vue({
  4. el: "#app",
  5. data: {
  6. list: [],
  7. total: 0,
  8. params: {
  9. page: 1,
  10. page_num: 6,
  11. },
  12. pagination_list: [],
  13. },
  14. mounted() {
  15. this.getList();
  16. },
  17. methods: {
  18. text(val) {
  19. return language[val][localStorage.getItem("language")];
  20. },
  21. lan_key(val) {
  22. return `${val}${localStorage.getItem("language") == "en" ? "_en" : ""}`;
  23. },
  24. changeCurrent(val) {
  25. if (val == this.params.page || val == "...") return;
  26. if (val < 1 || val > Math.ceil(this.total / this.params.page_num)) return;
  27. this.params.page = val;
  28. this.getList();
  29. },
  30. setPaginationList() {
  31. const allNum = Math.ceil(this.total / this.params.page_num);
  32. this.pagination_list = new Array(allNum)
  33. .fill(null)
  34. .map((_, index) => {
  35. if (index == 0 || index == allNum - 1) return index + 1;
  36. return Math.abs(index + 1 - this.params.page) >= 2
  37. ? Math.abs(index + 1 - this.params.page) == 2
  38. ? "..."
  39. : ""
  40. : index + 1;
  41. })
  42. .filter((i) => i);
  43. },
  44. getList() {
  45. Service.get_news(this.params)
  46. .then((res) => {
  47. this.list = res.data;
  48. this.total = res.total;
  49. this.setPaginationList();
  50. })
  51. .catch((err) => {
  52. console.log(err);
  53. });
  54. },
  55. },
  56. });