更新软件升级
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"server/models"
|
||||
"server/services"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// ApiSoftwareUpgradeController 开放接口:客户端检查更新(无需登录)
|
||||
type ApiSoftwareUpgradeController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// Check GET /api/softwareupgrade/check?code=desktop-app(可选 version 由客户端自行比对 latestVersion)
|
||||
func (c *ApiSoftwareUpgradeController) Check() {
|
||||
code := strings.TrimSpace(c.GetString("code"))
|
||||
if code == "" {
|
||||
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "缺少参数 code(产品标识)"}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
var row models.SystemSoftwareUpgrade
|
||||
err := models.Orm.QueryTable(new(models.SystemSoftwareUpgrade)).
|
||||
Filter("code", code).
|
||||
Filter("status", 1).
|
||||
Filter("delete_time__isnull", true).
|
||||
One(&row)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{"code": 404, "msg": "产品不存在或已停用"}
|
||||
_ = c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
latest := strings.TrimSpace(row.LatestVersion)
|
||||
if latest == "" {
|
||||
latest = "0.0.0"
|
||||
}
|
||||
|
||||
scheme, host := services.PublicRequestBaseURL(&c.Controller)
|
||||
dl := services.ResolveSoftwareDownloadURL(scheme, host, row.DownloadURL, row.FileID)
|
||||
|
||||
data := map[string]interface{}{
|
||||
"latestVersion": latest,
|
||||
"downloadUrl": dl,
|
||||
"forceUpdate": row.ForceUpdate == 1,
|
||||
"releaseNotes": "",
|
||||
}
|
||||
if row.ReleaseNotes != nil {
|
||||
data["releaseNotes"] = *row.ReleaseNotes
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": data}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"server/models"
|
||||
"server/pkg/jwtutil"
|
||||
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
type PlatformComplaintController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *PlatformComplaintController) platformClaims() (*jwtutil.Claims, error) {
|
||||
auth := c.Ctx.Request.Header.Get("Authorization")
|
||||
if auth == "" {
|
||||
return nil, fmt.Errorf("未登录")
|
||||
}
|
||||
parts := strings.SplitN(auth, " ", 2)
|
||||
if len(parts) != 2 || parts[0] != "Bearer" {
|
||||
return nil, fmt.Errorf("认证信息格式错误")
|
||||
}
|
||||
claims, err := jwtutil.ParseToken(parts[1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("无效的token")
|
||||
}
|
||||
if claims.UserType != "platform" {
|
||||
return nil, fmt.Errorf("无权访问")
|
||||
}
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
func (c *PlatformComplaintController) jsonErr(httpStatus, bizCode int, msg string) {
|
||||
c.Ctx.Output.SetStatus(httpStatus)
|
||||
c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *PlatformComplaintController) ok(data interface{}) {
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": data}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
func categoryNameMap(ids []uint64) map[uint64]string {
|
||||
m := make(map[uint64]string)
|
||||
if len(ids) == 0 {
|
||||
return m
|
||||
}
|
||||
seen := make(map[uint64]bool)
|
||||
var uniq []uint64
|
||||
for _, id := range ids {
|
||||
if id > 0 && !seen[id] {
|
||||
seen[id] = true
|
||||
uniq = append(uniq, id)
|
||||
}
|
||||
}
|
||||
if len(uniq) == 0 {
|
||||
return m
|
||||
}
|
||||
var cats []models.ComplaintCategory
|
||||
_, _ = models.Orm.QueryTable(new(models.ComplaintCategory)).
|
||||
Filter("id__in", uniq).
|
||||
Filter("delete_time__isnull", true).
|
||||
All(&cats)
|
||||
for _, x := range cats {
|
||||
m[x.ID] = x.Name
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// List GET /platform/complaint/list?page=1&pageSize=20&categoryId=&status=&keyword=
|
||||
func (c *PlatformComplaintController) List() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
page, _ := c.GetInt("page", 1)
|
||||
pageSize, _ := c.GetInt("pageSize", 20)
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 {
|
||||
pageSize = 20
|
||||
}
|
||||
if pageSize > 200 {
|
||||
pageSize = 200
|
||||
}
|
||||
var categoryID uint64
|
||||
if s := strings.TrimSpace(c.GetString("categoryId")); s != "" {
|
||||
if v, err := strconv.ParseUint(s, 10, 64); err == nil {
|
||||
categoryID = v
|
||||
}
|
||||
}
|
||||
statusStr := strings.TrimSpace(c.GetString("status"))
|
||||
keyword := strings.TrimSpace(c.GetString("keyword"))
|
||||
|
||||
qs := models.Orm.QueryTable(new(models.PlatformComplaint)).Filter("delete_time__isnull", true)
|
||||
if categoryID > 0 {
|
||||
qs = qs.Filter("category_id", categoryID)
|
||||
}
|
||||
if statusStr != "" {
|
||||
if st, err := strconv.Atoi(statusStr); err == nil {
|
||||
qs = qs.Filter("status", st)
|
||||
}
|
||||
}
|
||||
if keyword != "" {
|
||||
cond := orm.NewCondition().
|
||||
Or("title__icontains", keyword).
|
||||
Or("content__icontains", keyword).
|
||||
Or("contact_name__icontains", keyword).
|
||||
Or("contact_phone__icontains", keyword).
|
||||
Or("contact_email__icontains", keyword)
|
||||
qs = qs.SetCond(cond)
|
||||
}
|
||||
|
||||
total, _ := qs.Count()
|
||||
var rows []models.PlatformComplaint
|
||||
_, err := qs.OrderBy("-id").Limit(pageSize, (page-1)*pageSize).All(&rows)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "获取失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
ids := make([]uint64, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
ids = append(ids, r.CategoryID)
|
||||
}
|
||||
names := categoryNameMap(ids)
|
||||
list := make([]map[string]interface{}, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
list = append(list, map[string]interface{}{
|
||||
"id": r.ID,
|
||||
"categoryId": r.CategoryID,
|
||||
"categoryName": names[r.CategoryID],
|
||||
"title": r.Title,
|
||||
"content": r.Content,
|
||||
"contactName": r.ContactName,
|
||||
"contactPhone": r.ContactPhone,
|
||||
"contactEmail": r.ContactEmail,
|
||||
"status": r.Status,
|
||||
"replyContent": r.ReplyContent,
|
||||
"replyTime": r.ReplyTime,
|
||||
"tid": r.Tid,
|
||||
"remark": r.Remark,
|
||||
"createTime": r.CreateTime,
|
||||
"updateTime": r.UpdateTime,
|
||||
})
|
||||
}
|
||||
c.ok(map[string]interface{}{
|
||||
"list": list,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"pageSize": pageSize,
|
||||
})
|
||||
}
|
||||
|
||||
// Detail GET /platform/complaint/:id
|
||||
func (c *PlatformComplaintController) Detail() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
c.jsonErr(400, 400, "无效ID")
|
||||
return
|
||||
}
|
||||
var row models.PlatformComplaint
|
||||
err = models.Orm.QueryTable(new(models.PlatformComplaint)).
|
||||
Filter("id", id).
|
||||
Filter("delete_time__isnull", true).
|
||||
One(&row)
|
||||
if err != nil {
|
||||
c.jsonErr(404, 404, "记录不存在")
|
||||
return
|
||||
}
|
||||
names := categoryNameMap([]uint64{row.CategoryID})
|
||||
c.ok(map[string]interface{}{
|
||||
"id": row.ID,
|
||||
"categoryId": row.CategoryID,
|
||||
"categoryName": names[row.CategoryID],
|
||||
"title": row.Title,
|
||||
"content": row.Content,
|
||||
"contactName": row.ContactName,
|
||||
"contactPhone": row.ContactPhone,
|
||||
"contactEmail": row.ContactEmail,
|
||||
"status": row.Status,
|
||||
"replyContent": row.ReplyContent,
|
||||
"replyTime": row.ReplyTime,
|
||||
"tid": row.Tid,
|
||||
"remark": row.Remark,
|
||||
"createTime": row.CreateTime,
|
||||
"updateTime": row.UpdateTime,
|
||||
})
|
||||
}
|
||||
|
||||
type complaintPayload struct {
|
||||
CategoryID *uint64 `json:"categoryId"`
|
||||
Title *string `json:"title"`
|
||||
Content *string `json:"content"`
|
||||
ContactName *string `json:"contactName"`
|
||||
ContactPhone *string `json:"contactPhone"`
|
||||
ContactEmail *string `json:"contactEmail"`
|
||||
Status *int8 `json:"status"`
|
||||
ReplyContent *string `json:"replyContent"`
|
||||
Tid *uint64 `json:"tid"`
|
||||
Remark *string `json:"remark"`
|
||||
}
|
||||
|
||||
// Create POST /platform/complaint
|
||||
func (c *PlatformComplaintController) Create() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
body, _ := io.ReadAll(c.Ctx.Request.Body)
|
||||
var p complaintPayload
|
||||
if err := json.Unmarshal(body, &p); err != nil {
|
||||
c.jsonErr(400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
if p.CategoryID == nil || *p.CategoryID == 0 || p.Title == nil || strings.TrimSpace(*p.Title) == "" ||
|
||||
p.Content == nil || strings.TrimSpace(*p.Content) == "" {
|
||||
c.jsonErr(400, 400, "分类、标题、内容不能为空")
|
||||
return
|
||||
}
|
||||
row := models.PlatformComplaint{
|
||||
CategoryID: *p.CategoryID,
|
||||
Title: strings.TrimSpace(*p.Title),
|
||||
Content: strings.TrimSpace(*p.Content),
|
||||
Status: 0,
|
||||
}
|
||||
if p.ContactName != nil {
|
||||
row.ContactName = p.ContactName
|
||||
}
|
||||
if p.ContactPhone != nil {
|
||||
row.ContactPhone = p.ContactPhone
|
||||
}
|
||||
if p.ContactEmail != nil {
|
||||
row.ContactEmail = p.ContactEmail
|
||||
}
|
||||
if p.Tid != nil {
|
||||
row.Tid = p.Tid
|
||||
}
|
||||
if p.Status != nil {
|
||||
row.Status = *p.Status
|
||||
}
|
||||
if p.Remark != nil {
|
||||
row.Remark = p.Remark
|
||||
}
|
||||
id, err := models.Orm.Insert(&row)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "创建失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
c.ok(map[string]interface{}{"id": id})
|
||||
}
|
||||
|
||||
// Update POST /platform/complaint/:id
|
||||
func (c *PlatformComplaintController) Update() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
c.jsonErr(400, 400, "无效ID")
|
||||
return
|
||||
}
|
||||
body, _ := io.ReadAll(c.Ctx.Request.Body)
|
||||
var p complaintPayload
|
||||
if err := json.Unmarshal(body, &p); err != nil {
|
||||
c.jsonErr(400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
up := map[string]interface{}{}
|
||||
if p.CategoryID != nil && *p.CategoryID > 0 {
|
||||
up["category_id"] = *p.CategoryID
|
||||
}
|
||||
if p.Title != nil {
|
||||
up["title"] = strings.TrimSpace(*p.Title)
|
||||
}
|
||||
if p.Content != nil {
|
||||
up["content"] = strings.TrimSpace(*p.Content)
|
||||
}
|
||||
if p.ContactName != nil {
|
||||
up["contact_name"] = p.ContactName
|
||||
}
|
||||
if p.ContactPhone != nil {
|
||||
up["contact_phone"] = p.ContactPhone
|
||||
}
|
||||
if p.ContactEmail != nil {
|
||||
up["contact_email"] = p.ContactEmail
|
||||
}
|
||||
if p.Status != nil {
|
||||
up["status"] = *p.Status
|
||||
}
|
||||
if p.ReplyContent != nil {
|
||||
s := strings.TrimSpace(*p.ReplyContent)
|
||||
up["reply_content"] = s
|
||||
if s != "" {
|
||||
now := time.Now()
|
||||
up["reply_time"] = now
|
||||
}
|
||||
}
|
||||
if p.Tid != nil {
|
||||
up["tid"] = p.Tid
|
||||
}
|
||||
if p.Remark != nil {
|
||||
up["remark"] = p.Remark
|
||||
}
|
||||
if len(up) == 0 {
|
||||
c.jsonErr(400, 400, "无更新字段")
|
||||
return
|
||||
}
|
||||
n, err := models.Orm.QueryTable(new(models.PlatformComplaint)).
|
||||
Filter("id", id).
|
||||
Filter("delete_time__isnull", true).
|
||||
Update(up)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "更新失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
c.jsonErr(404, 404, "记录不存在")
|
||||
return
|
||||
}
|
||||
c.ok(nil)
|
||||
}
|
||||
|
||||
// Delete DELETE /platform/complaint/:id
|
||||
func (c *PlatformComplaintController) Delete() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
c.jsonErr(400, 400, "无效ID")
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
n, err := models.Orm.QueryTable(new(models.PlatformComplaint)).
|
||||
Filter("id", id).
|
||||
Filter("delete_time__isnull", true).
|
||||
Update(map[string]interface{}{"delete_time": now})
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "删除失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
c.jsonErr(404, 404, "记录不存在")
|
||||
return
|
||||
}
|
||||
c.ok(nil)
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"server/models"
|
||||
"server/pkg/jwtutil"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
type PlatformComplaintCategoryController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *PlatformComplaintCategoryController) platformClaims() (*jwtutil.Claims, error) {
|
||||
auth := c.Ctx.Request.Header.Get("Authorization")
|
||||
if auth == "" {
|
||||
return nil, fmt.Errorf("未登录")
|
||||
}
|
||||
parts := strings.SplitN(auth, " ", 2)
|
||||
if len(parts) != 2 || parts[0] != "Bearer" {
|
||||
return nil, fmt.Errorf("认证信息格式错误")
|
||||
}
|
||||
claims, err := jwtutil.ParseToken(parts[1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("无效的token")
|
||||
}
|
||||
if claims.UserType != "platform" {
|
||||
return nil, fmt.Errorf("无权访问")
|
||||
}
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
func (c *PlatformComplaintCategoryController) jsonErr(httpStatus, bizCode int, msg string) {
|
||||
c.Ctx.Output.SetStatus(httpStatus)
|
||||
c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *PlatformComplaintCategoryController) ok(data interface{}) {
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": data}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// List GET /platform/complaintCategory/list
|
||||
func (c *PlatformComplaintCategoryController) List() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
var rows []models.ComplaintCategory
|
||||
_, err := models.Orm.QueryTable(new(models.ComplaintCategory)).
|
||||
Filter("delete_time__isnull", true).
|
||||
OrderBy("sort", "id").
|
||||
All(&rows)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "获取失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
c.ok(rows)
|
||||
}
|
||||
|
||||
// SelectList GET /platform/complaintCategory/select — 仅启用,供下拉
|
||||
func (c *PlatformComplaintCategoryController) SelectList() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
var rows []models.ComplaintCategory
|
||||
_, err := models.Orm.QueryTable(new(models.ComplaintCategory)).
|
||||
Filter("delete_time__isnull", true).
|
||||
Filter("status", 1).
|
||||
OrderBy("sort", "id").
|
||||
All(&rows)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "获取失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
c.ok(rows)
|
||||
}
|
||||
|
||||
type complaintCategoryPayload struct {
|
||||
Name *string `json:"name"`
|
||||
Code *string `json:"code"`
|
||||
Sort *int `json:"sort"`
|
||||
Status *int8 `json:"status"`
|
||||
}
|
||||
|
||||
// Create POST /platform/complaintCategory
|
||||
func (c *PlatformComplaintCategoryController) Create() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
body, _ := io.ReadAll(c.Ctx.Request.Body)
|
||||
var p complaintCategoryPayload
|
||||
if err := json.Unmarshal(body, &p); err != nil || p.Name == nil || strings.TrimSpace(*p.Name) == "" {
|
||||
c.jsonErr(400, 400, "分类名称不能为空")
|
||||
return
|
||||
}
|
||||
sort := 0
|
||||
if p.Sort != nil {
|
||||
sort = *p.Sort
|
||||
}
|
||||
st := int8(1)
|
||||
if p.Status != nil {
|
||||
st = *p.Status
|
||||
}
|
||||
row := models.ComplaintCategory{
|
||||
Name: strings.TrimSpace(*p.Name),
|
||||
Code: p.Code,
|
||||
Sort: sort,
|
||||
Status: st,
|
||||
}
|
||||
id, err := models.Orm.Insert(&row)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "创建失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
c.ok(map[string]interface{}{"id": id})
|
||||
}
|
||||
|
||||
// Update POST /platform/complaintCategory/:id
|
||||
func (c *PlatformComplaintCategoryController) Update() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
c.jsonErr(400, 400, "无效ID")
|
||||
return
|
||||
}
|
||||
body, _ := io.ReadAll(c.Ctx.Request.Body)
|
||||
var p complaintCategoryPayload
|
||||
if err := json.Unmarshal(body, &p); err != nil {
|
||||
c.jsonErr(400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
up := map[string]interface{}{}
|
||||
if p.Name != nil {
|
||||
up["name"] = strings.TrimSpace(*p.Name)
|
||||
}
|
||||
if p.Code != nil {
|
||||
up["code"] = strings.TrimSpace(*p.Code)
|
||||
}
|
||||
if p.Sort != nil {
|
||||
up["sort"] = *p.Sort
|
||||
}
|
||||
if p.Status != nil {
|
||||
up["status"] = *p.Status
|
||||
}
|
||||
if len(up) == 0 {
|
||||
c.jsonErr(400, 400, "无更新字段")
|
||||
return
|
||||
}
|
||||
n, err := models.Orm.QueryTable(new(models.ComplaintCategory)).
|
||||
Filter("id", id).
|
||||
Filter("delete_time__isnull", true).
|
||||
Update(up)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "更新失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
c.jsonErr(404, 404, "记录不存在")
|
||||
return
|
||||
}
|
||||
c.ok(nil)
|
||||
}
|
||||
|
||||
// Delete DELETE /platform/complaintCategory/:id
|
||||
func (c *PlatformComplaintCategoryController) Delete() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
c.jsonErr(400, 400, "无效ID")
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
n, err := models.Orm.QueryTable(new(models.ComplaintCategory)).
|
||||
Filter("id", id).
|
||||
Filter("delete_time__isnull", true).
|
||||
Update(map[string]interface{}{"delete_time": now})
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "删除失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
c.jsonErr(404, 404, "记录不存在")
|
||||
return
|
||||
}
|
||||
c.ok(nil)
|
||||
}
|
||||
@@ -23,13 +23,15 @@ type PlatformFileController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
const fileUploadMaxBytes = 50 * 1024 * 1024
|
||||
const fileUploadMaxMB = 200
|
||||
const fileUploadMaxBytes = fileUploadMaxMB * 1024 * 1024
|
||||
|
||||
var fileTypeByCategory = map[string]uint8{
|
||||
"image": 1,
|
||||
"document": 2,
|
||||
"video": 3,
|
||||
"audio": 4,
|
||||
"image": 1,
|
||||
"document": 2,
|
||||
"video": 3,
|
||||
"audio": 4,
|
||||
"appsupgrade": 2,
|
||||
}
|
||||
|
||||
var allowedExtByCategory = map[string][]string{
|
||||
@@ -37,6 +39,8 @@ var allowedExtByCategory = map[string][]string{
|
||||
"document": {"pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt"},
|
||||
"video": {"mp4", "webm", "mov"},
|
||||
"audio": {"mp3", "wav", "ogg"},
|
||||
// 安装包 / 软件升级(上传时 cate 选 appsupgrade 分类即可,扩展名在此放行)
|
||||
"appsupgrade": {"zip", "exe", "dmg", "msi", "msix", "apk", "deb", "rpm", "7z", "tar", "gz", "pkg"},
|
||||
}
|
||||
|
||||
func (c *PlatformFileController) platformClaims() (*jwtutil.Claims, error) {
|
||||
@@ -90,7 +94,10 @@ func detectFileType(ext string) uint8 {
|
||||
for cat, exts := range allowedExtByCategory {
|
||||
for _, e := range exts {
|
||||
if e == ext {
|
||||
return fileTypeByCategory[cat]
|
||||
if t, ok := fileTypeByCategory[cat]; ok {
|
||||
return t
|
||||
}
|
||||
return 2
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -472,7 +479,7 @@ func (c *PlatformFileController) UploadFile() {
|
||||
defer fh.Close()
|
||||
|
||||
if header != nil && header.Size > fileUploadMaxBytes {
|
||||
c.jsonErr(400, 400, "文件大小不能超过50MB")
|
||||
c.jsonErr(400, 400, fmt.Sprintf("文件大小不能超过%dMB", fileUploadMaxMB))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -497,7 +504,7 @@ func (c *PlatformFileController) UploadFile() {
|
||||
}
|
||||
if n > fileUploadMaxBytes {
|
||||
_ = os.Remove(tmpPath)
|
||||
c.jsonErr(400, 400, "文件大小不能超过50MB")
|
||||
c.jsonErr(400, 400, fmt.Sprintf("文件大小不能超过%dMB", fileUploadMaxMB))
|
||||
return
|
||||
}
|
||||
sum, err := md5HashFile(tmpPath)
|
||||
|
||||
@@ -0,0 +1,323 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"server/models"
|
||||
"server/pkg/jwtutil"
|
||||
"server/services"
|
||||
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
type PlatformSoftwareUpgradeController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *PlatformSoftwareUpgradeController) platformClaims() (*jwtutil.Claims, error) {
|
||||
auth := c.Ctx.Request.Header.Get("Authorization")
|
||||
if auth == "" {
|
||||
return nil, fmt.Errorf("未登录")
|
||||
}
|
||||
parts := strings.SplitN(auth, " ", 2)
|
||||
if len(parts) != 2 || parts[0] != "Bearer" {
|
||||
return nil, fmt.Errorf("认证信息格式错误")
|
||||
}
|
||||
claims, err := jwtutil.ParseToken(parts[1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("无效的token")
|
||||
}
|
||||
if claims.UserType != "platform" {
|
||||
return nil, fmt.Errorf("无权访问")
|
||||
}
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
func (c *PlatformSoftwareUpgradeController) jsonErr(httpStatus, bizCode int, msg string) {
|
||||
c.Ctx.Output.SetStatus(httpStatus)
|
||||
c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *PlatformSoftwareUpgradeController) ok(data interface{}) {
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": data}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *PlatformSoftwareUpgradeController) backfillDownloadURL(productID uint64) {
|
||||
var row models.SystemSoftwareUpgrade
|
||||
err := models.Orm.QueryTable(new(models.SystemSoftwareUpgrade)).
|
||||
Filter("id", productID).
|
||||
Filter("delete_time__isnull", true).
|
||||
One(&row)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if row.FileID == nil || *row.FileID == 0 {
|
||||
return
|
||||
}
|
||||
if row.DownloadURL != nil && strings.TrimSpace(*row.DownloadURL) != "" {
|
||||
return
|
||||
}
|
||||
scheme, host := services.PublicRequestBaseURL(&c.Controller)
|
||||
u := services.ResolveSoftwareDownloadURL(scheme, host, nil, row.FileID)
|
||||
if u == "" {
|
||||
return
|
||||
}
|
||||
_, _ = models.Orm.QueryTable(new(models.SystemSoftwareUpgrade)).
|
||||
Filter("id", productID).
|
||||
Update(map[string]interface{}{"download_url": u})
|
||||
}
|
||||
|
||||
func (c *PlatformSoftwareUpgradeController) rowToMap(row *models.SystemSoftwareUpgrade) map[string]interface{} {
|
||||
scheme, host := services.PublicRequestBaseURL(&c.Controller)
|
||||
resolved := services.ResolveSoftwareDownloadURL(scheme, host, row.DownloadURL, row.FileID)
|
||||
return map[string]interface{}{
|
||||
"id": row.ID,
|
||||
"name": row.Name,
|
||||
"code": row.Code,
|
||||
"latestVersion": row.LatestVersion,
|
||||
"fileId": row.FileID,
|
||||
"downloadUrl": row.DownloadURL,
|
||||
"resolvedDownloadUrl": resolved,
|
||||
"forceUpdate": row.ForceUpdate,
|
||||
"releaseNotes": row.ReleaseNotes,
|
||||
"status": row.Status,
|
||||
"sort": row.Sort,
|
||||
"createTime": row.CreateTime,
|
||||
"updateTime": row.UpdateTime,
|
||||
}
|
||||
}
|
||||
|
||||
// List GET /platform/softwareupgrade/list
|
||||
func (c *PlatformSoftwareUpgradeController) List() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
page, _ := c.GetInt("page", 1)
|
||||
pageSize, _ := c.GetInt("pageSize", 20)
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 {
|
||||
pageSize = 20
|
||||
}
|
||||
if pageSize > 200 {
|
||||
pageSize = 200
|
||||
}
|
||||
keyword := strings.TrimSpace(c.GetString("keyword"))
|
||||
qs := models.Orm.QueryTable(new(models.SystemSoftwareUpgrade)).Filter("delete_time__isnull", true)
|
||||
if keyword != "" {
|
||||
cond := orm.NewCondition().Or("name__icontains", keyword).Or("code__icontains", keyword)
|
||||
qs = qs.SetCond(cond)
|
||||
}
|
||||
total, _ := qs.Count()
|
||||
var rows []models.SystemSoftwareUpgrade
|
||||
_, err := qs.OrderBy("sort", "-id").Limit(pageSize, (page-1)*pageSize).All(&rows)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "获取失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
list := make([]map[string]interface{}, 0, len(rows))
|
||||
for i := range rows {
|
||||
list = append(list, c.rowToMap(&rows[i]))
|
||||
}
|
||||
c.ok(map[string]interface{}{
|
||||
"list": list, "total": total, "page": page, "pageSize": pageSize,
|
||||
})
|
||||
}
|
||||
|
||||
// Detail GET /platform/softwareupgrade/:id
|
||||
func (c *PlatformSoftwareUpgradeController) Detail() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
c.jsonErr(400, 400, "无效ID")
|
||||
return
|
||||
}
|
||||
var row models.SystemSoftwareUpgrade
|
||||
err = models.Orm.QueryTable(new(models.SystemSoftwareUpgrade)).
|
||||
Filter("id", id).
|
||||
Filter("delete_time__isnull", true).
|
||||
One(&row)
|
||||
if err != nil {
|
||||
c.jsonErr(404, 404, "记录不存在")
|
||||
return
|
||||
}
|
||||
c.ok(c.rowToMap(&row))
|
||||
}
|
||||
|
||||
type softwareUpgradePayload struct {
|
||||
Name *string `json:"name"`
|
||||
Code *string `json:"code"`
|
||||
LatestVersion *string `json:"latestVersion"`
|
||||
FileID *uint64 `json:"fileId"`
|
||||
DownloadURL *string `json:"downloadUrl"`
|
||||
ForceUpdate *int8 `json:"forceUpdate"`
|
||||
ReleaseNotes *string `json:"releaseNotes"`
|
||||
Status *int8 `json:"status"`
|
||||
Sort *int `json:"sort"`
|
||||
}
|
||||
|
||||
// Create POST /platform/softwareupgrade
|
||||
func (c *PlatformSoftwareUpgradeController) Create() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
body, _ := io.ReadAll(c.Ctx.Request.Body)
|
||||
var p softwareUpgradePayload
|
||||
if err := json.Unmarshal(body, &p); err != nil {
|
||||
c.jsonErr(400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
if p.Name == nil || strings.TrimSpace(*p.Name) == "" || p.Code == nil || strings.TrimSpace(*p.Code) == "" {
|
||||
c.jsonErr(400, 400, "名称与产品标识 code 不能为空")
|
||||
return
|
||||
}
|
||||
v := "0.0.0"
|
||||
if p.LatestVersion != nil && strings.TrimSpace(*p.LatestVersion) != "" {
|
||||
v = strings.TrimSpace(*p.LatestVersion)
|
||||
}
|
||||
row := models.SystemSoftwareUpgrade{
|
||||
Name: strings.TrimSpace(*p.Name),
|
||||
Code: strings.TrimSpace(*p.Code),
|
||||
LatestVersion: v,
|
||||
DownloadURL: p.DownloadURL,
|
||||
ForceUpdate: 0,
|
||||
Status: 1,
|
||||
Sort: 0,
|
||||
}
|
||||
if p.ForceUpdate != nil {
|
||||
row.ForceUpdate = *p.ForceUpdate
|
||||
}
|
||||
if p.ReleaseNotes != nil {
|
||||
row.ReleaseNotes = p.ReleaseNotes
|
||||
}
|
||||
if p.Status != nil {
|
||||
row.Status = *p.Status
|
||||
}
|
||||
if p.Sort != nil {
|
||||
row.Sort = *p.Sort
|
||||
}
|
||||
if p.FileID != nil && *p.FileID > 0 {
|
||||
row.FileID = p.FileID
|
||||
}
|
||||
id, err := models.Orm.Insert(&row)
|
||||
if err != nil {
|
||||
if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
|
||||
c.jsonErr(400, 400, "产品标识 code 已存在")
|
||||
return
|
||||
}
|
||||
c.jsonErr(500, 500, "创建失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
c.backfillDownloadURL(uint64(id))
|
||||
c.ok(map[string]interface{}{"id": id})
|
||||
}
|
||||
|
||||
// Update POST /platform/softwareupgrade/:id
|
||||
func (c *PlatformSoftwareUpgradeController) Update() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
c.jsonErr(400, 400, "无效ID")
|
||||
return
|
||||
}
|
||||
body, _ := io.ReadAll(c.Ctx.Request.Body)
|
||||
var p softwareUpgradePayload
|
||||
if err := json.Unmarshal(body, &p); err != nil {
|
||||
c.jsonErr(400, 400, "参数错误")
|
||||
return
|
||||
}
|
||||
up := map[string]interface{}{}
|
||||
if p.Name != nil {
|
||||
up["name"] = strings.TrimSpace(*p.Name)
|
||||
}
|
||||
if p.Code != nil {
|
||||
up["code"] = strings.TrimSpace(*p.Code)
|
||||
}
|
||||
if p.LatestVersion != nil {
|
||||
up["latest_version"] = strings.TrimSpace(*p.LatestVersion)
|
||||
}
|
||||
if p.FileID != nil {
|
||||
if *p.FileID == 0 {
|
||||
up["file_id"] = nil
|
||||
} else {
|
||||
up["file_id"] = *p.FileID
|
||||
}
|
||||
}
|
||||
if p.DownloadURL != nil {
|
||||
up["download_url"] = strings.TrimSpace(*p.DownloadURL)
|
||||
}
|
||||
if p.ForceUpdate != nil {
|
||||
up["force_update"] = *p.ForceUpdate
|
||||
}
|
||||
if p.ReleaseNotes != nil {
|
||||
up["release_notes"] = *p.ReleaseNotes
|
||||
}
|
||||
if p.Status != nil {
|
||||
up["status"] = *p.Status
|
||||
}
|
||||
if p.Sort != nil {
|
||||
up["sort"] = *p.Sort
|
||||
}
|
||||
if len(up) == 0 {
|
||||
c.jsonErr(400, 400, "无更新字段")
|
||||
return
|
||||
}
|
||||
n, err := models.Orm.QueryTable(new(models.SystemSoftwareUpgrade)).
|
||||
Filter("id", id).
|
||||
Filter("delete_time__isnull", true).
|
||||
Update(up)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "更新失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
c.jsonErr(404, 404, "记录不存在")
|
||||
return
|
||||
}
|
||||
c.backfillDownloadURL(id)
|
||||
c.ok(nil)
|
||||
}
|
||||
|
||||
// Delete DELETE /platform/softwareupgrade/:id
|
||||
func (c *PlatformSoftwareUpgradeController) Delete() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
c.jsonErr(400, 400, "无效ID")
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
n, err := models.Orm.QueryTable(new(models.SystemSoftwareUpgrade)).
|
||||
Filter("id", id).
|
||||
Filter("delete_time__isnull", true).
|
||||
Update(map[string]interface{}{"delete_time": now})
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "删除失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
c.jsonErr(404, 404, "记录不存在")
|
||||
return
|
||||
}
|
||||
c.ok(nil)
|
||||
}
|
||||
Reference in New Issue
Block a user