1.0.57 • Published 5 years ago

@jv2/egg-jv-common v1.0.57

Weekly downloads
51
License
MIT
Repository
github
Last release
5 years ago

jv 账号系统公共服务插件

服务于jv项目各个子系统的一个egg的公共服务插件

目前实现功能点:

  • 登录验证
  • 消息推送

插件配置项说明:

config.xx.js

exports.jvCommon = {
    app: false,
    defaultHost: 'http://jv.jm47.com',
    useMiddleAuth: true,  //登录验证中间件是否开启
    main: {
        "loginUrl": "http://jv.jm47.com/login", //用户中心设置登录
        "logoutUrl": "http://jv.jm47.com/logout", //用户中心设置登出
        "systemUrl": "http://jv.jm47.com", //请求地址
    },
    jvAppId: "A_v6gtjmQ4RH5i1ICRBt",//子系统的jvAppId
    ignorePath: /^\/api\/.*/  //中间件需要忽略的请求路由配置 单个正则或者正则数组都可以
}

1. 登录验证(中间件)

useMiddleAuth 控制整个中间件是否使用

//该中间件在每一次请求时验证是否登录,且登录态是否有效
详情见 /middleware/passportJvAuth.js

//可配置需要登录验证的接口规则,决定每个请求是否需要登录验证

//根据配置获取路由规则信息
let ignorePath = ctx.app.config.jvCommon.ignorePath;

let pathRegs = ignorePath.reduce((item, next) => {
    item.push(pathToRegexp(next));
    return item;
}, []);

//匹配到,则直接跳过中间件
for (let i = 0; i < pathRegs.length; i++) {
    if (pathRegs[i].exec(ctx.request.url)) {
        next();
        return;
    }
}

获取当前登录态

引入当前插件后,业务系统只需从context即可获取登录信息。

/**
 * 当前用户登录态
 * 只有已登录的用户才会有,否则为null
 */
currentSession:

session格式请参数基础系统里的session定义

2. 企业微信消息推送

目前支持:1.文本消息 2.文字卡片消息 3.图片信息 4.图文消息

入参参考 企业微信API

使用:

this.ctx.sendMsg({
    "touser": "UserID1|UserID2|UserID3",
    "toparty": "PartyID1|PartyID2",
    "totag": "TagID1 | TagID2",
    "msgtype": "text",
    "agentid": 1,
    "text": {
        "content": "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。"
    },
    "safe": 0,
    "enable_id_trans": 0,
    "enable_duplicate_check": 0,
});
//对curl做一层封装,便于以后扩展

const httpCallSymbol = Symbol.for('context#httpCall');

module.exports = {
    initDataType(headers) {
        let contentType = headers['content-type'] || '';
        let dataType;

        if (contentType.toLowerCase().startsWith('application/json')) {
            dataType = 'json';
        } else {
            dataType = this.app.config.curl.defDataType;
        }

        return dataType;
    },

    async curlGet(url, data = {}, headers = {}) {
        if (!url) {
            return null;
        }
        let dataType = this.initDataType(headers);
        return await this[httpCallSymbol](url, 'GET', data, dataType, headers);
    },
    async curlPost(url, data = {}, headers = {}) {
        if (!url) {
            return null;
        }
        let dataType = this.initDataType(headers);
        return await this[httpCallSymbol](url, 'POST', data, dataType, headers);
    },
    async [httpCallSymbol](url, method, data, dataType, headers) {
        
        const result = await this.ctx.curl(url, {
            beforeRequest: options => {
                for (const header in headers) {
                    options.headers[header] = headers[header];
                }
            },
            method: method,
            data: data,
            dataType: dataType,
            timeout: this.app.config.curl.timeout //连接和返回的超时时间
        });

        if (result.status !== 200) {
            this.logger.error(`[context httpCall] error: ${JSON.stringify(result)}`);
            return null;
        }
        return result.data;
    }
};

测试方法:

进入test/fixtures/apps/jv-common-server-test目录下

执行 npm inpm link ../../../.. ,完成后,npm run debug 或者 npm run start,项目启动后,修改项目代码,做插件测试。

关于httpclient请求的代理问题

在 config.xxxx.js 中配置如下:

exports.httpclient = {
      request: {
        enableProxy: true,
        rejectUnauthorized: false,
        proxy: process.env.http_proxy,
      },
    };

启动时, http_proxy=http://127.0.0.1:8888 npm run dev 在前面加上需要代理到的ip即可

1.0.57

5 years ago

1.0.56

5 years ago

1.0.54

5 years ago

1.0.53

6 years ago

1.0.51

6 years ago

1.0.49

6 years ago

1.0.47

6 years ago

1.0.46

6 years ago

1.0.44

6 years ago

1.0.43

6 years ago

1.0.42

6 years ago

1.0.41

6 years ago

1.0.40

6 years ago

1.0.39

6 years ago

1.0.38

6 years ago

1.0.37

6 years ago

1.0.36

6 years ago

1.0.35

6 years ago

1.0.34

6 years ago

1.0.33

6 years ago

1.0.32

6 years ago

1.0.31

6 years ago

1.0.30

6 years ago

1.0.29

6 years ago

1.0.28

6 years ago

1.0.27

6 years ago

1.0.26

6 years ago

1.0.25

6 years ago

1.0.24

6 years ago

1.0.23

6 years ago

1.0.22

6 years ago

1.0.21

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago