0.5.1 • Published 4 years ago

egg-mp-session v0.5.1

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

egg-mp-session

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

Install

$ npm i egg-mp-session --save-dev

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