question.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. package community
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "hongze/hongze_yb/controller/response"
  7. "hongze/hongze_yb/global"
  8. userLogic "hongze/hongze_yb/logic/user"
  9. "hongze/hongze_yb/logic/yb_community_question"
  10. "hongze/hongze_yb/models/request"
  11. "hongze/hongze_yb/models/tables/yb_resource"
  12. "strings"
  13. respond "hongze/hongze_yb/models/response"
  14. responseModel "hongze/hongze_yb/models/response"
  15. "hongze/hongze_yb/services"
  16. "hongze/hongze_yb/services/community"
  17. "hongze/hongze_yb/services/user"
  18. userService "hongze/hongze_yb/services/user"
  19. "hongze/hongze_yb/services/wx_app"
  20. "hongze/hongze_yb/utils"
  21. "os"
  22. "path"
  23. "strconv"
  24. "time"
  25. )
  26. // QuestionList 问答列表
  27. // @Tags 问答社区模块
  28. // @Description 获取问答列表
  29. // @Param page_index query int false "页码"
  30. // @Param page_size query int false "每页数量"
  31. // @Param only_mine query int false "只看我的"
  32. // @Param variety_tag_id query int false "标签ID"
  33. // @Param reply_status query int false "回复状态 0-全部 2-未回答 3-已回答,4-待回答(研究员)"
  34. // @Success 200 {object} []respond.CommunityQuestionItem
  35. // @failure 400 {string} string "获取失败"
  36. // @Router /question/list [get]
  37. func QuestionList(c *gin.Context) {
  38. var req request.QuestionListReq
  39. if err := c.Bind(&req); err != nil {
  40. response.Fail("参数有误", c)
  41. return
  42. }
  43. if req.PageIndex == 0 {
  44. req.PageIndex = 1
  45. }
  46. if req.PageSize == 0 {
  47. req.PageSize = utils.PageSize20
  48. }
  49. userinfo := user.GetInfoByClaims(c)
  50. list, err := community.GetQuestionList(req.PageIndex, req.PageSize, req.OnlyMine, req.VarietyTagId, req.ReplyStatus, req.GroupId, userinfo)
  51. if err != nil {
  52. response.FailMsg("获取失败", "QuestionList ErrMsg:"+err.Error(), c)
  53. return
  54. }
  55. // 点赞/吐槽数据
  56. err = yb_community_question.HandleLikeOrTeaseByCommunityQuestionItemList(userinfo.UserID, list)
  57. if err != nil {
  58. response.FailMsg("获取失败", "QuestionList ErrMsg:"+err.Error(), c)
  59. return
  60. }
  61. // 评论数据
  62. err = yb_community_question.HandleCommentByCommunityQuestionItemList(userinfo.UserID, list)
  63. if err != nil {
  64. response.FailMsg("获取失败", "QuestionList ErrMsg:"+err.Error(), c)
  65. return
  66. }
  67. response.OkData("获取成功", list, c)
  68. }
  69. // QuestionDetail 问答详情
  70. // @Tags 问答社区模块
  71. // @Description 获取问答详情
  72. // @Param question_id query int true "问答ID"
  73. // @Success 200 {object} respond.CommunityQuestionItem
  74. // @failure 400 {string} string "获取失败"
  75. // @Router /question/detail [get]
  76. func QuestionDetail(c *gin.Context) {
  77. var req request.QuestionDetailReq
  78. if err := c.Bind(&req); err != nil {
  79. response.Fail("参数有误", c)
  80. return
  81. }
  82. if req.QuestionId == 0 {
  83. response.Fail("参数有误", c)
  84. return
  85. }
  86. userinfo := user.GetInfoByClaims(c)
  87. item, errMsg, err := community.GetQuestionDetail(req.QuestionId, userinfo)
  88. if err != nil {
  89. response.FailMsg(errMsg, "QuestionDetail ErrMsg:"+err.Error(), c)
  90. return
  91. }
  92. response.OkData("获取成功", item, c)
  93. }
  94. // QuestionAsk 发布提问
  95. // @Tags 问答社区模块
  96. // @Description 发布提问
  97. // @Param question_content query string true "问题内容"
  98. // @Success 200 {string} string "操作成功"
  99. // @failure 400 {string} string "操作失败"
  100. // @Router /question/ask [post]
  101. func QuestionAsk(c *gin.Context) {
  102. // 是否为备用小程序
  103. copyYb := c.Request.Header.Get("CopyYb")
  104. var req request.QuestionAskReq
  105. if err := c.ShouldBind(&req); err != nil {
  106. response.Fail("参数有误", c)
  107. return
  108. }
  109. if req.QuestionContent == "" {
  110. response.Fail("内容不可为空", c)
  111. return
  112. }
  113. contentRune := []rune(req.QuestionContent)
  114. if len(contentRune) > 50 {
  115. response.Fail("内容不可超过50个字符", c)
  116. return
  117. }
  118. if req.VarietyClassifyId <= 0 {
  119. response.Fail("请选择品种分类", c)
  120. return
  121. }
  122. if req.VarietyTagId <= 0 {
  123. response.Fail("请选择品种", c)
  124. return
  125. }
  126. // 敏感词校验, 只有小程序的用户才能走敏感词过滤接口
  127. userinfo := user.GetInfoByClaims(c)
  128. if userinfo.RecordInfo.OpenID != "" && (userinfo.RecordInfo.CreatePlatform == utils.USER_RECORD_PLATFORM_YB || userinfo.RecordInfo.CreatePlatform == utils.USER_RECORD_PLATFORM_COPY_YB) {
  129. checkResult, e := wx_app.MsgSecCheck(userinfo.RecordInfo.OpenID, req.QuestionContent, copyYb)
  130. if e == nil {
  131. if checkResult.Result != nil && checkResult.Result.Suggest != "pass" {
  132. errMsg := "含有违禁词, 不允许发布: " + checkResult.Result.Suggest + ", 命中标签: " + strconv.Itoa(checkResult.Result.Label)
  133. response.FailMsg("含有违禁词, 不允许发布", errMsg, c)
  134. return
  135. }
  136. }
  137. }
  138. if err := community.CreateQuestion(int(userinfo.UserID), req.VarietyClassifyId, req.VarietyTagId, userinfo.Mobile, userinfo.RealName, req.QuestionContent); err != nil {
  139. response.FailMsg("提交失败", "QuestionAsk ErrMsg:"+err.Error(), c)
  140. return
  141. }
  142. response.Ok("操作成功", c)
  143. }
  144. // QuestionReply 发布回复
  145. // @Tags 问答社区模块
  146. // @Description 发布回复
  147. // @Param question_id query int true "问答ID"
  148. // @Param audio_list query object true "音频列表"
  149. // @Success 200 {string} string "操作成功"
  150. // @failure 400 {string} string "操作失败"
  151. // @Router /question/reply [post]
  152. func QuestionReply(c *gin.Context) {
  153. var req request.QuestionReplyReq
  154. if err := c.ShouldBind(&req); err != nil {
  155. response.Fail("参数有误", c)
  156. return
  157. }
  158. if req.QuestionId == 0 {
  159. response.Fail("参数有误", c)
  160. return
  161. }
  162. if len(req.AudioList) == 0 {
  163. response.Fail("音频不可为空", c)
  164. return
  165. }
  166. for i := range req.AudioList {
  167. if req.AudioList[i].AudioUrl == "" || req.AudioList[i].AudioPlaySeconds == "" || req.AudioList[i].AudioSize == "" {
  168. response.Fail("音频信息有误, 请重新上传", c)
  169. return
  170. }
  171. if !strings.Contains(req.AudioList[i].AudioUrl, utils.ALIYUN_OSS_HOST) {
  172. response.Fail("音频地址有误, 请重新上传", c)
  173. return
  174. }
  175. }
  176. userinfo := user.GetInfoByClaims(c)
  177. msg, err := community.ReplyUserQuestion(int(userinfo.UserID), req.QuestionId, req.AudioList)
  178. if err != nil {
  179. response.FailMsg(msg, "QuestionReply ErrMsg:"+err.Error(), c)
  180. return
  181. }
  182. response.Ok("操作成功", c)
  183. }
  184. // QuestionReplyRead 已读回复
  185. // @Tags 问答社区模块
  186. // @Description 已读回复
  187. // @Param question_ids query string true "问答IDs"
  188. // @Success 200 {string} string "操作成功"
  189. // @failure 400 {string} string "操作失败"
  190. // @Router /question/reply/read [post]
  191. func QuestionReplyRead(c *gin.Context) {
  192. var req request.QuestionReadReq
  193. if err := c.ShouldBind(&req); err != nil {
  194. response.Fail("参数有误", c)
  195. return
  196. }
  197. if req.QuestionIds == "" {
  198. response.Fail("参数有误", c)
  199. return
  200. }
  201. userinfo := user.GetInfoByClaims(c)
  202. if err := community.ReadQuestionReply(req.QuestionIds, userinfo); err != nil {
  203. response.FailMsg("操作失败", "QuestionReplyRead ErrMsg:"+err.Error(), c)
  204. return
  205. }
  206. response.Ok("操作成功", c)
  207. }
  208. // QuestionListTotal 问答列表数量统计
  209. // @Tags 问答社区模块
  210. // @Description 问答列表数量统计
  211. // @Success 200 {object} respond.CommunityQuestionListTotal
  212. // @failure 400 {string} string "获取失败"
  213. // @Router /question/list/total [get]
  214. func QuestionListTotal(c *gin.Context) {
  215. userinfo := user.GetInfoByClaims(c)
  216. resp, err := community.GetQuestionListTotal(userinfo)
  217. if err != nil {
  218. response.FailMsg("获取失败", "QuestionReplyTotal ErrMsg:"+err.Error(), c)
  219. return
  220. }
  221. response.OkData("获取成功", resp, c)
  222. }
  223. // QuestionUploadAudio 上传回复音频
  224. // @Tags 问答社区模块
  225. // @Description 上传回复音频
  226. // @Param file query string true "音频文件"
  227. // @Success 200 {string} string "上传成功"
  228. // @failure 400 {string} string "上传失败"
  229. // @Router /question/reply/upload_audio [post]
  230. func QuestionUploadAudio(c *gin.Context) {
  231. file, err := c.FormFile("file")
  232. if err != nil {
  233. response.FailMsg("获取资源失败", "获取资源失败, Err:"+err.Error(), c)
  234. return
  235. }
  236. ext := path.Ext(file.Filename)
  237. if ext != ".mp3" {
  238. response.Fail("暂仅支持mp3格式", c)
  239. return
  240. }
  241. dateDir := time.Now().Format("20060102")
  242. localDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  243. if err := os.MkdirAll(localDir, 0766); err != nil {
  244. response.FailMsg("存储目录创建失败", "QuestionUploadAudio 存储目录创建失败, Err:"+err.Error(), c)
  245. return
  246. }
  247. randStr := utils.GetRandStringNoSpecialChar(28)
  248. filtName := randStr + ext
  249. fpath := localDir + "/" + filtName
  250. defer func() {
  251. _ = os.Remove(fpath)
  252. }()
  253. // 生成文件至指定目录
  254. if err := c.SaveUploadedFile(file, fpath); err != nil {
  255. response.FailMsg("文件生成失败", "QuestionUploadAudio 文件生成失败, Err:"+err.Error(), c)
  256. return
  257. }
  258. // 获取音频文件时长
  259. //fByte, err := ioutil.ReadFile(fpath)
  260. //if err != nil {
  261. // response.FailMsg("读取本地文件失败", "QuestionUploadAudio 读取本地文件失败", c)
  262. // return
  263. //}
  264. //if len(fByte) <= 0 {
  265. // response.FailMsg("文件大小有误", "QuestionUploadAudio 文件大小有误", c)
  266. // return
  267. //}
  268. seconds, err := utils.GetVideoPlaySeconds(fpath) //services.GetMP3PlayDuration(fByte)
  269. if err != nil {
  270. response.FailMsg("读取文件时长失败", "QuestionUploadAudio 读取文件时长失败", c)
  271. return
  272. }
  273. // 音频大小MB
  274. fi, err := os.Stat(fpath)
  275. if err != nil {
  276. response.FailMsg("读取文件大小失败", "QuestionUploadAudio 读取文件大小失败", c)
  277. return
  278. }
  279. mb := utils.Bit2MB(fi.Size(), 2)
  280. // 上传文件至阿里云
  281. ossDir := "yb_wx/community_question_audio/"
  282. resourceUrl, err := services.UploadAliyunToDir(filtName, fpath, ossDir)
  283. if err != nil {
  284. response.FailMsg("文件上传失败", "QuestionUploadAudio 文件上传失败, Err:"+err.Error(), c)
  285. return
  286. }
  287. resp := &respond.CommunityQuestionAudioUpload{
  288. AudioURL: resourceUrl,
  289. AudioPlaySeconds: fmt.Sprint(seconds),
  290. AudioSize: fmt.Sprint(mb),
  291. }
  292. // 记录文件
  293. userInfo := user.GetInfoByClaims(c)
  294. go func() {
  295. extendData := struct {
  296. FileName string
  297. AudioURL string
  298. AudioPlaySeconds string
  299. AudioSize string
  300. UserId int
  301. }{
  302. file.Filename,
  303. resourceUrl,
  304. fmt.Sprint(seconds),
  305. fmt.Sprint(mb),
  306. int(userInfo.UserID),
  307. }
  308. dataByte, e := json.Marshal(extendData)
  309. if e != nil {
  310. return
  311. }
  312. re := new(yb_resource.YbResource)
  313. re.ResourceUrl = resourceUrl
  314. re.ResourceType = yb_resource.ResourceTypeAudio
  315. re.ExtendData = string(dataByte)
  316. re.CreateTime = time.Now().Local()
  317. if e = re.Create(); e != nil {
  318. return
  319. }
  320. }()
  321. response.OkData("上传成功", resp, c)
  322. }
  323. // QuestionUnread 我的问答未读数
  324. // @Tags 问答社区模块
  325. // @Description 我的问答未读数
  326. // @Success 200 {int} int "获取成功"
  327. // @failure 400 {string} string "获取失败"
  328. // @Router /question/unread [get]
  329. func QuestionUnread(c *gin.Context) {
  330. userinfo := user.GetInfoByClaims(c)
  331. total, err := community.GetMyQuestionUnread(userinfo)
  332. if err != nil {
  333. fmt.Println(err.Error())
  334. response.FailMsg("获取失败", "QuestionUnread ErrMsg:"+err.Error(), c)
  335. return
  336. }
  337. response.OkData("获取成功", total, c)
  338. }
  339. // ResearchGroupList 研究方向分组列表
  340. // @Tags 问答社区模块
  341. // @Description 研究方向分组列表
  342. // @Success 200 {int} int "获取成功"
  343. // @failure 400 {string} string "获取失败"
  344. // @Router /question/research_group [get]
  345. func ResearchGroupList(c *gin.Context) {
  346. list, err := community.GetResearchGroupTree()
  347. if err != nil {
  348. response.FailMsg("获取失败", "ResearchGroupList ErrMsg:"+err.Error(), c)
  349. return
  350. }
  351. response.OkData("获取成功", list, c)
  352. }
  353. // AddAudioLog 添加用户点击音频日志
  354. // @Tags 问答社区模块
  355. // @Description 添加用户点击音频日志
  356. // @Param community_question_audio_id query int true "音频ID"
  357. // @Param source_agent query int true "操作来源,1:小程序,2:小程序 pc 3:弘则研究公众号,4:web pc"
  358. // @Success 200 {string} string "操作成功"
  359. // @failure 400 {string} string "操作失败"
  360. // @Router /AddAudioLog [post]
  361. func AddAudioLog(c *gin.Context) {
  362. var req request.CommunityAudioListenLogReq
  363. if err := c.ShouldBind(&req); err != nil {
  364. response.Fail("参数有误", c)
  365. return
  366. }
  367. if req.CommunityQuestionAudioID == 0 {
  368. response.Fail("请输入音频ID", c)
  369. return
  370. }
  371. if req.SourceAgent == 0 {
  372. response.Fail("请输入操作来源", c)
  373. return
  374. }
  375. userinfo := user.GetInfoByClaims(c)
  376. newId, err := community.AddAudioListenLog(userinfo, req.CommunityQuestionAudioID, req.SourceAgent)
  377. if err != nil {
  378. response.Fail("操作失败: "+err.Error(), c)
  379. return
  380. }
  381. response.OkData("操作成功", newId, c)
  382. }
  383. // SetLikeOrTease 问答点赞/吐槽设置
  384. func SetLikeOrTease(c *gin.Context) {
  385. var req request.ReqCommunityQuestionLikeTease
  386. if c.ShouldBind(&req) != nil {
  387. response.Fail("参数异常", c)
  388. return
  389. }
  390. userinfo := userService.GetInfoByClaims(c)
  391. // 不传来源默认为问答社区
  392. if req.Source == 0 {
  393. req.Source = 1
  394. }
  395. communityQuestionLikeTease, likeNum, teaseNum, err, errMsg := yb_community_question.SetLikeOrTease(userinfo.UserID, req.CommunityQuestionId, req.OpType, req.Enable, req.SourceAgent, req.Source)
  396. if err != nil {
  397. response.FailMsg(errMsg, err.Error(), c)
  398. return
  399. }
  400. response.OkData("操作成功", responseModel.RespCommunityQuestionLikeTease{
  401. LikeTotal: likeNum,
  402. TeaseTotal: teaseNum,
  403. OpType: communityQuestionLikeTease.OpType,
  404. Enabled: communityQuestionLikeTease.Enabled,
  405. }, c)
  406. return
  407. }
  408. // QuestionTransfer 转移问答
  409. // @Tags 问答社区模块
  410. // @Description 转移问答
  411. // @Param request body request.QuestionTransferReq true "type json string"
  412. // @Success 200 {string} string "操作成功"
  413. // @Failure 400 {string} string "操作失败"
  414. // @Router /question/transfer [post]
  415. func QuestionTransfer(c *gin.Context) {
  416. var req request.QuestionTransferReq
  417. if err := c.ShouldBind(&req); err != nil {
  418. response.Fail("参数有误", c)
  419. return
  420. }
  421. if req.CommunityQuestionID <= 0 {
  422. response.Fail("参数有误", c)
  423. return
  424. }
  425. if req.VarietyClassifyId <= 0 {
  426. response.Fail("请选择品种分类", c)
  427. return
  428. }
  429. if req.VarietyTagId <= 0 {
  430. response.Fail("请选择品种", c)
  431. return
  432. }
  433. if req.AdminId <= 0 {
  434. response.Fail("请选择研究员", c)
  435. return
  436. }
  437. userInfo := user.GetInfoByClaims(c)
  438. isInner, isResearcher, _, adminInfo, e := userLogic.GetUserIdentity(int(userInfo.CompanyID), userInfo.Mobile, userInfo.Email)
  439. if e != nil {
  440. response.FailMsg("操作失败", "QuestionTransfer ErrMsg:"+e.Error(), c)
  441. return
  442. }
  443. if isInner == 0 || isResearcher == 0 {
  444. response.Fail("无权操作", c)
  445. return
  446. }
  447. errMsg, e := community.TransferQuestion(req.CommunityQuestionID, int(userInfo.UserID), int(adminInfo.AdminID), req.VarietyClassifyId, req.VarietyTagId, req.AdminId, adminInfo.RealName)
  448. if e != nil {
  449. response.FailMsg(errMsg, "QuestionTransfer ErrMsg:"+e.Error(), c)
  450. return
  451. }
  452. response.Ok("操作成功", c)
  453. }
  454. // QuestionStop 终止问答
  455. // @Tags 问答社区模块
  456. // @Description 终止问答
  457. // @Param request body request.QuestionStopReq true "type json string"
  458. // @Success 200 {string} string "操作成功"
  459. // @failure 400 {string} string "操作失败"
  460. // @Router /question/stop [post]
  461. func QuestionStop(c *gin.Context) {
  462. // 是否为备用小程序
  463. copyYb := c.Request.Header.Get("CopyYb")
  464. var req request.QuestionStopReq
  465. if err := c.ShouldBind(&req); err != nil {
  466. response.Fail("参数有误", c)
  467. return
  468. }
  469. if req.CommunityQuestionID <= 0 {
  470. response.Fail("参数有误", c)
  471. return
  472. }
  473. if req.Reason == "" {
  474. response.Fail("请输入终止原因", c)
  475. return
  476. }
  477. // 敏感词校验, 只有小程序的用户才能走敏感词过滤接口
  478. userinfo := user.GetInfoByClaims(c)
  479. if userinfo.RecordInfo.OpenID != "" && userinfo.RecordInfo.CreatePlatform == 6 {
  480. checkResult, e := wx_app.MsgSecCheck(userinfo.RecordInfo.OpenID, req.Reason, copyYb)
  481. if e == nil {
  482. if checkResult.Result != nil && checkResult.Result.Suggest != "pass" {
  483. errMsg := "含有违禁词, 不允许发布: " + checkResult.Result.Suggest + ", 命中标签: " + strconv.Itoa(checkResult.Result.Label)
  484. response.FailMsg("含有违禁词, 不允许发布", errMsg, c)
  485. return
  486. }
  487. }
  488. }
  489. userInfo := user.GetInfoByClaims(c)
  490. isInner, isResearcher, _, _, e := userLogic.GetUserIdentity(int(userInfo.CompanyID), userInfo.Mobile, userInfo.Email)
  491. if e != nil {
  492. response.FailMsg("操作失败", "QuestionTransfer ErrMsg:"+e.Error(), c)
  493. return
  494. }
  495. if isInner == 0 || isResearcher == 0 {
  496. response.Fail("无权操作", c)
  497. return
  498. }
  499. errMsg, e := community.StopQuestion(req.CommunityQuestionID, int(userInfo.UserID), req.Reason)
  500. if e != nil {
  501. response.FailMsg(errMsg, "QuestionStop ErrMsg:"+e.Error(), c)
  502. return
  503. }
  504. response.Ok("操作成功", c)
  505. }