Coder Social home page Coder Social logo

gf-wx's Introduction

package main

import (
	"fmt"
	"github.com/arieslee/gf-wx"
	"github.com/arieslee/gf-wx/mp/config"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/glog"
	"github.com/gogf/gf/util/gconv"
	"github.com/gogf/gf/util/grand"
)

func main() {
	s := g.Server()
	cfg := &config.MpConfig{
		AppID:     "123",
		AppSecret: "123",
	}
	wechat := gf_wx.NewWechat()
	wechat.InitRedis() //如果需要的话
	mp := wechat.GetMp(cfg)
	oauth := mp.GetOauth()
	s.BindHandler("GET:/index", func(r *ghttp.Request) {
		state := grand.S(32)
		g.Redis().Do("SET", "state", state)
		redirectURL := oauth.GetAuthCodeURL("http://wx-test.hchmc.cn/callback", "snsapi_userinfo", state)
		r.Response.RedirectTo(redirectURL)
	})
	// 获取微信用户信息
	s.BindHandler("GET:/callback", func(r *ghttp.Request) {
		code := r.GetString("code")
		state := r.GetString("state")
		localState, _ := g.Redis().Do("GET", "state")
		if gconv.String(localState) != state {
			fmt.Printf("非法请求")
			return
		}
		accessToken, err := oauth.GetAccessToken(code)
		if err != nil {
			fmt.Println(err.Error())
		}
		if accessToken != nil {
			openId := accessToken.OpenID
			token := accessToken.AccessToken
			info, _ := oauth.GetUserInfo(token, openId)
			glog.Line().Println(info)
		} else {
			fmt.Printf("accessToken无效")
			return
		}
	})
    // js sdk
	s.BindHandler("GET:/js", func(r *ghttp.Request) {
		jsSdk := mp.GetJS()
		jsStr := jsSdk.BuildConfigStr(r.URL.String())
		fmt.Println(jsStr)
	})
	s.SetPort(8080)
	s.Run()
}

gf-wx's People

Contributors

arieslee avatar

Stargazers

 avatar Guan bin avatar

Watchers

Guan bin 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.