question.go 12 KB

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