diff --git a/README.md b/README.md index 0efa7b5..4ed9c1b 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ - [x] swagger api 文档 - [x] JWT (刷新、黑名单) - [x] 接口测试 +- [x] 推送 - [ ] vue 管理员后台系统 - [ ] taro web 移动端 (小程序 ... 端) - [ ] flutter 移动端 app (Android iOS 端) diff --git a/app/controllers/api/user/actived_index.go b/app/controllers/api/user/actived_index.go new file mode 100644 index 0000000..62fa399 --- /dev/null +++ b/app/controllers/api/user/actived_index.go @@ -0,0 +1,22 @@ +package user + +import ( + . "gin_bbs/app/controllers" + "gin_bbs/app/helpers" + "gin_bbs/app/viewmodels" + + "github.com/gin-gonic/gin" +) + +// ActivedIndex 活跃用户列表 +func ActivedIndex(c *gin.Context) { + activeUsersVM := make([]map[string]interface{}, 0) + activeUsers := helpers.NewActiveUser().GetActiveUsers() + for _, v := range activeUsers { + activeUsersVM = append(activeUsersVM, viewmodels.NewUserAPISerializer(v)) + } + + SendOKResponse(c, ListData{ + List: activeUsersVM, + }) +} diff --git a/app/helpers/jpush.go b/app/helpers/jpush.go new file mode 100644 index 0000000..3a2ccd4 --- /dev/null +++ b/app/helpers/jpush.go @@ -0,0 +1,47 @@ +package helpers + +import ( + "gin_bbs/config" + + jpushclient "github.com/ylywyn/jpush-api-go-client" +) + +var ( + // JpushClient - + JpushClient *JPush +) + +// JPush 极光推送 +type JPush struct { + Key string + Secret string +} + +// NewJPush - +func NewJPush() *JPush { + return &JPush{ + Key: config.AppConfig.JPushKey, + Secret: config.AppConfig.JPushSecret, + } +} + +// Send - +func (j *JPush) Send(content string, pushids []string) { + var pf jpushclient.Platform + pf.All() + + var ad jpushclient.Audience + ad.SetID(pushids) + + var msg jpushclient.Message + msg.Content = content + + payload := jpushclient.NewPushPayLoad() + payload.SetPlatform(&pf) + payload.SetAudience(&ad) + payload.SetMessage(&msg) + + bytes, _ := payload.ToBytes() + c := jpushclient.NewPushClient(j.Secret, j.Key) + c.Send(bytes) +} diff --git a/app/models/user/user.go b/app/models/user/user.go index 0aea8b1..67fff29 100644 --- a/app/models/user/user.go +++ b/app/models/user/user.go @@ -43,6 +43,8 @@ type User struct { RememberToken string `gorm:"column:remember_token;type:varchar(100)"` // 用于实现记住我功能,存入 cookie 中,下次带上时,即可直接登录 NotificationCount int `gorm:"column:notification_count;not null;default:0"` // 未读通知数 + + RegistrationID uint `gorm:"column:registration_id;unique;default:NULL"` // Jpush 中的唯一标识 } // TableName 表名 diff --git a/app/viewmodels/user.go b/app/viewmodels/user.go index 264c936..d435baa 100644 --- a/app/viewmodels/user.go +++ b/app/viewmodels/user.go @@ -42,18 +42,21 @@ func NewUserViewModelSerializer(u *userModel.User) *UserViewModel { // NewUserAPISerializer api data func NewUserAPISerializer(u *userModel.User) map[string]interface{} { r := map[string]interface{}{ - "id": u.ID, - "name": u.Name, - "email": u.Email, - "avatar": u.Avatar, - "introduction": u.Introduction, - "bound_phone": false, - "bound_wechat": false, - "last_actived_at": helpers.GetUserActivedLastActivedAt(u).Format(constants.DateTimeLayout), - "created_at": u.CreatedAt.Format(constants.DateTimeLayout), - "updated_at": u.UpdatedAt.Format(constants.DateTimeLayout), + "id": u.ID, + "name": u.Name, + "email": u.Email, + "avatar": u.Avatar, + "introduction": u.Introduction, + "bound_phone": false, + "bound_wechat": false, + "created_at": u.CreatedAt.Format(constants.DateTimeLayout), + "updated_at": u.UpdatedAt.Format(constants.DateTimeLayout), } + t := helpers.GetUserActivedLastActivedAt(u) + if t != nil { + r["last_actived_at"] = t.Format(constants.DateTimeLayout) + } if u.Phone != "" { r["bound_phone"] = true } diff --git a/config.example.yaml b/config.example.yaml index 8a55551..2c897e5 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -47,3 +47,8 @@ WEIXIN: APP_ID: APP_SECRET: REDIRECT_URL: # OAuth 授权后的跳转链接,默认为 APP.URL + +# 极光推送 +JPUSH: + KEY: + SECRET: diff --git a/config/app.go b/config/app.go index 7ea74a6..409b495 100644 --- a/config/app.go +++ b/config/app.go @@ -47,6 +47,10 @@ type appConfig struct { WeixinAppID string WeixinAppSecret string WeixinRedirectURL string + + // 极光推送 + JPushKey string + JPushSecret string } func newAppConfig() *appConfig { @@ -83,5 +87,8 @@ func newAppConfig() *appConfig { WeixinAppID: viper.GetString("WEIXIN.APP_ID"), WeixinAppSecret: viper.GetString("WEIXIN.APP_SECRET"), WeixinRedirectURL: viper.GetString("WEIXIN.REDIRECT_URL"), + + JPushKey: viper.GetString("JPUSH.KEY"), + JPushSecret: viper.GetString("JPUSH.SECRET"), } } diff --git a/go.mod b/go.mod index 57f2657..91c34c5 100644 --- a/go.mod +++ b/go.mod @@ -52,6 +52,7 @@ require ( github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect github.com/xeipuuv/gojsonschema v1.1.0 // indirect github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect + github.com/ylywyn/jpush-api-go-client v0.0.0-20190308023352-25c040ff462d github.com/yudai/gojsondiff v1.0.0 // indirect github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect github.com/yunpian/yunpian-go-sdk v0.0.0-20171206021512-2193bf8a7459 diff --git a/go.sum b/go.sum index 7a2b76a..ad0eeb8 100644 --- a/go.sum +++ b/go.sum @@ -276,6 +276,8 @@ github.com/xeipuuv/gojsonschema v1.1.0/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4m github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 h1:6fRhSjgLCkTD3JnJxvaJ4Sj+TYblw757bqYgZaOq5ZY= github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0/go.mod h1:/LWChgwKmvncFJFHJ7Gvn9wZArjbV5/FppcK2fKk/tI= +github.com/ylywyn/jpush-api-go-client v0.0.0-20190308023352-25c040ff462d h1:TO8BmdoeDBVPx7TrEKDvsb2jB0+YVjc1O7x0i7vgyeo= +github.com/ylywyn/jpush-api-go-client v0.0.0-20190308023352-25c040ff462d/go.mod h1:Nv7wKD2/bCdKUFNKcJRa99a+1+aSLlCRJFriFYdjz/I= github.com/yudai/gojsondiff v1.0.0 h1:27cbfqXLVEJ1o8I6v3y9lg8Ydm53EKqHXAOMxEGlCOA= github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg= github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3IfnEUduWvb9is428/nNb5L3U01M= diff --git a/routes/api.go b/routes/api.go index f5fbe1b..dd99a7d 100644 --- a/routes/api.go +++ b/routes/api.go @@ -56,6 +56,8 @@ func registerAPI(r *router.MyRoute, middlewares ...gin.HandlerFunc) { wrapper.GetToken(authorization.Destroy)) } + // 获取活跃用户列表 + r.Register("GET", "api.users.actived", "/users/actived", user.ActivedIndex) // +++++++++++++++ 用户相关 +++++++++++++++ userRouter := r.Group("/user", middleware.TokenAuth()) {