book.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package controllers
  2. import (
  3. "strings"
  4. "regexp"
  5. "strconv"
  6. "time"
  7. "encoding/json"
  8. "html/template"
  9. "github.com/lifei6671/godoc/models"
  10. "github.com/lifei6671/godoc/utils"
  11. "github.com/astaxie/beego"
  12. "github.com/astaxie/beego/orm"
  13. )
  14. type BookController struct {
  15. BaseController
  16. }
  17. func (c *BookController) Index() {
  18. c.Prepare()
  19. c.TplName = "book/index.tpl"
  20. pageIndex, _ := c.GetInt("page", 1)
  21. books,totalCount,err := models.NewBook().FindToPager(pageIndex,10,c.Member.MemberId)
  22. if err != nil {
  23. c.Abort("500")
  24. }
  25. html := utils.GetPagerHtml(c.Ctx.Request.RequestURI,pageIndex,10,totalCount)
  26. c.Data["PageHtml"] = html
  27. b,err := json.Marshal(books)
  28. if err != nil {
  29. c.Data["Result"] = template.JS("[]")
  30. }else{
  31. c.Data["Result"] = template.JS(string(b))
  32. }
  33. }
  34. // Dashboard 项目概要 .
  35. func (c *BookController) Dashboard() {
  36. c.Prepare()
  37. c.TplName = "book/dashboard.tpl"
  38. key := c.Ctx.Input.Param(":key")
  39. if key == ""{
  40. c.Abort("404")
  41. }
  42. book,err := models.NewBookResult().FindByIdentify(key,c.Member.MemberId)
  43. if err != nil {
  44. if err == models.ErrPermissionDenied {
  45. c.Abort("403")
  46. }
  47. c.Abort("500")
  48. }
  49. c.Data["Model"] = *book
  50. }
  51. // Setting 项目设置 .
  52. func (c *BookController) Setting() {
  53. c.Prepare()
  54. c.TplName = "book/setting.tpl"
  55. key := c.Ctx.Input.Param(":key")
  56. if key == ""{
  57. c.Abort("404")
  58. }
  59. book,err := models.NewBookResult().FindByIdentify(key,c.Member.MemberId)
  60. if err != nil {
  61. if err == models.ErrPermissionDenied {
  62. c.Abort("403")
  63. }
  64. c.Abort("500")
  65. }
  66. c.Data["Model"] = *book
  67. }
  68. //用户列表.
  69. func (c *BookController) Users() {
  70. c.Prepare()
  71. c.TplName = "book/users.tpl"
  72. key := c.Ctx.Input.Param(":key")
  73. pageIndex,_ := c.GetInt("page",1)
  74. if key == ""{
  75. c.Abort("404")
  76. }
  77. book,err := models.NewBookResult().FindByIdentify(key,c.Member.MemberId)
  78. if err != nil {
  79. if err == models.ErrPermissionDenied {
  80. c.Abort("403")
  81. }
  82. c.Abort("500")
  83. }
  84. c.Data["Model"] = *book
  85. members,totalCount,err := models.NewMemberRelationshipResult().FindForUsersByBookId(book.BookId,pageIndex,15)
  86. html := utils.GetPagerHtml(c.Ctx.Request.RequestURI,pageIndex,10,totalCount)
  87. c.Data["PageHtml"] = html
  88. b,err := json.Marshal(members)
  89. if err != nil {
  90. c.Data["Result"] = template.JS("[]")
  91. }else{
  92. c.Data["Result"] = template.JS(string(b))
  93. }
  94. }
  95. func (c *BookController) AddMember() {
  96. identify := c.GetString("identify")
  97. account := c.GetString("account")
  98. role_id,_ := c.GetInt("role_id",3)
  99. if identify == "" || account == ""{
  100. c.JsonResult(6001,"参数错误")
  101. }
  102. book ,err := models.NewBookResult().FindByIdentify("identify",c.Member.MemberId)
  103. if err != nil {
  104. if err == models.ErrPermissionDenied {
  105. c.JsonResult(403,"权限不足")
  106. }
  107. if err == orm.ErrNoRows {
  108. c.JsonResult(404,"项目不能存在")
  109. }
  110. c.JsonResult(6002,err.Error())
  111. }
  112. if book.RoleId != 0 && book.RoleId != 1 {
  113. c.JsonResult(403,"权限不足")
  114. }
  115. member := models.NewMember()
  116. if err := member.FindByAccount(account) ; err != nil {
  117. c.JsonResult(404,"用户不存在")
  118. }
  119. if _,err := models.NewRelationship().FindForRoleId(book.BookId,member.MemberId);err == orm.ErrNoRows {
  120. c.JsonResult(6003,"用户已存在该项目中")
  121. }
  122. relationship := models.NewRelationship()
  123. relationship.BookId = book.BookId
  124. relationship.MemberId = member.MemberId
  125. relationship.RoleId = role_id
  126. if err := relationship.Insert(); err == nil {
  127. c.JsonResult(0,"ok",member)
  128. }
  129. c.JsonResult(500,err.Error())
  130. }
  131. func (c *BookController) Create() {
  132. if c.Ctx.Input.IsPost() {
  133. book_name := strings.TrimSpace(c.GetString("book_name",""))
  134. identify := strings.TrimSpace(c.GetString("identify",""))
  135. description := strings.TrimSpace(c.GetString("description",""))
  136. privately_owned,_ := strconv.Atoi(c.GetString("privately_owned"))
  137. comment_status := c.GetString("comment_status")
  138. if book_name == "" {
  139. c.JsonResult(6001,"项目名称不能为空")
  140. }
  141. if identify == "" {
  142. c.JsonResult(6002,"项目标识不能为空")
  143. }
  144. if ok,err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`,identify); !ok || err != nil {
  145. c.JsonResult(6003,"文档标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
  146. }
  147. if strings.Count(identify,"") > 50 {
  148. c.JsonResult(6004,"文档标识不能超过50字")
  149. }
  150. if strings.Count(description,"") > 500 {
  151. c.JsonResult(6004,"项目描述不能大于500字")
  152. }
  153. if privately_owned !=0 && privately_owned != 1 {
  154. privately_owned = 1
  155. }
  156. if comment_status != "open" && comment_status != "closed" && comment_status != "group_only" && comment_status != "registered_only" {
  157. comment_status = "closed"
  158. }
  159. book := models.NewBook()
  160. if books,err := book.FindByField("identify",identify); err == nil || len(books) > 0 {
  161. c.JsonResult(6006,"项目标识已存在")
  162. }
  163. book.BookName = book_name
  164. book.Description = description
  165. book.CommentCount = 0
  166. book.PrivatelyOwned = privately_owned
  167. book.CommentStatus = comment_status
  168. book.Identify = identify
  169. book.DocCount = 0
  170. book.MemberId = c.Member.MemberId
  171. book.CommentCount = 0
  172. book.Version = time.Now().Unix()
  173. book.Cover = beego.AppConfig.String("cover")
  174. err := book.Insert()
  175. if err != nil {
  176. c.JsonResult(6005,err.Error())
  177. }
  178. c.JsonResult(0,"ok",book)
  179. }
  180. c.JsonResult(6001,"error")
  181. }
  182. // Edit 编辑项目.
  183. func (p *BookController) Edit() {
  184. p.TplName = "book/edit.tpl"
  185. }
  186. // Delete 删除项目.
  187. func (p *BookController) Delete() {
  188. p.StopRun()
  189. }
  190. // Transfer 转让项目.
  191. func (p *BookController)Transfer() {
  192. }