|
@@ -1,13 +1,21 @@
|
|
|
package voice_broadcast
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"hongze/hongze_yb/controller/response"
|
|
|
+ "hongze/hongze_yb/global"
|
|
|
"hongze/hongze_yb/models/request"
|
|
|
voiceResp "hongze/hongze_yb/models/response"
|
|
|
+ "hongze/hongze_yb/models/tables/voice_broadcast"
|
|
|
"hongze/hongze_yb/services"
|
|
|
"hongze/hongze_yb/services/user"
|
|
|
"hongze/hongze_yb/utils"
|
|
|
+ "io/ioutil"
|
|
|
+ "os"
|
|
|
+ "path"
|
|
|
+ "strconv"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
// BroadcastList
|
|
@@ -15,6 +23,7 @@ import (
|
|
|
// @Param page_index query int false "页码"
|
|
|
// @Param page_size query int false "每页数量"
|
|
|
// @Param broadcast_id query int false "语音播报id"
|
|
|
+// @Param section_id query int false "板块id"
|
|
|
// @Success 200 {object} []voiceResp.BroadcastListResp
|
|
|
// @failure 400 {string} string "获取失败"
|
|
|
// @Router /list [get]
|
|
@@ -32,7 +41,7 @@ func BroadcastList(c *gin.Context) {
|
|
|
}
|
|
|
|
|
|
userinfo := user.GetInfoByClaims(c)
|
|
|
- list, err := services.GetVoiceBroadcastList(req.PageIndex, req.PageSize, req.BroadcastId, userinfo)
|
|
|
+ list, err := services.GetVoiceBroadcastList(req.PageIndex, req.PageSize, req.SectionId, req.BroadcastId, userinfo)
|
|
|
if err != nil {
|
|
|
|
|
|
response.FailMsg("获取语音播报列表失败,"+err.Error(), "QuestionList ErrMsg:"+err.Error(), c)
|
|
@@ -50,3 +59,112 @@ func BroadcastList(c *gin.Context) {
|
|
|
response.OkData("获取成功", resp, c)
|
|
|
}
|
|
|
|
|
|
+// AddBroadcast
|
|
|
+// @Description 新建语音播报
|
|
|
+// @Param file query string true "音频文件"
|
|
|
+// @Success 200 {string} string "发布成功"
|
|
|
+// @failure 400 {string} string "发布失败"
|
|
|
+// @Router /add [post]
|
|
|
+func AddBroadcast(c *gin.Context) {
|
|
|
+ //var req request.AddBroadcastReq
|
|
|
+ broadcastName := c.PostForm("broadcast_name")
|
|
|
+ fmt.Println("broadcastName:",broadcastName)
|
|
|
+ nsectionId := c.PostForm("section_id")
|
|
|
+ sectionId, _ := strconv.Atoi(nsectionId)
|
|
|
+ sectionName := c.PostForm("section_name")
|
|
|
+ nvarietyId := c.PostForm("variety_id")
|
|
|
+ varietyId, _ := strconv.Atoi(nvarietyId)
|
|
|
+ varietyName := c.PostForm("variety_name")
|
|
|
+ nauthorId := c.PostForm("author_id")
|
|
|
+ authorId, _ := strconv.Atoi(nauthorId)
|
|
|
+ author := c.PostForm("author")
|
|
|
+ file, err := c.FormFile("file")
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("获取资源失败", "获取资源失败, Err:"+err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ ext := path.Ext(file.Filename)
|
|
|
+ if ext != ".mp3" {
|
|
|
+ response.Fail("暂仅支持mp3格式", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ dateDir := time.Now().Format("20060102")
|
|
|
+ localDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
|
|
|
+ if err := os.MkdirAll(localDir, 0766); err != nil {
|
|
|
+ response.FailMsg("存储目录创建失败", "QuestionUploadAudio 存储目录创建失败, Err:"+err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ randStr := utils.GetRandStringNoSpecialChar(28)
|
|
|
+ filtName := randStr + ext
|
|
|
+ fpath := localDir + "/" + filtName
|
|
|
+ defer func() {
|
|
|
+ _ = os.Remove(fpath)
|
|
|
+ }()
|
|
|
+ // 生成文件至指定目录
|
|
|
+ if err := c.SaveUploadedFile(file, fpath); err != nil {
|
|
|
+ response.FailMsg("文件生成失败", "QuestionUploadAudio 文件生成失败, Err:"+err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 获取音频文件时长
|
|
|
+ fByte, err := ioutil.ReadFile(fpath)
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("读取本地文件失败", "QuestionUploadAudio 读取本地文件失败", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(fByte) <= 0 {
|
|
|
+ response.FailMsg("文件大小有误", "QuestionUploadAudio 文件大小有误", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ seconds, err := services.GetMP3PlayDuration(fByte)
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("读取文件时长失败", "QuestionUploadAudio 读取文件时长失败", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 音频大小MB
|
|
|
+ fi, err := os.Stat(fpath)
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("读取文件大小失败", "QuestionUploadAudio 读取文件大小失败", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ mb := utils.Bit2MB(fi.Size(), 2)
|
|
|
+ // 上传文件至阿里云
|
|
|
+ ossDir := "yb_wx/voice_broadcast/"
|
|
|
+ resourceUrl, err := services.UploadAliyunToDir(filtName, fpath, ossDir)
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("文件上传失败", "QuestionUploadAudio 文件上传失败, Err:"+err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ voiceBroadcast := voice_broadcast.VoiceBroadcast{
|
|
|
+ BroadcastName: broadcastName,
|
|
|
+ SectionId: sectionId,
|
|
|
+ SectionName: sectionName,
|
|
|
+ VarietyId: varietyId,
|
|
|
+ VarietyName: varietyName,
|
|
|
+ AuthorId: authorId,
|
|
|
+ Author: author,
|
|
|
+ VoiceUrl: resourceUrl,
|
|
|
+ VoicePlaySeconds: fmt.Sprint(seconds),
|
|
|
+ VoiceSize: fmt.Sprint(mb),
|
|
|
+ CreateTime: time.Now().Format(utils.FormatDateTime),
|
|
|
+ }
|
|
|
+ err = voiceBroadcast.AddVoiceBroadcast()
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("AddUserViewHistory err", err.Error())
|
|
|
+ }
|
|
|
+ response.Ok("发布成功", c)
|
|
|
+}
|
|
|
+
|
|
|
+// BroadcastList
|
|
|
+// @Description 语音播报列表
|
|
|
+// @Param page_index query int false "页码"
|
|
|
+// @Param page_size query int false "每页数量"
|
|
|
+// @Param broadcast_id query int false "语音播报id"
|
|
|
+// @Param section_id query int false "板块id"
|
|
|
+// @Success 200 {object} []voiceResp.BroadcastListResp
|
|
|
+// @failure 400 {string} string "获取失败"
|
|
|
+// @Router /section/list [get]
|
|
|
+func SectionList(c *gin.Context) {
|
|
|
+
|
|
|
+}
|