voice_broadcast.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. package voice_broadcast
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "hongze/hongze_yb/controller/response"
  5. "hongze/hongze_yb/models/request"
  6. voiceResp "hongze/hongze_yb/models/response"
  7. "hongze/hongze_yb/models/tables/voice_broadcast"
  8. "hongze/hongze_yb/models/tables/voice_section"
  9. "hongze/hongze_yb/services"
  10. "hongze/hongze_yb/services/company"
  11. "hongze/hongze_yb/services/user"
  12. "hongze/hongze_yb/utils"
  13. "strconv"
  14. "strings"
  15. )
  16. // BroadcastList
  17. // @Description 语音播报列表
  18. // @Param page_index query int false "页码"
  19. // @Param page_size query int false "每页数量"
  20. // @Param broadcast_id query int false "语音播报id"
  21. // @Param section_id query int false "板块id"
  22. // @Success 200 {object} []voiceResp.BroadcastListResp
  23. // @failure 400 {string} string "获取失败"
  24. // @Router /list [Post]
  25. func BroadcastList(c *gin.Context) {
  26. var req request.BroadcastListReq
  27. if err := c.Bind(&req); err != nil {
  28. response.Fail("参数有误", c)
  29. return
  30. }
  31. if req.PageIndex == 0 {
  32. req.PageIndex = 1
  33. }
  34. if req.PageSize == 0 {
  35. req.PageSize = utils.PageSize20
  36. }
  37. userinfo := user.GetInfoByClaims(c)
  38. ok, checkInfo, _, err := company.CheckBaseFiccPermission(userinfo.CompanyID, int(userinfo.UserID))
  39. if err != nil {
  40. response.FailMsg("用户权限验证失败", "CheckBaseAuth-用户权限验证失败"+err.Error(), c)
  41. c.Abort()
  42. return
  43. }
  44. if !ok {
  45. response.AuthError(checkInfo, "暂无权限", c)
  46. c.Abort()
  47. return
  48. }
  49. list, err := services.GetVoiceBroadcastList(req.PageIndex, req.PageSize, req.SectionId, req.BroadcastId, userinfo)
  50. if err != nil {
  51. response.FailMsg("获取语音播报列表失败,"+err.Error(), "QuestionList ErrMsg:"+err.Error(), c)
  52. return
  53. }
  54. isVoiceAdmin, _, err := services.GetVoiceAdminByUserInfo(userinfo)
  55. if err != nil && err != utils.ErrNoRow {
  56. response.FailMsg("获取语音管理员信息失败", "QuestionList ErrMsg:"+err.Error(), c)
  57. return
  58. }
  59. var resp voiceResp.BroadcastListResp
  60. resp.List = list
  61. resp.IsVoiceAdmin = isVoiceAdmin
  62. response.OkData("获取成功", resp, c)
  63. }
  64. // AddBroadcast
  65. // @Description 新建语音播报
  66. // @Param file query string true "音频文件"
  67. // @Success 200 {string} string "发布成功"
  68. // @failure 400 {string} string "发布失败"
  69. // @Router /add [post]
  70. //func AddBroadcast(c *gin.Context) {
  71. // broadcastName := c.PostForm("broadcast_name")
  72. // nsectionId := c.PostForm("section_id")
  73. // sectionId, _ := strconv.Atoi(nsectionId)
  74. // sectionName := c.PostForm("section_name")
  75. // nvarietyId := c.PostForm("variety_id")
  76. // varietyId, _ := strconv.Atoi(nvarietyId)
  77. // varietyName := c.PostForm("variety_name")
  78. // nauthorId := c.PostForm("author_id")
  79. // authorId, _ := strconv.Atoi(nauthorId)
  80. // author := c.PostForm("author")
  81. // imgUrl := c.PostForm("img_url")
  82. // file, err := c.FormFile("file")
  83. // if err != nil {
  84. // response.FailMsg("获取资源失败", "获取资源失败, Err:"+err.Error(), c)
  85. // return
  86. // }
  87. // if imgUrl == "" {
  88. // response.Fail("图片不能为空", c)
  89. // return
  90. // }
  91. // // 生成动态分享图
  92. // createTimeStr := time.Now().Local().Format(utils.FormatDate)
  93. // pars := services.VoiceBroadcastShareImgPars{
  94. // BackgroundImg: imgUrl,
  95. // Title: sectionName,
  96. // CreateTime: createTimeStr,
  97. // }
  98. // parsByte, e := json.Marshal(pars)
  99. // if e != nil {
  100. // response.Fail("分享图参数有误", c)
  101. // return
  102. // }
  103. // shareImg, e := services.GetDynamicShareImg(services.VoiceBroadcastShareImgSource, string(parsByte))
  104. // if e != nil {
  105. // response.Fail("生成分享图失败", c)
  106. // return
  107. // }
  108. //
  109. // ext := path.Ext(file.Filename)
  110. // if ext != ".mp3" {
  111. // response.Fail("暂仅支持mp3格式", c)
  112. // return
  113. // }
  114. // dateDir := time.Now().Format("20060102")
  115. // localDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  116. // if err := os.MkdirAll(localDir, 0766); err != nil {
  117. // response.FailMsg("存储目录创建失败", "QuestionUploadAudio 存储目录创建失败, Err:"+err.Error(), c)
  118. // return
  119. // }
  120. // randStr := utils.GetRandStringNoSpecialChar(28)
  121. // filtName := randStr + ext
  122. // fpath := localDir + "/" + filtName
  123. // defer func() {
  124. // _ = os.Remove(fpath)
  125. // }()
  126. // // 生成文件至指定目录
  127. // if err := c.SaveUploadedFile(file, fpath); err != nil {
  128. // response.FailMsg("文件生成失败", "QuestionUploadAudio 文件生成失败, Err:"+err.Error(), c)
  129. // return
  130. // }
  131. // // 获取音频文件时长
  132. // fByte, err := ioutil.ReadFile(fpath)
  133. // if err != nil {
  134. // response.FailMsg("读取本地文件失败", "QuestionUploadAudio 读取本地文件失败", c)
  135. // return
  136. // }
  137. // if len(fByte) <= 0 {
  138. // response.FailMsg("文件大小有误", "QuestionUploadAudio 文件大小有误", c)
  139. // return
  140. // }
  141. // seconds, err := services.GetMP3PlayDuration(fByte)
  142. // if err != nil {
  143. // response.FailMsg("读取文件时长失败", "QuestionUploadAudio 读取文件时长失败", c)
  144. // return
  145. // }
  146. // // 音频大小MB
  147. // fi, err := os.Stat(fpath)
  148. // if err != nil {
  149. // response.FailMsg("读取文件大小失败", "QuestionUploadAudio 读取文件大小失败", c)
  150. // return
  151. // }
  152. // mb := utils.Bit2MB(fi.Size(), 2)
  153. // // 上传文件至阿里云
  154. // ossDir := "yb_wx/voice_broadcast/"
  155. // resourceUrl, err := services.UploadAliyunToDir(filtName, fpath, ossDir)
  156. // if err != nil {
  157. // response.FailMsg("文件上传失败", "QuestionUploadAudio 文件上传失败, Err:"+err.Error(), c)
  158. // return
  159. // }
  160. //
  161. // voiceBroadcast := voice_broadcast.VoiceBroadcast{
  162. // BroadcastName: broadcastName,
  163. // SectionId: sectionId,
  164. // SectionName: sectionName,
  165. // VarietyId: varietyId,
  166. // VarietyName: varietyName,
  167. // AuthorId: authorId,
  168. // Author: author,
  169. // ImgUrl: shareImg,
  170. // VoiceUrl: resourceUrl,
  171. // VoicePlaySeconds: fmt.Sprint(seconds),
  172. // VoiceSize: fmt.Sprint(mb),
  173. // CreateTime: time.Now().Format(utils.FormatDateTime),
  174. // }
  175. // err = voiceBroadcast.AddVoiceBroadcast()
  176. // if err != nil {
  177. // fmt.Println("AddUserViewHistory err", err.Error())
  178. // }
  179. // response.OkData("发布成功", voiceBroadcast.BroadcastId, c)
  180. //}
  181. // PublishBroadcast
  182. // @Description 发布语音播报
  183. // @Param broadcast_id query int false "语音播报ID"
  184. // @Param broadcast_name query string true "语音标题"
  185. // @Param section_id query int true "板块ID"
  186. // @Param section_name query string true "板块名称"
  187. // @Param variety_id query int true "品种ID"
  188. // @Param variety_name query string true "品种名称"
  189. // @Param author_id query int true "作者ID"
  190. // @Param author query string true "作者名称"
  191. // @Param img_url query string true "分享图背景"
  192. // @Param Imgs query string true "图片,英文逗号拼接"
  193. // @Param publish_type query string true "发布类型: 0-仅发布 1-发布并推送 2-定时发布"
  194. // @Param pre_publish_time query string false "预发布时间"
  195. // @Param file query string true "音频文件"
  196. // @Success 200 {string} string "发布成功"
  197. // @failure 400 {string} string "发布失败"
  198. // @Router /add [post]
  199. func PublishBroadcast(c *gin.Context) {
  200. var req request.SaveBroadcastReq
  201. if err := c.Bind(&req); err != nil {
  202. response.Fail("参数有误", c)
  203. return
  204. }
  205. // 参数校验
  206. if req.BroadcastName == "" {
  207. response.Fail("请输入标题", c)
  208. return
  209. }
  210. if req.SectionId <= 0 || req.SectionName == "" {
  211. response.Fail("请选择品种", c)
  212. return
  213. }
  214. if req.SectionId <= 0 || req.SectionName == "" {
  215. response.Fail("请选择板块", c)
  216. return
  217. }
  218. if req.File == nil {
  219. response.Fail("请上传音频", c)
  220. return
  221. }
  222. if req.ImgUrl == "" {
  223. response.Fail("分享图背景不能为空", c)
  224. return
  225. }
  226. if req.PublishType == 2 && req.PrePublishTime == "" {
  227. response.Fail("定时发布请选择发布时间", c)
  228. return
  229. }
  230. if req.Imgs != "" {
  231. imgList := strings.Split(req.Imgs, ",")
  232. if len(imgList) > 5 {
  233. response.Fail("最多插入五张图片", c)
  234. return
  235. }
  236. }
  237. // 发布语音
  238. if msg, e := services.PublishVoiceBroadcast(req.BroadcastId, req.SectionId, req.VarietyId, req.AuthorId, req.PublishType, req.BroadcastName,
  239. req.SectionName, req.VarietyName, req.Author, req.ImgUrl, req.PrePublishTime, req.Imgs, req.File); e != nil {
  240. response.FailMsg(msg, e.Error(), c)
  241. return
  242. }
  243. response.Ok("发布成功", c)
  244. }
  245. // SectionList
  246. // @Description 语音播报板块列表
  247. // @Success 200 {object} []voiceResp.VarietyList
  248. // @failure 400 {string} string "获取失败"
  249. // @Router /section/list [get]
  250. func SectionList(c *gin.Context) {
  251. sList, err := voice_section.GetVoiceSection()
  252. if err != nil {
  253. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  254. }
  255. vList, err := voice_section.GetVoiceVariety()
  256. if err != nil {
  257. response.FailMsg("查询语音播报板块失败", "GetVoiceSection, Err:"+err.Error(), c)
  258. }
  259. var sectionList []voiceResp.SectionList
  260. var varietyList []voiceResp.VarietyList
  261. var resp []voiceResp.VarietyList
  262. //var resp voiceResp.SectionListResp
  263. //for _, s := range sList {
  264. // section := voiceResp.SectionList{
  265. // SectionId: s.SectionId,
  266. // SectionName: s.SectionName,
  267. // Status: s.Status,
  268. // }
  269. // sectionList = append(sectionList, section)
  270. //}
  271. var newsList []*voice_section.VoiceSection
  272. //var bannedSectionList []*voice_section.VoiceSection
  273. //查找被禁用的板块ids
  274. var bannedIds []int
  275. for _, section := range sList {
  276. if section.Status == 0 {
  277. //bannedSectionList = append(bannedSectionList, section)
  278. bannedIds = append(bannedIds, section.SectionId)
  279. } else {
  280. newsList = append(newsList, section)
  281. }
  282. }
  283. //如果有被禁用的板块,去语音列表查找被禁用板块有没有语音
  284. var lists []*voice_broadcast.VoiceBroadcast
  285. if len(bannedIds) > 0 {
  286. lists, err = voice_section.GetVoiceSectionFromBroadcast(bannedIds)
  287. if err != nil {
  288. response.FailMsg("查询语音播报禁用板块失败", "GetVoiceSectionFromBroadcast, Err:"+err.Error(), c)
  289. }
  290. }
  291. //被禁用板块有语音,依然显示该板块
  292. if len(lists) > 0 {
  293. //清空切片,用新的
  294. newsList = newsList[0:0]
  295. bannedMap := make(map[int]int)
  296. for _, broadcast := range lists {
  297. bannedMap[broadcast.SectionId] = broadcast.SectionId
  298. }
  299. for _, section := range sList {
  300. _, ok := bannedMap[section.SectionId]
  301. if section.Status != 0 || ok {
  302. newsList = append(newsList, section)
  303. }
  304. }
  305. }
  306. for _, v := range vList {
  307. variety := voiceResp.VarietyList{
  308. VarietyId: v.VarietyId,
  309. VarietyName: v.VarietyName,
  310. }
  311. varietyList = append(varietyList, variety)
  312. }
  313. for _, v := range varietyList {
  314. for _, s := range newsList {
  315. if v.VarietyId == s.VarietyId {
  316. section := voiceResp.SectionList{
  317. ImgUrl: s.ImgUrl,
  318. SectionId: s.SectionId,
  319. SectionName: s.SectionName,
  320. Status: s.Status,
  321. }
  322. sectionList = append(sectionList, section)
  323. }
  324. }
  325. if len(sectionList) == 0 {
  326. continue
  327. }
  328. v.Children = sectionList
  329. resp = append(resp, v)
  330. sectionList = []voiceResp.SectionList{}
  331. }
  332. response.OkData("上传成功", resp, c)
  333. }
  334. // DelBroadcast
  335. // @Description 删除语音播报
  336. // @Param broadcast_id query int false "语音播报id"
  337. // @Success 200 {string} string "删除成功"
  338. // @failure 400 {string} string "删除失败"
  339. // @Router /delete [get]
  340. func DelBroadcast(c *gin.Context) {
  341. sbroadcastId := c.DefaultQuery("broadcast_id", "0")
  342. broadcastId, err := strconv.Atoi(sbroadcastId)
  343. if err != nil {
  344. response.FailMsg("转换id失败,请输入正确的id", "strconv.Atoi, Err:"+err.Error(), c)
  345. }
  346. if broadcastId <= 0 {
  347. response.FailMsg("参数错误", "参数有误", c)
  348. return
  349. }
  350. var item voice_broadcast.VoiceBroadcast
  351. item.BroadcastId = broadcastId
  352. err = item.DelVoiceBroadcast()
  353. if err != nil {
  354. response.FailMsg("删除语音播报失败", "DelVoiceBroadcast, Err:"+err.Error(), c)
  355. }
  356. response.Ok("删除成功", c)
  357. }
  358. // AddStatistics
  359. // @Description 新增语音播报记录
  360. // @Param file query string true "音频文件"
  361. // @Success 200 {string} string "新增成功"
  362. // @failure 400 {string} string "新增失败"
  363. // @Router /statistics/add [post]
  364. func AddStatistics(c *gin.Context) {
  365. var req request.AddBroadcastStatisticsReq
  366. if err := c.Bind(&req); err != nil {
  367. response.Fail("参数有误", c)
  368. return
  369. }
  370. if req.BroadcastId <= 0 {
  371. response.Fail("参数有误", c)
  372. }
  373. userinfo := user.GetInfoByClaims(c)
  374. go services.AddBroadcastRecord(userinfo, req.Source, req.BroadcastId)
  375. response.Ok("新增记录成功", c)
  376. }
  377. // BroadcastDetail 获取语音播报详情
  378. // @Tags 语音播报模块
  379. // @Description 获取语音播报详情
  380. // @Param variety_tag_id query int true "标签ID"
  381. // @Success 200 {object} response.PriceDrivenItem
  382. // @failure 400 {string} string "获取失败"
  383. // @Router /detail [get]
  384. func BroadcastDetail(c *gin.Context) {
  385. var req request.BroadcastDetailReq
  386. if err := c.Bind(&req); err != nil {
  387. response.Fail("参数有误", c)
  388. return
  389. }
  390. if req.BroadcastId <= 0 {
  391. response.Fail("参数有误", c)
  392. return
  393. }
  394. userInfo := user.GetInfoByClaims(c)
  395. item, e := voice_broadcast.GetBroadcastById(req.BroadcastId)
  396. if e != nil {
  397. response.FailMsg("获取失败", "BroadcastDetail ErrMsg:"+e.Error(), c)
  398. return
  399. }
  400. resp := &voiceResp.Broadcast{
  401. BroadcastId: item.BroadcastId,
  402. BroadcastName: item.BroadcastName,
  403. SectionId: item.SectionId,
  404. SectionName: item.SectionName,
  405. VarietyId: item.VarietyId,
  406. VarietyName: item.VarietyName,
  407. AuthorId: item.AuthorId,
  408. Author: item.Author,
  409. ImgUrl: item.ImgUrl,
  410. VoiceUrl: item.VoiceUrl,
  411. VoicePlaySeconds: item.VoicePlaySeconds,
  412. VoiceSize: item.VoiceSize,
  413. CreateTime: item.CreateTime,
  414. }
  415. // 是否为作者、是否可以推送消息
  416. if int(userInfo.UserID) == item.AuthorId {
  417. resp.IsAuthor = true
  418. if item.MsgState == 0 {
  419. resp.CouldSendMsg = true
  420. }
  421. }
  422. response.OkData("获取成功", resp, c)
  423. }
  424. // MsgSend 语音播报消息推送
  425. // @Tags 语音播报模块
  426. // @Description 语音播报消息推送
  427. // @Param broadcast_id query int true "语音播报ID"
  428. // @Success 200 {string} string "操作成功"
  429. // @failure 400 {string} string "操作失败"
  430. // @Router /msg_send [post]
  431. func MsgSend(c *gin.Context) {
  432. var req request.BroadcastMsgSendReq
  433. if err := c.Bind(&req); err != nil {
  434. response.Fail("参数有误", c)
  435. return
  436. }
  437. if req.BroadcastId <= 0 {
  438. response.Fail("参数有误", c)
  439. return
  440. }
  441. userInfo := user.GetInfoByClaims(c)
  442. errMsg, err := services.SendBroadcastMsg(req.BroadcastId, int(userInfo.UserID))
  443. if err != nil {
  444. response.FailMsg(errMsg, "MsgSend ErrMsg:"+err.Error(), c)
  445. return
  446. }
  447. response.Ok("操作成功", c)
  448. }