更新记录

1.0.0(2026-06-05) 下载此版本

  • 初始版本发布
  • 集成 Pinia 状态管理
  • 提供持久化存储功能
  • 内置 App、User、Layout 三个 store 模块
  • 支持兼容旧代码的对象式 API

平台兼容性

uni-app(3.7.9)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
-
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
- - - - -

uni-app x(3.7.9)

Chrome Safari Android iOS 鸿蒙 微信小程序
- - - - -

dk-store 状态管理插件

一个基于 Pinia 的 uni-app 状态管理插件,提供持久化存储功能和常用 store 模块。

功能特性

  • 基于 Pinia 的状态管理
  • 自动持久化存储到本地
  • 包含常用 store 模块:App、User、Layout
  • 提供兼容旧代码的对象式 API
  • 支持 Vue2 和 Vue3

安装

在 HBuilderX 中导入本插件即可使用。

快速开始

在 main.js 中配置

import { createSSRApp } from 'vue'
import { createI18n } from 'vue-i18n'
import App from './App'
import i18n from '@/uni_modules/dk-i18n/js_sdk/index.js'
import { pinia, Pinia } from '@/uni_modules/dk-store/index.js'

i18n.init()

export function createApp() {
  const app = createSSRApp(App)
  app.use(pinia)

  const vue3I18n = createI18n(i18n.getI18nConfig())
  app.use(vue3I18n)

  return {
    app,
    Pinia
  }
}

使用 store

Composition API 风格

import { useAppStore, useUserStore, useLayoutStore } from '@/uni_modules/dk-store/index.js'

export default {
  setup() {
    const appStore = useAppStore()
    const userStore = useUserStore()

    // 使用状态
    console.log(appStore.locale)
    console.log(userStore.token)

    // 调用 actions
    appStore.setLocale('en')
    userStore.setToken('your-token')

    return {
      appStore,
      userStore
    }
  }
}

兼容对象式 API

import { App, User, Layout } from '@/uni_modules/dk-store/index.js'

// 获取状态
const locale = App.locale()
const token = User.token()

// 调用方法
App.setLocale('en')
User.setToken('your-token')

内置 Store 模块

App Store

应用配置模块,管理主题、语言、侧边栏等状态。

import { App, useAppStore } from '@/uni_modules/dk-store/index.js'

// 状态
App.locale()           // 当前语言
App.theme()            // 当前主题
App.sidebarCollapsed() // 侧边栏是否折叠
App.historyLocation()  // 历史位置

// 方法
App.setLocale(locale, callback)
App.setAppTheme(theme)
App.setSidebarCollapsed(value)
App.saveHistoryLocation(location)
App.reset()

User Store

用户信息模块,管理用户登录状态、权限等。

import { User, useUserStore } from '@/uni_modules/dk-store/index.js'

// 状态
User.token()           // Token
User.info()            // 用户信息
User.permissions()     // 权限
User.isGuest()         // 是否游客
User.isSuper()         // 是否超级管理员

// 方法
User.setToken(token)
User.setInfo(info)
User.loadPermissions(permissions)
User.can(permission)
User.orCan(permission1, permission2)
User.logout(callback)
User.reset()

Layout Store

布局模块,管理标签页、菜单等。

import { Layout, useLayoutStore } from '@/uni_modules/dk-store/index.js'

// 状态
Layout.state()         // 完整状态
Layout.state().tabs    // 标签页列表
Layout.state().choices // 选项列表
Layout.state().choiceId // 当前选项ID

// 方法
Layout.clear()
Layout.reset()

工具函数

import {
  cloneValue,
  createStorageModule,
  readStorageState,
  writeStorageState,
  resetStorageState,
  bindPersistedStore,
  filterState
} from '@/uni_modules/dk-store/index.js'

灵活的持久化配置

配置选项

bindPersistedStore 函数支持第三个参数,可以配置哪些字段需要持久化:

// 1. 白名单模式 - 只持久化指定字段
bindPersistedStore(store, key, {
  pick: ['token', 'info']  // 只持久化 token 和 info
})

// 2. 黑名单模式 - 排除某些字段不持久化
bindPersistedStore(store, key, {
  omit: ['permissions']    // 除了 permissions 外都持久化
})

// 3. 不配置 - 持久化所有字段(默认行为)
bindPersistedStore(store, key)

使用示例

在 store 中配置:

const getUserStore = () =>
    bindPersistedStore(useUserStore(pinia), USER_STORAGE_KEY, {
        pick: ['token', 'info']  // 只持久化 token 和 info
    });

注意事项

  • pickomit 同时存在时,pick 优先级更高
  • 未配置时默认持久化整个 store 的 state
  • 持久化的字段在读取时会与 initialState 合并

目录结构

uni_modules/dk-store/
├── uniCloud/
├── components/
├── utssdk/
├── hybrid/
├── pages/
├── static/
├── wxcomponents/
├── js_sdk/
│   ├── pinia.js           # Pinia 实例
│   ├── create-storage.js  # 存储工具
│   ├── app.store.js       # App store
│   ├── user.store.js      # User store
│   └── layout.store.js    # Layout store
├── index.js               # 入口文件(主)
├── license.md
├── package.json
├── readme.md
└── changelog.md

兼容性

  • HBuilderX 3.1.0+
  • uni-app 4.1.0+
  • uni-app x 4.2.0+

更新日志

changelog.md

许可证

license.md

隐私、权限声明

1. 本插件需要申请的系统权限列表:

none

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

none

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

none

许可协议

许可证

MIT License

Copyright (c) 2026 kordar

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

暂无用户评论。