123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- package controllers
- import (
- "encoding/json"
- "hongze/hongze_clpt/models"
- "hongze/hongze_clpt/services"
- "hongze/hongze_clpt/utils"
- "os"
- "path"
- "time"
- )
- //资源管理-图片上传,合同上传等
- type ResourceController struct {
- BaseAuthController
- }
- type BaseResourceController struct {
- BaseCommonController
- }
- // @Title 获取短信验证码
- // @Description 获取短信验证码接口
- // @Param request body models.MsgCodeResp true "type json string"
- // @Success Ret=200 获取成功
- // @router /getSmsCode [post]
- func (this *BaseResourceController) GetSmsCode() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.MsgCodeResp
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- mobile := req.Mobile
- if mobile == "" {
- br.Msg = "请输入手机号"
- return
- }
- areaNum := req.AreaNum
- msgCode := utils.GetRandDigit(4)
- var result bool
- if areaNum == "86" || areaNum == "" || areaNum == "0" {
- result = services.SendSmsCode(mobile, msgCode)
- } else {
- result = services.SendSmsCodeGj(mobile, msgCode, areaNum)
- }
- //发送成功
- if result {
- item := new(models.MsgCode)
- item.OpenId = ""
- item.Code = msgCode
- item.Mobile = mobile
- item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
- item.Enabled = 1
- item.CreatedTime = time.Now()
- err := models.AddMsgCode(item)
- if err != nil {
- br.Msg = "发送失败"
- br.ErrMsg = "发送失败,Err:" + err.Error()
- return
- }
- br.Msg = "发送成功"
- } else {
- br.Msg = "发送失败"
- }
- br.Ret = 200
- br.Success = true
- }
- // @Title 图片上传
- // @Description 图片上传接口
- // @Param file query file true "文件"
- // @Success 200 新增成功
- // @router /image/upload [post]
- func (this *ResourceController) Upload() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- f, h, err := this.GetFile("file")
- if err != nil {
- br.Msg = "获取资源信息失败"
- br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
- return
- }
- ext := path.Ext(h.Filename)
- dateDir := time.Now().Format("20060102")
- uploadDir := "static/temp/" + dateDir
- err = os.MkdirAll(uploadDir, 777)
- if err != nil {
- br.Msg = "存储目录创建失败"
- br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
- return
- }
- randStr := utils.GetRandStringNoSpecialChar(28)
- fileName := randStr + ext
- fpath := uploadDir + "/" + fileName
- defer f.Close() //关闭上传文件
- err = this.SaveToFile("file", fpath)
- if err != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败,Err:" + err.Error()
- return
- }
- savePath := uploadDir + time.Now().Format("200601/20060102/")
- savePath += fileName
- //上传到阿里云
- err = services.UploadFileToAliyun("", fpath, savePath)
- if err != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败,Err:" + err.Error()
- return
- }
- fileHost := "https://hzstatic.hzinsights.com/"
- resourceUrl := fileHost + savePath
- defer func() {
- os.Remove(fpath)
- }()
- item := new(models.Resource)
- item.ResourceUrl = resourceUrl
- item.ResourceType = 1
- item.CreateTime = time.Now()
- newId, err := models.AddResource(item)
- if err != nil {
- br.Msg = "资源上传失败"
- br.ErrMsg = "资源上传失败,Err:" + err.Error()
- return
- }
- resp := new(models.ResourceResp)
- resp.Id = newId
- resp.ResourceUrl = resourceUrl
- br.Msg = "上传成功"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- return
- }
- // @Title 文件上传
- // @Description 文件上传接口
- // @Param file query file true "文件"
- // @Success 200 新增成功
- // @router /file/upload [post]
- func (this *ResourceController) FileUpload() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- f, h, err := this.GetFile("file")
- if err != nil {
- br.Msg = "获取资源信息失败"
- br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
- return
- }
- ext := path.Ext(h.Filename)
- dateDir := time.Now().Format("20060102")
- uploadDir := "static/temp/" + dateDir
- err = os.MkdirAll(uploadDir, 777)
- if err != nil {
- br.Msg = "存储目录创建失败"
- br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
- return
- }
- randStr := utils.GetRandStringNoSpecialChar(28)
- fileName := randStr + ext
- fpath := uploadDir + "/" + fileName
- defer f.Close() //关闭上传文件
- err = this.SaveToFile("file", fpath)
- if err != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败,Err:" + err.Error()
- return
- }
- savePath := uploadDir + time.Now().Format("200601/20060102/")
- savePath += fileName
- //上传到阿里云
- err = services.UploadFileToAliyun("", fpath, savePath)
- if err != nil {
- br.Msg = "文件上传失败"
- br.ErrMsg = "文件上传失败,Err:" + err.Error()
- return
- }
- fileHost := "https://hzstatic.hzinsights.com/"
- resourceUrl := fileHost + savePath
- defer func() {
- os.Remove(fpath)
- }()
- item := new(models.Resource)
- item.ResourceUrl = resourceUrl
- item.ResourceType = 1
- item.CreateTime = time.Now()
- newId, err := models.AddResource(item)
- if err != nil {
- br.Msg = "资源上传失败"
- br.ErrMsg = "资源上传失败,Err:" + err.Error()
- return
- }
- resp := new(models.FileResourceResp)
- resp.Id = newId
- resp.ResourceUrl = resourceUrl
- resp.ResourceName = h.Filename
- br.Msg = "上传成功"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- return
- }
|