material.go 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  1. package material
  2. import (
  3. "archive/zip"
  4. "encoding/json"
  5. "eta/eta_api/controllers"
  6. "eta/eta_api/models"
  7. "eta/eta_api/models/material"
  8. "eta/eta_api/services"
  9. materialService "eta/eta_api/services/material"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "github.com/h2non/filetype"
  13. "github.com/rdlucklib/rdluck_tools/http"
  14. "github.com/rdlucklib/rdluck_tools/paging"
  15. "io/ioutil"
  16. "os"
  17. "path"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. // MaterialController 逻辑导图
  23. type MaterialController struct {
  24. controllers.BaseAuthController
  25. }
  26. // ClassifyMaterialItems
  27. // @Title 获取所有素材库分类接口-包含素材库
  28. // @Description 获取所有素材库分类接口-包含素材库
  29. // @Param IsShowMe query bool true "是否只看我的,true、false"
  30. // @Success 200 {object} data_manage.ChartClassifyListResp
  31. // @router /classify/materialList [get]
  32. func (this *MaterialController) ClassifyMaterialItems() {
  33. br := new(models.BaseResponse).Init()
  34. defer func() {
  35. this.Data["json"] = br
  36. this.ServeJSON()
  37. }()
  38. resp := new(material.MaterialClassifyListResp)
  39. MaterialClassifyId, _ := this.GetInt("MaterialClassifyId")
  40. isShowMe, _ := this.GetBool("IsShowMe")
  41. if isShowMe {
  42. errMsg, err := materialService.GetMaterialClassifyListForMe(*this.SysUser, resp, MaterialClassifyId)
  43. if err != nil {
  44. br.Msg = errMsg
  45. br.ErrMsg = err.Error()
  46. return
  47. }
  48. // 移除没有权限的图表
  49. br.Ret = 200
  50. br.Success = true
  51. br.Msg = "获取成功"
  52. br.Data = resp
  53. fmt.Println("source my classify")
  54. return
  55. }
  56. rootList, err := material.GetMaterialClassifyAndInfoByParentId(MaterialClassifyId)
  57. if err != nil && err.Error() != utils.ErrNoRow() {
  58. br.Msg = "获取失败"
  59. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  60. return
  61. }
  62. classifyAll, err := material.GetMaterialClassifyAndInfoByParentId(MaterialClassifyId)
  63. if err != nil && err.Error() != utils.ErrNoRow() {
  64. br.Msg = "获取失败"
  65. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  66. return
  67. }
  68. nodeAll := make([]*material.MaterialClassifyItems, 0)
  69. for k := range rootList {
  70. rootNode := rootList[k]
  71. materialService.MaterialClassifyItemsMakeTreeV2(this.SysUser, classifyAll, rootNode)
  72. nodeAll = append(nodeAll, rootNode)
  73. }
  74. //newAll := materialService.MaterialItemsMakeTree(nodeAll, sandListMap, MaterialClassifyId)
  75. resp.AllNodes = nodeAll
  76. br.Ret = 200
  77. br.Success = true
  78. br.Msg = "获取成功"
  79. br.Data = resp
  80. }
  81. // AddMaterialClassify
  82. // @Title 新增素材库分类
  83. // @Description 新增材库分类接口
  84. // @Param request body data_manage.AddChartClassifyReq true "type json string"
  85. // @Success 200 Ret=200 保存成功
  86. // @router /classify/add [post]
  87. func (this *MaterialController) AddMaterialClassify() {
  88. br := new(models.BaseResponse).Init()
  89. defer func() {
  90. this.Data["json"] = br
  91. this.ServeJSON()
  92. }()
  93. var req material.AddMaterialClassifyReq
  94. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  95. if err != nil {
  96. br.Msg = "参数解析异常!"
  97. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  98. return
  99. }
  100. if req.ClassifyName == "" {
  101. br.Msg = "请输入分类名称"
  102. br.IsSendEmail = false
  103. return
  104. }
  105. if req.ParentId < 0 {
  106. br.Msg = "参数错误"
  107. br.IsSendEmail = false
  108. return
  109. }
  110. level := 1
  111. levelPath := ""
  112. if req.ParentId > 0 {
  113. //查找父级分类
  114. parentClassify, e := material.GetMaterialClassifyById(req.ParentId)
  115. if e != nil {
  116. br.Msg = "获取父级分类失败"
  117. br.ErrMsg = "获取父级分类失败,Err:" + e.Error()
  118. return
  119. }
  120. level = parentClassify.Level + 1
  121. levelPath = parentClassify.LevelPath
  122. }
  123. var count int
  124. switch this.Lang {
  125. case utils.LANG_EN:
  126. count, err = material.GetMaterialClassifyNameEnCount(req.ClassifyName, req.ParentId)
  127. default:
  128. count, err = material.GetMaterialClassifyNameCount(req.ClassifyName, req.ParentId)
  129. }
  130. if err != nil {
  131. br.Msg = "判断名称是否已存在失败"
  132. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  133. return
  134. }
  135. if count > 0 {
  136. br.Msg = "分类名称已存在,请重新输入"
  137. br.IsSendEmail = false
  138. return
  139. }
  140. //获取该层级下最大的排序数
  141. classify := new(material.MaterialClassify)
  142. maxSort, _ := material.GetMaterialClassifyMaxSort(req.ParentId)
  143. classify.ParentId = req.ParentId
  144. classify.ClassifyName = req.ClassifyName
  145. classify.ClassifyNameEn = req.ClassifyName
  146. classify.CreateTime = time.Now()
  147. classify.ModifyTime = time.Now()
  148. classify.SysUserId = this.SysUser.AdminId
  149. classify.SysUserRealName = this.SysUser.RealName
  150. classify.Level = level
  151. classify.Sort = maxSort + 1
  152. classifyId, e := material.AddMaterialClassify(classify)
  153. if e != nil {
  154. br.Msg = "保存分类失败"
  155. br.ErrMsg = "保存分类失败,Err:" + e.Error()
  156. return
  157. }
  158. if req.ParentId > 0 {
  159. levelPath = fmt.Sprintf("%s,%d", levelPath, classifyId)
  160. } else {
  161. levelPath = fmt.Sprintf("%d", classifyId)
  162. }
  163. classify.ClassifyId = int(classifyId)
  164. classify.LevelPath = levelPath
  165. e = classify.Update([]string{"LevelPath"})
  166. if e != nil {
  167. br.Msg = "保存分类失败"
  168. br.ErrMsg = "保存分类失败,Err:" + e.Error()
  169. return
  170. }
  171. br.Ret = 200
  172. br.Msg = "保存成功"
  173. br.Success = true
  174. }
  175. // EditMaterialClassify
  176. // @Title 修改素材库分类
  177. // @Description 修改素材库分类接口
  178. // @Param request body data_manage.EditChartClassifyReq true "type json string"
  179. // @Success 200 Ret=200 修改成功
  180. // @router /classify/edit [post]
  181. func (this *MaterialController) EditMaterialClassify() {
  182. br := new(models.BaseResponse).Init()
  183. defer func() {
  184. this.Data["json"] = br
  185. this.ServeJSON()
  186. }()
  187. var req material.EditMaterialClassifyReq
  188. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  189. if err != nil {
  190. br.Msg = "参数解析异常!"
  191. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  192. return
  193. }
  194. if req.ClassifyName == "" {
  195. br.Msg = "请输入分类名称"
  196. br.IsSendEmail = false
  197. return
  198. }
  199. if req.ClassifyId <= 0 {
  200. br.Msg = "参数错误"
  201. br.IsSendEmail = false
  202. return
  203. }
  204. // 只允许修改分类名称
  205. item, err := material.GetMaterialClassifyById(req.ClassifyId)
  206. if err != nil {
  207. if err.Error() == utils.ErrNoRow() {
  208. br.Msg = "分类不存在"
  209. return
  210. }
  211. br.Msg = "保存失败"
  212. br.Msg = "获取分类信息失败,Err:" + err.Error()
  213. return
  214. }
  215. var count int
  216. updateStr := make([]string, 0)
  217. switch this.Lang {
  218. case utils.LANG_EN:
  219. count, err = material.GetMaterialClassifyNameEnNotSelfCount(req.ClassifyId, req.ClassifyName, item.ParentId)
  220. item.ClassifyNameEn = req.ClassifyName
  221. updateStr = append(updateStr, "ClassifyNameEn")
  222. default:
  223. count, err = material.GetMaterialClassifyNameNotSelfCount(req.ClassifyId, req.ClassifyName, item.ParentId)
  224. item.ClassifyName = req.ClassifyName
  225. updateStr = append(updateStr, "ClassifyName")
  226. }
  227. if err != nil {
  228. br.Msg = "判断名称是否已存在失败"
  229. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  230. return
  231. }
  232. if count > 0 {
  233. br.Msg = "分类名称已存在,请重新输入"
  234. br.IsSendEmail = false
  235. return
  236. }
  237. item.ModifyTime = time.Now()
  238. updateStr = append(updateStr, "ModifyTime")
  239. err = item.Update(updateStr)
  240. if err != nil {
  241. br.Msg = "保存分类失败"
  242. br.ErrMsg = "保存分类失败,Err:" + err.Error()
  243. return
  244. }
  245. //todo 测试更新子集的levelPath
  246. /*levelPath := ""
  247. oldLevelPath := item.LevelPath
  248. if item.ParentId > 0 {
  249. //查找父级分类
  250. parentClassify, e := material.GetMaterialClassifyById(item.ParentId)
  251. if e != nil {
  252. br.Msg = "获取父级分类失败"
  253. br.ErrMsg = "获取父级分类失败,Err:" + e.Error()
  254. return
  255. }
  256. levelPath = fmt.Sprintf("%s,%d", parentClassify.LevelPath, item.ClassifyId)
  257. tmpList, e := material.GetMaterialClassifyByLevelPath(oldLevelPath)
  258. if e != nil {
  259. br.Msg = "保存分类失败"
  260. br.ErrMsg = "保存分类失败,Err:" + e.Error()
  261. return
  262. }
  263. // 把原先的父级levePath,替换成最新的父级序列
  264. for _, tmp := range tmpList {
  265. tmp.LevelPath = strings.Replace(tmp.LevelPath, oldLevelPath, levelPath, -1)
  266. tmp.ModifyTime = time.Now()
  267. e = tmp.Update([]string{"LevelPath", "ModifyTime"})
  268. if e != nil {
  269. br.Msg = "保存分类失败"
  270. br.ErrMsg = "保存分类失败,Err:" + e.Error()
  271. return
  272. }
  273. }
  274. } else {
  275. // 只有更改了父级才需要更新levelPath
  276. }*/
  277. br.Ret = 200
  278. br.Msg = "保存成功"
  279. br.Success = true
  280. br.IsAddLog = true
  281. }
  282. // DeleteMaterialClassifyCheck
  283. // @Title 删除素材库检测接口
  284. // @Description 删除素材库检测接口
  285. // @Param request body data_manage.ChartClassifyDeleteCheckResp true "type json string"
  286. // @Success 200 Ret=200 检测成功
  287. // @router /classify/del/check [post]
  288. func (this *MaterialController) DeleteMaterialClassifyCheck() {
  289. br := new(models.BaseResponse).Init()
  290. defer func() {
  291. this.Data["json"] = br
  292. this.ServeJSON()
  293. }()
  294. var req material.MaterialClassifyDeleteCheckReq
  295. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  296. if err != nil {
  297. br.Msg = "参数解析异常!"
  298. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  299. return
  300. }
  301. if req.ClassifyId < 0 {
  302. br.Msg = "参数错误"
  303. br.IsSendEmail = false
  304. return
  305. }
  306. var deleteStatus int
  307. var tipsMsg string
  308. //删除分类
  309. // 查询当前的分类
  310. classifyInfo, err := material.GetMaterialClassifyById(req.ClassifyId)
  311. if err != nil {
  312. br.Msg = "分类不存在"
  313. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  314. return
  315. }
  316. classifyIds := strings.Split(classifyInfo.LevelPath, ",")
  317. if len(classifyIds) > 0 {
  318. //判断素材库分类下,是否含有素材库
  319. count, e := material.GetMaterialInfoCountByClassifyIds(classifyIds)
  320. if e != nil {
  321. br.Msg = "删除失败"
  322. br.ErrMsg = "分类下是否含有指标失败,Err:" + e.Error()
  323. return
  324. }
  325. if count > 0 {
  326. deleteStatus = 1
  327. tipsMsg = "该分类下关联素材库不可删除"
  328. } else {
  329. if len(classifyIds) > 1 {
  330. deleteStatus = 2
  331. tipsMsg = "确认删除当前目录及包含的子目录吗"
  332. }
  333. }
  334. }
  335. if deleteStatus == 0 {
  336. tipsMsg = "可删除,进行删除操作"
  337. }
  338. resp := new(material.MaterialClassifyDeleteCheckResp)
  339. resp.DeleteStatus = deleteStatus
  340. resp.TipsMsg = tipsMsg
  341. br.Ret = 200
  342. br.Msg = "检测成功"
  343. br.Success = true
  344. br.Data = resp
  345. }
  346. // DeleteMaterialClassify
  347. // @Title 删除素材库分类/素材库
  348. // @Description 删除素材库分类/素材库接口
  349. // @Param request body data_manage.DeleteChartClassifyReq true "type json string"
  350. // @Success 200 Ret=200 删除成功
  351. // @router /classify/del [post]
  352. func (this *MaterialController) DeleteMaterialClassify() {
  353. br := new(models.BaseResponse).Init()
  354. defer func() {
  355. this.Data["json"] = br
  356. this.ServeJSON()
  357. }()
  358. sysUser := this.SysUser
  359. if sysUser == nil {
  360. br.Msg = "请登录"
  361. br.ErrMsg = "请登录,SysUser Is Empty"
  362. br.Ret = 408
  363. return
  364. }
  365. var req material.DeleteMaterialClassifyReq
  366. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  367. if err != nil {
  368. br.Msg = "参数解析异常!"
  369. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  370. return
  371. }
  372. if req.ClassifyId < 0 {
  373. br.Msg = "参数错误"
  374. br.IsSendEmail = false
  375. return
  376. }
  377. // 查询当前的分类
  378. classifyInfo, err := material.GetMaterialClassifyById(req.ClassifyId)
  379. if err != nil {
  380. br.Msg = "分类不存在"
  381. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  382. return
  383. }
  384. classifyIds := strings.Split(classifyInfo.LevelPath, ",")
  385. if len(classifyIds) > 0 {
  386. //判断素材库分类下,是否含有素材库
  387. count, e := material.GetMaterialInfoCountByClassifyIds(classifyIds)
  388. if e != nil {
  389. br.Msg = "删除失败"
  390. br.ErrMsg = "分类下是否含有指标失败,Err:" + e.Error()
  391. return
  392. }
  393. if count > 0 {
  394. br.Msg = "该目录下存在关联指标,不可删除"
  395. br.IsSendEmail = false
  396. return
  397. }
  398. err = material.DeleteMaterialClassify(classifyIds)
  399. if err != nil {
  400. br.Msg = "删除失败"
  401. br.ErrMsg = "删除失败,Err:" + err.Error()
  402. return
  403. }
  404. }
  405. //删除素材库
  406. /*if req.MaterialId > 0 {
  407. materialInfo, err := material.GetMaterialById(req.MaterialId)
  408. if err != nil {
  409. if err.Error() == utils.ErrNoRow() {
  410. br.Msg = "素材库已删除,请刷新页面"
  411. br.ErrMsg = "指标不存在,Err:" + err.Error()
  412. return
  413. } else {
  414. br.Msg = "删除失败"
  415. br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error()
  416. return
  417. }
  418. }
  419. if materialInfo == nil {
  420. br.Msg = "素材库已删除,请刷新页面"
  421. return
  422. }
  423. err = materialService.DeleteMaterial(req.MaterialId)
  424. if err != nil {
  425. br.Msg = err.Error()
  426. return
  427. }
  428. }*/
  429. br.Ret = 200
  430. br.Msg = "删除成功"
  431. br.Success = true
  432. br.IsAddLog = true
  433. }
  434. // ClassifyMove
  435. // @Title 素材库分类移动接口
  436. // @Description 素材库分类移动接口
  437. // @Success 200 {object} data_manage.MoveChartClassifyReq
  438. // @router /classify/move [post]
  439. func (this *MaterialController) ClassifyMove() {
  440. br := new(models.BaseResponse).Init()
  441. defer func() {
  442. this.Data["json"] = br
  443. this.ServeJSON()
  444. }()
  445. sysUser := this.SysUser
  446. if sysUser == nil {
  447. br.Msg = "请登录"
  448. br.ErrMsg = "请登录,SysUser Is Empty"
  449. br.Ret = 408
  450. return
  451. }
  452. var req material.MoveMaterialClassifyReq
  453. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  454. if err != nil {
  455. br.Msg = "参数解析异常!"
  456. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  457. return
  458. }
  459. if req.ClassifyId <= 0 {
  460. br.Msg = "参数错误"
  461. br.ErrMsg = "分类id小于等于0"
  462. return
  463. }
  464. //判断分类是否存在
  465. materialClassifyInfo, err := material.GetMaterialClassifyById(req.ClassifyId)
  466. if err != nil {
  467. if err.Error() == utils.ErrNoRow() {
  468. br.Msg = "分类不存在,请刷新页面"
  469. br.ErrMsg = "分类不存在,Err:" + err.Error()
  470. return
  471. }
  472. br.Msg = "移动失败"
  473. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  474. return
  475. }
  476. if materialClassifyInfo.ParentId != req.ParentClassifyId {
  477. count, err := material.GetMaterialClassifyNameNotSelfCount(materialClassifyInfo.ClassifyId, materialClassifyInfo.ClassifyName, materialClassifyInfo.ParentId)
  478. if err != nil {
  479. br.Msg = "判断名称是否已存在失败"
  480. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  481. return
  482. }
  483. if count > 0 {
  484. br.Msg = "移动失败,分类名称已存在"
  485. br.IsSendEmail = false
  486. return
  487. }
  488. }
  489. err, errMsg := materialService.MoveMaterialClassify(materialClassifyInfo, &req)
  490. if err != nil {
  491. br.Msg = errMsg
  492. if errMsg == "" {
  493. br.Msg = "移动失败"
  494. }
  495. br.ErrMsg = err.Error()
  496. }
  497. // todo权限校验
  498. //移动的是分类
  499. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  500. /*updateCol := make([]string, 0)
  501. if MaterialClassifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId != 0 {
  502. parentChartClassifyInfo, err := material.GetMaterialClassifyById(req.ParentClassifyId)
  503. if err != nil {
  504. br.Msg = "移动失败"
  505. br.ErrMsg = "获取上级分类信息失败,Err:" + err.Error()
  506. return
  507. }
  508. MaterialClassifyInfo.ParentId = parentChartClassifyInfo.ClassifyId
  509. MaterialClassifyInfo.Level = parentChartClassifyInfo.Level + 1
  510. MaterialClassifyInfo.ModifyTime = time.Now()
  511. updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  512. } else if MaterialClassifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId == 0 {
  513. //改为一级分类
  514. MaterialClassifyInfo.ParentId = req.ParentClassifyId
  515. MaterialClassifyInfo.Level = 1
  516. MaterialClassifyInfo.ModifyTime = time.Now()
  517. updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  518. }
  519. //如果有传入 上一个兄弟节点分类id
  520. if req.PrevId > 0 {
  521. if req.PrevType == 1 {
  522. //上一个节点是分类
  523. //上一个兄弟节点
  524. prevClassify, err := material.GetMaterialClassifyById(req.PrevId)
  525. if err != nil {
  526. br.Msg = "移动失败"
  527. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  528. return
  529. }
  530. //如果是移动在两个兄弟节点之间
  531. if req.NextId > 0 {
  532. if req.NextType == 1 {
  533. //上一个节点是分类 下一个节点是分类的情况
  534. //下一个兄弟节点
  535. nextClassify, err := material.GetMaterialClassifyById(req.NextId)
  536. if err != nil {
  537. br.Msg = "移动失败"
  538. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  539. return
  540. }
  541. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  542. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == MaterialClassifyInfo.Sort {
  543. //变更兄弟节点的排序
  544. updateSortStr := `sort + 2`
  545. _ = material.UpdateMaterialClassifySortByParentId(prevClassify.ParentId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr)
  546. _ = material.UpdateMaterialSortByClassifyId(prevClassify.ClassifyId, prevClassify.Sort, 0, updateSortStr)
  547. } else {
  548. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  549. if nextClassify.Sort-prevClassify.Sort == 1 {
  550. //变更兄弟节点的排序
  551. updateSortStr := `sort + 1`
  552. _ = material.UpdateMaterialClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
  553. _ = material.UpdateMaterialSortByClassifyId(prevClassify.ClassifyId, prevClassify.Sort, 0, updateSortStr)
  554. }
  555. }
  556. } else {
  557. //上一个节点是分类 下一个节点是素材库的情况
  558. //下一个兄弟节点
  559. nextChartInfo, err := material.GetMaterialById(req.NextId)
  560. if err != nil {
  561. br.Msg = "移动失败"
  562. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  563. return
  564. }
  565. //如果上一个兄弟(分类)与下一个兄弟(素材库)的排序权重是一致的,那么需要将下一个兄弟(素材库)(以及下个兄弟(素材库)的同样排序权重)的排序权重+2,自己变成上一个兄弟(分类)的排序权重+1
  566. if prevClassify.Sort == nextChartInfo.Sort || prevClassify.Sort == MaterialClassifyInfo.Sort {
  567. //变更兄弟节点的排序
  568. updateSortStr := `sort + 2`
  569. _ = material.UpdateMaterialClassifySortByParentId(prevClassify.ParentId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr)
  570. _ = material.UpdateMaterialSortByClassifyId(prevClassify.ClassifyId, prevClassify.Sort, 0, updateSortStr)
  571. } else {
  572. //如果下一个兄弟(素材库)的排序权重正好是上个兄弟节点(分类)的下一层,那么需要再加一层了
  573. if nextChartInfo.Sort-prevClassify.Sort == 1 {
  574. //变更兄弟节点的排序
  575. updateSortStr := `sort + 1`
  576. _ = material.UpdateMaterialClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.ClassifyId, updateSortStr)
  577. _ = material.UpdateMaterialSortByClassifyId(prevClassify.ClassifyId, prevClassify.Sort, 0, updateSortStr)
  578. }
  579. }
  580. }
  581. }
  582. MaterialClassifyInfo.Sort = prevClassify.Sort + 1
  583. MaterialClassifyInfo.ModifyTime = time.Now()
  584. updateCol = append(updateCol, "Sort", "ModifyTime")
  585. } else {
  586. //上一个节点是素材库
  587. prevMaterial, err := material.GetMaterialById(req.PrevId)
  588. if err != nil {
  589. br.Msg = "移动失败"
  590. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  591. return
  592. }
  593. //如果是移动在两个兄弟节点之间
  594. if req.NextId > 0 {
  595. if req.NextType == 1 {
  596. //上一个节点是素材库 下一个节点是分类的情况
  597. //下一个兄弟节点
  598. nextClassify, err := material.GetMaterialClassifyById(req.NextId)
  599. if err != nil {
  600. br.Msg = "移动失败"
  601. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  602. return
  603. }
  604. //如果上一个兄弟(素材库)与下一个兄弟(分类)的排序权重是一致的,那么需要将下一个兄弟(分类)(以及下个兄弟(分类)的同样排序权重)的排序权重+2,自己变成上一个兄弟(素材库)的排序权重+1
  605. if prevMaterial.Sort == nextClassify.Sort || prevMaterial.Sort == MaterialClassifyInfo.Sort {
  606. //变更兄弟节点的排序
  607. updateSortStr := `sort + 2`
  608. _ = material.UpdateMaterialClassifySortByParentId(prevMaterial.ClassifyId, 0, prevMaterial.Sort, updateSortStr)
  609. _ = material.UpdateMaterialSortByClassifyId(prevMaterial.ClassifyId, prevMaterial.Sort, prevMaterial.MaterialId, updateSortStr)
  610. } else {
  611. //如果下一个兄弟(分类)的排序权重正好是上个兄弟(素材库)节点的下一层,那么需要再加一层了
  612. if nextClassify.Sort-prevMaterial.Sort == 1 {
  613. //变更兄弟节点的排序
  614. updateSortStr := `sort + 1`
  615. _ = material.UpdateMaterialClassifySortByParentId(prevMaterial.ClassifyId, 0, prevMaterial.Sort, updateSortStr)
  616. _ = material.UpdateMaterialSortByClassifyId(prevMaterial.ClassifyId, prevMaterial.Sort, prevMaterial.MaterialId, updateSortStr)
  617. }
  618. }
  619. } else {
  620. //上一个节点是素材库 下一个节点是素材库的情况
  621. //下一个兄弟节点
  622. nextChartInfo, err := material.GetMaterialById(req.NextId)
  623. if err != nil {
  624. br.Msg = "移动失败"
  625. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  626. return
  627. }
  628. //如果上一个兄弟(素材库)与下一个兄弟(分类)的排序权重是一致的,那么需要将下一个兄弟(分类)(以及下个兄弟(分类)的同样排序权重)的排序权重+2,自己变成上一个兄弟(素材库)的排序权重+1
  629. if prevMaterial.Sort == nextChartInfo.Sort || prevMaterial.Sort == MaterialClassifyInfo.Sort {
  630. //变更兄弟节点的排序
  631. updateSortStr := `sort + 2`
  632. _ = material.UpdateMaterialClassifySortByParentId(prevMaterial.ClassifyId, 0, prevMaterial.Sort, updateSortStr)
  633. _ = material.UpdateMaterialSortByClassifyId(prevMaterial.ClassifyId, prevMaterial.Sort, prevMaterial.MaterialId, updateSortStr)
  634. } else {
  635. //如果下一个兄弟(分类)的排序权重正好是上个兄弟(素材库)节点的下一层,那么需要再加一层了
  636. if nextChartInfo.Sort-prevMaterial.Sort == 1 {
  637. //变更兄弟节点的排序
  638. updateSortStr := `sort + 1`
  639. _ = material.UpdateMaterialClassifySortByParentId(prevMaterial.ClassifyId, 0, prevMaterial.Sort, updateSortStr)
  640. _ = material.UpdateMaterialSortByClassifyId(prevMaterial.ClassifyId, prevMaterial.Sort, prevMaterial.MaterialId, updateSortStr)
  641. }
  642. }
  643. }
  644. }
  645. MaterialClassifyInfo.Sort = prevMaterial.Sort + 1
  646. MaterialClassifyInfo.ModifyTime = time.Now()
  647. updateCol = append(updateCol, "Sort", "ModifyTime")
  648. }
  649. } else {
  650. firstClassify, err := material.GetFirstMaterialClassifyByParentId(MaterialClassifyInfo.ParentId)
  651. if err != nil && err.Error() != utils.ErrNoRow() {
  652. br.Msg = "移动失败"
  653. br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  654. return
  655. }
  656. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  657. if firstClassify != nil && firstClassify.Sort == 0 {
  658. updateSortStr := ` sort + 1 `
  659. _ = material.UpdateMaterialClassifySortByParentId(firstClassify.ParentId, firstClassify.ClassifyId-1, 0, updateSortStr)
  660. }
  661. MaterialClassifyInfo.Sort = 0 //那就是排在第一位
  662. MaterialClassifyInfo.ModifyTime = time.Now()
  663. updateCol = append(updateCol, "Sort", "ModifyTime")
  664. }
  665. //更新
  666. if len(updateCol) > 0 {
  667. err = MaterialClassifyInfo.Update(updateCol)
  668. if err != nil {
  669. br.Msg = "移动失败"
  670. br.ErrMsg = "修改失败,Err:" + err.Error()
  671. return
  672. }
  673. // todo 记录整个层级的分类ID
  674. }*/
  675. br.Ret = 200
  676. br.Success = true
  677. br.Msg = "移动成功"
  678. }
  679. // List
  680. // @Title 素材列表接口
  681. // @Description 素材列表接口
  682. // @Param PageSize query int true "每页数据条数"
  683. // @Param CurrentIndex query int true "当前页页码,从1开始"
  684. // @Param ClassifyId query int true "分类id"
  685. // @Param Keyword query string true "搜索关键词"
  686. // @Param IsShowMe query bool true "是否只看我的,true、false"
  687. // @Success 200 {object} data_manage.ChartListResp
  688. // @router /list [get]
  689. func (this *MaterialController) List() {
  690. br := new(models.BaseResponse).Init()
  691. defer func() {
  692. this.Data["json"] = br
  693. this.ServeJSON()
  694. }()
  695. sysUser := this.SysUser
  696. if sysUser == nil {
  697. br.Msg = "请登录"
  698. br.ErrMsg = "请登录,SysUser Is Empty"
  699. br.Ret = 408
  700. return
  701. }
  702. classifyId, _ := this.GetInt("ClassifyId")
  703. pageSize, _ := this.GetInt("PageSize")
  704. currentIndex, _ := this.GetInt("CurrentIndex")
  705. keyword := this.GetString("Keyword")
  706. var total int
  707. page := paging.GetPaging(currentIndex, pageSize, total)
  708. var startSize int
  709. if pageSize <= 0 {
  710. pageSize = utils.PageSize20
  711. }
  712. if currentIndex <= 0 {
  713. currentIndex = 1
  714. }
  715. startSize = paging.StartIndex(currentIndex, pageSize)
  716. var condition string
  717. var pars []interface{}
  718. if classifyId <= 0 {
  719. br.Msg = "请选择分类"
  720. return
  721. }
  722. // 查询当前的分类
  723. classifyInfo, err := material.GetMaterialClassifyById(classifyId)
  724. if err != nil {
  725. br.Msg = "分类不存在"
  726. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  727. return
  728. }
  729. // 获取所有子分类
  730. childList, e := material.GetMaterialClassifyByLevelPath(classifyInfo.LevelPath)
  731. if e != nil {
  732. err = fmt.Errorf("保存分类失败,Err:" + e.Error())
  733. return
  734. }
  735. // 把原先的父级levePath,替换成最新的父级序列
  736. classifyIdMap := make(map[string]struct{})
  737. classifyIds := make([]string, 0)
  738. childClassifyMap := make(map[int]*material.MaterialClassify)
  739. for _, tmp := range childList {
  740. childClassifyMap[tmp.ClassifyId] = tmp
  741. //获取字符串前缀的位置
  742. after, _ := strings.CutPrefix(tmp.LevelPath, classifyInfo.LevelPath)
  743. fmt.Println("after", after)
  744. // 拼接字符串
  745. if after != "" {
  746. ids := strings.Split(after, ",")
  747. for _, v := range ids {
  748. if _, ok := classifyIdMap[v]; !ok {
  749. classifyIds = append(classifyIds, v)
  750. classifyIdMap[v] = struct{}{}
  751. }
  752. }
  753. }
  754. }
  755. classifyIds = append(classifyIds, strconv.Itoa(classifyId))
  756. if len(classifyIds) > 0 {
  757. condition += " AND classify_id IN(" + utils.GetOrmInReplace(len(classifyIds)) + ") "
  758. pars = append(pars, classifyIds)
  759. }
  760. if keyword != "" {
  761. condition += ` AND ( material_name LIKE '%` + keyword + `%' )`
  762. }
  763. //只看我的
  764. isShowMe, _ := this.GetBool("IsShowMe")
  765. if isShowMe {
  766. condition += ` AND sys_user_id = ? `
  767. pars = append(pars, sysUser.AdminId)
  768. }
  769. //获取图表信息
  770. list, err := material.GetMaterialListPageByCondition(condition, pars, startSize, pageSize)
  771. if err != nil && err.Error() != utils.ErrNoRow() {
  772. br.Success = true
  773. br.Msg = "获取素材库信息失败"
  774. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  775. return
  776. }
  777. for i, v := range list {
  778. if classifyTmp, ok := childClassifyMap[v.ClassifyId]; ok {
  779. list[i].ParentIds = classifyTmp.LevelPath
  780. }
  781. }
  782. resp := new(material.MaterialListResp)
  783. if list == nil || len(list) <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
  784. items := make([]*material.MaterialListItems, 0)
  785. resp.Paging = page
  786. resp.List = items
  787. br.Ret = 200
  788. br.Success = true
  789. br.Msg = "获取成功"
  790. br.Data = resp
  791. return
  792. }
  793. dataCount, err := material.GetMaterialListCountByCondition(condition, pars)
  794. if err != nil && err.Error() != utils.ErrNoRow() {
  795. br.Msg = "获取指标信息失败"
  796. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  797. return
  798. }
  799. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  800. resp.Paging = page
  801. resp.List = list
  802. br.Ret = 200
  803. br.Success = true
  804. br.Msg = "获取成功"
  805. br.Data = resp
  806. }
  807. // Save
  808. // @Title 新增/编辑保存素材库
  809. // @Description 新增/编辑保存素材库接口
  810. // @Param request body material.AddAndEditSandbox true "type json string"
  811. // @Success 200 {object} material.Material
  812. // @router /save [post]
  813. func (this *MaterialController) Save() {
  814. br := new(models.BaseResponse).Init()
  815. defer func() {
  816. this.Data["json"] = br
  817. this.ServeJSON()
  818. }()
  819. sysUser := this.SysUser
  820. if sysUser == nil {
  821. br.Msg = "请登录"
  822. br.ErrMsg = "请登录,SysUser Is Empty"
  823. br.Ret = 408
  824. return
  825. }
  826. var req material.AddAndEditMaterial
  827. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  828. if err != nil {
  829. br.Msg = "参数解析异常!"
  830. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  831. return
  832. }
  833. var materialResp *material.MaterialSaveResp
  834. var errMsg string
  835. if req.MaterialId <= 0 {
  836. //新增素材库
  837. materialResp, err = materialService.AddMaterial(req, sysUser.AdminId, sysUser.RealName)
  838. if err != nil {
  839. br.Msg = "保存失败!"
  840. if errMsg != `` {
  841. br.Msg = errMsg
  842. }
  843. br.ErrMsg = "保存失败,Err:" + err.Error()
  844. return
  845. }
  846. } else {
  847. //编辑素材库
  848. materialInfo := &material.Material{
  849. MaterialId: req.MaterialId,
  850. MaterialName: utils.TrimStr(req.MaterialName),
  851. ImgUrl: utils.TrimStr(req.ImgUrl),
  852. ModifyTime: time.Now(),
  853. ClassifyId: req.ClassifyId,
  854. }
  855. //缩略图为空时不更新
  856. var updateSandboxColumn = []string{}
  857. if req.ImgUrl == "" {
  858. updateSandboxColumn = []string{"Name", "Content", "MindmapData", "ModifyTime", "MaterialClassifyId", "Style"}
  859. } else {
  860. updateSandboxColumn = []string{"Name", "Content", "MindmapData", "ImgUrl", "ModifyTime", "MaterialClassifyId", "Style"}
  861. }
  862. err = materialInfo.Update(updateSandboxColumn)
  863. if err != nil {
  864. br.Msg = "保存失败!"
  865. if errMsg != `` {
  866. br.Msg = errMsg
  867. }
  868. br.ErrMsg = "保存失败,Err:" + err.Error()
  869. return
  870. }
  871. }
  872. msg := "保存成功"
  873. br.Ret = 200
  874. br.Success = true
  875. br.Msg = msg
  876. br.Data = materialResp
  877. }
  878. // Delete
  879. // @Title 删除素材库
  880. // @Description 删除素材库接口
  881. // @Param request body material.DeleteSandbox true "type json string"
  882. // @Success 200 标记成功
  883. // @router /del [post]
  884. func (this *MaterialController) Delete() {
  885. br := new(models.BaseResponse).Init()
  886. defer func() {
  887. this.Data["json"] = br
  888. this.ServeJSON()
  889. }()
  890. sysUser := this.SysUser
  891. if sysUser == nil {
  892. br.Msg = "请登录"
  893. br.ErrMsg = "请登录,SysUser Is Empty"
  894. br.Ret = 408
  895. return
  896. }
  897. var req material.DeleteMaterial
  898. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  899. if err != nil {
  900. br.Msg = "参数解析异常!"
  901. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  902. return
  903. }
  904. if req.MaterialId <= 0 {
  905. br.Msg = "缺少素材库编号"
  906. return
  907. }
  908. //删除素材库
  909. err = material.DeleteByMaterialIds([]int{req.MaterialId})
  910. if err != nil {
  911. br.Msg = "操作失败"
  912. br.ErrMsg = "操作失败,Err:" + err.Error()
  913. return
  914. }
  915. msg := "删除成功"
  916. br.Ret = 200
  917. br.Success = true
  918. br.Msg = msg
  919. }
  920. // BatchDelete
  921. // @Title 批量删除素材库
  922. // @Description 删除素材库接口
  923. // @Param request body material.DeleteSandbox true "type json string"
  924. // @Success 200 标记成功
  925. // @router /batch/del [post]
  926. func (this *MaterialController) BatchDelete() {
  927. br := new(models.BaseResponse).Init()
  928. defer func() {
  929. this.Data["json"] = br
  930. this.ServeJSON()
  931. }()
  932. sysUser := this.SysUser
  933. if sysUser == nil {
  934. br.Msg = "请登录"
  935. br.ErrMsg = "请登录,SysUser Is Empty"
  936. br.Ret = 408
  937. return
  938. }
  939. var req material.BatchDeleteMaterialReq
  940. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  941. if err != nil {
  942. br.Msg = "参数解析异常!"
  943. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  944. return
  945. }
  946. deleteMaterialIds := make([]int, 0)
  947. if req.IsSelectAll {
  948. classifyId := req.ClassifyId
  949. keyword := req.Keyword
  950. isShowMe := req.IsShowMe
  951. //获取图表信息
  952. list, e, msg := materialService.GetBatchSelectedMaterialList(classifyId, keyword, isShowMe, sysUser)
  953. if e != nil {
  954. br.Msg = "获取素材库信息失败"
  955. if msg != "" {
  956. br.Msg = msg
  957. }
  958. br.ErrMsg = "获取素材库信息失败,Err:" + e.Error()
  959. return
  960. }
  961. notSelectIds := make(map[int]struct{})
  962. if len(req.MaterialIds) >= 0 {
  963. for _, v := range req.MaterialIds {
  964. notSelectIds[v] = struct{}{}
  965. }
  966. }
  967. for _, v := range list {
  968. if _, ok := notSelectIds[v.MaterialId]; !ok {
  969. deleteMaterialIds = append(deleteMaterialIds, v.MaterialId)
  970. }
  971. }
  972. } else {
  973. deleteMaterialIds = req.MaterialIds
  974. }
  975. if len(deleteMaterialIds) <= 0 {
  976. br.Msg = "请选择删除的素材"
  977. return
  978. }
  979. //删除素材库
  980. err = material.DeleteByMaterialIds(deleteMaterialIds)
  981. if err != nil {
  982. br.Msg = err.Error()
  983. return
  984. }
  985. msg := "删除成功"
  986. br.Ret = 200
  987. br.Success = true
  988. br.Msg = msg
  989. }
  990. // GetMaterialVersionDetail
  991. // @Title 获取素材库版本数据详情(已保存的)
  992. // @Description 获取素材库版本数据详情接口(已保存的)
  993. // @Param SandboxVersionCode query string true "素材库版本code"
  994. // @Success 200 {object} material.MaterialVersion
  995. // @router /detail [get]
  996. func (this *MaterialController) GetMaterialDetail() {
  997. br := new(models.BaseResponse).Init()
  998. defer func() {
  999. this.Data["json"] = br
  1000. this.ServeJSON()
  1001. }()
  1002. sysUser := this.SysUser
  1003. if sysUser == nil {
  1004. br.Msg = "请登录"
  1005. br.ErrMsg = "请登录,SysUser Is Empty"
  1006. br.Ret = 408
  1007. return
  1008. }
  1009. sandboxId, _ := this.GetInt("SandboxId")
  1010. if sandboxId == 0 {
  1011. br.Msg = "缺少素材库Id"
  1012. return
  1013. }
  1014. //获取素材库数据详情(已保存的)
  1015. materialInfo, err := material.GetMaterialById(sandboxId)
  1016. if err != nil {
  1017. br.Msg = "获取失败"
  1018. br.ErrMsg = "获取失败,Err:" + err.Error()
  1019. return
  1020. }
  1021. msg := "获取成功"
  1022. br.Ret = 200
  1023. br.Success = true
  1024. br.Msg = msg
  1025. br.Data = materialInfo
  1026. }
  1027. // MaterialClassifyList
  1028. // @Title 获取所有素材库分类接口-不包含素材库
  1029. // @Description 获取所有素材库分类接口-不包含素材库
  1030. // @Param IsShowMe query bool true "是否只看我的,true、false"
  1031. // @Success 200 {object} data_manage.ChartClassifyListResp
  1032. // @router /classify/list [get]
  1033. func (this *MaterialController) MaterialClassifyList() {
  1034. br := new(models.BaseResponse).Init()
  1035. defer func() {
  1036. this.Data["json"] = br
  1037. this.ServeJSON()
  1038. }()
  1039. resp := new(material.MaterialClassifyListResp)
  1040. rootList, err := material.GetMaterialClassifyByParentId(0)
  1041. if err != nil && err.Error() != utils.ErrNoRow() {
  1042. br.Msg = "获取失败"
  1043. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1044. return
  1045. }
  1046. classifyAll, err := material.GetMaterialClassifyAll()
  1047. if err != nil && err.Error() != utils.ErrNoRow() {
  1048. br.Msg = "获取失败"
  1049. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1050. return
  1051. }
  1052. nodeAll := make([]*material.MaterialClassifyItems, 0)
  1053. for k := range rootList {
  1054. rootNode := rootList[k]
  1055. materialService.MaterialClassifyItemsMakeTree(this.SysUser, classifyAll, rootNode)
  1056. nodeAll = append(nodeAll, rootNode)
  1057. }
  1058. resp.AllNodes = nodeAll
  1059. br.Ret = 200
  1060. br.Success = true
  1061. br.Msg = "获取成功"
  1062. br.Data = resp
  1063. }
  1064. // BatchAdd
  1065. // @Title 批量新增素材
  1066. // @Description 新增/编辑保存素材库接口
  1067. // @Param request body material.AddAndEditSandbox true "type json string"
  1068. // @Success 200 {object} material.Material
  1069. // @router /batch/add [post]
  1070. func (this *MaterialController) BatchAdd() {
  1071. br := new(models.BaseResponse).Init()
  1072. defer func() {
  1073. this.Data["json"] = br
  1074. this.ServeJSON()
  1075. }()
  1076. sysUser := this.SysUser
  1077. if sysUser == nil {
  1078. br.Msg = "请登录"
  1079. br.ErrMsg = "请登录,SysUser Is Empty"
  1080. br.Ret = 408
  1081. return
  1082. }
  1083. var req material.BatchAddMaterialReq
  1084. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1085. if err != nil {
  1086. br.Msg = "参数解析异常!"
  1087. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1088. return
  1089. }
  1090. classifyId := req.ClassifyId
  1091. if classifyId <= 0 {
  1092. br.Msg = "请选择分类"
  1093. return
  1094. }
  1095. materialNames := make([]string, 0)
  1096. for _, v := range req.MaterialList {
  1097. if v.MaterialName == "" {
  1098. br.Msg = "请填写图片名称"
  1099. return
  1100. }
  1101. if v.ImgUrl == "" {
  1102. br.Msg = "请上传图片"
  1103. return
  1104. }
  1105. materialNames = append(materialNames, v.MaterialName)
  1106. }
  1107. if len(materialNames) == 0 {
  1108. br.Msg = "请填写图片名称"
  1109. return
  1110. }
  1111. existList := make([]*material.Material, 0)
  1112. switch this.Lang {
  1113. case utils.LANG_EN:
  1114. existList, err = material.GetMaterialByNameEns(materialNames)
  1115. if err != nil {
  1116. br.Msg = "获取失败"
  1117. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1118. return
  1119. }
  1120. if len(existList) > 0 {
  1121. msg := "图片名称:"
  1122. for _, v := range existList {
  1123. msg += v.MaterialNameEn + " "
  1124. }
  1125. br.Msg = fmt.Sprintf("%s 已存在", msg)
  1126. return
  1127. }
  1128. default:
  1129. // 判断文件名是否已存在
  1130. existList, err = material.GetMaterialByNames(materialNames)
  1131. if err != nil {
  1132. br.Msg = "获取失败"
  1133. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1134. return
  1135. }
  1136. if len(existList) > 0 {
  1137. msg := "图片名称:"
  1138. for _, v := range existList {
  1139. msg += v.MaterialName + " "
  1140. }
  1141. br.Msg = fmt.Sprintf("%s 已存在", msg)
  1142. return
  1143. }
  1144. }
  1145. err = materialService.BatchAddMaterial(req.MaterialList, classifyId, sysUser.AdminId, sysUser.AdminName)
  1146. if err != nil {
  1147. br.Msg = "文件上传失败"
  1148. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  1149. return
  1150. }
  1151. msg := "添加成功"
  1152. br.Ret = 200
  1153. br.Success = true
  1154. br.Msg = msg
  1155. }
  1156. // Upload
  1157. // @Title 上传素材
  1158. // @Description 上传素材接口
  1159. // @Param request body material.AddAndEditSandbox true "type json string"
  1160. // @Success 200 {object} material.Material
  1161. // @router /upload [post]
  1162. func (this *MaterialController) Upload() {
  1163. br := new(models.BaseResponse).Init()
  1164. defer func() {
  1165. this.Data["json"] = br
  1166. this.ServeJSON()
  1167. }()
  1168. sysUser := this.SysUser
  1169. if sysUser == nil {
  1170. br.Msg = "请登录"
  1171. br.ErrMsg = "请登录,SysUser Is Empty"
  1172. br.Ret = 408
  1173. return
  1174. }
  1175. f, h, err := this.GetFile("file")
  1176. if err != nil {
  1177. br.Msg = "获取资源信息失败"
  1178. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  1179. return
  1180. }
  1181. defer f.Close() //关闭上传文件
  1182. // 不依赖于文件扩展名检查文件格式
  1183. fileData, e := ioutil.ReadAll(f)
  1184. if e != nil {
  1185. br.Msg = "上传失败"
  1186. br.ErrMsg = "读取文件失败, Err: " + e.Error()
  1187. return
  1188. }
  1189. pass := filetype.IsImage(fileData)
  1190. if !pass {
  1191. br.Msg = "文件格式有误"
  1192. br.ErrMsg = "文件格式有误"
  1193. return
  1194. }
  1195. ext := path.Ext(h.Filename)
  1196. randStr := utils.GetRandStringNoSpecialChar(28)
  1197. fileName := randStr + ext
  1198. // 上传到阿里云
  1199. ossDir := utils.RESOURCE_DIR + "material_dir/"
  1200. savePath := ossDir + time.Now().Format("200601/20060102/") + fileName
  1201. // 上传文件
  1202. resourceUrl, err := services.CommonUploadToOssAndFileName(h, fileName, savePath)
  1203. if err != nil {
  1204. br.Msg = "文件上传失败"
  1205. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  1206. return
  1207. }
  1208. resp := new(models.UploadImgResp)
  1209. resp.ResourceUrl = resourceUrl
  1210. br.Msg = "上传成功"
  1211. br.Ret = 200
  1212. br.Success = true
  1213. br.Data = resp
  1214. }
  1215. // BatchChangeClassify
  1216. // @Title 批量更换分类
  1217. // @Description 批量更换分类
  1218. // @Param request body material.DeleteSandbox true "type json string"
  1219. // @Success 200 标记成功
  1220. // @router /batch/changeClassify [post]
  1221. func (this *MaterialController) BatchChangeClassify() {
  1222. br := new(models.BaseResponse).Init()
  1223. defer func() {
  1224. this.Data["json"] = br
  1225. this.ServeJSON()
  1226. }()
  1227. sysUser := this.SysUser
  1228. if sysUser == nil {
  1229. br.Msg = "请登录"
  1230. br.ErrMsg = "请登录,SysUser Is Empty"
  1231. br.Ret = 408
  1232. return
  1233. }
  1234. var req material.BatchChangeClassifyMaterialReq
  1235. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1236. if err != nil {
  1237. br.Msg = "参数解析异常!"
  1238. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1239. return
  1240. }
  1241. if req.NewClassifyId <= 0 {
  1242. br.Msg = "请选择新的分类"
  1243. return
  1244. }
  1245. updateMaterialIds := make([]int, 0)
  1246. if req.IsSelectAll {
  1247. classifyId := req.ClassifyId
  1248. keyword := req.Keyword
  1249. isShowMe := req.IsShowMe
  1250. //获取图表信息
  1251. list, e, msg := materialService.GetBatchSelectedMaterialList(classifyId, keyword, isShowMe, sysUser)
  1252. if e != nil {
  1253. br.Msg = "获取素材库信息失败"
  1254. if msg != "" {
  1255. br.Msg = msg
  1256. }
  1257. br.ErrMsg = "获取素材库信息失败,Err:" + e.Error()
  1258. return
  1259. }
  1260. notSelectIds := make(map[int]struct{})
  1261. if len(req.MaterialIds) >= 0 {
  1262. for _, v := range req.MaterialIds {
  1263. notSelectIds[v] = struct{}{}
  1264. }
  1265. }
  1266. for _, v := range list {
  1267. if _, ok := notSelectIds[v.MaterialId]; !ok {
  1268. updateMaterialIds = append(updateMaterialIds, v.MaterialId)
  1269. }
  1270. }
  1271. } else {
  1272. updateMaterialIds = req.MaterialIds
  1273. }
  1274. if len(updateMaterialIds) <= 0 {
  1275. br.Msg = "请选择变更的素材"
  1276. return
  1277. }
  1278. // 判断新分类是否存在
  1279. _, err = material.GetMaterialClassifyById(req.NewClassifyId)
  1280. if err != nil {
  1281. if err.Error() == utils.ErrNoRow() {
  1282. br.Msg = "分类不存在"
  1283. return
  1284. }
  1285. br.Msg = "获取分类信息失败"
  1286. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  1287. }
  1288. //更换分类素材库
  1289. err = material.UpdateClassifyByMaterialIds(updateMaterialIds, req.NewClassifyId, time.Now())
  1290. if err != nil {
  1291. br.Msg = err.Error()
  1292. return
  1293. }
  1294. msg := "操作成功"
  1295. br.Ret = 200
  1296. br.Success = true
  1297. br.Msg = msg
  1298. }
  1299. // ChangeClassify
  1300. // @Title 更换分类
  1301. // @Description 批量更换分类
  1302. // @Param request body material.DeleteSandbox true "type json string"
  1303. // @Success 200 标记成功
  1304. // @router /changeClassify [post]
  1305. func (this *MaterialController) ChangeClassify() {
  1306. br := new(models.BaseResponse).Init()
  1307. defer func() {
  1308. this.Data["json"] = br
  1309. this.ServeJSON()
  1310. }()
  1311. sysUser := this.SysUser
  1312. if sysUser == nil {
  1313. br.Msg = "请登录"
  1314. br.ErrMsg = "请登录,SysUser Is Empty"
  1315. br.Ret = 408
  1316. return
  1317. }
  1318. var req material.ChangeClassifyMaterialReq
  1319. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1320. if err != nil {
  1321. br.Msg = "参数解析异常!"
  1322. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1323. return
  1324. }
  1325. if req.MaterialId <= 0 {
  1326. br.Msg = "缺少素材库编号"
  1327. return
  1328. }
  1329. if req.NewClassifyId <= 0 {
  1330. br.Msg = "请选择新的分类"
  1331. return
  1332. }
  1333. // 判断素材是否存在
  1334. info, err := material.GetMaterialById(req.MaterialId)
  1335. if err != nil {
  1336. if err.Error() == utils.ErrNoRow() {
  1337. br.Msg = "素材不存在"
  1338. return
  1339. }
  1340. br.Msg = "获取素材库信息失败"
  1341. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1342. return
  1343. }
  1344. if req.NewClassifyId == info.ClassifyId {
  1345. br.Msg = "请选择不同的分类"
  1346. return
  1347. }
  1348. // 判断新分类是否存在
  1349. _, err = material.GetMaterialClassifyById(req.NewClassifyId)
  1350. if err != nil {
  1351. if err.Error() == utils.ErrNoRow() {
  1352. br.Msg = "分类不存在"
  1353. return
  1354. }
  1355. br.Msg = "获取分类信息失败"
  1356. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  1357. }
  1358. info.ClassifyId = req.NewClassifyId
  1359. info.ModifyTime = time.Now()
  1360. //更换分类素材库
  1361. err = info.Update([]string{"ClassifyId", "ModifyTime"})
  1362. if err != nil {
  1363. br.Msg = err.Error()
  1364. return
  1365. }
  1366. msg := "操作成功"
  1367. br.Ret = 200
  1368. br.Success = true
  1369. br.Msg = msg
  1370. }
  1371. // Rename
  1372. // @Title 素材重命名
  1373. // @Description 素材重命名
  1374. // @Param request body material.DeleteSandbox true "type json string"
  1375. // @Success 200 标记成功
  1376. // @router /rename [post]
  1377. func (this *MaterialController) Rename() {
  1378. br := new(models.BaseResponse).Init()
  1379. defer func() {
  1380. this.Data["json"] = br
  1381. this.ServeJSON()
  1382. }()
  1383. sysUser := this.SysUser
  1384. if sysUser == nil {
  1385. br.Msg = "请登录"
  1386. br.ErrMsg = "请登录,SysUser Is Empty"
  1387. br.Ret = 408
  1388. return
  1389. }
  1390. var req material.RenameMaterialReq
  1391. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1392. if err != nil {
  1393. br.Msg = "参数解析异常!"
  1394. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1395. return
  1396. }
  1397. if req.MaterialId <= 0 {
  1398. br.Msg = "缺少素材库编号"
  1399. return
  1400. }
  1401. if req.MaterialName == "" {
  1402. br.Msg = "缺少素材库名称"
  1403. return
  1404. }
  1405. // 判断素材是否存在
  1406. info, err := material.GetMaterialById(req.MaterialId)
  1407. if err != nil {
  1408. if err.Error() == utils.ErrNoRow() {
  1409. br.Msg = "素材不存在"
  1410. return
  1411. }
  1412. br.Msg = "获取素材库信息失败"
  1413. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1414. return
  1415. }
  1416. var exist *material.Material
  1417. updateStr := make([]string, 0)
  1418. switch this.Lang {
  1419. case utils.LANG_EN:
  1420. // 判断名称是否重复
  1421. if info.MaterialNameEn == req.MaterialName {
  1422. br.Msg = "名称未修改"
  1423. return
  1424. }
  1425. exist, err = material.GetMaterialByNameEn(req.MaterialName)
  1426. info.MaterialNameEn = req.MaterialName
  1427. updateStr = append(updateStr, "MaterialNameEn")
  1428. default:
  1429. // 判断名称是否重复
  1430. if info.MaterialName == req.MaterialName {
  1431. br.Msg = "名称未修改"
  1432. return
  1433. }
  1434. exist, err = material.GetMaterialByName(req.MaterialName)
  1435. info.MaterialName = req.MaterialName
  1436. updateStr = append(updateStr, "MaterialName")
  1437. }
  1438. if err != nil && err.Error() != utils.ErrNoRow() {
  1439. br.Msg = "获取失败"
  1440. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1441. return
  1442. }
  1443. if err == nil && exist.MaterialId > 0 {
  1444. br.Msg = "图片名称已存在"
  1445. return
  1446. }
  1447. info.ModifyTime = time.Now()
  1448. updateStr = append(updateStr, "ModifyTime")
  1449. //更换分类素材库
  1450. err = info.Update(updateStr)
  1451. if err != nil {
  1452. br.Msg = err.Error()
  1453. return
  1454. }
  1455. msg := "操作成功"
  1456. br.Ret = 200
  1457. br.Success = true
  1458. br.Msg = msg
  1459. }
  1460. // BatchDownload
  1461. // @Title 批量下载素材
  1462. // @Description 批量下载素材
  1463. // @Param request body material.DeleteSandbox true "type json string"
  1464. // @Success 200 标记成功
  1465. // @router /batch/download [post]
  1466. func (this *MaterialController) BatchDownload() {
  1467. br := new(models.BaseResponse).Init()
  1468. defer func() {
  1469. this.Data["json"] = br
  1470. this.ServeJSON()
  1471. }()
  1472. sysUser := this.SysUser
  1473. if sysUser == nil {
  1474. br.Msg = "请登录"
  1475. br.ErrMsg = "请登录,SysUser Is Empty"
  1476. br.Ret = 408
  1477. return
  1478. }
  1479. var req material.BatchDeleteMaterialReq
  1480. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1481. if err != nil {
  1482. br.Msg = "参数解析异常!"
  1483. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1484. return
  1485. }
  1486. downMaterialList := make([]*material.MaterialListItems, 0)
  1487. if req.IsSelectAll {
  1488. classifyId := req.ClassifyId
  1489. keyword := req.Keyword
  1490. isShowMe := req.IsShowMe
  1491. //获取图表信息
  1492. list, e, msg := materialService.GetBatchSelectedMaterialList(classifyId, keyword, isShowMe, sysUser)
  1493. if e != nil {
  1494. br.Msg = "获取素材库信息失败"
  1495. if msg != "" {
  1496. br.Msg = msg
  1497. }
  1498. br.ErrMsg = "获取素材库信息失败,Err:" + e.Error()
  1499. return
  1500. }
  1501. notSelectIds := make(map[int]struct{})
  1502. if len(req.MaterialIds) >= 0 {
  1503. for _, v := range req.MaterialIds {
  1504. notSelectIds[v] = struct{}{}
  1505. }
  1506. }
  1507. for _, v := range list {
  1508. if _, ok := notSelectIds[v.MaterialId]; !ok {
  1509. downMaterialList = append(downMaterialList, v)
  1510. }
  1511. }
  1512. } else {
  1513. if len(req.MaterialIds) > 0 {
  1514. // 批量查询指标数据
  1515. downMaterialList, err = material.GetMaterialByIds(req.MaterialIds)
  1516. if err != nil {
  1517. br.Msg = "获取素材库信息失败"
  1518. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1519. return
  1520. }
  1521. }
  1522. }
  1523. if len(downMaterialList) <= 0 {
  1524. br.Msg = "请选择要下载的素材"
  1525. return
  1526. }
  1527. // 创建zip
  1528. zipName := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
  1529. savePath := zipName
  1530. zipFile, err := os.Create(zipName)
  1531. if err != nil {
  1532. br.Msg = "操作失败"
  1533. br.ErrMsg = "生成压缩文件失败, Err: " + err.Error()
  1534. return
  1535. }
  1536. zipWriter := zip.NewWriter(zipFile)
  1537. // 生成zip过程中报错关闭
  1538. defer func() {
  1539. if err != nil {
  1540. _ = zipWriter.Close()
  1541. _ = zipFile.Close()
  1542. }
  1543. _ = os.Remove(savePath)
  1544. }()
  1545. // 获取资源, 写入zip
  1546. zipFileName := ""
  1547. for i := range downMaterialList {
  1548. if downMaterialList[i].MaterialName == "" || downMaterialList[i].ImgUrl == "" {
  1549. continue
  1550. }
  1551. fmt.Printf("开始压缩第%d个文件\n", i+1)
  1552. dotIndex := strings.LastIndex(downMaterialList[i].ImgUrl, ".")
  1553. // 如果找不到点,或者点是文件名的第一个字符(不合法情况),则返回空字符串
  1554. if dotIndex == -1 || dotIndex == 0 {
  1555. continue
  1556. }
  1557. fileExt := downMaterialList[i].ImgUrl[dotIndex+1:]
  1558. ioWriter, err := zipWriter.Create(fmt.Sprintf("%s.%s", downMaterialList[i].MaterialName, fileExt))
  1559. if err != nil {
  1560. if os.IsPermission(err) {
  1561. br.Msg = "操作失败"
  1562. br.ErrMsg = "打包权限不足, Err: " + err.Error()
  1563. return
  1564. }
  1565. br.Msg = "操作失败"
  1566. br.ErrMsg = "压缩出错, Err: " + err.Error()
  1567. return
  1568. }
  1569. var content []byte
  1570. content, err = http.Get(downMaterialList[i].ImgUrl)
  1571. if err != nil {
  1572. br.Msg = "操作失败"
  1573. br.ErrMsg = "资源获取失败, Err: " + err.Error()
  1574. return
  1575. }
  1576. _, err = ioWriter.Write(content)
  1577. if err != nil {
  1578. br.Msg = "操作失败"
  1579. br.ErrMsg = "压缩文件写入失败, Err: " + err.Error()
  1580. return
  1581. }
  1582. if zipFileName == "" {
  1583. zipFileName = downMaterialList[i].MaterialName
  1584. }
  1585. fmt.Printf("第%d个文件写入成功\n", i+1)
  1586. }
  1587. // 生成zip后关闭,否则下载文件会损坏
  1588. _ = zipWriter.Close()
  1589. _ = zipFile.Close()
  1590. this.Ctx.Output.Download(savePath, fmt.Sprintf("%s.zip", zipFileName))
  1591. msg := "操作成功"
  1592. br.Ret = 200
  1593. br.Success = true
  1594. br.Msg = msg
  1595. }
  1596. // Download
  1597. // @Title 下载素材
  1598. // @Description 下载素材
  1599. // @Param request body material.DeleteSandbox true "type json string"
  1600. // @Success 200 标记成功
  1601. // @router /download [post]
  1602. func (this *MaterialController) Download() {
  1603. br := new(models.BaseResponse).Init()
  1604. defer func() {
  1605. this.Data["json"] = br
  1606. this.ServeJSON()
  1607. }()
  1608. sysUser := this.SysUser
  1609. if sysUser == nil {
  1610. br.Msg = "请登录"
  1611. br.ErrMsg = "请登录,SysUser Is Empty"
  1612. br.Ret = 408
  1613. return
  1614. }
  1615. var req material.RenameMaterialReq
  1616. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1617. if err != nil {
  1618. br.Msg = "参数解析异常!"
  1619. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1620. return
  1621. }
  1622. if req.MaterialId <= 0 {
  1623. br.Msg = "缺少素材库编号"
  1624. return
  1625. }
  1626. // 判断素材是否存在
  1627. info, err := material.GetMaterialById(req.MaterialId)
  1628. if err != nil {
  1629. if err.Error() == utils.ErrNoRow() {
  1630. br.Msg = "素材不存在"
  1631. return
  1632. }
  1633. br.Msg = "获取素材库信息失败"
  1634. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1635. return
  1636. }
  1637. if info.ImgUrl == "" {
  1638. br.Msg = "素材地址为空"
  1639. return
  1640. }
  1641. fileName := info.MaterialName
  1642. // 查找文件名中最后一个点的位置
  1643. dotIndex := strings.LastIndex(info.ImgUrl, ".")
  1644. // 如果找不到点,或者点是文件名的第一个字符(不合法情况),则返回空字符串
  1645. if dotIndex == -1 || dotIndex == 0 {
  1646. br.Msg = "素材地址错误"
  1647. return
  1648. }
  1649. fileName += "." + info.ImgUrl[dotIndex+1:] // 添加扩展名到文件名
  1650. // 获取路径中的文件名
  1651. urlFileName := path.Base(info.ImgUrl)
  1652. uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
  1653. if e := os.MkdirAll(uploadDir, utils.DIR_MOD); e != nil {
  1654. br.Msg = "存储目录创建失败"
  1655. br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
  1656. return
  1657. }
  1658. var content []byte
  1659. content, err = http.Get(info.ImgUrl)
  1660. if err != nil {
  1661. br.Msg = "操作失败"
  1662. br.ErrMsg = "资源获取失败, Err: " + err.Error()
  1663. return
  1664. }
  1665. filePath := uploadDir + "/" + urlFileName
  1666. ioWriter, err := os.Create(filePath)
  1667. if err != nil {
  1668. br.Msg = "操作失败"
  1669. br.ErrMsg = "文件创建失败, Err: " + err.Error()
  1670. return
  1671. }
  1672. n, err := ioWriter.Write(content)
  1673. fmt.Println("n", n)
  1674. if err != nil {
  1675. br.Msg = "操作失败"
  1676. br.ErrMsg = "压缩文件写入失败, Err: " + err.Error()
  1677. return
  1678. }
  1679. this.Ctx.Output.Download(filePath, fileName)
  1680. defer func() {
  1681. _ = os.Remove(filePath)
  1682. }()
  1683. br.Ret = 200
  1684. br.Success = true
  1685. br.Msg = "success"
  1686. }