# 介绍

cool-admin插件基于midwayjs组件 (opens new window),形成自己特有的插件方式。

# redis插件示例

git clone https://github.com/cool-team-official/cool-admin-midway-plugin-demo.git

# 依赖

package.json中的devDependencies添加midwayjs-cool-core

yarn add midwayjs-cool-core -D

WARNING

注意不要添加到dependencies

# 插件信息

插件信息配置在package.json文件

示例

{
    "name": "midwayjs-cool-redis",
    "pluginName": "Redis缓存",
    "version": "1.1.6",
    "description": "替换系统的默认缓存为redis",
    "author": "cool官方",
    "contact": "邮箱:team@cool-js.com",
    "main": "dist/index.js",
    "typings": "dist/index.d.ts",
    "scripts": {
        "build": "midway-bin build -c",
        "test": "midway-bin test --ts",
        "cov": "midway-bin cov --ts",
        "ci": "npm run cov"
    },
    "keywords": [],
    "files": [
        "dist/**/*.js",
        "dist/**/*.d.ts",
        "dist/**/*.json"
    ],
    "license": "MIT",
    "devDependencies": {
        "@midwayjs/cli": "^1.2.40",
        "@midwayjs/core": "^2.7.0",
        "@midwayjs/decorator": "^2.7.0",
        "@midwayjs/orm": "^1.3.0",
        "@types/ioredis": "^4.19.3",
        "@types/jest": "^26.0.20",
        "@types/node": "14",
        "cross-env": "^7.0.3",
        "egg": "^2.29.1",
        "jest": "^26.6.3",
        "midwayjs-cool-core": "^3.0.19",
        "mwts": "^1.1.2",
        "ts-jest": "^26.5.0",
        "typescript": "^4.1.3"
    },
    "repository": {
        "type": "git",
        "url": "https://github.com/cool-team-official/cool-admin-midway"
    },
    "dependencies": {
        "ioredis": "^4.19.4"
    }
}

# 初始化

需要在midway组件的configuration.ts初始化你的插件

// src/configuration.ts
import { Configuration, Inject } from '@midwayjs/decorator';
import { ILifeCycle, IMidwayContainer } from '@midwayjs/core';
import { Logger } from '@midwayjs/decorator';
import { ILogger } from '@midwayjs/logger';
import { RedisCacheHandler } from './redis';
import { join } from 'path';
// @ts-ignore
import * as config from "./package.json";
import { CoolPlugin, COOL_CACHE_KEY } from 'midwayjs-cool-core';

@Configuration({
  // 按照命名规范: midwayjs-cool-空间名
  namespace: config.name.split('-')[2],
  importConfigs: [
    join(__dirname, 'config')
  ]
})
export class AutoConfiguration implements ILifeCycle {
  @Logger()
  coreLogger: ILogger;

  @Inject('cool:coolPlugin')
  coolPlugin: CoolPlugin;


  async onReady(container?: IMidwayContainer): Promise<void> {
    // 初始化你的插件
    await this.coolPlugin.install(RedisCacheHandler, async () => {
      this.coreLogger.info('\x1B[36m [cool:core] midwayjs cool redis component ready \x1B[0m');
    }, COOL_CACHE_KEY);
  }

}

# 配置

# 配置页

位于src/view.json,格式cool-admin-vue前端表单相同

[{
    "label": "配置",
    "prop": "redis",
    "component": {
        "name": "cl-codemirror",
        "attrs": {
            "placeholder": "{\"host\":\"127.0.0.1\",\"password\":\"\",\"port\":6379,\"db\":2}"
        },
        "props":{
            "height": "200px"
        }
    },
    "value": "{\"host\":\"127.0.0.1\",\"password\":\"\",\"port\":6379,\"db\":2}",
    "props": {
        "label-width": "80px"
    },
    "rules": {
        "required": true,
        "message": "值不能为空"
    }
}]

# 方法一

登录后台管理系统,插件管理/插件列表,点击配置

# 方法二

src/config/xxx.ts

config.cool = {
    // redis为插件名称
    redis: {
        host: "127.0.0.1",
        password: "",
        port: 6379,
        db: 0
    },
};

TIP

方法二比方法一优先取值,也就是如果存在方法二的配置,那么方法一的配置将失效

# 打包发布

yarn build

// 需要到npm官网注册账户密码
npm login

npm publish

WARNING

如果有将npm的镜像改为国内镜像,需要将其改为官方镜像

npm config set registry https://registry.npmjs.org

否则npm login会一直失败