analyst.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm_ht/models"
  5. "eta/eta_mini_crm_ht/models/request"
  6. "eta/eta_mini_crm_ht/models/response"
  7. "eta/eta_mini_crm_ht/utils"
  8. "eta/eta_mini_crm_ht/utils/oss"
  9. "fmt"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "os"
  12. "path"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. const ()
  18. type AnalystController struct {
  19. BaseAuthController
  20. }
  21. // UploadImage
  22. // @Title 上传头像
  23. // @Description 上传头像
  24. // @Param File query file true "文件"
  25. // @Success 200 {object} models.ReportAuthorResp
  26. // @router /uploadImage [post]
  27. func (this *AnalystController) UploadImage() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. f, h, err := this.GetFile("File")
  34. if err != nil {
  35. br.Msg = "获取资源信息失败"
  36. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  37. return
  38. }
  39. if !utils.CheckImageFormat(h.Filename, f, h) {
  40. br.Msg = "图片上传失败"
  41. br.ErrMsg = "图片上传失败,不支持的文件格式"
  42. return
  43. }
  44. defer f.Close()
  45. ext := path.Ext(h.Filename)
  46. size, err := strconv.Atoi(utils.UPLOAD_IMG_SIZE)
  47. if err != nil {
  48. size = 15
  49. }
  50. if h.Size > 1024*int64(size) {
  51. br.Msg = fmt.Sprintf("头像文件大小不能超过%dK", size)
  52. return
  53. }
  54. dateDir := time.Now().Format(time.DateOnly)
  55. uploadDir := utils.STATIC_DIR + "ht/analyst/" + dateDir
  56. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  57. if err != nil {
  58. br.Msg = "存储目录创建失败"
  59. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  60. return
  61. }
  62. randStr := utils.GetRandStringNoSpecialChar(28)
  63. fileName := randStr + ext
  64. fpath := uploadDir + "/" + fileName
  65. err = this.SaveToFile("File", fpath)
  66. if err != nil {
  67. br.Msg = "头像上传失败"
  68. br.ErrMsg = "头像上传失败,Err:" + err.Error()
  69. return
  70. }
  71. thumbnailPath, err := utils.ImageResize(fpath, 1200)
  72. if err != nil {
  73. br.Msg = "头像上传失败"
  74. br.ErrMsg = "头像上传失败,Err:" + err.Error()
  75. return
  76. }
  77. ossUploadDir := utils.RESOURCE_DIR + "analyst/"
  78. saveOssPath := ossUploadDir + time.Now().Format("200601/20060102/")
  79. imgName := utils.GetRandStringNoSpecialChar(28)
  80. saveOssPath += imgName
  81. defer func() {
  82. _ = os.Remove(fpath)
  83. _ = os.Remove(thumbnailPath)
  84. }()
  85. ossClient := oss.NewOssClient()
  86. if ossClient == nil {
  87. br.Msg = "头像上传失败"
  88. br.ErrMsg = "初始化OSS服务失败"
  89. return
  90. }
  91. //新增上传缩略图
  92. thumbnailUrl, err := ossClient.UploadFile("", thumbnailPath, saveOssPath+"_thumbnail")
  93. if err != nil {
  94. br.Msg = "头像上传失败"
  95. br.ErrMsg = "头像上传失败,Err:" + err.Error()
  96. return
  97. }
  98. imgUrl, err := ossClient.UploadFile("", fpath, saveOssPath)
  99. if err != nil {
  100. br.Msg = "头像上传失败"
  101. br.ErrMsg = "头像上传失败,Err:" + err.Error()
  102. return
  103. }
  104. base := path.Base(h.Filename)
  105. thumbnailBase := path.Base(h.Filename + "_thumbnail")
  106. resp := new(response.HeadImageUploadResp)
  107. resp.Url = thumbnailUrl
  108. resp.OriginUrl = imgUrl
  109. resp.FileName = thumbnailBase
  110. resp.OriginFileName = base
  111. br.Data = resp
  112. br.Msg = "上传成功"
  113. br.Ret = 200
  114. br.Success = true
  115. }
  116. // GetAnalystByName
  117. // @Title 查询研究员信息
  118. // @Description 查询研究员信息
  119. // @Param File query file true "文件"
  120. // @Success 200 {object} models.ReportAuthorResp
  121. // @router /analyst [get]
  122. func (this *AnalystController) GetAnalystByName(AnalystName string) {
  123. br := new(models.BaseResponse).Init()
  124. defer func() {
  125. this.Data["json"] = br
  126. this.ServeJSON()
  127. }()
  128. analyst, err := models.GetAnalystByName(AnalystName)
  129. if err != nil {
  130. br.Msg = "获取失败"
  131. br.ErrMsg = "获取失败,Err:" + err.Error()
  132. return
  133. }
  134. br.Ret = 200
  135. br.Success = true
  136. br.Msg = "获取成功"
  137. br.Data = analyst.ToView()
  138. }
  139. // List
  140. // @Title 列表
  141. // @Description 列表
  142. // @Param File query file true "文件"
  143. // @Success 200 {object} models.ReportAuthorResp
  144. // @router /list [get]
  145. func (this *AnalystController) List() {
  146. br := new(models.BaseResponse).Init()
  147. defer func() {
  148. this.Data["json"] = br
  149. this.ServeJSON()
  150. }()
  151. pageSize, _ := this.GetInt("PageSize")
  152. currentIndex, _ := this.GetInt("CurrentIndex")
  153. KeyWord := this.GetString("KeyWord")
  154. var startSize int
  155. var condition string
  156. if pageSize <= 0 {
  157. pageSize = utils.PageSize20
  158. }
  159. if currentIndex <= 0 {
  160. currentIndex = 1
  161. }
  162. if KeyWord != "" {
  163. condition += " AND name like '%" + KeyWord + "%'"
  164. }
  165. startSize = utils.StartIndex(currentIndex, pageSize)
  166. total, err := models.GetAnalystCount(condition)
  167. if err != nil {
  168. br.Msg = "获取失败"
  169. br.ErrMsg = "获取失败,Err:" + err.Error()
  170. return
  171. }
  172. list, err := models.GetAnalystList(condition, startSize, pageSize)
  173. if err != nil {
  174. br.Msg = "获取失败"
  175. br.ErrMsg = "获取失败,Err:" + err.Error()
  176. return
  177. }
  178. analystList := make([]*models.AnalystView, 0)
  179. for _, v := range list {
  180. analystList = append(analystList, &models.AnalystView{
  181. Id: v.Id,
  182. Name: v.Name,
  183. HeadImgURL: v.HeadImgUrl,
  184. HeadOriginImgUrl: v.HeadOriginImgUrl,
  185. Position: v.Position,
  186. InvestmentCertificate: v.InvestmentCertificate,
  187. ProfessionalCertificate: v.ProfessionalCertificate,
  188. Introduction: v.Introduction,
  189. CreatedTime: v.CreatedTime.Format(time.DateTime),
  190. })
  191. }
  192. page := paging.GetPaging(currentIndex, pageSize, total)
  193. resp := new(response.AnalystListResp)
  194. resp.List = analystList
  195. resp.Paging = page
  196. br.Ret = 200
  197. br.Success = true
  198. br.Msg = "获取成功"
  199. br.Data = resp
  200. }
  201. // edit
  202. // @Title 编辑研究员
  203. // @Description 编辑研究员
  204. // @Param request body request.AnalystEditReq true "type json string"
  205. // @Success 200 string 编辑成功
  206. // @router /edit [post]
  207. func (this *AnalystController) Edit() {
  208. br := new(models.BaseResponse).Init()
  209. defer func() {
  210. this.Data["json"] = br
  211. this.ServeJSON()
  212. }()
  213. var req request.AnalystEditReq
  214. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  215. if err != nil {
  216. br.Msg = "参数解析异常!"
  217. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  218. return
  219. }
  220. if req.Id <= 0 {
  221. br.Msg = "请选择用户"
  222. return
  223. }
  224. if req.HeadImgUrl == "" {
  225. br.Msg = "请输入头像地址"
  226. return
  227. }
  228. req.Introduction = strings.TrimSpace(req.Introduction)
  229. analyst, err := models.GetAnalystById(req.Id)
  230. if err != nil {
  231. if err.Error() == utils.ErrNoRow() {
  232. br.Msg = "用户不存在或已删除,请重新刷新页面"
  233. br.ErrMsg = "用户不存在或已删除,请重新刷新页面,Err:" + err.Error()
  234. return
  235. }
  236. br.Msg = "编辑用户失败"
  237. br.ErrMsg = "编辑用户失败, 系统错误,Err:" + err.Error()
  238. return
  239. }
  240. if req.HeadImgUrl != `` {
  241. analyst.HeadImgUrl = req.HeadImgUrl
  242. }
  243. analyst.Introduction = req.Introduction
  244. analyst.Position = req.Position
  245. analyst.HeadOriginImgUrl = req.HeadOriginImgUrl
  246. analyst.ProfessionalCertificate = req.ProfessionalCertificate
  247. analyst.InvestmentCertificate = req.InvestmentCertificate
  248. analyst.UpdatedTime = time.Now()
  249. err = analyst.Update([]string{"HeadImgURL", "HeadOriginImgURL", "Introduction", "Position", "InvestmentCertificate", "ProfessionalCertificate", "UpdatedTime"})
  250. if err != nil {
  251. br.Msg = "编辑失败"
  252. br.ErrMsg = "编辑失败, 系统错误,Err:" + err.Error()
  253. return
  254. }
  255. br.Msg = "编辑成功"
  256. br.Success = true
  257. br.Ret = 200
  258. }