# 为什么使用uni-config-center
实际开发中很多插件需要配置文件才可以正常运行,如果每个插件都单独进行配置的话就会产生下面这样的目录结构
```bash
cloudfunctions
└─────common 公共模块
├─plugin-a // 插件A对应的目录
│ ├─index.js
│ ├─config.json // plugin-a对应的配置文件
│ └─other-file.cert // plugin-a依赖的其他文件
└─plugin-b // plugin-b对应的目录
├─index.js
└─config.json // plugin-b对应的配置文件
```
假设插件作者要发布一个项目模板,里面使用了很多需要配置的插件,无论是作者发布还是用户使用都是一个大麻烦。
uni-config-center就是用了统一管理这些配置文件的,使用uni-config-center后的目录结构如下
```bash
cloudfunctions
└─────common 公共模块
├─plugin-a // 插件A对应的目录
│ └─index.js
├─plugin-b // plugin-b对应的目录
│ └─index.js
└─uni-config-center
├─index.js // config-center入口文件
├─plugin-a
│ ├─config.json // plugin-a对应的配置文件
│ └─other-file.cert // plugin-a依赖的其他文件
└─plugin-b
└─config.json // plugin-b对应的配置文件
```
使用uni-config-center后的优势
- 配置文件统一管理,分离插件主体和配置信息,更新插件更方便
- 支持对config.json设置schema,插件使用者在HBuilderX内编写config.json文件时会有更好的提示(后续HBuilderX会提供支持)
# 用法
在要使用uni-config-center的公共模块或云函数内引入uni-config-center依赖,请参考:[使用公共模块](https://uniapp.dcloud.net.cn/uniCloud/cf-common)
```js
const createConfig = require('uni-config-center')
const uniIdConfig = createConfig({
pluginId: 'uni-id', // 插件id
defaultConfig: { // 默认配置
tokenExpiresIn: 7200,
tokenExpiresThreshold: 600,
},
customMerge: function(defaultConfig, userConfig) { // 自定义默认配置和用户配置的合并规则,不设置的情况侠会对默认配置和用户配置进行深度合并
// defaudltConfig 默认配置
// userConfig 用户配置
return Object.assign(defaultConfig, userConfig)
}
})
// 以如下配置为例
// {
// "tokenExpiresIn": 7200,
// "passwordErrorLimit": 6,
// "bindTokenToDevice": false,
// "passwordErrorRetryTime": 3600,
// "app-plus": {
// "tokenExpiresIn": 2592000
// },
// "service": {
// "sms": {
// "codeExpiresIn": 300
// }
// }
// }
// 获取配置
uniIdConfig.config() // 获取全部配置,注意:uni-config-center内不存在对应插件目录时会返回空对象
uniIdConfig.config('tokenExpiresIn') // 指定键值获取配置,返回:7200
uniIdConfig.config('service.sms.codeExpiresIn') // 指定键值获取配置,返回:300
uniIdConfig.config('tokenExpiresThreshold', 600) // 指定键值获取配置,如果不存在则取传入的默认值,返回:600
// 获取文件绝对路径
uniIdConfig.resolve('custom-token.js') // 获取uni-config-center/uni-id/custom-token.js文件的路径
// 引用文件(require)
uniIDConfig.requireFile('custom-token.js') // 使用require方式引用uni-config-center/uni-id/custom-token.js文件。文件不存在时返回undefined,文件内有其他错误导致require失败时会抛出错误。
// 判断是否包含某文件
uniIDConfig.hasFile('custom-token.js') // 配置目录是否包含某文件,true: 文件存在,false: 文件不存在
```
没有合适的资源?快使用搜索试试~ 我知道了~
疯狂猜歌疯狂猜歌赢现金小程序源码.zip
共245个文件
json:46个
js:43个
wxss:32个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 2 下载量 81 浏览量
2022-06-23
23:22:50
上传
评论 1
收藏 1.11MB ZIP 举报
温馨提示
疯狂猜歌疯狂猜歌赢现金小程序源码.zip
资源推荐
资源详情
资源评论
收起资源包目录
疯狂猜歌疯狂猜歌赢现金小程序源码.zip (245个子文件)
uniicons.css 8KB
.DS_Store 8KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
index.html 400B
vendor.js 305KB
vendor.js 113KB
index.js 112KB
renwu.js 43KB
game.js 40KB
turn.js 38KB
supplyModal.js 38KB
hbpage.js 37KB
pai.js 37KB
uni-icons.js 37KB
exchange.js 37KB
mine.js 37KB
index.js 36KB
titTop.js 36KB
musicResult.js 35KB
hbProgress.js 34KB
index.js 31KB
icons.js 26KB
main.js 23KB
runtime.js 12KB
index.js 11KB
index.js 6KB
renwu.js 5KB
apis.js 4KB
runtime.js 4KB
game.js 4KB
supplyModal.js 4KB
turn.js 3KB
index.js 3KB
pai.js 3KB
hbpage.js 3KB
mine.js 3KB
main.js 2KB
exchange.js 2KB
musicResult.js 2KB
titTop.js 2KB
uni-icons.js 2KB
hbProgress.js 2KB
public.js 1KB
request.js 641B
main.js 414B
app.js 89B
app.js 89B
manifest.json 3KB
pages.json 2KB
package.json 2KB
package.json 2KB
package.json 2KB
package.json 2KB
launch.json 573B
project.config.json 560B
project.config.json 560B
app.json 531B
app.json 531B
package.json 442B
package.json 354B
game.json 257B
game.json 257B
index.json 244B
index.json 244B
hbpage.json 214B
hbpage.json 214B
mine.json 200B
mine.json 200B
sitemap.json 191B
sitemap.json 191B
package.json 177B
exchange.json 140B
pai.json 140B
turn.json 140B
renwu.json 140B
exchange.json 140B
pai.json 140B
turn.json 140B
renwu.json 140B
titTop.json 124B
titTop.json 124B
hbProgress.json 108B
hbProgress.json 108B
musicResult.json 48B
index.json 48B
supplyModal.json 48B
uni-icons.json 48B
musicResult.json 48B
index.json 48B
supplyModal.json 48B
uni-icons.json 48B
.automator.json 0B
.automator.json 0B
vendor.js.map 432KB
共 245 条
- 1
- 2
- 3
资源评论
- 01111002023-06-15终于找到了超赞的宝藏资源,果断冲冲冲,支持!
- m0_622030842022-12-19资源很实用,对我启发很大,有很好的参考价值,内容详细。
「已注销」
- 粉丝: 794
- 资源: 3613
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功