Skip to content

Commit

Permalink
push
Browse files Browse the repository at this point in the history
  • Loading branch information
Away0x committed May 21, 2019
1 parent 07365c2 commit 9eaba68
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
- [x] swagger api 文档
- [x] JWT (刷新、黑名单)
- [x] 接口测试
- [x] 推送
- [ ] vue 管理员后台系统
- [ ] taro web 移动端 (小程序 ... 端)
- [ ] flutter 移动端 app (Android iOS 端)
Expand Down
22 changes: 22 additions & 0 deletions app/controllers/api/user/actived_index.go
Original file line number Diff line number Diff line change
@@ -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,
})
}
47 changes: 47 additions & 0 deletions app/helpers/jpush.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions app/models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 表名
Expand Down
23 changes: 13 additions & 10 deletions app/viewmodels/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ WEIXIN:
APP_ID:
APP_SECRET:
REDIRECT_URL: # OAuth 授权后的跳转链接,默认为 APP.URL

# 极光推送
JPUSH:
KEY:
SECRET:
7 changes: 7 additions & 0 deletions config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ type appConfig struct {
WeixinAppID string
WeixinAppSecret string
WeixinRedirectURL string

// 极光推送
JPushKey string
JPushSecret string
}

func newAppConfig() *appConfig {
Expand Down Expand Up @@ -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"),
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 2 additions & 0 deletions routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down

0 comments on commit 9eaba68

Please sign in to comment.