npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jv2/egg-jv-common-server

v1.0.0

Published

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

Downloads

3

Readme

jv 账号系统公共服务插件

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

目前实现功能点:

  • 登录验证
  • 消息推送

插件配置项说明:

config.xx.js


exports.jvCommon = {
    //登录验证中间件
    useMiddleAuth: true,
    port: {
        "loginUrl": "https://jv.oa.com/admin/login", //用户中心设置登录
        "logoutUrl": "https://jv.oa.com/admin/logout", //用户中心设置登出
        "mainSystemUrl": "https://jv.oa.com", //请求地址
    },
    appId: "A_v6gtjmQ4RH5i1ICRBt", //子系统的appid
    ignorePath: ['/', '/test/:id'] //中间件需要忽略的请求路由配置
}

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;
    }
}

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) {

        let host = this.app.config.jvCommon.port.main_system_url || this.app.config.jvCommon.defaultHost;

        url = `${host}${url}`;

        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即可