public.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package controller
  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"
  8. "hongze/hongze_yb/models/request"
  9. "hongze/hongze_yb/models/tables/yb_suncode_pars"
  10. "hongze/hongze_yb/services"
  11. "hongze/hongze_yb/services/alarm_msg"
  12. "hongze/hongze_yb/utils"
  13. "os"
  14. "path"
  15. "time"
  16. )
  17. // GetApplyVarietyList
  18. // @Tags 公共模块
  19. // @Summary 获取所有可以申请的品种权限列表
  20. // @Description 获取所有可以申请的品种权限列表
  21. // @Security ApiKeyAuth
  22. // @securityDefinitions.basic BasicAuth
  23. // @Param Authorization header string true "微信登录后获取到的token"
  24. // @Accept json
  25. // @Product json
  26. // @Success 200 {object} []logic.ApplyVariety "获取成功"
  27. // @failure 400 {string} string "获取失败"
  28. // @Router /public/get_apply_variety_list [get]
  29. func GetApplyVarietyList(c *gin.Context) {
  30. list := logic.GetApplyVarietyList()
  31. response.OkData("获取成功", list, c)
  32. }
  33. // Upload
  34. // @Tags 公共模块
  35. // @Summary 文件上传
  36. // @Description 文件上传
  37. // @Security ApiKeyAuth
  38. // @securityDefinitions.basic BasicAuth
  39. // @Param Authorization header string true "微信登录后获取到的token"
  40. // @Param file formData file false "操作描述"
  41. // @Accept multipart/form-data
  42. // @Product json
  43. // @Success 200 {object} string "上传成功"
  44. // @failure 400 {string} string "上传失败,存储目录创建失败"
  45. // @Router /public/upload [post]
  46. func Upload(c *gin.Context) {
  47. // 单文件
  48. file, err := c.FormFile("file")
  49. fmt.Println("file", file)
  50. if err != nil {
  51. response.FailData("获取资源失败", "获取资源失败,Err:"+err.Error(), c)
  52. return
  53. }
  54. ext := path.Ext(file.Filename)
  55. dateDir := time.Now().Format("20060102")
  56. uploadDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  57. err = os.MkdirAll(uploadDir, 0766)
  58. if err != nil {
  59. response.FailData("存储目录创建失败", "存储目录创建失败,Err:"+err.Error(), c)
  60. return
  61. }
  62. randStr := utils.GetRandStringNoSpecialChar(28)
  63. fileName := randStr + ext
  64. fpath := uploadDir + "/" + fileName
  65. // 上传文件至指定目录
  66. err = c.SaveUploadedFile(file, fpath)
  67. if err != nil {
  68. response.FailData("文件上传失败", "文件上传失败,Err:"+err.Error(), c)
  69. return
  70. }
  71. defer func() {
  72. os.Remove(fpath)
  73. }()
  74. //上传到阿里云
  75. resourceUrl, err := services.UploadAliyun(fileName, fpath)
  76. if err != nil {
  77. response.FailData("文件上传失败", "文件上传失败,Err:"+err.Error(), c)
  78. return
  79. }
  80. response.OkData("文件上传成功", resourceUrl, c)
  81. }
  82. // GetSharePoster
  83. // @Tags 公共模块
  84. // @Summary 获取分享海报
  85. // @Description 获取分享海报
  86. // @Security ApiKeyAuth
  87. // @securityDefinitions.basic BasicAuth
  88. // @Param request body services.SharePosterReq true "type json string"
  89. // @Accept application/json
  90. // @Success 200 {object} string "获取成功"
  91. // @failure 400 {string} string "获取失败"
  92. // @Router /public/get_share_poster [post]
  93. func GetSharePoster(c *gin.Context) {
  94. var req services.SharePosterReq
  95. if c.ShouldBind(&req) != nil {
  96. response.Fail("参数异常", c)
  97. return
  98. }
  99. if req.Source == "" {
  100. response.Fail("来源有误", c)
  101. return
  102. }
  103. imgUrl, err := services.CreatePosterFromSourceV2(req.CodePage, req.CodeScene, req.Source, req.Version, req.Pars)
  104. if err != nil {
  105. response.FailData("获取分享海报失败", "获取分享海报失败, Err: "+err.Error(), c)
  106. return
  107. }
  108. response.OkData("获取成功", imgUrl, c)
  109. }
  110. // GetSuncodeScene 获取小程序太阳码scene值
  111. // @Tags 公共模块
  112. // @Summary 获取小程序太阳码scene值
  113. // @Description 获取小程序太阳码scene值
  114. // @Security ApiKeyAuth
  115. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  116. // @Param scene_key query string true "scene_key值"
  117. // @Success 200 {string} string "获取成功"
  118. // @failure 400 {string} string "获取失败"
  119. // @Router /public/get_suncode_scene [get]
  120. func GetSuncodeScene(c *gin.Context) {
  121. reqKey := c.DefaultQuery("scene_key", "")
  122. if reqKey == "" {
  123. response.Fail("参数有误", c)
  124. }
  125. pars, err := yb_suncode_pars.GetSceneByKey(reqKey)
  126. if err != nil && err != utils.ErrNoRow {
  127. response.FailMsg("获取失败", "GetSuncodeScene获取失败, Err: "+err.Error(), c)
  128. return
  129. }
  130. scene := ""
  131. if pars != nil {
  132. scene = pars.Scene
  133. }
  134. response.OkData("获取成功", scene, c)
  135. }
  136. // GetVarietyTagTree 标签树
  137. // @Tags 公共模块
  138. // @Summary 标签树
  139. // @Description 标签树
  140. // @Security ApiKeyAuth
  141. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  142. // @Param scene_key query string true "scene_key值"
  143. // @Success 200 {string} string "获取成功"
  144. // @failure 400 {string} string "获取失败"
  145. // @Router /public/get_variety_tag_tree [get]
  146. func GetVarietyTagTree(c *gin.Context) {
  147. list, err := services.GetTagTree()
  148. if err != nil {
  149. response.FailMsg("获取标签树失败", "获取标签树失败, Err: "+err.Error(), c)
  150. return
  151. }
  152. response.OkData("获取成功", list, c)
  153. }
  154. // WechatWarning 小程序前端预警提示
  155. // @Tags 公共模块
  156. // @Description 小程序前端预警提示
  157. // @Param content query string true "预警信息"
  158. // @Success 200 {string} string "操作成功"
  159. // @failure 400 {string} string "操作失败"
  160. // @Router /public/wechat_warning [post]
  161. func WechatWarning(c *gin.Context) {
  162. var req request.WechatWarningReq
  163. if err := c.Bind(&req); err != nil {
  164. response.Fail("参数有误", c)
  165. return
  166. }
  167. if req.Content != "" {
  168. tips := fmt.Sprintf("研报小程序前端报错预警-ErrMsg: %s", req.Content)
  169. global.LOG.Warning(tips)
  170. go alarm_msg.SendAlarmMsg(tips, 2)
  171. }
  172. response.Ok("操作成功", c)
  173. }