Skip to content

chrislone/egg-mp-session

Repository files navigation

egg-mp-session

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i egg-mp-session --save

Usage

// {app_root}/config/plugin.js
exports.mpSession = {
  enable: true,
  package: 'egg-mp-session',
};

登录

async login(ctx) {
  const { code } = ctx.request.body;
  const loginResult = await ctx.mpSession.login({ code });

  ctx.body = {
    tokenId: loginResult.tokenId,
  };
}

获取信息

async info(ctx) {
  /**
  * => {openid=xxxxxxxx, session_id=xxxxxxxx}
  */
  const userInfo = await ctx.mpSession.info();

  ctx.body = {
    userInfo
  };
}

更新用户信息过期时间

async update(ctx) {
  /**
  * 返回 redis egg-redis 的 pexpire 操作结果
  * ref:https://redis.io/commands/pexpire
  */
  const redisStatus = await ctx.mpSession.update();

  ctx.body = {
    status: redisStatus
  };
}

退出登录,删除 redis 中的用户信息

async logout(ctx) {
  /**
  * 返回 redis egg-redis 的 del 操作结果
  * ref:https://redis.io/commands/del
  */
  const redisStatus = await ctx.mpSession.logout();

  ctx.body = {
    status: redisStatus
  };
}

Configuration

// {app_root}/config/config.default.js
exports.mpSession = {};

see config/config.default.js for more detail.

Example

Questions & Suggestions

Please open an issue here.

License

MIT

About

Egg.js 微信小程序会话管理插件。

Resources

License

Stars

Watchers

Forks

Packages

No packages published