ManagerController.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. func (c *ManagerController) TeamBookList() {
  794. c.Prepare()
  795. c.TplName = "manager/team_book_list.tpl"
  796. }
  797. func (c *ManagerController) TeamBookAdd() {
  798. c.Prepare()
  799. }
  800. func (c *ManagerController) TeamBookDelete() {
  801. c.Prepare()
  802. }