ManagerController.go 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "html/template"
  5. "regexp"
  6. "strings"
  7. "math"
  8. "path/filepath"
  9. "strconv"
  10. "github.com/astaxie/beego"
  11. "github.com/astaxie/beego/logs"
  12. "github.com/astaxie/beego/orm"
  13. "github.com/lifei6671/mindoc/conf"
  14. "github.com/lifei6671/mindoc/models"
  15. "github.com/lifei6671/mindoc/utils"
  16. "github.com/lifei6671/mindoc/utils/filetil"
  17. "github.com/lifei6671/mindoc/utils/pagination"
  18. "gopkg.in/russross/blackfriday.v2"
  19. "io/ioutil"
  20. "os"
  21. )
  22. type ManagerController struct {
  23. BaseController
  24. }
  25. func (c *ManagerController) Prepare() {
  26. c.BaseController.Prepare()
  27. if !c.Member.IsAdministrator() {
  28. c.Abort("403")
  29. }
  30. }
  31. func (c *ManagerController) Index() {
  32. c.TplName = "manager/index.tpl"
  33. c.Data["Model"] = models.NewDashboard().Query()
  34. }
  35. // 用户列表.
  36. func (c *ManagerController) Users() {
  37. c.Prepare()
  38. c.TplName = "manager/users.tpl"
  39. pageIndex, _ := c.GetInt("page", 0)
  40. members, totalCount, err := models.NewMember().FindToPager(pageIndex, conf.PageSize)
  41. if err != nil {
  42. c.Data["ErrorMessage"] = err.Error()
  43. return
  44. }
  45. if totalCount > 0 {
  46. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  47. c.Data["PageHtml"] = pager.HtmlPages()
  48. for _, item := range members {
  49. item.Avatar = conf.URLForWithCdnImage(item.Avatar)
  50. }
  51. } else {
  52. c.Data["PageHtml"] = ""
  53. }
  54. b, err := json.Marshal(members)
  55. if err != nil {
  56. c.Data["Result"] = template.JS("[]")
  57. } else {
  58. c.Data["Result"] = template.JS(string(b))
  59. }
  60. }
  61. // 添加用户.
  62. func (c *ManagerController) CreateMember() {
  63. c.Prepare()
  64. account := strings.TrimSpace(c.GetString("account"))
  65. password1 := strings.TrimSpace(c.GetString("password1"))
  66. password2 := strings.TrimSpace(c.GetString("password2"))
  67. email := strings.TrimSpace(c.GetString("email"))
  68. phone := strings.TrimSpace(c.GetString("phone"))
  69. role, _ := c.GetInt("role", 1)
  70. status, _ := c.GetInt("status", 0)
  71. if ok, err := regexp.MatchString(conf.RegexpAccount, account); account == "" || !ok || err != nil {
  72. c.JsonResult(6001, "账号只能由英文字母数字组成,且在3-50个字符")
  73. }
  74. if l := strings.Count(password1, ""); password1 == "" || l > 50 || l < 6 {
  75. c.JsonResult(6002, "密码必须在6-50个字符之间")
  76. }
  77. if password1 != password2 {
  78. c.JsonResult(6003, "确认密码不正确")
  79. }
  80. if ok, err := regexp.MatchString(conf.RegexpEmail, email); !ok || err != nil || email == "" {
  81. c.JsonResult(6004, "邮箱格式不正确")
  82. }
  83. if role != 0 && role != 1 && role != 2 {
  84. role = 1
  85. }
  86. if status != 0 && status != 1 {
  87. status = 0
  88. }
  89. member := models.NewMember()
  90. if _, err := member.FindByAccount(account); err == nil && member.MemberId > 0 {
  91. c.JsonResult(6005, "账号已存在")
  92. }
  93. member.Account = account
  94. member.Password = password1
  95. member.Role = conf.SystemRole(role)
  96. member.Avatar = conf.GetDefaultAvatar()
  97. member.CreateAt = c.Member.MemberId
  98. member.Email = email
  99. member.RealName = strings.TrimSpace(c.GetString("real_name", ""))
  100. if phone != "" {
  101. member.Phone = phone
  102. }
  103. if err := member.Add(); err != nil {
  104. c.JsonResult(6006, err.Error())
  105. }
  106. c.JsonResult(0, "ok", member)
  107. }
  108. //更新用户状态.
  109. func (c *ManagerController) UpdateMemberStatus() {
  110. c.Prepare()
  111. member_id, _ := c.GetInt("member_id", 0)
  112. status, _ := c.GetInt("status", 0)
  113. if member_id <= 0 {
  114. c.JsonResult(6001, "参数错误")
  115. }
  116. if status != 0 && status != 1 {
  117. status = 0
  118. }
  119. member := models.NewMember()
  120. if _, err := member.Find(member_id); err != nil {
  121. c.JsonResult(6002, "用户不存在")
  122. }
  123. if member.MemberId == c.Member.MemberId {
  124. c.JsonResult(6004, "不能变更自己的状态")
  125. }
  126. if member.Role == conf.MemberSuperRole {
  127. c.JsonResult(6005, "不能变更超级管理员的状态")
  128. }
  129. member.Status = status
  130. if err := member.Update(); err != nil {
  131. logs.Error("", err)
  132. c.JsonResult(6003, "用户状态设置失败")
  133. }
  134. c.JsonResult(0, "ok", member)
  135. }
  136. //变更用户权限.
  137. func (c *ManagerController) ChangeMemberRole() {
  138. c.Prepare()
  139. memberId, _ := c.GetInt("member_id", 0)
  140. role, _ := c.GetInt("role", 0)
  141. if memberId <= 0 {
  142. c.JsonResult(6001, "参数错误")
  143. }
  144. if role != int(conf.MemberAdminRole) && role != int(conf.MemberGeneralRole) {
  145. c.JsonResult(6001, "用户权限不正确")
  146. }
  147. member := models.NewMember()
  148. if _, err := member.Find(memberId); err != nil {
  149. c.JsonResult(6002, "用户不存在")
  150. }
  151. if member.MemberId == c.Member.MemberId {
  152. c.JsonResult(6004, "不能变更自己的权限")
  153. }
  154. if member.Role == conf.MemberSuperRole {
  155. c.JsonResult(6005, "不能变更超级管理员的权限")
  156. }
  157. member.Role = conf.SystemRole(role)
  158. if err := member.Update(); err != nil {
  159. c.JsonResult(6003, "用户权限设置失败")
  160. }
  161. member.ResolveRoleName()
  162. c.JsonResult(0, "ok", member)
  163. }
  164. //编辑用户信息.
  165. func (c *ManagerController) EditMember() {
  166. c.Prepare()
  167. c.TplName = "manager/edit_users.tpl"
  168. member_id, _ := c.GetInt(":id", 0)
  169. if member_id <= 0 {
  170. c.Abort("404")
  171. }
  172. member, err := models.NewMember().Find(member_id)
  173. if err != nil {
  174. beego.Error(err)
  175. c.Abort("404")
  176. }
  177. if c.Ctx.Input.IsPost() {
  178. password1 := c.GetString("password1")
  179. password2 := c.GetString("password2")
  180. email := c.GetString("email")
  181. phone := c.GetString("phone")
  182. description := c.GetString("description")
  183. member.Email = email
  184. member.Phone = phone
  185. member.Description = description
  186. member.RealName = c.GetString("real_name")
  187. if password1 != "" && password2 != password1 {
  188. c.JsonResult(6001, "确认密码不正确")
  189. }
  190. if password1 != "" && member.AuthMethod != conf.AuthMethodLDAP {
  191. member.Password = password1
  192. }
  193. if err := member.Valid(password1 == ""); err != nil {
  194. c.JsonResult(6002, err.Error())
  195. }
  196. if password1 != "" {
  197. password, err := utils.PasswordHash(password1)
  198. if err != nil {
  199. beego.Error(err)
  200. c.JsonResult(6003, "对用户密码加密时出错")
  201. }
  202. member.Password = password
  203. }
  204. if err := member.Update(); err != nil {
  205. c.JsonResult(6004, err.Error())
  206. }
  207. c.JsonResult(0, "ok")
  208. }
  209. c.Data["Model"] = member
  210. }
  211. //删除一个用户,并将该用户的所有信息转移到超级管理员上.
  212. func (c *ManagerController) DeleteMember() {
  213. c.Prepare()
  214. member_id, _ := c.GetInt("id", 0)
  215. if member_id <= 0 {
  216. c.JsonResult(404, "参数错误")
  217. }
  218. member, err := models.NewMember().Find(member_id)
  219. if err != nil {
  220. beego.Error(err)
  221. c.JsonResult(500, "用户不存在")
  222. }
  223. if member.Role == conf.MemberSuperRole {
  224. c.JsonResult(500, "不能删除超级管理员")
  225. }
  226. superMember, err := models.NewMember().FindByFieldFirst("role", 0)
  227. if err != nil {
  228. beego.Error(err)
  229. c.JsonResult(5001, "未能找到超级管理员")
  230. }
  231. err = models.NewMember().Delete(member_id, superMember.MemberId)
  232. if err != nil {
  233. beego.Error(err)
  234. c.JsonResult(5002, "删除失败")
  235. }
  236. c.JsonResult(0, "ok")
  237. }
  238. //项目列表.
  239. func (c *ManagerController) Books() {
  240. c.Prepare()
  241. c.TplName = "manager/books.tpl"
  242. pageIndex, _ := c.GetInt("page", 1)
  243. books, totalCount, err := models.NewBookResult().FindToPager(pageIndex, conf.PageSize)
  244. if err != nil {
  245. c.Abort("500")
  246. }
  247. if totalCount > 0 {
  248. //html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, 8, totalCount)
  249. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  250. c.Data["PageHtml"] = pager.HtmlPages()
  251. } else {
  252. c.Data["PageHtml"] = ""
  253. }
  254. for i, book := range books {
  255. books[i].Description = utils.StripTags(string(blackfriday.Run([]byte(book.Description))))
  256. books[i].ModifyTime = book.ModifyTime.Local()
  257. books[i].CreateTime = book.CreateTime.Local()
  258. }
  259. c.Data["Lists"] = books
  260. }
  261. //编辑项目.
  262. func (c *ManagerController) EditBook() {
  263. c.Prepare()
  264. c.TplName = "manager/edit_book.tpl"
  265. identify := c.GetString(":key")
  266. if identify == "" {
  267. c.Abort("404")
  268. }
  269. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  270. if err != nil {
  271. c.Abort("500")
  272. }
  273. if c.Ctx.Input.IsPost() {
  274. bookName := strings.TrimSpace(c.GetString("book_name"))
  275. description := strings.TrimSpace(c.GetString("description", ""))
  276. commentStatus := c.GetString("comment_status")
  277. tag := strings.TrimSpace(c.GetString("label"))
  278. orderIndex, _ := c.GetInt("order_index", 0)
  279. isDownload := strings.TrimSpace(c.GetString("is_download")) == "on"
  280. enableShare := strings.TrimSpace(c.GetString("enable_share")) == "on"
  281. isUseFirstDocument := strings.TrimSpace(c.GetString("is_use_first_document")) == "on"
  282. autoRelease := strings.TrimSpace(c.GetString("auto_release")) == "on"
  283. publisher := strings.TrimSpace(c.GetString("publisher"))
  284. historyCount, _ := c.GetInt("history_count", 0)
  285. if strings.Count(description, "") > 500 {
  286. c.JsonResult(6004, "项目描述不能大于500字")
  287. }
  288. if commentStatus != "open" && commentStatus != "closed" && commentStatus != "group_only" && commentStatus != "registered_only" {
  289. commentStatus = "closed"
  290. }
  291. if tag != "" {
  292. tags := strings.Split(tag, ";")
  293. if len(tags) > 10 {
  294. c.JsonResult(6005, "最多允许添加10个标签")
  295. }
  296. }
  297. book.Publisher = publisher
  298. book.HistoryCount = historyCount
  299. book.BookName = bookName
  300. book.Description = description
  301. book.CommentStatus = commentStatus
  302. book.Label = tag
  303. book.OrderIndex = orderIndex
  304. if autoRelease {
  305. book.AutoRelease = 1
  306. } else {
  307. book.AutoRelease = 0
  308. }
  309. if isDownload {
  310. book.IsDownload = 0
  311. } else {
  312. book.IsDownload = 1
  313. }
  314. if enableShare {
  315. book.IsEnableShare = 0
  316. } else {
  317. book.IsEnableShare = 1
  318. }
  319. if isUseFirstDocument {
  320. book.IsUseFirstDocument = 1
  321. } else {
  322. book.IsUseFirstDocument = 0
  323. }
  324. if err := book.Update(); err != nil {
  325. c.JsonResult(6006, "保存失败")
  326. }
  327. c.JsonResult(0, "ok")
  328. }
  329. if book.PrivateToken != "" {
  330. book.PrivateToken = conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken)
  331. }
  332. bookResult := models.NewBookResult()
  333. bookResult.ToBookResult(*book)
  334. c.Data["Model"] = bookResult
  335. }
  336. // 删除项目.
  337. func (c *ManagerController) DeleteBook() {
  338. c.Prepare()
  339. bookId, _ := c.GetInt("book_id", 0)
  340. if bookId <= 0 {
  341. c.JsonResult(6001, "参数错误")
  342. }
  343. book := models.NewBook()
  344. err := book.ThoroughDeleteBook(bookId)
  345. if err == orm.ErrNoRows {
  346. c.JsonResult(6002, "项目不存在")
  347. }
  348. if err != nil {
  349. logs.Error("DeleteBook => ", err)
  350. c.JsonResult(6003, "删除失败")
  351. }
  352. c.JsonResult(0, "ok")
  353. }
  354. // CreateToken 创建访问来令牌.
  355. func (c *ManagerController) CreateToken() {
  356. c.Prepare()
  357. action := c.GetString("action")
  358. identify := c.GetString("identify")
  359. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  360. if err != nil {
  361. c.JsonResult(6001, "项目不存在")
  362. }
  363. if action == "create" {
  364. if book.PrivatelyOwned == 0 {
  365. c.JsonResult(6001, "公开项目不能创建阅读令牌")
  366. }
  367. book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
  368. if err := book.Update(); err != nil {
  369. logs.Error("生成阅读令牌失败 => ", err)
  370. c.JsonResult(6003, "生成阅读令牌失败")
  371. }
  372. c.JsonResult(0, "ok", conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken))
  373. } else {
  374. book.PrivateToken = ""
  375. if err := book.Update(); err != nil {
  376. logs.Error("CreateToken => ", err)
  377. c.JsonResult(6004, "删除令牌失败")
  378. }
  379. c.JsonResult(0, "ok", "")
  380. }
  381. }
  382. //项目设置.
  383. func (c *ManagerController) Setting() {
  384. c.Prepare()
  385. c.TplName = "manager/setting.tpl"
  386. options, err := models.NewOption().All()
  387. if c.Ctx.Input.IsPost() {
  388. for _, item := range options {
  389. item.OptionValue = c.GetString(item.OptionName)
  390. item.InsertOrUpdate()
  391. }
  392. c.JsonResult(0, "ok")
  393. }
  394. if err != nil {
  395. c.Abort("500")
  396. }
  397. c.Data["SITE_TITLE"] = c.Option["SITE_NAME"]
  398. for _, item := range options {
  399. c.Data[item.OptionName] = item.OptionValue
  400. }
  401. }
  402. // Transfer 转让项目.
  403. func (c *ManagerController) Transfer() {
  404. c.Prepare()
  405. account := c.GetString("account")
  406. if account == "" {
  407. c.JsonResult(6004, "接受者账号不能为空")
  408. }
  409. member, err := models.NewMember().FindByAccount(account)
  410. if err != nil {
  411. logs.Error("FindByAccount => ", err)
  412. c.JsonResult(6005, "接受用户不存在")
  413. }
  414. if member.Status != 0 {
  415. c.JsonResult(6006, "接受用户已被禁用")
  416. }
  417. if !c.Member.IsAdministrator() {
  418. c.Abort("403")
  419. }
  420. identify := c.GetString("identify")
  421. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  422. if err != nil {
  423. c.JsonResult(6001, err.Error())
  424. }
  425. rel, err := models.NewRelationship().FindFounder(book.BookId)
  426. if err != nil {
  427. beego.Error("FindFounder => ", err)
  428. c.JsonResult(6009, "查询项目创始人失败")
  429. }
  430. if member.MemberId == rel.MemberId {
  431. c.JsonResult(6007, "不能转让给自己")
  432. }
  433. err = models.NewRelationship().Transfer(book.BookId, rel.MemberId, member.MemberId)
  434. if err != nil {
  435. logs.Error("Transfer => ", err)
  436. c.JsonResult(6008, err.Error())
  437. }
  438. c.JsonResult(0, "ok")
  439. }
  440. func (c *ManagerController) Comments() {
  441. c.Prepare()
  442. c.TplName = "manager/comments.tpl"
  443. if !c.Member.IsAdministrator() {
  444. c.Abort("403")
  445. }
  446. }
  447. //DeleteComment 标记评论为已删除
  448. func (c *ManagerController) DeleteComment() {
  449. c.Prepare()
  450. comment_id, _ := c.GetInt("comment_id", 0)
  451. if comment_id <= 0 {
  452. c.JsonResult(6001, "参数错误")
  453. }
  454. comment := models.NewComment()
  455. if _, err := comment.Find(comment_id); err != nil {
  456. c.JsonResult(6002, "评论不存在")
  457. }
  458. comment.Approved = 3
  459. if err := comment.Update("approved"); err != nil {
  460. c.JsonResult(6003, "删除评论失败")
  461. }
  462. c.JsonResult(0, "ok", comment)
  463. }
  464. //设置项目私有状态.
  465. func (c *ManagerController) PrivatelyOwned() {
  466. c.Prepare()
  467. status := c.GetString("status")
  468. identify := c.GetString("identify")
  469. if status != "open" && status != "close" {
  470. c.JsonResult(6003, "参数错误")
  471. }
  472. state := 0
  473. if status == "open" {
  474. state = 0
  475. } else {
  476. state = 1
  477. }
  478. if !c.Member.IsAdministrator() {
  479. c.Abort("403")
  480. }
  481. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  482. if err != nil {
  483. c.JsonResult(6001, err.Error())
  484. }
  485. book.PrivatelyOwned = state
  486. logs.Info("", state, status)
  487. err = book.Update()
  488. if err != nil {
  489. logs.Error("PrivatelyOwned => ", err)
  490. c.JsonResult(6004, "保存失败")
  491. }
  492. c.JsonResult(0, "ok")
  493. }
  494. //附件列表.
  495. func (c *ManagerController) AttachList() {
  496. c.Prepare()
  497. c.TplName = "manager/attach_list.tpl"
  498. pageIndex, _ := c.GetInt("page", 1)
  499. attachList, totalCount, err := models.NewAttachment().FindToPager(pageIndex, conf.PageSize)
  500. if err != nil {
  501. c.Abort("500")
  502. }
  503. if totalCount > 0 {
  504. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  505. c.Data["PageHtml"] = pager.HtmlPages()
  506. } else {
  507. c.Data["PageHtml"] = ""
  508. }
  509. for _, item := range attachList {
  510. p := filepath.Join(conf.WorkingDirectory, item.FilePath)
  511. item.IsExist = filetil.FileExists(p)
  512. }
  513. c.Data["Lists"] = attachList
  514. }
  515. //附件详情.
  516. func (c *ManagerController) AttachDetailed() {
  517. c.Prepare()
  518. c.TplName = "manager/attach_detailed.tpl"
  519. attach_id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  520. if attach_id <= 0 {
  521. c.Abort("404")
  522. }
  523. attach, err := models.NewAttachmentResult().Find(attach_id)
  524. if err != nil {
  525. beego.Error("AttachDetailed => ", err)
  526. if err == orm.ErrNoRows {
  527. c.Abort("404")
  528. } else {
  529. c.Abort("500")
  530. }
  531. }
  532. attach.FilePath = filepath.Join(conf.WorkingDirectory, attach.FilePath)
  533. attach.HttpPath = conf.URLForWithCdnImage(attach.HttpPath)
  534. attach.IsExist = filetil.FileExists(attach.FilePath)
  535. c.Data["Model"] = attach
  536. }
  537. //删除附件.
  538. func (c *ManagerController) AttachDelete() {
  539. c.Prepare()
  540. attachId, _ := c.GetInt("attach_id")
  541. if attachId <= 0 {
  542. c.Abort("404")
  543. }
  544. attach, err := models.NewAttachment().Find(attachId)
  545. if err != nil {
  546. beego.Error("AttachDelete => ", err)
  547. c.JsonResult(6001, err.Error())
  548. }
  549. attach.FilePath = filepath.Join(conf.WorkingDirectory, attach.FilePath)
  550. if err := attach.Delete(); err != nil {
  551. beego.Error("AttachDelete => ", err)
  552. c.JsonResult(6002, err.Error())
  553. }
  554. c.JsonResult(0, "ok")
  555. }
  556. //标签列表
  557. func (c *ManagerController) LabelList() {
  558. c.Prepare()
  559. c.TplName = "manager/label_list.tpl"
  560. pageIndex, _ := c.GetInt("page", 1)
  561. labels, totalCount, err := models.NewLabel().FindToPager(pageIndex, conf.PageSize)
  562. if err != nil {
  563. c.ShowErrorPage(50001, err.Error())
  564. }
  565. if totalCount > 0 {
  566. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  567. c.Data["PageHtml"] = pager.HtmlPages()
  568. } else {
  569. c.Data["PageHtml"] = ""
  570. }
  571. c.Data["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(conf.PageSize)))
  572. c.Data["Lists"] = labels
  573. }
  574. //删除标签
  575. func (c *ManagerController) LabelDelete() {
  576. labelId, err := strconv.Atoi(c.Ctx.Input.Param(":id"))
  577. if err != nil {
  578. beego.Error("获取删除标签参数时出错:", err)
  579. c.JsonResult(50001, "参数错误")
  580. }
  581. if labelId <= 0 {
  582. c.JsonResult(50001, "参数错误")
  583. }
  584. label, err := models.NewLabel().FindFirst("label_id", labelId)
  585. if err != nil {
  586. beego.Error("查询标签时出错:", err)
  587. c.JsonResult(50001, "查询标签时出错:"+err.Error())
  588. }
  589. if err := label.Delete(); err != nil {
  590. c.JsonResult(50002, "删除失败:"+err.Error())
  591. } else {
  592. c.JsonResult(0, "ok")
  593. }
  594. }
  595. func (c *ManagerController) Config() {
  596. c.Prepare()
  597. c.TplName = "manager/config.tpl"
  598. if c.Ctx.Input.IsPost() {
  599. content := strings.TrimSpace(c.GetString("configFileTextArea"))
  600. if content == "" {
  601. c.JsonResult(500, "配置文件不能为空")
  602. }
  603. tf, err := ioutil.TempFile(os.TempDir(), "mindoc")
  604. if err != nil {
  605. beego.Error("创建临时文件失败 ->", err)
  606. c.JsonResult(5001, "创建临时文件失败")
  607. }
  608. defer tf.Close()
  609. tf.WriteString(content)
  610. err = beego.LoadAppConfig("ini", tf.Name())
  611. if err != nil {
  612. beego.Error("加载配置文件失败 ->", err)
  613. c.JsonResult(5002, "加载配置文件失败")
  614. }
  615. err = filetil.CopyFile(tf.Name(), conf.ConfigurationFile)
  616. if err != nil {
  617. beego.Error("保存配置文件失败 ->", err)
  618. c.JsonResult(5003, "保存配置文件失败")
  619. }
  620. c.JsonResult(0, "保存成功")
  621. }
  622. c.Data["ConfigContent"] = ""
  623. if b, err := ioutil.ReadFile(conf.ConfigurationFile); err == nil {
  624. c.Data["ConfigContent"] = string(b)
  625. }
  626. }
  627. func (c *ManagerController) Team() {
  628. c.Prepare()
  629. c.TplName = "manager/team.tpl"
  630. pageIndex, _ := c.GetInt("page", 0)
  631. teams, totalCount, err := models.NewTeam().FindToPager(pageIndex, conf.PageSize)
  632. if err != nil && err != orm.ErrNoRows {
  633. c.ShowErrorPage(500, err.Error())
  634. }
  635. if err == orm.ErrNoRows || len(teams) <= 0 {
  636. c.Data["Result"] = template.JS("[]")
  637. c.Data["PageHtml"] = ""
  638. return
  639. }
  640. if totalCount > 0 {
  641. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  642. c.Data["PageHtml"] = pager.HtmlPages()
  643. } else {
  644. c.Data["PageHtml"] = ""
  645. }
  646. b, err := json.Marshal(teams)
  647. if err != nil {
  648. c.Data["Result"] = template.JS("[]")
  649. } else {
  650. c.Data["Result"] = template.JS(string(b))
  651. }
  652. }
  653. func (c *ManagerController) TeamCreate() {
  654. c.Prepare()
  655. teamName := c.GetString("teamName")
  656. if teamName == "" {
  657. c.JsonResult(5001, "团队名称不能为空")
  658. }
  659. team := models.NewTeam()
  660. team.MemberId = c.Member.MemberId
  661. team.TeamName = teamName
  662. if err := team.Save(); err == nil {
  663. c.JsonResult(0, "OK", team)
  664. } else {
  665. c.JsonResult(5002, err.Error())
  666. }
  667. }
  668. func (c *ManagerController) TeamEdit() {
  669. c.Prepare()
  670. teamName := c.GetString("teamName")
  671. teamId, _ := c.GetInt("teamId")
  672. if teamName == "" {
  673. c.JsonResult(5001, "团队名称不能为空")
  674. }
  675. if teamId <= 0 {
  676. c.JsonResult(5002, "团队标识不能为空")
  677. }
  678. team, err := models.NewTeam().First(teamId)
  679. c.CheckJsonError(5003, err)
  680. team.TeamName = teamName
  681. err = team.Save()
  682. c.CheckJsonError(5004, err)
  683. c.JsonResult(0, "OK", team)
  684. }
  685. func (c *ManagerController) TeamDelete() {
  686. c.Prepare()
  687. teamId, _ := c.GetInt("teamId")
  688. if teamId <= 0 {
  689. c.JsonResult(5002, "团队标识不能为空")
  690. }
  691. err := models.NewTeam().Delete(teamId)
  692. c.CheckJsonError(5001, err)
  693. c.JsonResult(0, "")
  694. }
  695. func (c *ManagerController) TeamMemberList() {
  696. c.Prepare()
  697. c.TplName = "manager/team_member_list.tpl"
  698. teamId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  699. pageIndex, _ := c.GetInt("page", 0)
  700. if teamId <= 0 {
  701. c.ShowErrorPage(500, "参数错误")
  702. }
  703. team, err := models.NewTeam().First(teamId)
  704. if err == orm.ErrNoRows {
  705. c.ShowErrorPage(404, "团队不存在")
  706. }
  707. c.CheckErrorResult(500, err)
  708. c.Data["Model"] = team
  709. teams, totalCount, err := models.NewTeamMember().FindToPager(teamId, pageIndex, conf.PageSize)
  710. if err != nil && err != orm.ErrNoRows {
  711. c.ShowErrorPage(500, err.Error())
  712. }
  713. if err == orm.ErrNoRows || len(teams) <= 0 {
  714. c.Data["Result"] = template.JS("[]")
  715. c.Data["PageHtml"] = ""
  716. return
  717. }
  718. if totalCount > 0 {
  719. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  720. c.Data["PageHtml"] = pager.HtmlPages()
  721. } else {
  722. c.Data["PageHtml"] = ""
  723. }
  724. b, err := json.Marshal(teams)
  725. if err != nil {
  726. beego.Error("编码 JSON 结果失败 ->", err)
  727. c.Data["Result"] = template.JS("[]")
  728. } else {
  729. c.Data["Result"] = template.JS(string(b))
  730. }
  731. }
  732. //搜索团队用户.
  733. func (c *ManagerController) TeamSearchMember() {
  734. c.Prepare()
  735. teamId, _ := c.GetInt("teamId")
  736. keyword := strings.TrimSpace(c.GetString("q"))
  737. if teamId <= 0 {
  738. c.JsonResult(500, "参数错误")
  739. }
  740. searchResult, err := models.NewTeamMember().FindNotJoinMemberByAccount(teamId, keyword, 10)
  741. if err != nil {
  742. c.JsonResult(500, err.Error())
  743. }
  744. c.JsonResult(0, "OK", searchResult)
  745. }
  746. func (c *ManagerController) TeamMemberAdd() {
  747. c.Prepare()
  748. teamId, _ := c.GetInt("teamId")
  749. memberId, _ := c.GetInt("memberId")
  750. roleId, _ := c.GetInt("roleId")
  751. if teamId <= 0 || memberId <= 0 || roleId <= 0 || roleId > int(conf.BookObserver) {
  752. c.JsonResult(5001, "参数不正确")
  753. }
  754. teamMember := models.NewTeamMember()
  755. teamMember.MemberId = memberId
  756. teamMember.TeamId = teamId
  757. teamMember.RoleId = conf.BookRole(roleId)
  758. if err := teamMember.Save(); err != nil {
  759. c.CheckJsonError(5001, err)
  760. }
  761. teamMember.Include()
  762. c.JsonResult(0, "OK", teamMember)
  763. }
  764. func (c *ManagerController) TeamMemberDelete() {
  765. c.Prepare()
  766. memberId, _ := c.GetInt("memberId")
  767. teamId, _ := c.GetInt("teamId")
  768. teamMember, err := models.NewTeamMember().FindFirst(teamId, memberId)
  769. if err != nil {
  770. c.JsonResult(5001, "用户不存在或已禁用")
  771. }
  772. err = teamMember.Delete(teamMember.TeamMemberId)
  773. if err != nil {
  774. c.JsonResult(5002, "删除失败")
  775. }
  776. c.JsonResult(0, "ok")
  777. }
  778. func (c *ManagerController) TeamChangeMemberRole() {
  779. c.Prepare()
  780. memberId, _ := c.GetInt("memberId")
  781. roleId, _ := c.GetInt("roleId")
  782. teamId, _ := c.GetInt("teamId")
  783. if memberId <= 0 || roleId <= 0 || teamId <= 0 || roleId > int(conf.BookObserver) {
  784. c.JsonResult(5001, "参数错误")
  785. }
  786. teamMember, err := models.NewTeamMember().ChangeRoleId(teamId, memberId, conf.BookRole(roleId))
  787. if err != nil {
  788. c.JsonResult(5002, err.Error())
  789. } else {
  790. c.JsonResult(0, "OK", teamMember)
  791. }
  792. }
  793. //团队项目列表.
  794. func (c *ManagerController) TeamBookList() {
  795. c.Prepare()
  796. c.TplName = "manager/team_book_list.tpl"
  797. teamId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  798. pageIndex, _ := c.GetInt("page", 0)
  799. if teamId <= 0 {
  800. c.JsonResult(5002, "团队标识不能为空")
  801. }
  802. team, err := models.NewTeam().First(teamId)
  803. if err == orm.ErrNoRows {
  804. c.ShowErrorPage(404, "团队不存在")
  805. }
  806. c.CheckErrorResult(500, err)
  807. c.Data["Model"] = team
  808. teams, totalCount, err := models.NewTeamRelationship().FindToPager(teamId, pageIndex, conf.PageSize)
  809. if err != nil && err != orm.ErrNoRows {
  810. c.ShowErrorPage(500, err.Error())
  811. }
  812. if err == orm.ErrNoRows || len(teams) <= 0 {
  813. c.Data["Result"] = template.JS("[]")
  814. c.Data["PageHtml"] = ""
  815. return
  816. }
  817. if totalCount > 0 {
  818. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  819. c.Data["PageHtml"] = pager.HtmlPages()
  820. } else {
  821. c.Data["PageHtml"] = ""
  822. }
  823. b, err := json.Marshal(teams)
  824. if err != nil {
  825. beego.Error("编码 JSON 结果失败 ->", err)
  826. c.Data["Result"] = template.JS("[]")
  827. } else {
  828. c.Data["Result"] = template.JS(string(b))
  829. }
  830. }
  831. //给团队增加项目.
  832. func (c *ManagerController) TeamBookAdd() {
  833. c.Prepare()
  834. teamId, _ := c.GetInt("teamId")
  835. bookId, _ := c.GetInt("bookId")
  836. if teamId <= 0 || bookId <= 0 {
  837. c.JsonResult(500, "参数错误")
  838. }
  839. teamRel := models.NewTeamRelationship()
  840. teamRel.BookId = bookId
  841. teamRel.TeamId = teamId
  842. err := teamRel.Save()
  843. if err != nil {
  844. c.JsonResult(5001, err.Error())
  845. } else {
  846. teamRel.Include()
  847. c.JsonResult(0, "OK", teamRel)
  848. }
  849. }
  850. //搜索未参与的项目.
  851. func (c *ManagerController) TeamSearchBook() {
  852. c.Prepare()
  853. teamId, _ := c.GetInt("teamId")
  854. keyword := strings.TrimSpace(c.GetString("q"))
  855. if teamId <= 0 {
  856. c.JsonResult(500, "参数错误")
  857. }
  858. searchResult, err := models.NewTeamRelationship().FindNotJoinBookByName(teamId, keyword, 10)
  859. if err != nil {
  860. c.JsonResult(500, err.Error())
  861. }
  862. c.JsonResult(0, "OK", searchResult)
  863. }
  864. func (c *ManagerController) TeamBookDelete() {
  865. c.Prepare()
  866. teamRelationshipId, _ := c.GetInt("teamRelId")
  867. if teamRelationshipId <= 0 {
  868. c.JsonResult(500, "参数错误")
  869. }
  870. err := models.NewTeamRelationship().Delete(teamRelationshipId)
  871. if err != nil {
  872. c.JsonResult(5001, "删除失败")
  873. }
  874. c.JsonResult(0, "OK")
  875. }