|
@@ -0,0 +1,321 @@
|
|
|
+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/models/tables/voice_section"
|
|
|
+ "hongze/hongze_yb/services"
|
|
|
+ "hongze/hongze_yb/services/company"
|
|
|
+ "hongze/hongze_yb/services/user"
|
|
|
+ "hongze/hongze_yb/services/wechat"
|
|
|
+ "hongze/hongze_yb/utils"
|
|
|
+ "io/ioutil"
|
|
|
+ "os"
|
|
|
+ "path"
|
|
|
+ "strconv"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// 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 /list [Post]
|
|
|
+func BroadcastList(c *gin.Context) {
|
|
|
+ var req request.BroadcastListReq
|
|
|
+ if err := c.Bind(&req); err != nil {
|
|
|
+ response.Fail("参数有误", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if req.PageIndex == 0 {
|
|
|
+ req.PageIndex = 1
|
|
|
+ }
|
|
|
+ if req.PageSize == 0 {
|
|
|
+ req.PageSize = utils.PageSize20
|
|
|
+ }
|
|
|
+
|
|
|
+ userinfo := user.GetInfoByClaims(c)
|
|
|
+ ok, checkInfo, _, err := company.CheckBaseFiccPermission(userinfo.CompanyID, int(userinfo.UserID))
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("用户权限验证失败", "CheckBaseAuth-用户权限验证失败" + err.Error(), c)
|
|
|
+ c.Abort()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !ok {
|
|
|
+ response.AuthError(checkInfo, "暂无权限", c)
|
|
|
+ c.Abort()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ 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)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ isVoiceAdmin, _, err := services.GetVoiceAdminByUserInfo(userinfo)
|
|
|
+ if err != nil && err != utils.ErrNoRow {
|
|
|
+ response.FailMsg("获取语音管理员信息失败", "QuestionList ErrMsg:"+err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var resp voiceResp.BroadcastListResp
|
|
|
+ resp.List = list
|
|
|
+ resp.IsVoiceAdmin = isVoiceAdmin
|
|
|
+ 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) {
|
|
|
+ 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")
|
|
|
+ imgUrl := c.PostForm("img_url")
|
|
|
+ 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,
|
|
|
+ ImgUrl: imgUrl,
|
|
|
+ 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())
|
|
|
+ }
|
|
|
+
|
|
|
+ // 推送回复消息给用户
|
|
|
+ go wechat.SendVoiceBroadcastWxMsg(voiceBroadcast.BroadcastId, voiceBroadcast.SectionName, voiceBroadcast.BroadcastName)
|
|
|
+
|
|
|
+ //同花顺客群
|
|
|
+ go services.SendVoiceBroadcastToThs(voiceBroadcast)
|
|
|
+ response.Ok("发布成功", c)
|
|
|
+}
|
|
|
+
|
|
|
+// SectionList
|
|
|
+// @Description 语音播报板块列表
|
|
|
+// @Success 200 {object} []voiceResp.VarietyList
|
|
|
+// @failure 400 {string} string "获取失败"
|
|
|
+// @Router /section/list [get]
|
|
|
+func SectionList(c *gin.Context) {
|
|
|
+ sList, err := voice_section.GetVoiceSection()
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
|
|
|
+ }
|
|
|
+ vList, err := voice_section.GetVoiceVariety()
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
|
|
|
+ }
|
|
|
+ var sectionList []voiceResp.SectionList
|
|
|
+ var varietyList []voiceResp.VarietyList
|
|
|
+ var resp []voiceResp.VarietyList
|
|
|
+ //var resp voiceResp.SectionListResp
|
|
|
+ //for _, s := range sList {
|
|
|
+ // section := voiceResp.SectionList{
|
|
|
+ // SectionId: s.SectionId,
|
|
|
+ // SectionName: s.SectionName,
|
|
|
+ // Status: s.Status,
|
|
|
+ // }
|
|
|
+ // sectionList = append(sectionList, section)
|
|
|
+ //}
|
|
|
+ var newsList []*voice_section.VoiceSection
|
|
|
+ //var bannedSectionList []*voice_section.VoiceSection
|
|
|
+
|
|
|
+ //查找被禁用的板块ids
|
|
|
+ var bannedIds []int
|
|
|
+ for _, section := range sList {
|
|
|
+ if section.Status == 0 {
|
|
|
+ //bannedSectionList = append(bannedSectionList, section)
|
|
|
+ bannedIds = append(bannedIds, section.SectionId)
|
|
|
+ } else {
|
|
|
+ newsList = append(newsList, section)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //如果有被禁用的板块,去语音列表查找被禁用板块有没有语音
|
|
|
+ var lists []*voice_broadcast.VoiceBroadcast
|
|
|
+ if len(bannedIds) > 0{
|
|
|
+ lists, err = voice_section.GetVoiceSectionFromBroadcast(bannedIds)
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("查询语音播报禁用板块失败", "GetVoiceSectionFromBroadcast, Err:"+err.Error(), c)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //被禁用板块有语音,依然显示该板块
|
|
|
+ if len(lists) > 0 {
|
|
|
+ //清空切片,用新的
|
|
|
+ newsList = newsList[0:0]
|
|
|
+ bannedMap := make(map[int]int)
|
|
|
+ for _, broadcast := range lists {
|
|
|
+ bannedMap[broadcast.SectionId] = broadcast.SectionId
|
|
|
+ }
|
|
|
+ for _, section := range sList {
|
|
|
+ _,ok := bannedMap[section.SectionId]
|
|
|
+ if section.Status != 0 || ok {
|
|
|
+ newsList = append(newsList, section)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range vList {
|
|
|
+ variety := voiceResp.VarietyList{
|
|
|
+ VarietyId: v.VarietyId,
|
|
|
+ VarietyName: v.VarietyName,
|
|
|
+ }
|
|
|
+ varietyList = append(varietyList, variety)
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range varietyList {
|
|
|
+ for _, s := range newsList {
|
|
|
+ if v.VarietyId == s.VarietyId {
|
|
|
+ section := voiceResp.SectionList{
|
|
|
+ ImgUrl: s.ImgUrl,
|
|
|
+ SectionId: s.SectionId,
|
|
|
+ SectionName: s.SectionName,
|
|
|
+ Status: s.Status,
|
|
|
+ }
|
|
|
+ sectionList = append(sectionList, section)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(sectionList) == 0{
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ v.Children = sectionList
|
|
|
+ resp = append(resp, v)
|
|
|
+ sectionList = []voiceResp.SectionList{}
|
|
|
+ }
|
|
|
+ response.OkData("上传成功", resp, c)
|
|
|
+}
|
|
|
+
|
|
|
+// DelBroadcast
|
|
|
+// @Description 删除语音播报
|
|
|
+// @Param broadcast_id query int false "语音播报id"
|
|
|
+// @Success 200 {string} string "删除成功"
|
|
|
+// @failure 400 {string} string "删除失败"
|
|
|
+// @Router /delete [get]
|
|
|
+func DelBroadcast(c *gin.Context) {
|
|
|
+ sbroadcastId := c.DefaultQuery("broadcast_id", "0")
|
|
|
+ broadcastId, err := strconv.Atoi(sbroadcastId)
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("转换id失败,请输入正确的id", "strconv.Atoi, Err:"+err.Error(), c)
|
|
|
+ }
|
|
|
+ if broadcastId <= 0 {
|
|
|
+ response.FailMsg("参数错误","参数有误", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var item voice_broadcast.VoiceBroadcast
|
|
|
+ item.BroadcastId = broadcastId
|
|
|
+ err = item.DelVoiceBroadcast()
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("删除语音播报失败", "DelVoiceBroadcast, Err:"+err.Error(), c)
|
|
|
+ }
|
|
|
+ response.Ok("删除成功", c)
|
|
|
+}
|
|
|
+
|
|
|
+// AddStatistics
|
|
|
+// @Description 新增语音播报记录
|
|
|
+// @Param file query string true "音频文件"
|
|
|
+// @Success 200 {string} string "新增成功"
|
|
|
+// @failure 400 {string} string "新增失败"
|
|
|
+// @Router /statistics/add [post]
|
|
|
+func AddStatistics(c *gin.Context) {
|
|
|
+ var req request.AddBroadcastStatisticsReq
|
|
|
+ if err := c.Bind(&req); err != nil {
|
|
|
+ response.Fail("参数有误", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.BroadcastId <= 0{
|
|
|
+ response.Fail("参数有误", c)
|
|
|
+ }
|
|
|
+ userinfo := user.GetInfoByClaims(c)
|
|
|
+
|
|
|
+ go services.AddBroadcastRecord(userinfo, req.Source, req.BroadcastId)
|
|
|
+
|
|
|
+ response.Ok("新增记录成功", c)
|
|
|
+}
|