index.js 1.47 KB
import { defineStore } from 'pinia';
import _ from 'lodash';

export const mainStore = defineStore('main', {
  state: () => {
    return {
      msg: 'Hello world',
      count: 0,
      auth: false,
      comment_num: 0,
      video_detail: {},
      scrollTop: 0,
      scrollTopCollection: 0,
      scrollTopLike: 0,
      scrollTopPerson: 0,
      keepPages: 'default', // 很坑爹,空值全部都缓存
    };
  },
  getters: {
    getKeepPages (state) {
      return state.keepPages
    }
  },
  actions: {
    changeState (state) {
      this.auth = state;
    },
    changeCommentNum (num) {
      this.comment_num = num;
    },
    changeVideoDetail (v) {
      this.video_detail = v;
    },
    changeScrollTop (v) {
      this.scrollTop = v;
    },
    changeScrollTopCollection (v) {
      this.scrollTopCollection = v;
    },
    changeScrollTopLike (v) {
      this.scrollTopLike = v;
    },
    changeScrollTopPerson (v) {
      this.scrollTopPerson = v;
    },
    changeKeepPages () { // 清空所有缓存,用一个不存在的值覆盖
      this.keepPages = 'default';
    },
    keepThisPage (page) { // 新增缓存页
      const arr = this.keepPages.split(",");
      arr.push(page);
      this.keepPages = arr + "";
    },
    removeThisPage (page) { // 删除缓存页
      const arr = this.keepPages.split(",");
      const index = arr.findIndex(x => x === page);
      if (index > 0) {
        arr.splice(index, 1);
      }
      this.keepPages = arr + "";
    }
  },
});