question.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package community
  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. respond "hongze/hongze_yb/models/response"
  9. "hongze/hongze_yb/services"
  10. "hongze/hongze_yb/services/community"
  11. "hongze/hongze_yb/services/user"
  12. "hongze/hongze_yb/services/wx_app"
  13. "hongze/hongze_yb/utils"
  14. "io/ioutil"
  15. "os"
  16. "path"
  17. "strconv"
  18. "time"
  19. )
  20. // QuestionList 问答列表
  21. // @Tags 问答社区模块
  22. // @Description 获取问答列表
  23. // @Param page_index query int false "页码"
  24. // @Param page_size query int false "每页数量"
  25. // @Param only_mine query int false "只看我的"
  26. // @Param variety_tag_id query int false "标签ID"
  27. // @Param reply_status query int false "回复状态 0-全部 2-待回答 3-已回答"
  28. // @Success 200 {object} []respond.CommunityQuestionItem
  29. // @failure 400 {string} string "获取失败"
  30. // @Router /question/list [get]
  31. func QuestionList(c *gin.Context) {
  32. var req request.QuestionListReq
  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. list, err := community.GetQuestionList(req.PageIndex, req.PageSize, req.OnlyMine, req.VarietyTagId, req.ReplyStatus, req.GroupId, userinfo)
  45. if err != nil {
  46. response.FailMsg("获取失败", "QuestionList ErrMsg:"+err.Error(), c)
  47. return
  48. }
  49. response.OkData("获取成功", list, c)
  50. }
  51. // QuestionDetail 问答详情
  52. // @Tags 问答社区模块
  53. // @Description 获取问答详情
  54. // @Param question_id query int true "问答ID"
  55. // @Success 200 {object} respond.CommunityQuestionItem
  56. // @failure 400 {string} string "获取失败"
  57. // @Router /question/detail [get]
  58. func QuestionDetail(c *gin.Context) {
  59. var req request.QuestionDetailReq
  60. if err := c.Bind(&req); err != nil {
  61. response.Fail("参数有误", c)
  62. return
  63. }
  64. if req.QuestionId == 0 {
  65. response.Fail("参数有误", c)
  66. return
  67. }
  68. userinfo := user.GetInfoByClaims(c)
  69. item, errMsg, err := community.GetQuestionDetail(req.QuestionId, userinfo)
  70. if err != nil {
  71. response.FailMsg(errMsg, "QuestionDetail ErrMsg:"+err.Error(), c)
  72. return
  73. }
  74. response.OkData("获取成功", item, c)
  75. }
  76. // QuestionAsk 发布提问
  77. // @Tags 问答社区模块
  78. // @Description 发布提问
  79. // @Param question_content query string true "问题内容"
  80. // @Success 200 {string} string "操作成功"
  81. // @failure 400 {string} string "操作失败"
  82. // @Router /question/ask [post]
  83. func QuestionAsk(c *gin.Context) {
  84. var req request.QuestionAskReq
  85. if err := c.ShouldBind(&req); err != nil {
  86. response.Fail("参数有误", c)
  87. return
  88. }
  89. if req.QuestionContent == "" {
  90. response.Fail("内容不可为空", c)
  91. return
  92. }
  93. contentRune := []rune(req.QuestionContent)
  94. if len(contentRune) > 50 {
  95. response.Fail("内容不可超过50个字符", c)
  96. return
  97. }
  98. // 敏感词校验, 只有小程序的用户才能走敏感词过滤接口
  99. userinfo := user.GetInfoByClaims(c)
  100. if userinfo.RecordInfo.OpenID != "" && userinfo.RecordInfo.CreatePlatform == 6 {
  101. checkResult, e := wx_app.MsgSecCheck(userinfo.RecordInfo.OpenID, req.QuestionContent)
  102. if e == nil {
  103. if checkResult.Result != nil && checkResult.Result.Suggest != "pass" {
  104. errMsg := "含有违禁词, 不允许发布: " + checkResult.Result.Suggest + ", 命中标签: " + strconv.Itoa(checkResult.Result.Label)
  105. response.FailMsg("含有违禁词, 不允许发布", errMsg, c)
  106. return
  107. }
  108. }
  109. }
  110. if err := community.CreateQuestion(int(userinfo.UserID), userinfo.Mobile, userinfo.RealName, req.QuestionContent); err != nil {
  111. response.FailMsg("提交失败", "QuestionAsk ErrMsg:"+err.Error(), c)
  112. return
  113. }
  114. response.Ok("操作成功", c)
  115. }
  116. // QuestionReply 发布回复
  117. // @Tags 问答社区模块
  118. // @Description 发布回复
  119. // @Param question_id query int true "问答ID"
  120. // @Param audio_list query object true "音频列表"
  121. // @Success 200 {string} string "操作成功"
  122. // @failure 400 {string} string "操作失败"
  123. // @Router /question/reply [post]
  124. func QuestionReply(c *gin.Context) {
  125. var req request.QuestionReplyReq
  126. if err := c.ShouldBind(&req); err != nil {
  127. response.Fail("参数有误", c)
  128. return
  129. }
  130. if req.QuestionId == 0 {
  131. response.Fail("参数有误", c)
  132. return
  133. }
  134. if len(req.AudioList) == 0 {
  135. response.Fail("音频不可为空", c)
  136. return
  137. }
  138. userinfo := user.GetInfoByClaims(c)
  139. msg, err := community.ReplyUserQuestion(int(userinfo.UserID), req.QuestionId, req.AudioList)
  140. if err != nil {
  141. response.FailMsg(msg, "QuestionReply ErrMsg:"+err.Error(), c)
  142. return
  143. }
  144. response.Ok("操作成功", c)
  145. }
  146. // QuestionReplyRead 已读回复
  147. // @Tags 问答社区模块
  148. // @Description 已读回复
  149. // @Param question_ids query string true "问答IDs"
  150. // @Success 200 {string} string "操作成功"
  151. // @failure 400 {string} string "操作失败"
  152. // @Router /question/reply/read [post]
  153. func QuestionReplyRead(c *gin.Context) {
  154. var req request.QuestionReadReq
  155. if err := c.ShouldBind(&req); err != nil {
  156. response.Fail("参数有误", c)
  157. return
  158. }
  159. if req.QuestionIds == "" {
  160. response.Fail("参数有误", c)
  161. return
  162. }
  163. userinfo := user.GetInfoByClaims(c)
  164. if err := community.ReadQuestionReply(req.QuestionIds, userinfo); err != nil {
  165. response.FailMsg("操作失败", "QuestionReplyRead ErrMsg:"+err.Error(), c)
  166. return
  167. }
  168. response.Ok("操作成功", c)
  169. }
  170. // QuestionListTotal 问答列表数量统计
  171. // @Tags 问答社区模块
  172. // @Description 问答列表数量统计
  173. // @Success 200 {object} respond.CommunityQuestionListTotal
  174. // @failure 400 {string} string "获取失败"
  175. // @Router /question/list/total [get]
  176. func QuestionListTotal(c *gin.Context) {
  177. userinfo := user.GetInfoByClaims(c)
  178. resp, err := community.GetQuestionListTotal(userinfo)
  179. if err != nil {
  180. response.FailMsg("获取失败", "QuestionReplyTotal ErrMsg:"+err.Error(), c)
  181. return
  182. }
  183. response.OkData("获取成功", resp, c)
  184. }
  185. // QuestionUploadAudio 上传回复音频
  186. // @Tags 问答社区模块
  187. // @Description 上传回复音频
  188. // @Param file query string true "音频文件"
  189. // @Success 200 {string} string "上传成功"
  190. // @failure 400 {string} string "上传失败"
  191. // @Router /question/reply/upload_audio [post]
  192. func QuestionUploadAudio(c *gin.Context) {
  193. file, err := c.FormFile("file")
  194. if err != nil {
  195. response.FailMsg("获取资源失败", "获取资源失败, Err:"+err.Error(), c)
  196. return
  197. }
  198. ext := path.Ext(file.Filename)
  199. if ext != ".mp3" {
  200. response.Fail("暂仅支持mp3格式", c)
  201. return
  202. }
  203. dateDir := time.Now().Format("20060102")
  204. localDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  205. if err := os.MkdirAll(localDir, 0766); err != nil {
  206. response.FailMsg("存储目录创建失败", "QuestionUploadAudio 存储目录创建失败, Err:"+err.Error(), c)
  207. return
  208. }
  209. randStr := utils.GetRandStringNoSpecialChar(28)
  210. filtName := randStr + ext
  211. fpath := localDir + "/" + filtName
  212. defer func() {
  213. _ = os.Remove(fpath)
  214. }()
  215. // 生成文件至指定目录
  216. if err := c.SaveUploadedFile(file, fpath); err != nil {
  217. response.FailMsg("文件生成失败", "QuestionUploadAudio 文件生成失败, Err:"+err.Error(), c)
  218. return
  219. }
  220. // 获取音频文件时长
  221. fByte, err := ioutil.ReadFile(fpath)
  222. if err != nil {
  223. response.FailMsg("读取本地文件失败", "QuestionUploadAudio 读取本地文件失败", c)
  224. return
  225. }
  226. if len(fByte) <= 0 {
  227. response.FailMsg("文件大小有误", "QuestionUploadAudio 文件大小有误", c)
  228. return
  229. }
  230. seconds, err := services.GetMP3PlayDuration(fByte)
  231. if err != nil {
  232. response.FailMsg("读取文件时长失败", "QuestionUploadAudio 读取文件时长失败", c)
  233. return
  234. }
  235. // 音频大小MB
  236. fi, err := os.Stat(fpath)
  237. if err != nil {
  238. response.FailMsg("读取文件大小失败", "QuestionUploadAudio 读取文件大小失败", c)
  239. return
  240. }
  241. mb := utils.Bit2MB(fi.Size(), 2)
  242. // 上传文件至阿里云
  243. ossDir := "yb_wx/community_question_audio/"
  244. resourceUrl, err := services.UploadAliyunToDir(filtName, fpath, ossDir)
  245. if err != nil {
  246. response.FailMsg("文件上传失败", "QuestionUploadAudio 文件上传失败, Err:"+err.Error(), c)
  247. return
  248. }
  249. resp := &respond.CommunityQuestionAudioUpload{
  250. AudioURL: resourceUrl,
  251. AudioPlaySeconds: fmt.Sprint(seconds),
  252. AudioSize: fmt.Sprint(mb),
  253. }
  254. response.OkData("上传成功", resp, c)
  255. }
  256. // QuestionUnread 我的问答未读数
  257. // @Tags 问答社区模块
  258. // @Description 我的问答未读数
  259. // @Success 200 {int} int "获取成功"
  260. // @failure 400 {string} string "获取失败"
  261. // @Router /question/unread [get]
  262. func QuestionUnread(c *gin.Context) {
  263. userinfo := user.GetInfoByClaims(c)
  264. total, err := community.GetMyQuestionUnread(userinfo)
  265. if err != nil {
  266. fmt.Println(err.Error())
  267. response.FailMsg("获取失败", "QuestionUnread ErrMsg:"+err.Error(), c)
  268. return
  269. }
  270. response.OkData("获取成功", total, c)
  271. }
  272. // ResearchGroupList 研究方向分组列表
  273. // @Tags 问答社区模块
  274. // @Description 研究方向分组列表
  275. // @Success 200 {int} int "获取成功"
  276. // @failure 400 {string} string "获取失败"
  277. // @Router /question/research_group [get]
  278. func ResearchGroupList(c *gin.Context) {
  279. list, err := community.GetResearchGroupTree()
  280. if err != nil {
  281. fmt.Println(err.Error())
  282. response.FailMsg("获取失败", "ResearchGroupList ErrMsg:"+err.Error(), c)
  283. return
  284. }
  285. response.OkData("获取成功", list, c)
  286. }
  287. // AddAudioLog 添加用户点击音频日志
  288. // @Tags 问答社区模块
  289. // @Description 添加用户点击音频日志
  290. // @Param community_question_audio_id query int true "音频ID"
  291. // @Param source_agent query int true "操作来源,1:小程序,2:小程序 pc 3:弘则研究公众号,4:web pc"
  292. // @Success 200 {string} string "操作成功"
  293. // @failure 400 {string} string "操作失败"
  294. // @Router /AddAudioLog [post]
  295. func AddAudioLog(c *gin.Context) {
  296. var req request.CommunityAudioListenLogReq
  297. if err := c.ShouldBind(&req); err != nil {
  298. response.Fail("参数有误", c)
  299. return
  300. }
  301. if req.CommunityQuestionAudioID == 0 {
  302. response.Fail("请输入音频ID", c)
  303. return
  304. }
  305. if req.SourceAgent == 0 {
  306. response.Fail("请输入操作来源", c)
  307. return
  308. }
  309. userinfo := user.GetInfoByClaims(c)
  310. if err := community.AddAudioListenLog(userinfo, req.CommunityQuestionAudioID, req.SourceAgent); err != nil {
  311. response.Fail("操作失败: "+err.Error(), c)
  312. return
  313. }
  314. response.Ok("操作成功", c)
  315. }