feat: system notice
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crm/models"
|
||||
"crm/response"
|
||||
"crm/service"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type NoticeApi struct {
|
||||
noticeService *service.NoticeService
|
||||
}
|
||||
|
||||
func NewNoticeApi() *NoticeApi {
|
||||
noticeApi := NoticeApi{
|
||||
noticeService: &service.NoticeService{},
|
||||
}
|
||||
return ¬iceApi
|
||||
}
|
||||
|
||||
func (n *NoticeApi) Update(context *gin.Context) {
|
||||
var param models.NoticeUpdateParam
|
||||
if err := context.ShouldBind(¶m); err != nil {
|
||||
fmt.Println(err)
|
||||
response.Result(response.ErrCodeParamInvalid, nil, context)
|
||||
return
|
||||
}
|
||||
errCode := n.noticeService.Update(¶m)
|
||||
response.Result(errCode, nil, context)
|
||||
}
|
||||
|
||||
func (n *NoticeApi) GetCount(context *gin.Context) {
|
||||
uid, _ := strconv.Atoi(context.Request.Header.Get("uid"))
|
||||
if uid <= 0 {
|
||||
response.Result(response.ErrCodeParamInvalid, nil, context)
|
||||
return
|
||||
}
|
||||
unReadNotice, errCode := n.noticeService.UnReadCount(int64(uid))
|
||||
response.Result(errCode, unReadNotice, context)
|
||||
}
|
||||
|
||||
func (n *NoticeApi) Delete(context *gin.Context) {
|
||||
var param models.NoticeDeleteParam
|
||||
if err := context.ShouldBind(¶m); err != nil {
|
||||
response.Result(response.ErrCodeParamInvalid, nil, context)
|
||||
return
|
||||
}
|
||||
errCode := n.noticeService.Delete(¶m)
|
||||
response.Result(errCode, nil, context)
|
||||
}
|
||||
|
||||
func (n *NoticeApi) GetList(context *gin.Context) {
|
||||
uid, _ := strconv.Atoi(context.Request.Header.Get("uid"))
|
||||
if uid <= 0 {
|
||||
response.Result(response.ErrCodeParamInvalid, nil, context)
|
||||
return
|
||||
}
|
||||
noticeList, errCode := n.noticeService.GetList(int64(uid))
|
||||
response.Result(errCode, noticeList, context)
|
||||
}
|
||||
@@ -16,7 +16,7 @@ type SubscribeApi struct {
|
||||
|
||||
func NewSubscribeApi() *SubscribeApi {
|
||||
subscribeApi := SubscribeApi{
|
||||
subscribeService: &service.SubscribeService{},
|
||||
subscribeService: service.NewSubscribeService(),
|
||||
}
|
||||
return &subscribeApi
|
||||
}
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ type UserApi struct {
|
||||
|
||||
func NewUserApi() *UserApi {
|
||||
userApi := UserApi{
|
||||
userService: &service.UserService{},
|
||||
userService: service.NewUserService(),
|
||||
}
|
||||
return &userApi
|
||||
}
|
||||
|
||||
@@ -66,6 +66,12 @@ func Router() {
|
||||
route.GET("/subscribe/info", api.NewSubscribeApi().GetInfo)
|
||||
route.POST("/subscribe/pay", api.NewSubscribeApi().Pay)
|
||||
route.POST("/subscribe/notify", api.NewSubscribeApi().Notify)
|
||||
|
||||
// 通知模块
|
||||
route.GET("/notice/list", api.NewNoticeApi().GetList)
|
||||
route.GET("/notice/count", api.NewNoticeApi().GetCount)
|
||||
route.PUT("/notice/update", api.NewNoticeApi().Update)
|
||||
route.DELETE("/notice/delete", api.NewNoticeApi().Delete)
|
||||
}
|
||||
|
||||
// 启动、监听端口
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package models
|
||||
|
||||
type Notice struct {
|
||||
Id int64 `gorm:"primaryKey"`
|
||||
Content string `gorm:"content"`
|
||||
Status int `gorm:"status"`
|
||||
Creator int64 `gorm:"creator"`
|
||||
Created int64 `gorm:"created"`
|
||||
Updated int64 `gorm:"updated"`
|
||||
}
|
||||
|
||||
type NoticeParam struct {
|
||||
Content string `json:"content"`
|
||||
Creator int64 `gorm:"creator"`
|
||||
}
|
||||
|
||||
type NoticeUpdateParam struct {
|
||||
Id int64 `json:"id" binding:"required,gt=0"`
|
||||
}
|
||||
|
||||
type NoticeDeleteParam struct {
|
||||
Ids []int64 `json:"ids" binding:"required"`
|
||||
}
|
||||
|
||||
type UnReadNotice struct {
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type NoticeList struct {
|
||||
Id int64 `json:"id"`
|
||||
Content string `json:"content"`
|
||||
Status int `json:"status"`
|
||||
Created int64 `json:"created"`
|
||||
}
|
||||
@@ -19,6 +19,7 @@ const (
|
||||
CONTRACT = "contract"
|
||||
PRODUCT = "product"
|
||||
SUBSCRIBE = "subscribe"
|
||||
NOTICE = "notice"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -31,6 +32,13 @@ const (
|
||||
Prod = "prod"
|
||||
)
|
||||
|
||||
const (
|
||||
REGISTER_NOTICE_TEMPLATE = "你注册了账号"
|
||||
LOGIN_NOTICE_TEMPLATE = "你登录了账号"
|
||||
SUBSCRIBE_NOTICE_TEMPLATE1 = "你订阅了专业版"
|
||||
SUBSCRIBE_NOTICE_TEMPLATE2 = "你订阅了高级版"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
// RestPage 分页查询
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crm/global"
|
||||
"crm/models"
|
||||
"crm/response"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
Read = 1 // 已读
|
||||
UnRead = 2 // 未读
|
||||
)
|
||||
|
||||
type NoticeService struct {
|
||||
}
|
||||
|
||||
// 新建通知
|
||||
func (n *NoticeService) Create(param *models.NoticeParam) int {
|
||||
notice := models.Notice{
|
||||
Content: param.Content,
|
||||
Status: UnRead,
|
||||
Creator: param.Creator,
|
||||
Created: time.Now().Unix(),
|
||||
}
|
||||
if err := global.Db.Create(¬ice).Error; err != nil {
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
return response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 更新通知
|
||||
func (n *NoticeService) Update(param *models.NoticeUpdateParam) int {
|
||||
notice := models.Notice{
|
||||
Id: param.Id,
|
||||
Status: Read,
|
||||
Updated: time.Now().Unix(),
|
||||
}
|
||||
if err := global.Db.Model(¬ice).Updates(¬ice).Error; err != nil {
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
return response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 获取未读通知数量
|
||||
func (n *NoticeService) UnReadCount(uid int64) (models.UnReadNotice, int) {
|
||||
var unRead models.UnReadNotice
|
||||
raw := "select count(*) from notice where status = 2 and creator = ?"
|
||||
if err := global.Db.Raw(raw, uid).Scan(&unRead.Count).Error; err != nil {
|
||||
return unRead, response.ErrCodeFailed
|
||||
}
|
||||
return unRead, response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 删除通知
|
||||
func (n *NoticeService) Delete(param *models.NoticeDeleteParam) int {
|
||||
err := global.Db.Delete(&models.Notice{}, param.Ids).Error
|
||||
if err != nil {
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
return response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 获取通知列表
|
||||
func (n *NoticeService) GetList(uid int64) ([]*models.NoticeList, int) {
|
||||
noticeList := make([]*models.NoticeList, 0)
|
||||
if err := global.Db.Table(NOTICE).Where("creator = ?", uid).Order("status desc").Order("created desc").Find(¬iceList).Error; err != nil {
|
||||
return nil, response.ErrCodeFailed
|
||||
}
|
||||
return noticeList, response.ErrCodeSuccess
|
||||
}
|
||||
@@ -20,6 +20,14 @@ const (
|
||||
)
|
||||
|
||||
type SubscribeService struct {
|
||||
noticeService *NoticeService
|
||||
}
|
||||
|
||||
func NewSubscribeService() *SubscribeService {
|
||||
subscribeService := SubscribeService{
|
||||
noticeService: &NoticeService{},
|
||||
}
|
||||
return &subscribeService
|
||||
}
|
||||
|
||||
// 订阅专业版,发起支付
|
||||
@@ -96,11 +104,14 @@ func (s *SubscribeService) Callback(outTradeNo string) (string, int) {
|
||||
|
||||
// 创建订阅信息
|
||||
var expired int64
|
||||
var content string
|
||||
switch order.Version {
|
||||
case 2:
|
||||
expired = time.Now().Unix() + int64(2592000)
|
||||
content = SUBSCRIBE_NOTICE_TEMPLATE1
|
||||
case 3:
|
||||
expired = time.Now().Unix() + int64(31104000)
|
||||
content = SUBSCRIBE_NOTICE_TEMPLATE2
|
||||
}
|
||||
subscribe := models.Subscribe{
|
||||
Version: order.Version,
|
||||
@@ -121,11 +132,12 @@ func (s *SubscribeService) Callback(outTradeNo string) (string, int) {
|
||||
}
|
||||
}
|
||||
|
||||
// 修改支付状态
|
||||
key := fmt.Sprintf("uid:%v:pay:status", order.Uid)
|
||||
if err := global.Rdb.Set(ctx, key, Payed, time.Hour*10).Err(); err != nil {
|
||||
return StringNull, response.ErrCodeFailed
|
||||
}
|
||||
// 订阅通知
|
||||
s.noticeService.Create(&models.NoticeParam{
|
||||
Content: content,
|
||||
Creator: order.Uid,
|
||||
})
|
||||
|
||||
return global.Config.Alipay.PaySuccessURL, response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,14 @@ const (
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
noticeService *NoticeService
|
||||
}
|
||||
|
||||
func NewUserService() *UserService {
|
||||
userService := UserService{
|
||||
noticeService: &NoticeService{},
|
||||
}
|
||||
return &userService
|
||||
}
|
||||
|
||||
// 用户注册
|
||||
@@ -75,6 +83,12 @@ func (u *UserService) Register(param *models.UserCreateParam) int {
|
||||
}
|
||||
}
|
||||
|
||||
// 注册通知
|
||||
u.noticeService.Create(&models.NoticeParam{
|
||||
Content: REGISTER_NOTICE_TEMPLATE,
|
||||
Creator: newUser.Id,
|
||||
})
|
||||
|
||||
return response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
@@ -106,6 +120,12 @@ func (u *UserService) Login(param *models.UserLoginParam) (*models.UserInfo, int
|
||||
Token: token,
|
||||
}
|
||||
|
||||
// 登录通知
|
||||
u.noticeService.Create(&models.NoticeParam{
|
||||
Content: LOGIN_NOTICE_TEMPLATE,
|
||||
Creator: userInfo.Uid,
|
||||
})
|
||||
|
||||
return &userInfo, response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user