question.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 chart_permission_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.ChartPermissionId, req.ReplyStatus, 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. if err := community.ReplyUserQuestion(int(userinfo.UserID), req.QuestionId, req.AudioList); err != nil {
  140. response.FailMsg("提交失败", "QuestionReply ErrMsg:"+err.Error(), c)
  141. return
  142. }
  143. response.Ok("操作成功", c)
  144. }
  145. // QuestionReplyRead 已读回复
  146. // @Tags 问答社区模块
  147. // @Description 已读回复
  148. // @Param question_ids query string true "问答IDs"
  149. // @Success 200 {string} string "操作成功"
  150. // @failure 400 {string} string "操作失败"
  151. // @Router /question/reply/read [post]
  152. func QuestionReplyRead(c *gin.Context) {
  153. var req request.QuestionReadReq
  154. if err := c.ShouldBind(&req); err != nil {
  155. response.Fail("参数有误", c)
  156. return
  157. }
  158. if req.QuestionIds == "" {
  159. response.Fail("参数有误", c)
  160. return
  161. }
  162. userinfo := user.GetInfoByClaims(c)
  163. if err := community.ReadQuestionReply(req.QuestionIds, userinfo); err != nil {
  164. response.FailMsg("操作失败", "QuestionReplyRead ErrMsg:"+err.Error(), c)
  165. return
  166. }
  167. response.Ok("操作成功", c)
  168. }
  169. // QuestionListTotal 问答列表数量统计
  170. // @Tags 问答社区模块
  171. // @Description 问答列表数量统计
  172. // @Success 200 {object} respond.CommunityQuestionListTotal
  173. // @failure 400 {string} string "获取失败"
  174. // @Router /question/list/total [get]
  175. func QuestionListTotal(c *gin.Context) {
  176. userinfo := user.GetInfoByClaims(c)
  177. resp, err := community.GetQuestionListTotal(userinfo)
  178. if err != nil {
  179. response.FailMsg("获取失败", "QuestionReplyTotal ErrMsg:"+err.Error(), c)
  180. return
  181. }
  182. response.OkData("获取成功", resp, c)
  183. }
  184. // QuestionUploadAudio 上传回复音频
  185. // @Tags 问答社区模块
  186. // @Description 上传回复音频
  187. // @Param file query string true "音频文件"
  188. // @Success 200 {string} string "上传成功"
  189. // @failure 400 {string} string "上传失败"
  190. // @Router /question/reply/upload_audio [post]
  191. func QuestionUploadAudio(c *gin.Context) {
  192. file, err := c.FormFile("file")
  193. if err != nil {
  194. response.FailMsg("获取资源失败", "获取资源失败, Err:"+err.Error(), c)
  195. return
  196. }
  197. ext := path.Ext(file.Filename)
  198. if ext != ".mp3" {
  199. response.Fail("暂仅支持mp3格式", c)
  200. return
  201. }
  202. dateDir := time.Now().Format("20060102")
  203. localDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  204. if err := os.MkdirAll(localDir, 0766); err != nil {
  205. response.FailMsg("存储目录创建失败", "QuestionUploadAudio 存储目录创建失败, Err:"+err.Error(), c)
  206. return
  207. }
  208. randStr := utils.GetRandStringNoSpecialChar(28)
  209. filtName := randStr + ext
  210. fpath := localDir + "/" + filtName
  211. defer func() {
  212. _ = os.Remove(fpath)
  213. }()
  214. // 生成文件至指定目录
  215. if err := c.SaveUploadedFile(file, fpath); err != nil {
  216. response.FailMsg("文件生成失败", "QuestionUploadAudio 文件生成失败, Err:"+err.Error(), c)
  217. return
  218. }
  219. // 获取音频文件时长
  220. fByte, err := ioutil.ReadFile(fpath)
  221. if err != nil {
  222. response.FailMsg("读取本地文件失败", "QuestionUploadAudio 读取本地文件失败", c)
  223. return
  224. }
  225. seconds, err := services.GetMP3PlayDuration(fByte)
  226. if err != nil {
  227. response.FailMsg("读取文件时长失败", "QuestionUploadAudio 读取文件时长失败", c)
  228. return
  229. }
  230. // 音频大小MB
  231. fi, err := os.Stat(fpath)
  232. if err != nil {
  233. response.FailMsg("读取文件大小失败", "QuestionUploadAudio 读取文件大小失败", c)
  234. return
  235. }
  236. mb := utils.Bit2MB(fi.Size(), 2)
  237. // 上传文件至阿里云
  238. ossDir := "yb_wx/community_question_audio/"
  239. resourceUrl, err := services.UploadAliyunToDir(filtName, fpath, ossDir)
  240. if err != nil {
  241. response.FailMsg("文件上传失败", "QuestionUploadAudio 文件上传失败, Err:"+err.Error(), c)
  242. return
  243. }
  244. resp := &respond.CommunityQuestionAudioUpload{
  245. AudioURL: resourceUrl,
  246. AudioPlaySeconds: fmt.Sprint(seconds),
  247. AudioSize: fmt.Sprint(mb),
  248. }
  249. response.OkData("上传成功", resp, c)
  250. }
  251. // QuestionUnread 我的问答未读数
  252. // @Tags 问答社区模块
  253. // @Description 我的问答未读数
  254. // @Success 200 {int} int "获取成功"
  255. // @failure 400 {string} string "获取失败"
  256. // @Router /question/unread [get]
  257. func QuestionUnread(c *gin.Context) {
  258. userinfo := user.GetInfoByClaims(c)
  259. total, err := community.GetMyQuestionUnread(userinfo)
  260. if err != nil {
  261. fmt.Println(err.Error())
  262. response.FailMsg("获取失败", "QuestionUnread ErrMsg:"+err.Error(), c)
  263. return
  264. }
  265. response.OkData("获取成功", total, c)
  266. }