浏览代码

公告管理-时间格式转换

huangyan 1 月之前
父节点
当前提交
b242b8cc63
共有 4 个文件被更改,包括 111 次插入25 次删除
  1. 0 15
      .idea/deployment.xml
  2. 52 8
      controllers/bulletin.go
  3. 58 2
      models/Announcement/bulletin.go
  4. 1 0
      routers/bulletin.go

+ 0 - 15
.idea/deployment.xml

@@ -1,15 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="PublishConfigData" uploadOnCheckin="12489421-0ef7-4128-bf1d-1146ff757569" remoteFilesAllowedToDisappearOnAutoupload="false">
-    <serverData>
-      <paths name="huitong">
-        <serverdata>
-          <mappings>
-            <mapping local="$PROJECT_DIR$" web="/" />
-          </mappings>
-        </serverdata>
-      </paths>
-    </serverData>
-    <option name="myUploadOnCheckinName" value="huitong" />
-  </component>
-</project>

+ 52 - 8
controllers/bulletin.go

@@ -5,10 +5,10 @@ import (
 	"Cold_Api/controllers/lib"
 	"Cold_Api/models/Account"
 	"Cold_Api/models/Announcement"
-	"encoding/json"
 	beego "github.com/beego/beego/v2/server/web"
 	"github.com/google/uuid"
 	"math"
+	"strconv"
 	"time"
 )
 
@@ -60,7 +60,7 @@ func (b *BulletinController) AddBulletin() {
 			T_uuid:     v.T_uuid,
 			Title:      title,
 			Content:    content,
-			State:      1,
+			State:      0,
 			CreateTime: time.Time{},
 			UpdateTime: time.Time{},
 		}
@@ -109,7 +109,7 @@ func (b *BulletinController) DeleteBulletin() {
 		b.ServeJSON()
 		return
 	}
-	b.Data["json"] = lib.JSONS{Code: 200}
+	b.Data["json"] = lib.JSONS{Code: 200, Msg: "删除成功"}
 	b.ServeJSON()
 	return
 }
@@ -117,15 +117,29 @@ func (b *BulletinController) DeleteBulletin() {
 // UpdateBulletin 修改公告
 func (b *BulletinController) UpdateBulletin() {
 	var bulletin Announcement.Bulletin
-	err := json.Unmarshal(b.Ctx.Input.RequestBody, &bulletin)
-	if err != nil {
-		b.Data["json"] = lib.JSONS{Code: 302, Msg: "json序列化失败"}
+	bulletinid := b.GetString("bulletinid")
+	if len(bulletinid) == 0 {
+		b.Data["json"] = lib.JSONS{Code: 310, Msg: "bulletinid不能为空"}
 		b.ServeJSON()
 		return
 	}
+	bulletin.BulletinId = bulletinid
+	title := b.GetString("title")
+	if len(title) != 0 {
+		bulletin.Title = title
+	}
+	content := b.GetString("content")
+	if len(content) != 0 {
+		bulletin.Content = content
+	}
+	state := b.GetString("state")
+	if len(state) != 0 {
+		state_int, _ := strconv.Atoi(state)
+		bulletin.State = state_int
+	}
 	bulletin.T_uuid = b.Admin_r.T_uuid
 	bulletin.UpdateTime = time.Now()
-	err = Announcement.UpdateBulletin(bulletin)
+	err := Announcement.UpdateBulletin(bulletin)
 	if err != nil {
 		b.Data["json"] = lib.JSONS{Code: 301, Msg: "修改失败"}
 		b.ServeJSON()
@@ -157,7 +171,37 @@ func (b *BulletinController) GetBulletinsList() {
 	if page_z < 1 {
 		page_z = conf.Page_size
 	}
-	r_jsons.Data, r_jsons.Num = Announcement.Read_Bulletin_List(b.Admin_r.T_uuid, page, page_z)
+	is_show := b.GetString("is_show")
+	r_jsons.Data, r_jsons.Num = Announcement.Read_Bulletin_List(b.Admin_r.T_uuid, page, page_z, is_show)
+	r_jsons.Page = page
+	r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
+	b.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
+	b.ServeJSON()
+	return
+}
+
+// GetBulletinsListAdmin 返回管理员公告列表
+func (b *BulletinController) GetBulletinsListAdmin() {
+	type R_JSONS struct {
+		//必须的大写开头
+		Data      []Announcement.Bulletin
+		Num       int64
+		Page      int
+		Page_size int
+	}
+
+	var r_jsons R_JSONS
+
+	page, _ := b.GetInt("page")
+
+	if page < 1 {
+		page = 1
+	}
+	page_z, _ := b.GetInt("page_z")
+	if page_z < 1 {
+		page_z = conf.Page_size
+	}
+	r_jsons.Data, r_jsons.Num = Announcement.Read_Bulletin_List_Admin(b.Admin_r.T_uuid, page, page_z)
 	r_jsons.Page = page
 	r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
 	b.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}

+ 58 - 2
models/Announcement/bulletin.go

@@ -9,6 +9,7 @@ import (
 	"github.com/beego/beego/v2/adapter/orm"
 	orm2 "github.com/beego/beego/v2/client/orm"
 	"github.com/beego/beego/v2/core/logs"
+	"strconv"
 	"sync"
 	"time"
 )
@@ -25,6 +26,20 @@ type Bulletin struct {
 	UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"`     // auto_now_add 第一次保存时才设置时间
 }
 
+func (ab *Bulletin) MarshalJSON() ([]byte, error) {
+	loc, _ := time.LoadLocation("Asia/Shanghai")
+	type Alias Bulletin
+	return json.Marshal(&struct {
+		CreateTime string `json:"CreateTime,omitempty"`
+		UpdateTime string `json:"UpdateTime,omitempty"`
+		*Alias
+	}{
+		CreateTime: ab.CreateTime.In(loc).Format("2006-01-02 15:04:05"),
+		UpdateTime: ab.UpdateTime.In(loc).Format("2006-01-02 15:04:05"),
+		Alias:      (*Alias)(ab),
+	})
+}
+
 func (n *Bulletin) TableName() string {
 	return "bulletin"
 }
@@ -137,6 +152,7 @@ func UpdateBulletin(b Bulletin) (err error) {
 	query := o.QueryTable(new(Bulletin))
 	params := orm.Params{
 		"UpdateTime": b.UpdateTime,
+		"state":      b.State,
 	}
 	if b.Title != "" {
 		params["Title"] = b.Title
@@ -156,7 +172,7 @@ func UpdateBulletin(b Bulletin) (err error) {
 }
 
 // Read_Bulletin_List 获取公告列表
-func Read_Bulletin_List(t_uuid string, page int, page_z int) (BulletinList []Bulletin, cnt int64) {
+func Read_Bulletin_List(t_uuid string, page int, page_z int, is_show string) (BulletinList []Bulletin, cnt int64) {
 
 	o := orm.NewOrm()
 	// 也可以直接使用 Model 结构体作为表名
@@ -172,7 +188,10 @@ func Read_Bulletin_List(t_uuid string, page int, page_z int) (BulletinList []Bul
 	}
 	cond := orm.NewCondition()
 	cond1 := cond.And("State", 0).And("T_uuid", t_uuid)
-
+	if len(is_show) > 0 {
+		is_show_int, _ := strconv.Atoi(is_show)
+		cond1 = cond1.And("is_show", is_show_int)
+	}
 	//if len(T_name) > 0 {
 	//	cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name))
 	//}
@@ -210,3 +229,40 @@ func IsReadBulletin(id, t_uuid, bulletin_id string) (err error) {
 	}
 	return err
 }
+func Read_Bulletin_List_Admin(t_uuid string, page int, page_z int) (BulletinList []Bulletin, cnt int64) {
+	o := orm.NewOrm()
+	// 也可以直接使用 Model 结构体作为表名
+
+	qs := o.QueryTable(new(Bulletin))
+	var maps []Bulletin
+
+	var offset int64
+	if page <= 1 {
+		offset = 0
+	} else {
+		offset = int64((page - 1) * page_z)
+	}
+	cond := orm.NewCondition()
+	cond1 := cond.And("T_uuid", t_uuid)
+
+	//if len(T_name) > 0 {
+	//	cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name))
+	//}
+	var err error
+	if page_z == 9999 {
+		_, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-update_time").All(&maps)
+	} else {
+		_, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-update_time").All(&maps)
+	}
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+	}
+	cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+	}
+	for i, _ := range maps {
+		BulletinList = append(BulletinList, maps[i])
+	}
+	return BulletinList, cnt
+}

+ 1 - 0
routers/bulletin.go

@@ -14,6 +14,7 @@ func init() {
 		beego.NSRouter("/Bulletin/UpdateBulletin", &controllers.BulletinController{}, "*:UpdateBulletin"),
 		beego.NSRouter("/Bulletin/GetBulletinList", &controllers.BulletinController{}, "*:GetBulletinsList"),
 		beego.NSRouter("/Bulletin/IsReadBulletin", &controllers.BulletinController{}, "*:IsReadBulletin"),
+		beego.NSRouter("/Bulletin/GetBulletinsListAdmin", &controllers.BulletinController{}, "*:GetBulletinsListAdmin"),
 	)
 	beego.AddNamespace(ns)
 }