Code Monkey home page Code Monkey logo

egg-wxauth's Introduction

egg-wxauth

this is an plugin for weixin Auth2.

Documention

1.install

in your command line, type npm install egg-wxauth -S

  npm install egg-wxauth -S

2.update config

in egg project, find your config directory, then do like below

config/plugin.js

// make `wxauth` plugin enabled
exports.wxauth = {
  enabled: true,
  package: 'egg-wxauth',
}

config/config.default.js

config.wxauth = {
  appid: 'appid', // your appid
  appsecret: 'appsecret', // your appsecret
  // scope: 'snsapi_base', 'snsapi_base' default
}

3. Use as serveice

after 1 && 2 steps, it is auto injected into egg context already.then you can use this.ctx.service.wxauth to get methods what you want.

your service or controller.js

'use strict'

const Controller = require('egg').Controller

class HomeController extends Controller {
  * index() {
    // use egg-session(you can do this depend on your project)
    if (this.ctx.session.user) {
      // if user logged, then redirect to  your `loggin url`(http://egg.inrice.top/main)
      // `http://egg.inrice.top/main` exposed to user.
      this.ctx.redirect('http://egg.inrice.top/main')
    } else {
      // redirect url(use for getting the callback by weixin)
      this.ctx.service.wxauth.getCode('http://egg.inrice.top/auth?')
    }
  }
  // if weixin return a request, it will be enter `auth` methods that you defined.
  * auth() {
    const userData = yield this.ctx.service.wxauth.getUserInfo()
    if (!this.ctx.session.user) {
      this.ctx.session.user = userData
    }
    this.ctx.redirect('http://egg.inrice.top/main')
  }
  * main() {
    this.ctx.body = 'loggin success, this is your loggin url'
  }
}

module.exports = HomeController

4.API

  • getCode (redirectUrl)

This is a method for sending authorization requests to WeChat... After waiting for user action (if the scope is silent authorization to jump to redirectUrl), WeChat sends a get request to our redirectUrl.

  • getAccessToken()

This is a method for getting access_token and openid etc.like below

see detail

{
 "access_token":"ACCESS_TOKEN",
 "expires_in":7200,
 "refresh_token":"REFRESH_TOKEN",
 "openid":"OPENID",
 "scope":"SCOPE"
}
  • getUserInfo(),

This i a method for getting UseInfo when scope is snsapi_userinfo(otherwise, you don't need to use this method), see detail

when scope is snsapi_userinfo

{
 "openid":" OPENID",
 " nickname": "Mr.Right",
 "sex":"1",
 "province":"PROVINCE",
 "city": "CITY",
 "country":"COUNTRY",
 "headimgurl":"http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
"privilege":[ "PRIVILEGE1" "PRIVILEGE2"],
 "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}

egg-wxauth's People

Contributors

eliaztray avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.