From bbaf92be72074eab860f07150031046fc65c4742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=8C=E6=B5=B7=E4=BB=B2=E5=AD=90?= Date: Mon, 30 Mar 2020 21:28:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=94=A8=E6=88=B7=E6=8B=A6?= =?UTF-8?q?=E6=88=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 17 +++++++++++++++++ app/app-handler.go | 26 +++++++++++++++++++++++--- app/user-handler.go | 41 +++++++++++++++++++++++++++++++++++++++-- go.mod | 3 ++- server.go | 7 ++++--- service.rest | 16 +++++++--------- user/user-model.go | 21 ++++++++++++++++++--- user/user-service.go | 31 ++++++++++++++++++++++++++++++- 8 files changed, 140 insertions(+), 22 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c23774c --- /dev/null +++ b/.vscode/launch.json @@ -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": [] + } + ] +} \ No newline at end of file diff --git a/app/app-handler.go b/app/app-handler.go index d83f2ac..6abe933 100644 --- a/app/app-handler.go +++ b/app/app-handler.go @@ -8,9 +8,12 @@ import ( "github.com/silenceper/wechat" "github.com/silenceper/wechat/cache" "github.com/silenceper/wechat/miniprogram" + "yyjishu.com/rubbish-class/user" ) -var wxa *miniprogram.MiniProgram +var ( + wxa *miniprogram.MiniProgram +) func init() { mem := cache.NewMemory() @@ -23,7 +26,7 @@ func init() { 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 func Code2SessionHandler(c *gin.Context) { jscode := c.Param("jscode") @@ -33,5 +36,22 @@ func Code2SessionHandler(c *gin.Context) { c.AbortWithStatusJSON(http.StatusBadRequest, err.Error()) 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, + }) } diff --git a/app/user-handler.go b/app/user-handler.go index 7ba4b92..5dbdc05 100644 --- a/app/user-handler.go +++ b/app/user-handler.go @@ -1,9 +1,46 @@ 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) { - + 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) { diff --git a/go.mod b/go.mod index 1945ae0..6dae24f 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,10 @@ go 1.13 require ( 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/jinzhu/gorm v1.9.12 github.com/prometheus/common v0.9.1 + github.com/satori/go.uuid v1.2.0 // indirect github.com/silenceper/wechat v1.2.3 ) diff --git a/server.go b/server.go index bb7971a..9783492 100644 --- a/server.go +++ b/server.go @@ -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.GET(`/info`, app.UserInfoHandler) - userGroup.POST(`/info`, app.UserInfoHandler) + userGroup.PUT(`/info`, app.UserInfoHandler) userGroup.GET(`/list/:houseid`, app.UserListHandler) - userGroup.GET(`/code2session/:jscode`, app.Code2SessionHandler) + userGroup.GET(`/fee/:houseid`, app.FeeListHandler) //住户付费号码列表 } videoGroup := appGroup.Group(`/video`) diff --git a/service.rest b/service.rest index 6a74716..5e63b8c 100644 --- a/service.rest +++ b/service.rest @@ -2,7 +2,7 @@ //@url=https://api.xintijiao.com -@token=31_uHgfsKEOrH5l3EUXM_nb_u_lTL2fRwPzxGbgvOlbM9PTGHxsH9hBzvnnP-SKOf59fb0SdUwrs-wcpTKfd_I99g +@token=uHgfsKEOrH5l3EUXMI99g @refresh_token=31_T-96lys-lZtR4B9OtRMkFOqyX_zxJNwMH6vcKxenk7enK5jeDZ6Av-Ipj9aVWr1dzdsMaCACoxbmEFKmReHwTgtQNrOpY3TdiTkM5pEyvIQ @openid=oYmRQxLw6UKdlQsZYIkRKbWlCijI @@ -15,11 +15,10 @@ Content-Type: application/json "name":"ddd小区" } -### - +### 通用/community GET {{url}}/community/list HTTP/1.1 -### 用户统计 +### 用户统计user GET {{url}}/admin/user/stat HTTP/1.1 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 - -### 视频历史下载 -GET {{url}}/app/history/ozgDT5KzARnML4khh70BEypaLlf8 HTTP/1.1 +### +PUT {{url}}/app/user/info HTTP/1.1 +token:{{token}} ### 文件上传 POST {{url}}/app/upload HTTP/1.1 diff --git a/user/user-model.go b/user/user-model.go index 1760121..a6ac8e2 100644 --- a/user/user-model.go +++ b/user/user-model.go @@ -1,8 +1,23 @@ package user -import "github.com/jinzhu/gorm" +import ( + "time" +) type User struct { - gorm.Model - Username string `gorm:"type:varchar(20);"` + ID string `gorm:"varchar(50);primary_key"` + 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下发条件的情况下会返回 } diff --git a/user/user-service.go b/user/user-service.go index 0b1529f..b05a401 100644 --- a/user/user-service.go +++ b/user/user-service.go @@ -6,6 +6,7 @@ import ( "os" "strings" "sync" + "time" "yyjishu.com/rubbish-class/rubbish" ) @@ -15,6 +16,7 @@ var once sync.Once type UserService struct { } +//NewService 用户服务实例 func NewService() *UserService { u := &UserService{} once.Do(u.Init) @@ -39,8 +41,35 @@ func (u *UserService) StatActive(communityid *string) (*int, error) { 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() { - rubbish.DB.AutoMigrate(&User{}) + rubbish.DB.AutoMigrate(&User{}, &UserSession{}) } //SaveVideoFileService 文件上传服务