main.js 1.52 KB
/*
 * @Date: 2022-04-18 15:59:42
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2026-01-14 20:43:54
 * @FilePath: /xyxBooking-weapp/src/stores/main.js
 * @Description: 文件描述
 */
import { defineStore } from 'pinia'

/**
 * @description 全局主状态
 * - 存储用户信息与授权态等全局数据
 */
export const mainStore = defineStore('main', {
  state: () => {
    return {
      msg: 'Hello world',
      count: 0,
      auth: false,
      // keepPages: ['default'], // 小程序不支持这种 keep-alive 机制
      appUserInfo: null // 用户信息
    }
  },
  getters: {
    /**
     * @description 是否具备义工核销权限
     * @returns {boolean} true=义工,false=非义工
     */
    isVolunteer: state => {
      return !!(state.appUserInfo && state.appUserInfo.can_redeem === true)
    }
  },
  actions: {
    /**
     * @description 更新授权状态
     * @param {boolean} state 是否已授权
     * @returns {void} 无返回值
     */
    changeState(state) {
      this.auth = state
    },
    // setVolunteerStatus(status) {
    //   this.isVolunteer = status;
    // },
    // changeKeepPages () {
    //   this.keepPages = ['default'];
    // },
    // keepThisPage () {
    //   // 小程序路由缓存由框架控制
    // },
    // removeThisPage () {
    // },
    /**
     * @description 更新全局用户信息
     * @param {Object|null} info 用户信息对象
     * @returns {void} 无返回值
     */
    changeUserInfo(info) {
      this.appUserInfo = info
    }
  }
})