voice_broadcast.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package voice_broadcast
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "hongze/hongze_yb/controller/response"
  6. "hongze/hongze_yb/global"
  7. "hongze/hongze_yb/models/request"
  8. voiceResp "hongze/hongze_yb/models/response"
  9. "hongze/hongze_yb/models/tables/voice_broadcast"
  10. "hongze/hongze_yb/models/tables/voice_section"
  11. "hongze/hongze_yb/services"
  12. "hongze/hongze_yb/services/company"
  13. "hongze/hongze_yb/services/user"
  14. "hongze/hongze_yb/services/wechat"
  15. "hongze/hongze_yb/utils"
  16. "io/ioutil"
  17. "os"
  18. "path"
  19. "strconv"
  20. "time"
  21. )
  22. // BroadcastList
  23. // @Description 语音播报列表
  24. // @Param page_index query int false "页码"
  25. // @Param page_size query int false "每页数量"
  26. // @Param broadcast_id query int false "语音播报id"
  27. // @Param section_id query int false "板块id"
  28. // @Success 200 {object} []voiceResp.BroadcastListResp
  29. // @failure 400 {string} string "获取失败"
  30. // @Router /list [Post]
  31. func BroadcastList(c *gin.Context) {
  32. var req request.BroadcastListReq
  33. if err := c.Bind(&req); err != nil {
  34. response.Fail("参数有误", c)
  35. return
  36. }
  37. if req.PageIndex == 0 {
  38. req.PageIndex = 1
  39. }
  40. if req.PageSize == 0 {
  41. req.PageSize = utils.PageSize20
  42. }
  43. userinfo := user.GetInfoByClaims(c)
  44. ok, checkInfo, _, err := company.CheckBaseFiccPermission(userinfo.CompanyID, int(userinfo.UserID))
  45. if err != nil {
  46. response.FailMsg("用户权限验证失败", "CheckBaseAuth-用户权限验证失败" + err.Error(), c)
  47. c.Abort()
  48. return
  49. }
  50. if !ok {
  51. response.AuthError(checkInfo, "暂无权限", c)
  52. c.Abort()
  53. return
  54. }
  55. list, err := services.GetVoiceBroadcastList(req.PageIndex, req.PageSize, req.SectionId, req.BroadcastId, userinfo)
  56. if err != nil {
  57. response.FailMsg("获取语音播报列表失败,"+err.Error(), "QuestionList ErrMsg:"+err.Error(), c)
  58. return
  59. }
  60. isVoiceAdmin, _, err := services.GetVoiceAdminByUserInfo(userinfo)
  61. if err != nil && err != utils.ErrNoRow {
  62. response.FailMsg("获取语音管理员信息失败", "QuestionList ErrMsg:"+err.Error(), c)
  63. return
  64. }
  65. var resp voiceResp.BroadcastListResp
  66. resp.List = list
  67. resp.IsVoiceAdmin = isVoiceAdmin
  68. response.OkData("获取成功", resp, c)
  69. }
  70. // AddBroadcast
  71. // @Description 新建语音播报
  72. // @Param file query string true "音频文件"
  73. // @Success 200 {string} string "发布成功"
  74. // @failure 400 {string} string "发布失败"
  75. // @Router /add [post]
  76. func AddBroadcast(c *gin.Context) {
  77. broadcastName := c.PostForm("broadcast_name")
  78. fmt.Println("broadcastName:",broadcastName)
  79. nsectionId := c.PostForm("section_id")
  80. sectionId, _ := strconv.Atoi(nsectionId)
  81. sectionName := c.PostForm("section_name")
  82. nvarietyId := c.PostForm("variety_id")
  83. varietyId, _ := strconv.Atoi(nvarietyId)
  84. varietyName := c.PostForm("variety_name")
  85. nauthorId := c.PostForm("author_id")
  86. authorId, _ := strconv.Atoi(nauthorId)
  87. author := c.PostForm("author")
  88. imgUrl := c.PostForm("img_url")
  89. file, err := c.FormFile("file")
  90. if err != nil {
  91. response.FailMsg("获取资源失败", "获取资源失败, Err:"+err.Error(), c)
  92. return
  93. }
  94. ext := path.Ext(file.Filename)
  95. if ext != ".mp3" {
  96. response.Fail("暂仅支持mp3格式", c)
  97. return
  98. }
  99. dateDir := time.Now().Format("20060102")
  100. localDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  101. if err := os.MkdirAll(localDir, 0766); err != nil {
  102. response.FailMsg("存储目录创建失败", "QuestionUploadAudio 存储目录创建失败, Err:"+err.Error(), c)
  103. return
  104. }
  105. randStr := utils.GetRandStringNoSpecialChar(28)
  106. filtName := randStr + ext
  107. fpath := localDir + "/" + filtName
  108. defer func() {
  109. _ = os.Remove(fpath)
  110. }()
  111. // 生成文件至指定目录
  112. if err := c.SaveUploadedFile(file, fpath); err != nil {
  113. response.FailMsg("文件生成失败", "QuestionUploadAudio 文件生成失败, Err:"+err.Error(), c)
  114. return
  115. }
  116. // 获取音频文件时长
  117. fByte, err := ioutil.ReadFile(fpath)
  118. if err != nil {
  119. response.FailMsg("读取本地文件失败", "QuestionUploadAudio 读取本地文件失败", c)
  120. return
  121. }
  122. if len(fByte) <= 0 {
  123. response.FailMsg("文件大小有误", "QuestionUploadAudio 文件大小有误", c)
  124. return
  125. }
  126. seconds, err := services.GetMP3PlayDuration(fByte)
  127. if err != nil {
  128. response.FailMsg("读取文件时长失败", "QuestionUploadAudio 读取文件时长失败", c)
  129. return
  130. }
  131. // 音频大小MB
  132. fi, err := os.Stat(fpath)
  133. if err != nil {
  134. response.FailMsg("读取文件大小失败", "QuestionUploadAudio 读取文件大小失败", c)
  135. return
  136. }
  137. mb := utils.Bit2MB(fi.Size(), 2)
  138. // 上传文件至阿里云
  139. ossDir := "yb_wx/voice_broadcast/"
  140. resourceUrl, err := services.UploadAliyunToDir(filtName, fpath, ossDir)
  141. if err != nil {
  142. response.FailMsg("文件上传失败", "QuestionUploadAudio 文件上传失败, Err:"+err.Error(), c)
  143. return
  144. }
  145. voiceBroadcast := voice_broadcast.VoiceBroadcast{
  146. BroadcastName: broadcastName,
  147. SectionId: sectionId,
  148. SectionName: sectionName,
  149. VarietyId: varietyId,
  150. VarietyName: varietyName,
  151. AuthorId: authorId,
  152. Author: author,
  153. ImgUrl: imgUrl,
  154. VoiceUrl: resourceUrl,
  155. VoicePlaySeconds: fmt.Sprint(seconds),
  156. VoiceSize: fmt.Sprint(mb),
  157. CreateTime: time.Now().Format(utils.FormatDateTime),
  158. }
  159. err = voiceBroadcast.AddVoiceBroadcast()
  160. if err != nil {
  161. fmt.Println("AddUserViewHistory err", err.Error())
  162. }
  163. // 推送回复消息给用户
  164. go wechat.SendVoiceBroadcastWxMsg(voiceBroadcast.BroadcastId, voiceBroadcast.SectionName, voiceBroadcast.BroadcastName)
  165. //同花顺客群
  166. go services.SendVoiceBroadcastToThs(voiceBroadcast)
  167. response.Ok("发布成功", c)
  168. }
  169. // SectionList
  170. // @Description 语音播报板块列表
  171. // @Success 200 {object} []voiceResp.VarietyList
  172. // @failure 400 {string} string "获取失败"
  173. // @Router /section/list [get]
  174. func SectionList(c *gin.Context) {
  175. sList, err := voice_section.GetVoiceSection()
  176. if err != nil {
  177. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  178. }
  179. vList, err := voice_section.GetVoiceVariety()
  180. if err != nil {
  181. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  182. }
  183. var sectionList []voiceResp.SectionList
  184. var varietyList []voiceResp.VarietyList
  185. var resp []voiceResp.VarietyList
  186. //var resp voiceResp.SectionListResp
  187. //for _, s := range sList {
  188. // section := voiceResp.SectionList{
  189. // SectionId: s.SectionId,
  190. // SectionName: s.SectionName,
  191. // Status: s.Status,
  192. // }
  193. // sectionList = append(sectionList, section)
  194. //}
  195. var newsList []*voice_section.VoiceSection
  196. //var bannedSectionList []*voice_section.VoiceSection
  197. //查找被禁用的板块ids
  198. var bannedIds []int
  199. for _, section := range sList {
  200. if section.Status == 0 {
  201. //bannedSectionList = append(bannedSectionList, section)
  202. bannedIds = append(bannedIds, section.SectionId)
  203. } else {
  204. newsList = append(newsList, section)
  205. }
  206. }
  207. //如果有被禁用的板块,去语音列表查找被禁用板块有没有语音
  208. var lists []*voice_broadcast.VoiceBroadcast
  209. if len(bannedIds) > 0{
  210. lists, err = voice_section.GetVoiceSectionFromBroadcast(bannedIds)
  211. if err != nil {
  212. response.FailMsg("查询语音播报禁用板块失败", "GetVoiceSectionFromBroadcast, Err:"+err.Error(), c)
  213. }
  214. }
  215. //被禁用板块有语音,依然显示该板块
  216. if len(lists) > 0 {
  217. //清空切片,用新的
  218. newsList = newsList[0:0]
  219. bannedMap := make(map[int]int)
  220. for _, broadcast := range lists {
  221. bannedMap[broadcast.SectionId] = broadcast.SectionId
  222. }
  223. for _, section := range sList {
  224. _,ok := bannedMap[section.SectionId]
  225. if section.Status != 0 || ok {
  226. newsList = append(newsList, section)
  227. }
  228. }
  229. }
  230. for _, v := range vList {
  231. variety := voiceResp.VarietyList{
  232. VarietyId: v.VarietyId,
  233. VarietyName: v.VarietyName,
  234. }
  235. varietyList = append(varietyList, variety)
  236. }
  237. for _, v := range varietyList {
  238. for _, s := range newsList {
  239. if v.VarietyId == s.VarietyId {
  240. section := voiceResp.SectionList{
  241. ImgUrl: s.ImgUrl,
  242. SectionId: s.SectionId,
  243. SectionName: s.SectionName,
  244. Status: s.Status,
  245. }
  246. sectionList = append(sectionList, section)
  247. }
  248. }
  249. if len(sectionList) == 0{
  250. continue
  251. }
  252. v.Children = sectionList
  253. resp = append(resp, v)
  254. sectionList = []voiceResp.SectionList{}
  255. }
  256. response.OkData("上传成功", resp, c)
  257. }
  258. // DelBroadcast
  259. // @Description 删除语音播报
  260. // @Param broadcast_id query int false "语音播报id"
  261. // @Success 200 {string} string "删除成功"
  262. // @failure 400 {string} string "删除失败"
  263. // @Router /delete [get]
  264. func DelBroadcast(c *gin.Context) {
  265. sbroadcastId := c.DefaultQuery("broadcast_id", "0")
  266. broadcastId, err := strconv.Atoi(sbroadcastId)
  267. if err != nil {
  268. response.FailMsg("转换id失败,请输入正确的id", "strconv.Atoi, Err:"+err.Error(), c)
  269. }
  270. if broadcastId <= 0 {
  271. response.FailMsg("参数错误","参数有误", c)
  272. return
  273. }
  274. var item voice_broadcast.VoiceBroadcast
  275. item.BroadcastId = broadcastId
  276. err = item.DelVoiceBroadcast()
  277. if err != nil {
  278. response.FailMsg("删除语音播报失败", "DelVoiceBroadcast, Err:"+err.Error(), c)
  279. }
  280. response.Ok("删除成功", c)
  281. }
  282. // AddStatistics
  283. // @Description 新增语音播报记录
  284. // @Param file query string true "音频文件"
  285. // @Success 200 {string} string "新增成功"
  286. // @failure 400 {string} string "新增失败"
  287. // @Router /statistics/add [post]
  288. func AddStatistics(c *gin.Context) {
  289. var req request.AddBroadcastStatisticsReq
  290. if err := c.Bind(&req); err != nil {
  291. response.Fail("参数有误", c)
  292. return
  293. }
  294. if req.BroadcastId <= 0{
  295. response.Fail("参数有误", c)
  296. }
  297. userinfo := user.GetInfoByClaims(c)
  298. go services.AddBroadcastRecord(userinfo, req.Source, req.BroadcastId)
  299. response.Ok("新增记录成功", c)
  300. }