question.go 11 KB

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