voice_broadcast.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. package voice_broadcast
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "hongze/hongze_yb/controller/response"
  7. "hongze/hongze_yb/global"
  8. "hongze/hongze_yb/models/request"
  9. voiceResp "hongze/hongze_yb/models/response"
  10. "hongze/hongze_yb/models/tables/voice_broadcast"
  11. "hongze/hongze_yb/models/tables/voice_section"
  12. "hongze/hongze_yb/services"
  13. "hongze/hongze_yb/services/company"
  14. "hongze/hongze_yb/services/user"
  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. nsectionId := c.PostForm("section_id")
  79. sectionId, _ := strconv.Atoi(nsectionId)
  80. sectionName := c.PostForm("section_name")
  81. nvarietyId := c.PostForm("variety_id")
  82. varietyId, _ := strconv.Atoi(nvarietyId)
  83. varietyName := c.PostForm("variety_name")
  84. nauthorId := c.PostForm("author_id")
  85. authorId, _ := strconv.Atoi(nauthorId)
  86. author := c.PostForm("author")
  87. imgUrl := c.PostForm("img_url")
  88. file, err := c.FormFile("file")
  89. if err != nil {
  90. response.FailMsg("获取资源失败", "获取资源失败, Err:"+err.Error(), c)
  91. return
  92. }
  93. if imgUrl == "" {
  94. response.Fail("图片不能为空", c)
  95. return
  96. }
  97. // 生成动态分享图
  98. createTimeStr := time.Now().Local().Format(utils.FormatDate)
  99. pars := services.VoiceBroadcastShareImgPars{
  100. BackgroundImg: imgUrl,
  101. Title: sectionName,
  102. CreateTime: createTimeStr,
  103. }
  104. parsByte, e := json.Marshal(pars)
  105. if e != nil {
  106. response.Fail("分享图参数有误", c)
  107. return
  108. }
  109. shareImg, e := services.GetDynamicShareImg(services.VoiceBroadcastShareImgSource, string(parsByte))
  110. if e != nil {
  111. response.Fail("生成分享图失败", c)
  112. return
  113. }
  114. ext := path.Ext(file.Filename)
  115. if ext != ".mp3" {
  116. response.Fail("暂仅支持mp3格式", c)
  117. return
  118. }
  119. dateDir := time.Now().Format("20060102")
  120. localDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  121. if err := os.MkdirAll(localDir, 0766); err != nil {
  122. response.FailMsg("存储目录创建失败", "QuestionUploadAudio 存储目录创建失败, Err:"+err.Error(), c)
  123. return
  124. }
  125. randStr := utils.GetRandStringNoSpecialChar(28)
  126. filtName := randStr + ext
  127. fpath := localDir + "/" + filtName
  128. defer func() {
  129. _ = os.Remove(fpath)
  130. }()
  131. // 生成文件至指定目录
  132. if err := c.SaveUploadedFile(file, fpath); err != nil {
  133. response.FailMsg("文件生成失败", "QuestionUploadAudio 文件生成失败, Err:"+err.Error(), c)
  134. return
  135. }
  136. // 获取音频文件时长
  137. fByte, err := ioutil.ReadFile(fpath)
  138. if err != nil {
  139. response.FailMsg("读取本地文件失败", "QuestionUploadAudio 读取本地文件失败", c)
  140. return
  141. }
  142. if len(fByte) <= 0 {
  143. response.FailMsg("文件大小有误", "QuestionUploadAudio 文件大小有误", c)
  144. return
  145. }
  146. seconds, err := services.GetMP3PlayDuration(fByte)
  147. if err != nil {
  148. response.FailMsg("读取文件时长失败", "QuestionUploadAudio 读取文件时长失败", c)
  149. return
  150. }
  151. // 音频大小MB
  152. fi, err := os.Stat(fpath)
  153. if err != nil {
  154. response.FailMsg("读取文件大小失败", "QuestionUploadAudio 读取文件大小失败", c)
  155. return
  156. }
  157. mb := utils.Bit2MB(fi.Size(), 2)
  158. // 上传文件至阿里云
  159. ossDir := "yb_wx/voice_broadcast/"
  160. resourceUrl, err := services.UploadAliyunToDir(filtName, fpath, ossDir)
  161. if err != nil {
  162. response.FailMsg("文件上传失败", "QuestionUploadAudio 文件上传失败, Err:"+err.Error(), c)
  163. return
  164. }
  165. voiceBroadcast := voice_broadcast.VoiceBroadcast{
  166. BroadcastName: broadcastName,
  167. SectionId: sectionId,
  168. SectionName: sectionName,
  169. VarietyId: varietyId,
  170. VarietyName: varietyName,
  171. AuthorId: authorId,
  172. Author: author,
  173. ImgUrl: shareImg,
  174. VoiceUrl: resourceUrl,
  175. VoicePlaySeconds: fmt.Sprint(seconds),
  176. VoiceSize: fmt.Sprint(mb),
  177. CreateTime: time.Now().Format(utils.FormatDateTime),
  178. }
  179. err = voiceBroadcast.AddVoiceBroadcast()
  180. if err != nil {
  181. fmt.Println("AddUserViewHistory err", err.Error())
  182. }
  183. response.OkData("发布成功", voiceBroadcast.BroadcastId, c)
  184. }
  185. // SectionList
  186. // @Description 语音播报板块列表
  187. // @Success 200 {object} []voiceResp.VarietyList
  188. // @failure 400 {string} string "获取失败"
  189. // @Router /section/list [get]
  190. func SectionList(c *gin.Context) {
  191. sList, err := voice_section.GetVoiceSection()
  192. if err != nil {
  193. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  194. }
  195. vList, err := voice_section.GetVoiceVariety()
  196. if err != nil {
  197. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  198. }
  199. var sectionList []voiceResp.SectionList
  200. var varietyList []voiceResp.VarietyList
  201. var resp []voiceResp.VarietyList
  202. //var resp voiceResp.SectionListResp
  203. //for _, s := range sList {
  204. // section := voiceResp.SectionList{
  205. // SectionId: s.SectionId,
  206. // SectionName: s.SectionName,
  207. // Status: s.Status,
  208. // }
  209. // sectionList = append(sectionList, section)
  210. //}
  211. var newsList []*voice_section.VoiceSection
  212. //var bannedSectionList []*voice_section.VoiceSection
  213. //查找被禁用的板块ids
  214. var bannedIds []int
  215. for _, section := range sList {
  216. if section.Status == 0 {
  217. //bannedSectionList = append(bannedSectionList, section)
  218. bannedIds = append(bannedIds, section.SectionId)
  219. } else {
  220. newsList = append(newsList, section)
  221. }
  222. }
  223. //如果有被禁用的板块,去语音列表查找被禁用板块有没有语音
  224. var lists []*voice_broadcast.VoiceBroadcast
  225. if len(bannedIds) > 0 {
  226. lists, err = voice_section.GetVoiceSectionFromBroadcast(bannedIds)
  227. if err != nil {
  228. response.FailMsg("查询语音播报禁用板块失败", "GetVoiceSectionFromBroadcast, Err:"+err.Error(), c)
  229. }
  230. }
  231. //被禁用板块有语音,依然显示该板块
  232. if len(lists) > 0 {
  233. //清空切片,用新的
  234. newsList = newsList[0:0]
  235. bannedMap := make(map[int]int)
  236. for _, broadcast := range lists {
  237. bannedMap[broadcast.SectionId] = broadcast.SectionId
  238. }
  239. for _, section := range sList {
  240. _, ok := bannedMap[section.SectionId]
  241. if section.Status != 0 || ok {
  242. newsList = append(newsList, section)
  243. }
  244. }
  245. }
  246. for _, v := range vList {
  247. variety := voiceResp.VarietyList{
  248. VarietyId: v.VarietyId,
  249. VarietyName: v.VarietyName,
  250. }
  251. varietyList = append(varietyList, variety)
  252. }
  253. for _, v := range varietyList {
  254. for _, s := range newsList {
  255. if v.VarietyId == s.VarietyId {
  256. section := voiceResp.SectionList{
  257. ImgUrl: s.ImgUrl,
  258. SectionId: s.SectionId,
  259. SectionName: s.SectionName,
  260. Status: s.Status,
  261. }
  262. sectionList = append(sectionList, section)
  263. }
  264. }
  265. if len(sectionList) == 0 {
  266. continue
  267. }
  268. v.Children = sectionList
  269. resp = append(resp, v)
  270. sectionList = []voiceResp.SectionList{}
  271. }
  272. response.OkData("上传成功", resp, c)
  273. }
  274. // DelBroadcast
  275. // @Description 删除语音播报
  276. // @Param broadcast_id query int false "语音播报id"
  277. // @Success 200 {string} string "删除成功"
  278. // @failure 400 {string} string "删除失败"
  279. // @Router /delete [get]
  280. func DelBroadcast(c *gin.Context) {
  281. sbroadcastId := c.DefaultQuery("broadcast_id", "0")
  282. broadcastId, err := strconv.Atoi(sbroadcastId)
  283. if err != nil {
  284. response.FailMsg("转换id失败,请输入正确的id", "strconv.Atoi, Err:"+err.Error(), c)
  285. }
  286. if broadcastId <= 0 {
  287. response.FailMsg("参数错误", "参数有误", c)
  288. return
  289. }
  290. var item voice_broadcast.VoiceBroadcast
  291. item.BroadcastId = broadcastId
  292. err = item.DelVoiceBroadcast()
  293. if err != nil {
  294. response.FailMsg("删除语音播报失败", "DelVoiceBroadcast, Err:"+err.Error(), c)
  295. }
  296. response.Ok("删除成功", c)
  297. }
  298. // AddStatistics
  299. // @Description 新增语音播报记录
  300. // @Param file query string true "音频文件"
  301. // @Success 200 {string} string "新增成功"
  302. // @failure 400 {string} string "新增失败"
  303. // @Router /statistics/add [post]
  304. func AddStatistics(c *gin.Context) {
  305. var req request.AddBroadcastStatisticsReq
  306. if err := c.Bind(&req); err != nil {
  307. response.Fail("参数有误", c)
  308. return
  309. }
  310. if req.BroadcastId <= 0 {
  311. response.Fail("参数有误", c)
  312. }
  313. userinfo := user.GetInfoByClaims(c)
  314. go services.AddBroadcastRecord(userinfo, req.Source, req.BroadcastId)
  315. response.Ok("新增记录成功", c)
  316. }
  317. // BroadcastDetail 获取语音播报详情
  318. // @Tags 语音播报模块
  319. // @Description 获取语音播报详情
  320. // @Param variety_tag_id query int true "标签ID"
  321. // @Success 200 {object} response.PriceDrivenItem
  322. // @failure 400 {string} string "获取失败"
  323. // @Router /detail [get]
  324. func BroadcastDetail(c *gin.Context) {
  325. var req request.BroadcastDetailReq
  326. if err := c.Bind(&req); err != nil {
  327. response.Fail("参数有误", c)
  328. return
  329. }
  330. if req.BroadcastId <= 0 {
  331. response.Fail("参数有误", c)
  332. return
  333. }
  334. userInfo := user.GetInfoByClaims(c)
  335. item, e := voice_broadcast.GetBroadcastById(req.BroadcastId)
  336. if e != nil {
  337. response.FailMsg("获取失败", "BroadcastDetail ErrMsg:"+e.Error(), c)
  338. return
  339. }
  340. resp := &voiceResp.Broadcast{
  341. BroadcastId: item.BroadcastId,
  342. BroadcastName: item.BroadcastName,
  343. SectionId: item.SectionId,
  344. SectionName: item.SectionName,
  345. VarietyId: item.VarietyId,
  346. VarietyName: item.VarietyName,
  347. AuthorId: item.AuthorId,
  348. Author: item.Author,
  349. ImgUrl: item.ImgUrl,
  350. VoiceUrl: item.VoiceUrl,
  351. VoicePlaySeconds: item.VoicePlaySeconds,
  352. VoiceSize: item.VoiceSize,
  353. CreateTime: item.CreateTime,
  354. }
  355. // 是否为作者、是否可以推送消息
  356. if int(userInfo.UserID) == item.AuthorId {
  357. resp.IsAuthor = true
  358. if item.MsgState == 0 {
  359. resp.CouldSendMsg = true
  360. }
  361. }
  362. response.OkData("获取成功", resp, c)
  363. }
  364. // MsgSend 语音播报消息推送
  365. // @Tags 语音播报模块
  366. // @Description 语音播报消息推送
  367. // @Param broadcast_id query int true "语音播报ID"
  368. // @Success 200 {string} string "操作成功"
  369. // @failure 400 {string} string "操作失败"
  370. // @Router /msg_send [post]
  371. func MsgSend(c *gin.Context) {
  372. var req request.BroadcastMsgSendReq
  373. if err := c.Bind(&req); err != nil {
  374. response.Fail("参数有误", c)
  375. return
  376. }
  377. if req.BroadcastId <= 0 {
  378. response.Fail("参数有误", c)
  379. return
  380. }
  381. userInfo := user.GetInfoByClaims(c)
  382. errMsg, err := services.SendBroadcastMsg(req.BroadcastId, int(userInfo.UserID))
  383. if err != nil {
  384. response.FailMsg(errMsg, "MsgSend ErrMsg:"+err.Error(), c)
  385. return
  386. }
  387. response.Ok("操作成功", c)
  388. }