zhangzengfei
2023-09-05 63645d248c765244488cd34dbc1bb6528ca6b7c7
system-service/controllers/voice.go
@@ -1,159 +1,159 @@
package controllers
import (
   "basic.com/pubsub/protomsg.git"
   "basic.com/valib/bhomeclient.git"
   "encoding/json"
   "github.com/satori/go.uuid"
   "vamicro/system-service/models"
   "vamicro/system-service/service"
)
type VoiceController struct{}
type VoiceMenu struct {
   Id   string `json:"id"`
   Name string `json:"name"`
   Path string `json:"path"`
}
// @Summary 查找所有报警声音
// @Description 查找所有报警声音
// @Produce json
// @Tags 报警声音
// @Success 200 {string} json "{"code":200, msg:"",data:"",success:true}"
// @Failure 500 {string} json "{"code":500, msg:"",data:"[]",success:false}"
// @Router /data/api-v/voice/findAll [GET]
func (vc *VoiceController) FindAll(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
   var voiceMenus = make([]VoiceMenu, 0)
   var voice models.Voice
   voices, err := voice.FindAll()
   //config.Server.Voices
   if err == nil {
      for _, v := range voices {
         voiceMenus = append(voiceMenus, VoiceMenu{
            Id:   v.Id,
            Name: v.Name,
            Path: v.Path,
         })
      }
   }
   return &bhomeclient.Reply{Success: true, Data: voiceMenus}
}
// @Summary 获取报警声音列表
// @Description 获取报警声音列表
// @Security ApiKeyAuth
// @Produce json
// @Tags 报警声音
// @Param id formData string false "id"
// @Param name formData string true "名称"
// @Param mp3File formData string true "mp3文件地址"
// @Param g711aFile formData string true "g711a文件地址"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/voice/add [post]
func (vc *VoiceController) Add(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
   id := c.PostForm("id")
   name := c.PostForm("name")
   mp3File := c.PostForm("mp3File")
   //g711aFile := c.PostForm("g711aFile")
   if name == "" {
      return &bhomeclient.Reply{Msg: "name不能为空"}
   }
   if mp3File == "" {
      return &bhomeclient.Reply{Msg: "音频文件不能为空"}
   }
   b := false
   var err error
   v := models.Voice{
      Name: name,
      Path: mp3File,
   }
   dbMsg := protomsg.DbChangeMessage{
      Table: protomsg.TableChanged_T_Voice,
   }
   if id == "" { //新增
      v.Id = uuid.NewV4().String()
      b, err = v.Insert()
      dbMsg.Action = protomsg.DbAction_Insert
      dbMsg.Id = v.Id
   } else { //更新
      v.Id = id
      b, err = v.Update()
      dbMsg.Action = protomsg.DbAction_Update
      dbMsg.Id = id
   }
   if err == nil {
      pb, _ := json.Marshal(dbMsg)
      h.Bk.Publish(service.ProcName, pb)
      return &bhomeclient.Reply{Success: b, Msg: "保存成功"}
   } else {
      return &bhomeclient.Reply{Msg: err.Error()}
   }
}
// @Summary 删除报警声音
// @Description 删除报警声音
// @Security ApiKeyAuth
// @Produce json
// @Tags 报警声音
// @Param id query string true "id"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/voice/del [delete]
func (vc *VoiceController) Del(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
   id := c.Query("id")
   if id == "" {
      return &bhomeclient.Reply{Msg: "id不能为空"}
   }
   var v models.Voice
   b, err := v.DeleteById(id)
   if b {
      dbMsg := protomsg.DbChangeMessage{
         Table:  protomsg.TableChanged_T_Voice,
         Id:     id,
         Action: protomsg.DbAction_Delete,
      }
      pb, _ := json.Marshal(dbMsg)
      h.Bk.Publish(service.ProcName, pb)
      return &bhomeclient.Reply{Success: true, Msg: "删除成功"}
   } else {
      return &bhomeclient.Reply{Msg: err.Error()}
   }
}
// @Security ApiKeyAuth
// @Summary 上传音频
// @Description 上传音频
// @Accept x-www-form-urlencoded
// @Produce json
// @Tags 报警声音
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-v/voice/upload [post]
func (vc *VoiceController) Upload(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
   file, err := c.FormFile()
   if err == nil {
      var sv service.SysService
      if file.Name == "" || len(file.Bytes) == 0 {
         return &bhomeclient.Reply{Msg: "音频上传错误"}
      }
      filename := file.Name
      audioPath, err := sv.UploadVoice(file.Bytes, filename)
      if err != nil {
         if err.Error() == "audio format error" {
            return &bhomeclient.Reply{Msg: "声音只允许mp3,wav,wma的格式"}
         } else {
            return &bhomeclient.Reply{Msg: "声音上传失败:" + err.Error()}
         }
      }
      return &bhomeclient.Reply{Success: true, Data: audioPath}
   }
   return &bhomeclient.Reply{Msg: "未找到上传声音文件"}
}
package controllers
import (
   "basic.com/pubsub/protomsg.git"
   "basic.com/valib/bhomeclient.git"
   "encoding/json"
   "github.com/satori/go.uuid"
   "vamicro/system-service/models"
   "vamicro/system-service/service"
)
type VoiceController struct{}
type VoiceMenu struct {
   Id   string `json:"id"`
   Name string `json:"name"`
   Path string `json:"path"`
}
// @Summary 查找所有报警声音
// @Description 查找所有报警声音
// @Produce json
// @Tags 报警声音
// @Success 200 {string} json "{"code":200, msg:"",data:"",success:true}"
// @Failure 500 {string} json "{"code":500, msg:"",data:"[]",success:false}"
// @Router /data/api-v/voice/findAll [GET]
func (vc *VoiceController) FindAll(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
   var voiceMenus = make([]VoiceMenu, 0)
   var voice models.Voice
   voices, err := voice.FindAll()
   //config.Server.Voices
   if err == nil {
      for _, v := range voices {
         voiceMenus = append(voiceMenus, VoiceMenu{
            Id:   v.Id,
            Name: v.Name,
            Path: v.Path,
         })
      }
   }
   return &bhomeclient.Reply{Success: true, Data: voiceMenus}
}
// @Summary 获取报警声音列表
// @Description 获取报警声音列表
// @Security ApiKeyAuth
// @Produce json
// @Tags 报警声音
// @Param id formData string false "id"
// @Param name formData string true "名称"
// @Param mp3File formData string true "mp3文件地址"
// @Param g711aFile formData string true "g711a文件地址"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/voice/add [post]
func (vc *VoiceController) Add(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
   id := c.PostForm("id")
   name := c.PostForm("name")
   mp3File := c.PostForm("mp3File")
   //g711aFile := c.PostForm("g711aFile")
   if name == "" {
      return &bhomeclient.Reply{Msg: "name不能为空"}
   }
   if mp3File == "" {
      return &bhomeclient.Reply{Msg: "音频文件不能为空"}
   }
   b := false
   var err error
   v := models.Voice{
      Name: name,
      Path: mp3File,
   }
   dbMsg := protomsg.DbChangeMessage{
      Table: protomsg.TableChanged_T_Voice,
   }
   if id == "" { //新增
      v.Id = uuid.NewV4().String()
      b, err = v.Insert()
      dbMsg.Action = protomsg.DbAction_Insert
      dbMsg.Id = v.Id
   } else { //更新
      v.Id = id
      b, err = v.Update()
      dbMsg.Action = protomsg.DbAction_Update
      dbMsg.Id = id
   }
   if err == nil {
      pb, _ := json.Marshal(dbMsg)
      h.Bk.Publish(service.ProcName, pb)
      return &bhomeclient.Reply{Success: b, Msg: "保存成功"}
   } else {
      return &bhomeclient.Reply{Msg: err.Error()}
   }
}
// @Summary 删除报警声音
// @Description 删除报警声音
// @Security ApiKeyAuth
// @Produce json
// @Tags 报警声音
// @Param id query string true "id"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/voice/del [delete]
func (vc *VoiceController) Del(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
   id := c.Query("id")
   if id == "" {
      return &bhomeclient.Reply{Msg: "id不能为空"}
   }
   var v models.Voice
   b, err := v.DeleteById(id)
   if b {
      dbMsg := protomsg.DbChangeMessage{
         Table:  protomsg.TableChanged_T_Voice,
         Id:     id,
         Action: protomsg.DbAction_Delete,
      }
      pb, _ := json.Marshal(dbMsg)
      h.Bk.Publish(service.ProcName, pb)
      return &bhomeclient.Reply{Success: true, Msg: "删除成功"}
   } else {
      return &bhomeclient.Reply{Msg: err.Error()}
   }
}
// @Security ApiKeyAuth
// @Summary 上传音频
// @Description 上传音频
// @Accept x-www-form-urlencoded
// @Produce json
// @Tags 报警声音
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"", data:""}"
// @Router /data/api-v/voice/upload [post]
func (vc *VoiceController) Upload(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
   file, err := c.FormFile()
   if err == nil {
      var sv service.SysService
      if file.Name == "" || len(file.Bytes) == 0 {
         return &bhomeclient.Reply{Msg: "音频上传错误"}
      }
      filename := file.Name
      audioPath, err := sv.UploadVoice(file.Bytes, filename)
      if err != nil {
         if err.Error() == "audio format error" {
            return &bhomeclient.Reply{Msg: "声音只允许mp3,wav,wma的格式"}
         } else {
            return &bhomeclient.Reply{Msg: "声音上传失败:" + err.Error()}
         }
      }
      return &bhomeclient.Reply{Success: true, Data: audioPath}
   }
   return &bhomeclient.Reply{Msg: "未找到上传声音文件"}
}