question.go 16 KB

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