ManagerController.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "html/template"
  5. "regexp"
  6. "strings"
  7. "math"
  8. "path/filepath"
  9. "strconv"
  10. "io/ioutil"
  11. "os"
  12. "github.com/beego/beego/v2/client/orm"
  13. "github.com/beego/beego/v2/core/logs"
  14. "github.com/beego/beego/v2/server/web"
  15. "github.com/mindoc-org/mindoc/conf"
  16. "github.com/mindoc-org/mindoc/models"
  17. "github.com/mindoc-org/mindoc/utils"
  18. "github.com/mindoc-org/mindoc/utils/filetil"
  19. "github.com/mindoc-org/mindoc/utils/pagination"
  20. "github.com/russross/blackfriday/v2"
  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. logs.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. logs.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. logs.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. logs.Error(err)
  229. c.JsonResult(5001, "未能找到超级管理员")
  230. }
  231. err = models.NewMember().Delete(member_id, superMember.MemberId)
  232. if err != nil {
  233. logs.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. itemId, _ := c.GetInt("itemId")
  286. if strings.Count(description, "") > 500 {
  287. c.JsonResult(6004, "项目描述不能大于500字")
  288. }
  289. if commentStatus != "open" && commentStatus != "closed" && commentStatus != "group_only" && commentStatus != "registered_only" {
  290. commentStatus = "closed"
  291. }
  292. if tag != "" {
  293. tags := strings.Split(tag, ";")
  294. if len(tags) > 10 {
  295. c.JsonResult(6005, "最多允许添加10个标签")
  296. }
  297. }
  298. if !models.NewItemsets().Exist(itemId) {
  299. c.JsonResult(6006, "项目空间不存在")
  300. }
  301. book.Publisher = publisher
  302. book.HistoryCount = historyCount
  303. book.BookName = bookName
  304. book.Description = description
  305. book.CommentStatus = commentStatus
  306. book.Label = tag
  307. book.OrderIndex = orderIndex
  308. book.ItemId = itemId
  309. book.BookPassword = strings.TrimSpace(c.GetString("bPassword"))
  310. if autoRelease {
  311. book.AutoRelease = 1
  312. } else {
  313. book.AutoRelease = 0
  314. }
  315. if isDownload {
  316. book.IsDownload = 0
  317. } else {
  318. book.IsDownload = 1
  319. }
  320. if enableShare {
  321. book.IsEnableShare = 0
  322. } else {
  323. book.IsEnableShare = 1
  324. }
  325. if isUseFirstDocument {
  326. book.IsUseFirstDocument = 1
  327. } else {
  328. book.IsUseFirstDocument = 0
  329. }
  330. if err := book.Update(); err != nil {
  331. c.JsonResult(6006, "保存失败")
  332. }
  333. c.JsonResult(0, "ok")
  334. }
  335. if book.PrivateToken != "" {
  336. book.PrivateToken = conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken)
  337. }
  338. bookResult := models.NewBookResult()
  339. bookResult.ToBookResult(*book)
  340. c.Data["Model"] = bookResult
  341. }
  342. // 删除项目.
  343. func (c *ManagerController) DeleteBook() {
  344. c.Prepare()
  345. bookId, _ := c.GetInt("book_id", 0)
  346. if bookId <= 0 {
  347. c.JsonResult(6001, "参数错误")
  348. }
  349. book := models.NewBook()
  350. err := book.ThoroughDeleteBook(bookId)
  351. if err == orm.ErrNoRows {
  352. c.JsonResult(6002, "项目不存在")
  353. }
  354. if err != nil {
  355. logs.Error("删除失败 -> ", err)
  356. c.JsonResult(6003, "删除失败")
  357. }
  358. c.JsonResult(0, "ok")
  359. }
  360. // CreateToken 创建访问来令牌.
  361. func (c *ManagerController) CreateToken() {
  362. c.Prepare()
  363. action := c.GetString("action")
  364. identify := c.GetString("identify")
  365. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  366. if err != nil {
  367. c.JsonResult(6001, "项目不存在")
  368. }
  369. if action == "create" {
  370. if book.PrivatelyOwned == 0 {
  371. c.JsonResult(6001, "公开项目不能创建阅读令牌")
  372. }
  373. book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
  374. if err := book.Update(); err != nil {
  375. logs.Error("生成阅读令牌失败 => ", err)
  376. c.JsonResult(6003, "生成阅读令牌失败")
  377. }
  378. c.JsonResult(0, "ok", conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken))
  379. } else {
  380. book.PrivateToken = ""
  381. if err := book.Update(); err != nil {
  382. logs.Error("CreateToken => ", err)
  383. c.JsonResult(6004, "删除令牌失败")
  384. }
  385. c.JsonResult(0, "ok", "")
  386. }
  387. }
  388. //项目设置.
  389. func (c *ManagerController) Setting() {
  390. c.Prepare()
  391. c.TplName = "manager/setting.tpl"
  392. options, err := models.NewOption().All()
  393. if c.Ctx.Input.IsPost() {
  394. for _, item := range options {
  395. item.OptionValue = c.GetString(item.OptionName)
  396. item.InsertOrUpdate()
  397. }
  398. c.JsonResult(0, "ok")
  399. }
  400. if err != nil {
  401. c.Abort("500")
  402. }
  403. c.Data["SITE_TITLE"] = c.Option["SITE_NAME"]
  404. for _, item := range options {
  405. c.Data[item.OptionName] = item.OptionValue
  406. }
  407. }
  408. // Transfer 转让项目.
  409. func (c *ManagerController) Transfer() {
  410. c.Prepare()
  411. account := c.GetString("account")
  412. if account == "" {
  413. c.JsonResult(6004, "接受者账号不能为空")
  414. }
  415. member, err := models.NewMember().FindByAccount(account)
  416. if err != nil {
  417. logs.Error("FindByAccount => ", err)
  418. c.JsonResult(6005, "接受用户不存在")
  419. }
  420. if member.Status != 0 {
  421. c.JsonResult(6006, "接受用户已被禁用")
  422. }
  423. if !c.Member.IsAdministrator() {
  424. c.Abort("403")
  425. }
  426. identify := c.GetString("identify")
  427. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  428. if err != nil {
  429. c.JsonResult(6001, err.Error())
  430. }
  431. rel, err := models.NewRelationship().FindFounder(book.BookId)
  432. if err != nil {
  433. logs.Error("FindFounder => ", err)
  434. c.JsonResult(6009, "查询项目创始人失败")
  435. }
  436. if member.MemberId == rel.MemberId {
  437. c.JsonResult(6007, "不能转让给自己")
  438. }
  439. err = models.NewRelationship().Transfer(book.BookId, rel.MemberId, member.MemberId)
  440. if err != nil {
  441. logs.Error("Transfer => ", err)
  442. c.JsonResult(6008, err.Error())
  443. }
  444. c.JsonResult(0, "ok")
  445. }
  446. func (c *ManagerController) Comments() {
  447. c.Prepare()
  448. c.TplName = "manager/comments.tpl"
  449. if !c.Member.IsAdministrator() {
  450. c.Abort("403")
  451. }
  452. }
  453. //DeleteComment 标记评论为已删除
  454. func (c *ManagerController) DeleteComment() {
  455. c.Prepare()
  456. comment_id, _ := c.GetInt("comment_id", 0)
  457. if comment_id <= 0 {
  458. c.JsonResult(6001, "参数错误")
  459. }
  460. comment := models.NewComment()
  461. if _, err := comment.Find(comment_id); err != nil {
  462. c.JsonResult(6002, "评论不存在")
  463. }
  464. comment.Approved = 3
  465. if err := comment.Update("approved"); err != nil {
  466. c.JsonResult(6003, "删除评论失败")
  467. }
  468. c.JsonResult(0, "ok", comment)
  469. }
  470. //设置项目私有状态.
  471. func (c *ManagerController) PrivatelyOwned() {
  472. c.Prepare()
  473. status := c.GetString("status")
  474. identify := c.GetString("identify")
  475. if status != "open" && status != "close" {
  476. c.JsonResult(6003, "参数错误")
  477. }
  478. state := 0
  479. if status == "open" {
  480. state = 0
  481. } else {
  482. state = 1
  483. }
  484. if !c.Member.IsAdministrator() {
  485. c.Abort("403")
  486. }
  487. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  488. if err != nil {
  489. c.JsonResult(6001, err.Error())
  490. }
  491. book.PrivatelyOwned = state
  492. logs.Info("", state, status)
  493. err = book.Update()
  494. if err != nil {
  495. logs.Error("PrivatelyOwned => ", err)
  496. c.JsonResult(6004, "保存失败")
  497. }
  498. c.JsonResult(0, "ok")
  499. }
  500. //附件列表.
  501. func (c *ManagerController) AttachList() {
  502. c.Prepare()
  503. c.TplName = "manager/attach_list.tpl"
  504. pageIndex, _ := c.GetInt("page", 1)
  505. attachList, totalCount, err := models.NewAttachment().FindToPager(pageIndex, conf.PageSize)
  506. if err != nil {
  507. c.Abort("500")
  508. }
  509. if totalCount > 0 {
  510. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  511. c.Data["PageHtml"] = pager.HtmlPages()
  512. } else {
  513. c.Data["PageHtml"] = ""
  514. }
  515. for _, item := range attachList {
  516. p := filepath.Join(conf.WorkingDirectory, item.FilePath)
  517. item.IsExist = filetil.FileExists(p)
  518. }
  519. c.Data["Lists"] = attachList
  520. }
  521. //附件详情.
  522. func (c *ManagerController) AttachDetailed() {
  523. c.Prepare()
  524. c.TplName = "manager/attach_detailed.tpl"
  525. attach_id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  526. if attach_id <= 0 {
  527. c.Abort("404")
  528. }
  529. attach, err := models.NewAttachmentResult().Find(attach_id)
  530. if err != nil {
  531. logs.Error("AttachDetailed => ", err)
  532. if err == orm.ErrNoRows {
  533. c.Abort("404")
  534. } else {
  535. c.Abort("500")
  536. }
  537. }
  538. attach.FilePath = filepath.Join(conf.WorkingDirectory, attach.FilePath)
  539. attach.HttpPath = conf.URLForWithCdnImage(attach.HttpPath)
  540. attach.IsExist = filetil.FileExists(attach.FilePath)
  541. c.Data["Model"] = attach
  542. }
  543. //删除附件.
  544. func (c *ManagerController) AttachDelete() {
  545. c.Prepare()
  546. attachId, _ := c.GetInt("attach_id")
  547. if attachId <= 0 {
  548. c.Abort("404")
  549. }
  550. attach, err := models.NewAttachment().Find(attachId)
  551. if err != nil {
  552. logs.Error("AttachDelete => ", err)
  553. c.JsonResult(6001, err.Error())
  554. }
  555. attach.FilePath = filepath.Join(conf.WorkingDirectory, attach.FilePath)
  556. if err := attach.Delete(); err != nil {
  557. logs.Error("AttachDelete => ", err)
  558. c.JsonResult(6002, err.Error())
  559. }
  560. c.JsonResult(0, "ok")
  561. }
  562. //标签列表
  563. func (c *ManagerController) LabelList() {
  564. c.Prepare()
  565. c.TplName = "manager/label_list.tpl"
  566. pageIndex, _ := c.GetInt("page", 1)
  567. labels, totalCount, err := models.NewLabel().FindToPager(pageIndex, conf.PageSize)
  568. if err != nil {
  569. c.ShowErrorPage(50001, err.Error())
  570. }
  571. if totalCount > 0 {
  572. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  573. c.Data["PageHtml"] = pager.HtmlPages()
  574. } else {
  575. c.Data["PageHtml"] = ""
  576. }
  577. c.Data["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(conf.PageSize)))
  578. c.Data["Lists"] = labels
  579. }
  580. //删除标签
  581. func (c *ManagerController) LabelDelete() {
  582. labelId, err := strconv.Atoi(c.Ctx.Input.Param(":id"))
  583. if err != nil {
  584. logs.Error("获取删除标签参数时出错:", err)
  585. c.JsonResult(50001, "参数错误")
  586. }
  587. if labelId <= 0 {
  588. c.JsonResult(50001, "参数错误")
  589. }
  590. label, err := models.NewLabel().FindFirst("label_id", labelId)
  591. if err != nil {
  592. logs.Error("查询标签时出错:", err)
  593. c.JsonResult(50001, "查询标签时出错:"+err.Error())
  594. }
  595. if err := label.Delete(); err != nil {
  596. c.JsonResult(50002, "删除失败:"+err.Error())
  597. } else {
  598. c.JsonResult(0, "ok")
  599. }
  600. }
  601. func (c *ManagerController) Config() {
  602. c.Prepare()
  603. c.TplName = "manager/config.tpl"
  604. if c.Ctx.Input.IsPost() {
  605. content := strings.TrimSpace(c.GetString("configFileTextArea"))
  606. if content == "" {
  607. c.JsonResult(500, "配置文件不能为空")
  608. }
  609. tf, err := ioutil.TempFile(os.TempDir(), "mindoc")
  610. if err != nil {
  611. logs.Error("创建临时文件失败 ->", err)
  612. c.JsonResult(5001, "创建临时文件失败")
  613. }
  614. defer tf.Close()
  615. tf.WriteString(content)
  616. err = web.LoadAppConfig("ini", tf.Name())
  617. if err != nil {
  618. logs.Error("加载配置文件失败 ->", err)
  619. c.JsonResult(5002, "加载配置文件失败")
  620. }
  621. err = filetil.CopyFile(tf.Name(), conf.ConfigurationFile)
  622. if err != nil {
  623. logs.Error("保存配置文件失败 ->", err)
  624. c.JsonResult(5003, "保存配置文件失败")
  625. }
  626. c.JsonResult(0, "保存成功")
  627. }
  628. c.Data["ConfigContent"] = ""
  629. if b, err := ioutil.ReadFile(conf.ConfigurationFile); err == nil {
  630. c.Data["ConfigContent"] = string(b)
  631. }
  632. }
  633. func (c *ManagerController) Team() {
  634. c.Prepare()
  635. c.TplName = "manager/team.tpl"
  636. pageIndex, _ := c.GetInt("page", 0)
  637. teams, totalCount, err := models.NewTeam().FindToPager(pageIndex, conf.PageSize)
  638. if err != nil && err != orm.ErrNoRows {
  639. c.ShowErrorPage(500, err.Error())
  640. }
  641. if err == orm.ErrNoRows || len(teams) <= 0 {
  642. c.Data["Result"] = template.JS("[]")
  643. c.Data["PageHtml"] = ""
  644. return
  645. }
  646. if totalCount > 0 {
  647. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  648. c.Data["PageHtml"] = pager.HtmlPages()
  649. } else {
  650. c.Data["PageHtml"] = ""
  651. }
  652. b, err := json.Marshal(teams)
  653. if err != nil {
  654. c.Data["Result"] = template.JS("[]")
  655. } else {
  656. c.Data["Result"] = template.JS(string(b))
  657. }
  658. }
  659. func (c *ManagerController) TeamCreate() {
  660. c.Prepare()
  661. teamName := c.GetString("teamName")
  662. if teamName == "" {
  663. c.JsonResult(5001, "团队名称不能为空")
  664. }
  665. team := models.NewTeam()
  666. team.MemberId = c.Member.MemberId
  667. team.TeamName = teamName
  668. if err := team.Save(); err == nil {
  669. c.JsonResult(0, "OK", team)
  670. } else {
  671. c.JsonResult(5002, err.Error())
  672. }
  673. }
  674. func (c *ManagerController) TeamEdit() {
  675. c.Prepare()
  676. teamName := c.GetString("teamName")
  677. teamId, _ := c.GetInt("teamId")
  678. if teamName == "" {
  679. c.JsonResult(5001, "团队名称不能为空")
  680. }
  681. if teamId <= 0 {
  682. c.JsonResult(5002, "团队标识不能为空")
  683. }
  684. team, err := models.NewTeam().First(teamId)
  685. c.CheckJsonError(5003, err)
  686. team.TeamName = teamName
  687. err = team.Save()
  688. c.CheckJsonError(5004, err)
  689. c.JsonResult(0, "OK", team)
  690. }
  691. func (c *ManagerController) TeamDelete() {
  692. c.Prepare()
  693. teamId, _ := c.GetInt("teamId")
  694. if teamId <= 0 {
  695. c.JsonResult(5002, "团队标识不能为空")
  696. }
  697. err := models.NewTeam().Delete(teamId)
  698. c.CheckJsonError(5001, err)
  699. c.JsonResult(0, "OK")
  700. }
  701. func (c *ManagerController) TeamMemberList() {
  702. c.Prepare()
  703. c.TplName = "manager/team_member_list.tpl"
  704. teamId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  705. pageIndex, _ := c.GetInt("page", 0)
  706. if teamId <= 0 {
  707. c.ShowErrorPage(500, "参数错误")
  708. }
  709. team, err := models.NewTeam().First(teamId)
  710. if err == orm.ErrNoRows {
  711. c.ShowErrorPage(404, "团队不存在")
  712. }
  713. c.CheckErrorResult(500, err)
  714. c.Data["Model"] = team
  715. teams, totalCount, err := models.NewTeamMember().FindToPager(teamId, pageIndex, conf.PageSize)
  716. if err != nil && err != orm.ErrNoRows {
  717. c.ShowErrorPage(500, err.Error())
  718. }
  719. if err == orm.ErrNoRows || len(teams) <= 0 {
  720. c.Data["Result"] = template.JS("[]")
  721. c.Data["PageHtml"] = ""
  722. return
  723. }
  724. if totalCount > 0 {
  725. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  726. c.Data["PageHtml"] = pager.HtmlPages()
  727. } else {
  728. c.Data["PageHtml"] = ""
  729. }
  730. b, err := json.Marshal(teams)
  731. if err != nil {
  732. logs.Error("编码 JSON 结果失败 ->", err)
  733. c.Data["Result"] = template.JS("[]")
  734. } else {
  735. c.Data["Result"] = template.JS(string(b))
  736. }
  737. }
  738. //搜索团队用户.
  739. func (c *ManagerController) TeamSearchMember() {
  740. c.Prepare()
  741. teamId, _ := c.GetInt("teamId")
  742. keyword := strings.TrimSpace(c.GetString("q"))
  743. if teamId <= 0 {
  744. c.JsonResult(500, "参数错误")
  745. }
  746. searchResult, err := models.NewTeamMember().FindNotJoinMemberByAccount(teamId, keyword, 10)
  747. if err != nil {
  748. c.JsonResult(500, err.Error())
  749. }
  750. c.JsonResult(0, "OK", searchResult)
  751. }
  752. func (c *ManagerController) TeamMemberAdd() {
  753. c.Prepare()
  754. teamId, _ := c.GetInt("teamId")
  755. memberId, _ := c.GetInt("memberId")
  756. roleId, _ := c.GetInt("roleId")
  757. if teamId <= 0 || memberId <= 0 || roleId <= 0 || roleId > int(conf.BookObserver) {
  758. c.JsonResult(5001, "参数不正确")
  759. }
  760. teamMember := models.NewTeamMember()
  761. teamMember.MemberId = memberId
  762. teamMember.TeamId = teamId
  763. teamMember.RoleId = conf.BookRole(roleId)
  764. if err := teamMember.Save(); err != nil {
  765. c.CheckJsonError(5001, err)
  766. }
  767. teamMember.Include()
  768. c.JsonResult(0, "OK", teamMember)
  769. }
  770. func (c *ManagerController) TeamMemberDelete() {
  771. c.Prepare()
  772. memberId, _ := c.GetInt("memberId")
  773. teamId, _ := c.GetInt("teamId")
  774. teamMember, err := models.NewTeamMember().FindFirst(teamId, memberId)
  775. if err != nil {
  776. c.JsonResult(5001, "用户不存在或已禁用")
  777. }
  778. err = teamMember.Delete(teamMember.TeamMemberId)
  779. if err != nil {
  780. c.JsonResult(5002, "删除失败")
  781. }
  782. c.JsonResult(0, "ok")
  783. }
  784. func (c *ManagerController) TeamChangeMemberRole() {
  785. c.Prepare()
  786. memberId, _ := c.GetInt("memberId")
  787. roleId, _ := c.GetInt("roleId")
  788. teamId, _ := c.GetInt("teamId")
  789. if memberId <= 0 || roleId <= 0 || teamId <= 0 || roleId > int(conf.BookObserver) {
  790. c.JsonResult(5001, "参数错误")
  791. }
  792. teamMember, err := models.NewTeamMember().ChangeRoleId(teamId, memberId, conf.BookRole(roleId))
  793. if err != nil {
  794. c.JsonResult(5002, err.Error())
  795. } else {
  796. c.JsonResult(0, "OK", teamMember)
  797. }
  798. }
  799. //团队项目列表.
  800. func (c *ManagerController) TeamBookList() {
  801. c.Prepare()
  802. c.TplName = "manager/team_book_list.tpl"
  803. teamId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  804. pageIndex, _ := c.GetInt("page", 0)
  805. if teamId <= 0 {
  806. c.JsonResult(5002, "团队标识不能为空")
  807. }
  808. team, err := models.NewTeam().First(teamId)
  809. if err == orm.ErrNoRows {
  810. c.ShowErrorPage(404, "团队不存在")
  811. }
  812. c.CheckErrorResult(500, err)
  813. c.Data["Model"] = team
  814. teams, totalCount, err := models.NewTeamRelationship().FindToPager(teamId, pageIndex, conf.PageSize)
  815. if err != nil && err != orm.ErrNoRows {
  816. c.ShowErrorPage(500, err.Error())
  817. }
  818. if err == orm.ErrNoRows || len(teams) <= 0 {
  819. c.Data["Result"] = template.JS("[]")
  820. c.Data["PageHtml"] = ""
  821. return
  822. }
  823. if totalCount > 0 {
  824. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  825. c.Data["PageHtml"] = pager.HtmlPages()
  826. } else {
  827. c.Data["PageHtml"] = ""
  828. }
  829. b, err := json.Marshal(teams)
  830. if err != nil {
  831. logs.Error("编码 JSON 结果失败 ->", err)
  832. c.Data["Result"] = template.JS("[]")
  833. } else {
  834. c.Data["Result"] = template.JS(string(b))
  835. }
  836. }
  837. //给团队增加项目.
  838. func (c *ManagerController) TeamBookAdd() {
  839. c.Prepare()
  840. teamId, _ := c.GetInt("teamId")
  841. bookId, _ := c.GetInt("bookId")
  842. if teamId <= 0 || bookId <= 0 {
  843. c.JsonResult(500, "参数错误")
  844. }
  845. teamRel := models.NewTeamRelationship()
  846. teamRel.BookId = bookId
  847. teamRel.TeamId = teamId
  848. err := teamRel.Save()
  849. if err != nil {
  850. c.JsonResult(5001, err.Error())
  851. } else {
  852. teamRel.Include()
  853. c.JsonResult(0, "OK", teamRel)
  854. }
  855. }
  856. //搜索未参与的项目.
  857. func (c *ManagerController) TeamSearchBook() {
  858. c.Prepare()
  859. teamId, _ := c.GetInt("teamId")
  860. keyword := strings.TrimSpace(c.GetString("q"))
  861. if teamId <= 0 {
  862. c.JsonResult(500, "参数错误")
  863. }
  864. searchResult, err := models.NewTeamRelationship().FindNotJoinBookByName(teamId, keyword, 10)
  865. if err != nil {
  866. c.JsonResult(500, err.Error())
  867. }
  868. c.JsonResult(0, "OK", searchResult)
  869. }
  870. //删除团队项目.
  871. func (c *ManagerController) TeamBookDelete() {
  872. c.Prepare()
  873. teamRelationshipId, _ := c.GetInt("teamRelId")
  874. if teamRelationshipId <= 0 {
  875. c.JsonResult(500, "参数错误")
  876. }
  877. err := models.NewTeamRelationship().Delete(teamRelationshipId)
  878. if err != nil {
  879. c.JsonResult(5001, "删除失败")
  880. }
  881. c.JsonResult(0, "OK")
  882. }
  883. //项目空间列表.
  884. func (c *ManagerController) Itemsets() {
  885. c.Prepare()
  886. c.TplName = "manager/itemsets.tpl"
  887. pageIndex, _ := c.GetInt("page", 0)
  888. items, totalCount, err := models.NewItemsets().FindToPager(pageIndex, conf.PageSize)
  889. if err != nil && err != orm.ErrNoRows {
  890. c.ShowErrorPage(500, err.Error())
  891. }
  892. if err == orm.ErrNoRows || len(items) <= 0 {
  893. c.Data["Lists"] = items
  894. c.Data["PageHtml"] = ""
  895. return
  896. }
  897. if totalCount > 0 {
  898. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  899. c.Data["PageHtml"] = pager.HtmlPages()
  900. } else {
  901. c.Data["PageHtml"] = ""
  902. }
  903. c.Data["Lists"] = items
  904. }
  905. //编辑或添加项目空间.
  906. func (c *ManagerController) ItemsetsEdit() {
  907. c.Prepare()
  908. itemId, _ := c.GetInt("itemId")
  909. itemName := strings.TrimSpace(c.GetString("itemName"))
  910. itemKey := strings.TrimSpace(c.GetString("itemKey"))
  911. if itemName == "" || itemKey == "" {
  912. c.JsonResult(5001, "参数错误")
  913. }
  914. var item *models.Itemsets
  915. var err error
  916. if itemId > 0 {
  917. if item, err = models.NewItemsets().First(itemId); err != nil {
  918. if err == orm.ErrNoRows {
  919. c.JsonResult(5002, "项目空间不存在")
  920. } else {
  921. c.JsonResult(5003, "查询项目空间出错")
  922. }
  923. }
  924. } else {
  925. item = models.NewItemsets()
  926. }
  927. item.ItemKey = itemKey
  928. item.ItemName = itemName
  929. item.MemberId = c.Member.MemberId
  930. item.ModifyAt = c.Member.MemberId
  931. if err := item.Save(); err != nil {
  932. c.JsonResult(5004, err.Error())
  933. }
  934. c.JsonResult(0, "OK")
  935. }
  936. //删除项目空间.
  937. func (c *ManagerController) ItemsetsDelete() {
  938. c.Prepare()
  939. itemId, _ := c.GetInt("itemId")
  940. if err := models.NewItemsets().Delete(itemId); err != nil {
  941. c.JsonResult(5001, err.Error())
  942. }
  943. c.JsonResult(0, "OK")
  944. }