zhangzengfei
2023-09-04 e8e536d1cb52d2126c8c7ce2ba1c7a76f7208678
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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: "未找到上传声音文件"}
}