resource.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_web_mfyx/models"
  5. "hongze/hongze_web_mfyx/services"
  6. "hongze/hongze_web_mfyx/utils"
  7. "os"
  8. "path"
  9. "time"
  10. )
  11. // 资源管理-图片上传,合同上传等
  12. type ResourceController struct {
  13. BaseAuthController
  14. }
  15. type BaseResourceController struct {
  16. BaseCommonController
  17. }
  18. // @Title 获取短信验证码
  19. // @Description 获取短信验证码接口
  20. // @Param request body models.MsgCodeResp true "type json string"
  21. // @Success Ret=200 获取成功
  22. // @router /getSmsCode [post]
  23. func (this *BaseResourceController) GetSmsCode() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. var req models.MsgCodeResp
  30. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  31. if err != nil {
  32. br.Msg = "参数解析异常!"
  33. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  34. return
  35. }
  36. mobile := req.Mobile
  37. if mobile == "" {
  38. br.Msg = "请输入手机号"
  39. return
  40. }
  41. areaNum := req.AreaNum
  42. msgCode := utils.GetRandDigit(4)
  43. var result bool
  44. if areaNum == "86" || areaNum == "" || areaNum == "0" {
  45. result = services.SendSmsCode(mobile, msgCode)
  46. } else {
  47. result = services.SendSmsCodeGj(mobile, msgCode, areaNum)
  48. }
  49. //发送成功
  50. if result {
  51. item := new(models.MsgCode)
  52. item.OpenId = ""
  53. item.Code = msgCode
  54. item.Mobile = mobile
  55. item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
  56. item.Enabled = 1
  57. item.CreatedTime = time.Now()
  58. err := models.AddMsgCode(item)
  59. if err != nil {
  60. br.Msg = "发送失败"
  61. br.ErrMsg = "发送失败,Err:" + err.Error()
  62. return
  63. }
  64. br.Msg = "发送成功"
  65. } else {
  66. br.Msg = "发送失败"
  67. }
  68. br.Ret = 200
  69. br.Success = true
  70. }
  71. // @Title 图片上传
  72. // @Description 图片上传接口
  73. // @Param file query file true "文件"
  74. // @Success 200 新增成功
  75. // @router /image/upload [post]
  76. func (this *ResourceController) Upload() {
  77. br := new(models.BaseResponse).Init()
  78. defer func() {
  79. this.Data["json"] = br
  80. this.ServeJSON()
  81. }()
  82. f, h, err := this.GetFile("file")
  83. if err != nil {
  84. br.Msg = "获取资源信息失败"
  85. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  86. return
  87. }
  88. ext := path.Ext(h.Filename)
  89. dateDir := time.Now().Format("20060102")
  90. uploadDir := "static/temp/" + dateDir
  91. err = os.MkdirAll(uploadDir, 777)
  92. if err != nil {
  93. br.Msg = "存储目录创建失败"
  94. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  95. return
  96. }
  97. randStr := utils.GetRandStringNoSpecialChar(28)
  98. fileName := randStr + ext
  99. fpath := uploadDir + "/" + fileName
  100. defer f.Close() //关闭上传文件
  101. err = this.SaveToFile("file", fpath)
  102. if err != nil {
  103. br.Msg = "文件上传失败"
  104. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  105. return
  106. }
  107. savePath := uploadDir + time.Now().Format("200601/20060102/")
  108. savePath += fileName
  109. //上传到阿里云
  110. err = services.UploadFileToAliyun("", fpath, savePath)
  111. if err != nil {
  112. br.Msg = "文件上传失败"
  113. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  114. return
  115. }
  116. fileHost := "https://hzstatic.hzinsights.com/"
  117. resourceUrl := fileHost + savePath
  118. defer func() {
  119. os.Remove(fpath)
  120. }()
  121. item := new(models.Resource)
  122. item.ResourceUrl = resourceUrl
  123. item.ResourceType = 1
  124. item.CreateTime = time.Now()
  125. newId, err := models.AddResource(item)
  126. if err != nil {
  127. br.Msg = "资源上传失败"
  128. br.ErrMsg = "资源上传失败,Err:" + err.Error()
  129. return
  130. }
  131. resp := new(models.ResourceResp)
  132. resp.Id = newId
  133. resp.ResourceUrl = resourceUrl
  134. br.Msg = "上传成功"
  135. br.Ret = 200
  136. br.Success = true
  137. br.Data = resp
  138. return
  139. }
  140. // @Title 文件上传
  141. // @Description 文件上传接口
  142. // @Param file query file true "文件"
  143. // @Success 200 新增成功
  144. // @router /file/upload [post]
  145. func (this *ResourceController) FileUpload() {
  146. br := new(models.BaseResponse).Init()
  147. defer func() {
  148. this.Data["json"] = br
  149. this.ServeJSON()
  150. }()
  151. f, h, err := this.GetFile("file")
  152. if err != nil {
  153. br.Msg = "获取资源信息失败"
  154. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  155. return
  156. }
  157. ext := path.Ext(h.Filename)
  158. dateDir := time.Now().Format("20060102")
  159. uploadDir := "static/temp/" + dateDir
  160. err = os.MkdirAll(uploadDir, 777)
  161. if err != nil {
  162. br.Msg = "存储目录创建失败"
  163. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  164. return
  165. }
  166. randStr := utils.GetRandStringNoSpecialChar(28)
  167. fileName := randStr + ext
  168. fpath := uploadDir + "/" + fileName
  169. defer f.Close() //关闭上传文件
  170. err = this.SaveToFile("file", fpath)
  171. if err != nil {
  172. br.Msg = "文件上传失败"
  173. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  174. return
  175. }
  176. savePath := uploadDir + time.Now().Format("200601/20060102/")
  177. savePath += fileName
  178. //上传到阿里云
  179. err = services.UploadFileToAliyun("", fpath, savePath)
  180. if err != nil {
  181. br.Msg = "文件上传失败"
  182. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  183. return
  184. }
  185. fileHost := "https://hzstatic.hzinsights.com/"
  186. resourceUrl := fileHost + savePath
  187. defer func() {
  188. os.Remove(fpath)
  189. }()
  190. item := new(models.Resource)
  191. item.ResourceUrl = resourceUrl
  192. item.ResourceType = 1
  193. item.CreateTime = time.Now()
  194. newId, err := models.AddResource(item)
  195. if err != nil {
  196. br.Msg = "资源上传失败"
  197. br.ErrMsg = "资源上传失败,Err:" + err.Error()
  198. return
  199. }
  200. resp := new(models.FileResourceResp)
  201. resp.Id = newId
  202. resp.ResourceUrl = resourceUrl
  203. resp.ResourceName = h.Filename
  204. br.Msg = "上传成功"
  205. br.Ret = 200
  206. br.Success = true
  207. br.Data = resp
  208. return
  209. }