微信用户拦截
This commit is contained in:
parent
ec465919d3
commit
bbaf92be72
|
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Launch",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "auto",
|
||||||
|
"program": "${fileDirname}",
|
||||||
|
"env": {},
|
||||||
|
"args": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
@ -8,9 +8,12 @@ import (
|
||||||
"github.com/silenceper/wechat"
|
"github.com/silenceper/wechat"
|
||||||
"github.com/silenceper/wechat/cache"
|
"github.com/silenceper/wechat/cache"
|
||||||
"github.com/silenceper/wechat/miniprogram"
|
"github.com/silenceper/wechat/miniprogram"
|
||||||
|
"yyjishu.com/rubbish-class/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
var wxa *miniprogram.MiniProgram
|
var (
|
||||||
|
wxa *miniprogram.MiniProgram
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
mem := cache.NewMemory()
|
mem := cache.NewMemory()
|
||||||
|
|
@ -23,7 +26,7 @@ func init() {
|
||||||
wxa = wx.GetMiniProgram()
|
wxa = wx.GetMiniProgram()
|
||||||
}
|
}
|
||||||
|
|
||||||
//Code2SessionHandle 登录凭证校验,通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。
|
//Code2SessionHandler 登录凭证校验,通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。
|
||||||
//GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_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) {
|
func Code2SessionHandler(c *gin.Context) {
|
||||||
jscode := c.Param("jscode")
|
jscode := c.Param("jscode")
|
||||||
|
|
@ -33,5 +36,22 @@ func Code2SessionHandler(c *gin.Context) {
|
||||||
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error())
|
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, result)
|
if result.ErrCode != 0 {
|
||||||
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
||||||
|
"err": result.ErrMsg,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u := &user.UserSession{
|
||||||
|
//ID: uuid.Must(uuid.NewV4(), nil).String(),
|
||||||
|
OpenID: result.OpenID,
|
||||||
|
SessionKey: result.SessionKey,
|
||||||
|
UnionID: result.UnionID,
|
||||||
|
}
|
||||||
|
us := user.NewService()
|
||||||
|
us.SaveSession(u)
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"token": result.OpenID,
|
||||||
|
"expireat": 7200,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,46 @@
|
||||||
package app
|
package app
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"yyjishu.com/rubbish-class/user"
|
||||||
|
)
|
||||||
|
|
||||||
|
func UserCheckHandler(c *gin.Context) {
|
||||||
|
token := c.GetHeader("token")
|
||||||
|
userService := user.NewService()
|
||||||
|
r, err := userService.CheckSession(&token)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
||||||
|
"err": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !r {
|
||||||
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
||||||
|
"err": "无效token!",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
|
||||||
|
//UserInfoHandler 用户信息获取及变更
|
||||||
func UserInfoHandler(c *gin.Context) {
|
func UserInfoHandler(c *gin.Context) {
|
||||||
|
if c.Request.Method == "GET" {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"method": "get",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if c.Request.Method == "PUT" {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"method": "put",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusMethodNotAllowed, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func UserListHandler(c *gin.Context) {
|
func UserListHandler(c *gin.Context) {
|
||||||
|
|
|
||||||
3
go.mod
3
go.mod
|
|
@ -4,9 +4,10 @@ go 1.13
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.5.0
|
github.com/gin-gonic/gin v1.5.0
|
||||||
github.com/go-redis/redis/v7 v7.2.0
|
github.com/go-redis/redis/v7 v7.2.0 // indirect
|
||||||
github.com/go-sql-driver/mysql v1.4.1
|
github.com/go-sql-driver/mysql v1.4.1
|
||||||
github.com/jinzhu/gorm v1.9.12
|
github.com/jinzhu/gorm v1.9.12
|
||||||
github.com/prometheus/common v0.9.1
|
github.com/prometheus/common v0.9.1
|
||||||
|
github.com/satori/go.uuid v1.2.0 // indirect
|
||||||
github.com/silenceper/wechat v1.2.3
|
github.com/silenceper/wechat v1.2.3
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -69,14 +69,15 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
appGroup := rbGroup.Group(`/app`)
|
rbGroup.GET(`/app/code2session/:jscode`, app.Code2SessionHandler) //微信小程序用户认证
|
||||||
|
appGroup := rbGroup.Group(`/app`, app.UserCheckHandler)
|
||||||
{
|
{
|
||||||
userGroup := appGroup.Group(`/user`)
|
userGroup := appGroup.Group(`/user`)
|
||||||
{
|
{
|
||||||
userGroup.GET(`/info`, app.UserInfoHandler)
|
userGroup.GET(`/info`, app.UserInfoHandler)
|
||||||
userGroup.POST(`/info`, app.UserInfoHandler)
|
userGroup.PUT(`/info`, app.UserInfoHandler)
|
||||||
userGroup.GET(`/list/:houseid`, app.UserListHandler)
|
userGroup.GET(`/list/:houseid`, app.UserListHandler)
|
||||||
userGroup.GET(`/code2session/:jscode`, app.Code2SessionHandler)
|
|
||||||
userGroup.GET(`/fee/:houseid`, app.FeeListHandler) //住户付费号码列表
|
userGroup.GET(`/fee/:houseid`, app.FeeListHandler) //住户付费号码列表
|
||||||
}
|
}
|
||||||
videoGroup := appGroup.Group(`/video`)
|
videoGroup := appGroup.Group(`/video`)
|
||||||
|
|
|
||||||
16
service.rest
16
service.rest
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
//@url=https://api.xintijiao.com
|
//@url=https://api.xintijiao.com
|
||||||
|
|
||||||
@token=31_uHgfsKEOrH5l3EUXM_nb_u_lTL2fRwPzxGbgvOlbM9PTGHxsH9hBzvnnP-SKOf59fb0SdUwrs-wcpTKfd_I99g
|
@token=uHgfsKEOrH5l3EUXMI99g
|
||||||
@refresh_token=31_T-96lys-lZtR4B9OtRMkFOqyX_zxJNwMH6vcKxenk7enK5jeDZ6Av-Ipj9aVWr1dzdsMaCACoxbmEFKmReHwTgtQNrOpY3TdiTkM5pEyvIQ
|
@refresh_token=31_T-96lys-lZtR4B9OtRMkFOqyX_zxJNwMH6vcKxenk7enK5jeDZ6Av-Ipj9aVWr1dzdsMaCACoxbmEFKmReHwTgtQNrOpY3TdiTkM5pEyvIQ
|
||||||
@openid=oYmRQxLw6UKdlQsZYIkRKbWlCijI
|
@openid=oYmRQxLw6UKdlQsZYIkRKbWlCijI
|
||||||
|
|
||||||
|
|
@ -15,11 +15,10 @@ Content-Type: application/json
|
||||||
"name":"ddd小区"
|
"name":"ddd小区"
|
||||||
}
|
}
|
||||||
|
|
||||||
###
|
### 通用/community
|
||||||
|
|
||||||
GET {{url}}/community/list HTTP/1.1
|
GET {{url}}/community/list HTTP/1.1
|
||||||
|
|
||||||
### 用户统计
|
### 用户统计user
|
||||||
GET {{url}}/admin/user/stat HTTP/1.1
|
GET {{url}}/admin/user/stat HTTP/1.1
|
||||||
Authorization: Basic YWRtaW46YWRtaW4=
|
Authorization: Basic YWRtaW46YWRtaW4=
|
||||||
|
|
||||||
|
|
@ -29,12 +28,11 @@ Authorization: Basic YWRtaW46YWRtaW4=
|
||||||
|
|
||||||
############################### 以下微信小程序 ########################################
|
############################### 以下微信小程序 ########################################
|
||||||
|
|
||||||
@jscode={{$guid}}
|
GET {{url}}/app/code2session/1232131231 HTTP/1.1
|
||||||
|
|
||||||
GET {{url}}/app/code2session/{{jscode}} HTTP/1.1
|
###
|
||||||
|
PUT {{url}}/app/user/info HTTP/1.1
|
||||||
### 视频历史下载
|
token:{{token}}
|
||||||
GET {{url}}/app/history/ozgDT5KzARnML4khh70BEypaLlf8 HTTP/1.1
|
|
||||||
|
|
||||||
### 文件上传
|
### 文件上传
|
||||||
POST {{url}}/app/upload HTTP/1.1
|
POST {{url}}/app/upload HTTP/1.1
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,23 @@
|
||||||
package user
|
package user
|
||||||
|
|
||||||
import "github.com/jinzhu/gorm"
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
gorm.Model
|
ID string `gorm:"varchar(50);primary_key"`
|
||||||
Username string `gorm:"type:varchar(20);"`
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
DeletedAt *time.Time `gorm:"index"`
|
||||||
|
Username string `gorm:"type:varchar(20);"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserSession struct {
|
||||||
|
OpenID string `gorm:"type:varchar(50);primary_key;"`
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
DeletedAt *time.Time
|
||||||
|
ExpiredAt time.Time
|
||||||
|
SessionKey string `gorm:"type:varchar(50);index;"` // 会话密钥
|
||||||
|
UnionID string `gorm:"type:varchar(50);"` // 用户在开放平台的唯一标识符,在满足UnionID下发条件的情况下会返回
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"yyjishu.com/rubbish-class/rubbish"
|
"yyjishu.com/rubbish-class/rubbish"
|
||||||
)
|
)
|
||||||
|
|
@ -15,6 +16,7 @@ var once sync.Once
|
||||||
type UserService struct {
|
type UserService struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//NewService 用户服务实例
|
||||||
func NewService() *UserService {
|
func NewService() *UserService {
|
||||||
u := &UserService{}
|
u := &UserService{}
|
||||||
once.Do(u.Init)
|
once.Do(u.Init)
|
||||||
|
|
@ -39,8 +41,35 @@ func (u *UserService) StatActive(communityid *string) (*int, error) {
|
||||||
return &r, nil
|
return &r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Info 用户信息
|
||||||
|
func (u *UserService) Info() (*User, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Create 用户创建
|
||||||
|
func (u *UserService) Create() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//SaveSession 用户session存储
|
||||||
|
func (u *UserService) SaveSession(us *UserSession) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserService) CheckSession(token *string) (bool, error) {
|
||||||
|
usersession := &UserSession{}
|
||||||
|
if err := rubbish.DB.Where("open_id=?", *token).First(usersession).Error; err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if usersession != nil && usersession.ExpiredAt.After(time.Now()) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//Init 表初始化
|
||||||
func (u *UserService) Init() {
|
func (u *UserService) Init() {
|
||||||
rubbish.DB.AutoMigrate(&User{})
|
rubbish.DB.AutoMigrate(&User{}, &UserSession{})
|
||||||
}
|
}
|
||||||
|
|
||||||
//SaveVideoFileService 文件上传服务
|
//SaveVideoFileService 文件上传服务
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue