ManagerController.go 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. "time"
  20. )
  21. type ManagerController struct {
  22. BaseController
  23. }
  24. func (c *ManagerController) Prepare() {
  25. c.BaseController.Prepare()
  26. if !c.Member.IsAdministrator() {
  27. c.ShowErrorPage(403,"用户权限不足")
  28. }
  29. }
  30. func (c *ManagerController) Index() {
  31. c.Prepare()
  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 = 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. member_id, _ := c.GetInt("member_id", 0)
  140. role, _ := c.GetInt("role", 0)
  141. if member_id <= 0 {
  142. c.JsonResult(6001, "参数错误")
  143. }
  144. if role != conf.MemberAdminRole && role != conf.MemberGeneralRole {
  145. c.JsonResult(6001, "用户权限不正确")
  146. }
  147. member := models.NewMember()
  148. if _, err := member.Find(member_id); 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 = 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. memberId, _ := c.GetInt("id", 0)
  215. if memberId <= 0 {
  216. c.JsonResult(404, "参数错误")
  217. }
  218. member, err := models.NewMember().Find(memberId)
  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(memberId, 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. if err := attach.Delete(); err != nil {
  550. beego.Error("AttachDelete => ", err)
  551. c.JsonResult(6002, err.Error())
  552. }
  553. c.JsonResult(0, "ok")
  554. }
  555. //标签列表
  556. func (c *ManagerController) LabelList() {
  557. c.Prepare()
  558. c.TplName = "manager/label_list.tpl"
  559. pageIndex, _ := c.GetInt("page", 1)
  560. labels, totalCount, err := models.NewLabel().FindToPager(pageIndex, conf.PageSize)
  561. if err != nil {
  562. c.ShowErrorPage(50001, err.Error())
  563. }
  564. if totalCount > 0 {
  565. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  566. c.Data["PageHtml"] = pager.HtmlPages()
  567. } else {
  568. c.Data["PageHtml"] = ""
  569. }
  570. c.Data["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(conf.PageSize)))
  571. c.Data["Lists"] = labels
  572. }
  573. //删除标签
  574. func (c *ManagerController) LabelDelete() {
  575. labelId, err := strconv.Atoi(c.Ctx.Input.Param(":id"))
  576. if err != nil {
  577. beego.Error("获取删除标签参数时出错:", err)
  578. c.JsonResult(50001, "参数错误")
  579. }
  580. if labelId <= 0 {
  581. c.JsonResult(50001, "参数错误")
  582. }
  583. label, err := models.NewLabel().FindFirst("label_id", labelId)
  584. if err != nil {
  585. beego.Error("查询标签时出错:", err)
  586. c.JsonResult(50001, "查询标签时出错:"+err.Error())
  587. }
  588. if err := label.Delete(); err != nil {
  589. c.JsonResult(50002, "删除失败:"+err.Error())
  590. } else {
  591. c.JsonResult(0, "ok")
  592. }
  593. }
  594. // 用户组列表
  595. func (c *ManagerController) MemberGroupList() {
  596. c.Prepare()
  597. c.TplName = "manager/member_group_list.tpl"
  598. pageIndex, _ := c.GetInt("page", 1)
  599. memberGroupList ,totalCount,err := models.NewMemberGroup().FindByPager(pageIndex,conf.PageSize)
  600. if err != nil {
  601. c.ShowErrorPage(50001,"获取用户组失败")
  602. }
  603. if totalCount > 0 {
  604. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  605. c.Data["PageHtml"] = pager.HtmlPages()
  606. } else {
  607. c.Data["PageHtml"] = ""
  608. }
  609. c.Data["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(conf.PageSize)))
  610. b, err := json.Marshal(memberGroupList)
  611. if err != nil {
  612. c.Data["Result"] = template.JS("[]")
  613. } else {
  614. c.Data["Result"] = template.JS(string(b))
  615. }
  616. }
  617. //编辑或添加用户组
  618. func (c *ManagerController) MemberGroupEdit() {
  619. c.Prepare()
  620. c.TplName = "manager/member_group_edit.tpl"
  621. if !c.Member.IsSuperAdministrator() {
  622. c.ShowErrorPage(500,"只有超级管理员才能编辑或添加用户组")
  623. }
  624. groupId,_ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  625. memberGroup := models.NewMemberGroup()
  626. var err error
  627. if c.Ctx.Input.IsPost() {
  628. groupId,_ := c.GetInt("group_id",0)
  629. groupName := strings.TrimSpace(c.GetString("group_name"))
  630. if groupName == "" {
  631. if c.Ctx.Input.IsAjax() {
  632. c.JsonResult(6002,"用户组名称不能为空")
  633. }else{
  634. c.ShowErrorPage(500,"用户组名称不能为空")
  635. }
  636. }
  637. if groupId > 0 {
  638. memberGroup,err = models.NewMemberGroup().FindFirst(groupId)
  639. if err != nil {
  640. beego.Error("查询用户组失败",err)
  641. if c.Ctx.Input.IsAjax() {
  642. c.JsonResult(6001,"未查到指定的用户组信息")
  643. }else{
  644. c.ShowErrorPage(500,"用户组名称不能为空")
  645. }
  646. }
  647. memberGroup.GroupName = groupName
  648. memberGroup.ModifyAt = c.Member.MemberId
  649. }else{
  650. memberGroup.GroupName = groupName
  651. memberGroup.CreateTime = time.Now()
  652. memberGroup.CreateAt = c.Member.MemberId
  653. }
  654. if err := memberGroup.InsertOrUpdate(); err != nil {
  655. beego.Error("保存用户组失败 =>",err)
  656. c.JsonResult(500,"保存失败")
  657. }else{
  658. c.JsonResult(0,"保存成功",memberGroup)
  659. }
  660. }
  661. if groupId > 0 {
  662. memberGroup,err = memberGroup.FindFirst(groupId)
  663. if err != nil {
  664. c.ShowErrorPage(500,"查询用户组失败")
  665. }
  666. }
  667. c.Data["Model"] = memberGroup
  668. }
  669. //删除用户组
  670. func (c *ManagerController) MemberGroupDelete() {
  671. c.Prepare()
  672. if !c.Member.IsSuperAdministrator() {
  673. c.JsonResult(5001,"只有超级管理员才能删除用户组")
  674. }
  675. groupId,err := c.GetInt("group_id",0)
  676. if err != nil {
  677. beego.Error("获取参数失败 =>",err)
  678. c.JsonResult(5001,"获取用户组参数失败")
  679. }
  680. err = models.NewMemberGroup().Delete(groupId)
  681. if err != nil {
  682. beego.Error("删除用户组失败 =>",err)
  683. c.JsonResult(5001,"删除用户组失败")
  684. }
  685. c.JsonResult(0,"删除成功")
  686. }
  687. //用户组成员列表
  688. func (c *ManagerController) MemberGroupMemberList() {
  689. c.Prepare()
  690. c.TplName = "manager/member_group_member_list.tpl"
  691. pageIndex, _ := c.GetInt("page", 1)
  692. groupId,_ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  693. if groupId <= 0 {
  694. c.ShowErrorPage(404,"用户组参数不能为空")
  695. }
  696. memberGroup,err := models.NewMemberGroup().FindFirst(groupId)
  697. if err != nil {
  698. c.ShowErrorPage(404,"用户组不存在")
  699. }
  700. memberGroupMemberList ,totalCount,err := models.NewMemberGroupMembers().FindToPager(pageIndex,conf.PageSize,groupId)
  701. if err != nil {
  702. c.ShowErrorPage(50001,"获取用户组失败")
  703. }
  704. if totalCount > 0 {
  705. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  706. c.Data["PageHtml"] = pager.HtmlPages()
  707. } else {
  708. c.Data["PageHtml"] = ""
  709. }
  710. c.Data["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(conf.PageSize)))
  711. b, err := json.Marshal(memberGroupMemberList)
  712. if err != nil {
  713. c.Data["Result"] = template.JS("[]")
  714. } else {
  715. c.Data["Result"] = template.JS(string(b))
  716. }
  717. c.Data["Model"] = memberGroup
  718. }
  719. //添加用户组成员
  720. func (c *ManagerController) MemberGroupMemberEdit() {
  721. c.Prepare()
  722. if !c.Member.IsSuperAdministrator() {
  723. c.JsonResult(5000,"只有超级管理员才能添加用户")
  724. }
  725. if c.Ctx.Input.IsPost() {
  726. memberId, err := c.GetInt("member_id", 0)
  727. if err != nil || memberId <= 0 {
  728. c.JsonResult(6001, "用户参数错误")
  729. }
  730. groupId, err := c.GetInt("group_id", 0)
  731. if err != nil || groupId <= 0 {
  732. c.JsonResult(6002, "用户组参数错误")
  733. }
  734. if ! models.NewMember().Exist("member_id", memberId) {
  735. beego.Error("用户不存在 =>", err)
  736. c.JsonResult(6001, "用户不存在")
  737. }
  738. if models.NewMemberGroupMembers().IsJoin(groupId, memberId) {
  739. c.JsonResult(6003, "用户已加入当前用户组")
  740. }
  741. if !models.NewMember().Exist("member_id", memberId) {
  742. c.JsonResult(6004, "用户不存在")
  743. }
  744. memberGroupMember := models.NewMemberGroupMembers()
  745. memberGroupMember.MemberId = memberId
  746. memberGroupMember.CreateAt = c.Member.MemberId
  747. memberGroupMember.GroupId = groupId
  748. memberGroupMember.CreateTime = time.Now()
  749. if err := memberGroupMember.InsertOrUpdate(); err != nil {
  750. beego.Error("添加用户失败 =>", err)
  751. c.JsonResult(6005, "添加用户失败")
  752. } else {
  753. c.JsonResult(0, "添加成功",memberGroupMember.ToMemberRelationshipResult())
  754. }
  755. } else {
  756. c.ShowErrorPage(405, "不支持的请求方式")
  757. }
  758. }
  759. //删除用户组中的成员
  760. func (c *ManagerController) MemberGroupMemberDelete() {
  761. c.Prepare()
  762. if !c.Member.IsSuperAdministrator() {
  763. c.JsonResult(5000,"只有超级管理员才能删除用户")
  764. }
  765. if c.Ctx.Input.IsPost() {
  766. memberGroupId,err := c.GetInt("id",0)
  767. if err != nil || memberGroupId <= 0 {
  768. c.JsonResult(500,"参数错误")
  769. }
  770. if err := models.NewMemberGroupMembers().Delete(memberGroupId);err != nil {
  771. beego.Error("添加用户失败 =>",err)
  772. c.JsonResult(6005,"删除用户失败")
  773. }else{
  774. c.JsonResult(0,"删除成功")
  775. }
  776. }else{
  777. c.ShowErrorPage(405,"不支持的请求方式")
  778. }
  779. }
  780. func (c *ManagerController) MemberGroupMemberSearch() {
  781. c.Prepare()
  782. groupId,err := c.GetInt("group_id",0)
  783. if err != nil || groupId <= 0 {
  784. c.JsonResult(6002,"用户组参数错误")
  785. }
  786. q := strings.TrimSpace(c.GetString("q"))
  787. members,err := models.NewMemberGroupMembers().FindMemberGroupMemberNoJoinSearchResult(groupId,q)
  788. result := models.SelectMemberResult{}
  789. items := make([]models.KeyValueItem, 0)
  790. for _, member := range members {
  791. item := models.KeyValueItem{}
  792. item.Id = member.MemberId
  793. item.Text = member.Account
  794. items = append(items, item)
  795. }
  796. result.Result = items
  797. c.JsonResult(0, "OK", result)
  798. }