|
@@ -1,11 +1,8 @@
|
|
|
package voice_broadcast
|
|
|
|
|
|
import (
|
|
|
- "encoding/json"
|
|
|
- "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"
|
|
@@ -14,11 +11,8 @@ import (
|
|
|
"hongze/hongze_yb/services/company"
|
|
|
"hongze/hongze_yb/services/user"
|
|
|
"hongze/hongze_yb/utils"
|
|
|
- "io/ioutil"
|
|
|
- "os"
|
|
|
- "path"
|
|
|
"strconv"
|
|
|
- "time"
|
|
|
+ "strings"
|
|
|
)
|
|
|
|
|
|
// BroadcastList
|
|
@@ -79,116 +73,181 @@ func BroadcastList(c *gin.Context) {
|
|
|
// @Success 200 {string} string "发布成功"
|
|
|
// @failure 400 {string} string "发布失败"
|
|
|
// @Router /add [post]
|
|
|
-func AddBroadcast(c *gin.Context) {
|
|
|
- broadcastName := c.PostForm("broadcast_name")
|
|
|
- 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")
|
|
|
- imgUrl := c.PostForm("img_url")
|
|
|
- file, err := c.FormFile("file")
|
|
|
- if err != nil {
|
|
|
- response.FailMsg("获取资源失败", "获取资源失败, Err:"+err.Error(), c)
|
|
|
- return
|
|
|
- }
|
|
|
- if imgUrl == "" {
|
|
|
- response.Fail("图片不能为空", c)
|
|
|
- return
|
|
|
- }
|
|
|
- // 生成动态分享图
|
|
|
- createTimeStr := time.Now().Local().Format(utils.FormatDate)
|
|
|
- pars := services.VoiceBroadcastShareImgPars{
|
|
|
- BackgroundImg: imgUrl,
|
|
|
- Title: sectionName,
|
|
|
- CreateTime: createTimeStr,
|
|
|
- }
|
|
|
- parsByte, e := json.Marshal(pars)
|
|
|
- if e != nil {
|
|
|
- response.Fail("分享图参数有误", c)
|
|
|
- return
|
|
|
- }
|
|
|
- shareImg, e := services.GetDynamicShareImg(services.VoiceBroadcastShareImgSource, string(parsByte))
|
|
|
- if e != nil {
|
|
|
- response.Fail("生成分享图失败", c)
|
|
|
- return
|
|
|
- }
|
|
|
+//func AddBroadcast(c *gin.Context) {
|
|
|
+// broadcastName := c.PostForm("broadcast_name")
|
|
|
+// 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")
|
|
|
+// imgUrl := c.PostForm("img_url")
|
|
|
+// file, err := c.FormFile("file")
|
|
|
+// if err != nil {
|
|
|
+// response.FailMsg("获取资源失败", "获取资源失败, Err:"+err.Error(), c)
|
|
|
+// return
|
|
|
+// }
|
|
|
+// if imgUrl == "" {
|
|
|
+// response.Fail("图片不能为空", c)
|
|
|
+// return
|
|
|
+// }
|
|
|
+// // 生成动态分享图
|
|
|
+// createTimeStr := time.Now().Local().Format(utils.FormatDate)
|
|
|
+// pars := services.VoiceBroadcastShareImgPars{
|
|
|
+// BackgroundImg: imgUrl,
|
|
|
+// Title: sectionName,
|
|
|
+// CreateTime: createTimeStr,
|
|
|
+// }
|
|
|
+// parsByte, e := json.Marshal(pars)
|
|
|
+// if e != nil {
|
|
|
+// response.Fail("分享图参数有误", c)
|
|
|
+// return
|
|
|
+// }
|
|
|
+// shareImg, e := services.GetDynamicShareImg(services.VoiceBroadcastShareImgSource, string(parsByte))
|
|
|
+// if e != nil {
|
|
|
+// response.Fail("生成分享图失败", 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,
|
|
|
+// ImgUrl: shareImg,
|
|
|
+// 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.OkData("发布成功", voiceBroadcast.BroadcastId, c)
|
|
|
+//}
|
|
|
|
|
|
- ext := path.Ext(file.Filename)
|
|
|
- if ext != ".mp3" {
|
|
|
- response.Fail("暂仅支持mp3格式", c)
|
|
|
+// PublishBroadcast
|
|
|
+// @Description 发布语音播报
|
|
|
+// @Param broadcast_id query int false "语音播报ID"
|
|
|
+// @Param broadcast_name query string true "语音标题"
|
|
|
+// @Param section_id query int true "板块ID"
|
|
|
+// @Param section_name query string true "板块名称"
|
|
|
+// @Param variety_id query int true "品种ID"
|
|
|
+// @Param variety_name query string true "品种名称"
|
|
|
+// @Param author_id query int true "作者ID"
|
|
|
+// @Param author query string true "作者名称"
|
|
|
+// @Param img_url query string true "分享图背景"
|
|
|
+// @Param Imgs query string true "图片,英文逗号拼接"
|
|
|
+// @Param publish_type query string true "发布类型: 0-仅发布 1-发布并推送 2-定时发布"
|
|
|
+// @Param pre_publish_time query string false "预发布时间"
|
|
|
+// @Param file query string true "音频文件"
|
|
|
+// @Success 200 {string} string "发布成功"
|
|
|
+// @failure 400 {string} string "发布失败"
|
|
|
+// @Router /add [post]
|
|
|
+func PublishBroadcast(c *gin.Context) {
|
|
|
+ var req request.SaveBroadcastReq
|
|
|
+ if err := c.Bind(&req); err != nil {
|
|
|
+ response.Fail("参数有误", 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)
|
|
|
+ // 参数校验
|
|
|
+ if req.BroadcastName == "" {
|
|
|
+ response.Fail("请输入标题", 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)
|
|
|
+ if req.SectionId <= 0 || req.SectionName == "" {
|
|
|
+ response.Fail("请选择品种", c)
|
|
|
return
|
|
|
}
|
|
|
- // 获取音频文件时长
|
|
|
- fByte, err := ioutil.ReadFile(fpath)
|
|
|
- if err != nil {
|
|
|
- response.FailMsg("读取本地文件失败", "QuestionUploadAudio 读取本地文件失败", c)
|
|
|
+ if req.SectionId <= 0 || req.SectionName == "" {
|
|
|
+ response.Fail("请选择板块", c)
|
|
|
return
|
|
|
}
|
|
|
- if len(fByte) <= 0 {
|
|
|
- response.FailMsg("文件大小有误", "QuestionUploadAudio 文件大小有误", c)
|
|
|
+ if req.File == nil {
|
|
|
+ response.Fail("请上传音频", c)
|
|
|
return
|
|
|
}
|
|
|
- seconds, err := services.GetMP3PlayDuration(fByte)
|
|
|
- if err != nil {
|
|
|
- response.FailMsg("读取文件时长失败", "QuestionUploadAudio 读取文件时长失败", c)
|
|
|
+ if req.ImgUrl == "" {
|
|
|
+ response.Fail("分享图背景不能为空", c)
|
|
|
return
|
|
|
}
|
|
|
- // 音频大小MB
|
|
|
- fi, err := os.Stat(fpath)
|
|
|
- if err != nil {
|
|
|
- response.FailMsg("读取文件大小失败", "QuestionUploadAudio 读取文件大小失败", c)
|
|
|
+ if req.PublishType == 2 && req.PrePublishTime == "" {
|
|
|
+ response.Fail("定时发布请选择发布时间", 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
|
|
|
+ if req.Imgs != "" {
|
|
|
+ imgList := strings.Split(req.Imgs, ",")
|
|
|
+ if len(imgList) > 5 {
|
|
|
+ response.Fail("最多插入五张图片", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- voiceBroadcast := voice_broadcast.VoiceBroadcast{
|
|
|
- BroadcastName: broadcastName,
|
|
|
- SectionId: sectionId,
|
|
|
- SectionName: sectionName,
|
|
|
- VarietyId: varietyId,
|
|
|
- VarietyName: varietyName,
|
|
|
- AuthorId: authorId,
|
|
|
- Author: author,
|
|
|
- ImgUrl: shareImg,
|
|
|
- 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())
|
|
|
+ // 发布语音
|
|
|
+ if msg, e := services.PublishVoiceBroadcast(req.BroadcastId, req.SectionId, req.VarietyId, req.AuthorId, req.PublishType, req.BroadcastName,
|
|
|
+ req.SectionName, req.VarietyName, req.Author, req.ImgUrl, req.PrePublishTime, req.Imgs, req.File); e != nil {
|
|
|
+ response.FailMsg(msg, e.Error(), c)
|
|
|
+ return
|
|
|
}
|
|
|
- response.OkData("发布成功", voiceBroadcast.BroadcastId, c)
|
|
|
+ response.Ok("发布成功", c)
|
|
|
}
|
|
|
|
|
|
// SectionList
|