File size: 1,344 Bytes
f0953a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { defineStore } from "pinia";
import { doubanApi } from "@/api/douban";
import { HotListItem } from "@/types/douban";
import { ElMessage } from "element-plus";

interface StoreType {
  hotList: HotListItem[];
  loading: boolean;
  currentParams: CurrentParams;
}

interface CurrentParams {
  type: string;
  tag?: string;
}

export const useDoubanStore = defineStore("douban", {
  state: (): StoreType => ({
    hotList: [],
    loading: false,
    currentParams: {
      type: "movie",
      tag: "热门",
    },
  }),

  actions: {
    async getHotList() {
      this.loading = true;
      try {
        const params = {
          type: this.currentParams.type,
          tag: this.currentParams.tag || "热门",
          page_limit: "20",
          page_start: "0",
        };
        const result = await doubanApi.getHotList(params);
        if (result && result.length > 0) {
          this.hotList = result;
        } else {
          console.log("获取热门列表失败");
          ElMessage.warning("获取热门列表失败");
        }
      } catch (error) {
        ElMessage.error(error || "获取热门列表失败");
      } finally {
        this.loading = false;
      }
    },
    setCurrentParams(currentParams: CurrentParams) {
      this.currentParams = currentParams;
      this.getHotList();
    },
  },
});