2020-03-19 12:35:09 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/prometheus/common/log"
|
2020-03-24 12:33:14 +00:00
|
|
|
"github.com/silenceper/wechat"
|
|
|
|
|
"github.com/silenceper/wechat/cache"
|
|
|
|
|
"github.com/silenceper/wechat/miniprogram"
|
2020-03-19 12:35:09 +00:00
|
|
|
)
|
|
|
|
|
|
2020-03-24 12:33:14 +00:00
|
|
|
var wxa *miniprogram.MiniProgram
|
2020-03-19 12:35:09 +00:00
|
|
|
|
2020-03-24 12:33:14 +00:00
|
|
|
func init() {
|
|
|
|
|
mem := cache.NewMemory()
|
|
|
|
|
appConfig := &wechat.Config{
|
|
|
|
|
AppID: "wx49cced01eec31847",
|
|
|
|
|
AppSecret: "0090134e4a137554a271133d7f73e633",
|
|
|
|
|
Cache: mem,
|
|
|
|
|
}
|
|
|
|
|
wx := wechat.NewWechat(appConfig)
|
|
|
|
|
wxa = wx.GetMiniProgram()
|
2020-03-19 12:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Code2SessionHandle 登录凭证校验,通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。
|
|
|
|
|
//GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
|
|
|
|
|
func Code2SessionHandler(c *gin.Context) {
|
|
|
|
|
jscode := c.Param("jscode")
|
|
|
|
|
result, err := wxa.Code2Session(jscode)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn(err)
|
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
|
}
|