question.go 12 KB

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