scrm-go

article_controller.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package article
  2. import (
  3. "github.com/astaxie/beego"
  4. "fmt"
  5. "SCRM/controllers"
  6. "SCRM/service/article_service"
  7. "SCRM/enums"
  8. "SCRM/models"
  9. "time"
  10. "SCRM/utils"
  11. "encoding/json"
  12. "strconv"
  13. )
  14. func ArticleRouters() {
  15. beego.Router("/api/acticle/createacticle",&ArticleManage{},"Get:CreateArticle")
  16. beego.Router("/api/acticle/getArticleType",&ArticleManage{},"Get:GetArticleType")
  17. beego.Router("/api/acticle/getAllArticles",&ArticleManage{},"Get:GetAllArticles")
  18. beego.Router("/api/article/addCategory",&ArticleManage{},"Post:AddCategory")
  19. beego.Router("/api/acticle/getCategorys",&ArticleManage{},"Get:GetCategorys")
  20. beego.Router("/api/acticle/edit",&ArticleManage{},"Put:EditCategorys")
  21. }
  22. type ArticleManage struct {
  23. controllers.BaseAuthAPIController
  24. }
  25. func (this *ArticleManage) CreateArticle(){
  26. adminUserInfo := this.GetAdminUserInfo()
  27. userOrgID := int64(adminUserInfo.CurrentOrgId)
  28. actname := this.GetString("act_name")
  29. actcontent := this.GetString("act_content")
  30. orglogo := this.GetString("org_logo")
  31. acttype, _ := this.GetInt64("act_type")
  32. timenow := time.Now().Unix()
  33. fmt.Println("姓名:",actname,"文章内容",actcontent,"图片",orglogo,"文章类型",acttype,userOrgID)
  34. articles := models.Articles{
  35. Title: actname,
  36. Content: actcontent,
  37. Imgs: orglogo,
  38. ClassId: acttype,
  39. UserOrgId:userOrgID,
  40. Ctime:timenow,
  41. Status:1,
  42. }
  43. err := article_service.AddAritcle(articles)
  44. if err !=nil{
  45. this.ServeFailJsonSend(enums.ErrorCodeDataException, "插入文章失败")
  46. return
  47. }
  48. }
  49. func (this *ArticleManage) GetArticleType(){
  50. adminUserInfo := this.GetAdminUserInfo()
  51. userOrgID := adminUserInfo.CurrentOrgId
  52. category, err := article_service.FindArticleCategoryType(userOrgID)
  53. fmt.Println("文章分类列表",category,err)
  54. if err !=nil{
  55. this.ServeFailJsonSend(enums.ErrorCodeDataException, "获取文章分类列表失败")
  56. return
  57. }
  58. this.ServeSuccessJSON(map[string]interface{}{
  59. "category":category,
  60. })
  61. return
  62. }
  63. func (this *ArticleManage) GetAllArticles() {
  64. page, _ := this.GetInt64("page", 1)
  65. limit, _ := this.GetInt64("limit", 10)
  66. searchKey := this.GetString("search", "")
  67. classId,_ := this.GetInt64("classid",0)
  68. fmt.Println("页面",page,"限制",limit,"关键字",searchKey,"分类号",classId)
  69. if page <= 0 {
  70. page = 1
  71. }
  72. if limit <= 0 {
  73. limit = 10
  74. }
  75. adminUserInfo := this.GetAdminUserInfo()
  76. userOrgID := adminUserInfo.CurrentOrgId
  77. articles, total, err := article_service.FindAllArticle(userOrgID, page,limit, searchKey, classId)
  78. fmt.Println("文章内容是是么",articles)
  79. fmt.Println("total",total)
  80. fmt.Println("err",err)
  81. if err !=nil{
  82. this.ServeFailJsonSend(enums.ErrorCodeDataException, "获取文章列表失败")
  83. return
  84. }
  85. category, err := article_service.FindCategoryList(userOrgID)
  86. fmt.Println("category是傻",category,err)
  87. if err !=nil{
  88. this.ServeFailJsonSend(enums.ErrorCodeDataException, "获取文章分类列表失败")
  89. return
  90. }
  91. this.ServeSuccessJSON(map[string]interface{}{
  92. "articles":articles,
  93. "total":total,
  94. "category":category,
  95. })
  96. return
  97. }
  98. func (this *ArticleManage) AddCategory(){
  99. dataBody := make(map[string]interface{}, 0)
  100. err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  101. fmt.Println("err是什么呢",err)
  102. if err != nil {
  103. utils.ErrorLog(err.Error())
  104. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
  105. return
  106. }
  107. name:= dataBody["name"].(string)
  108. fmt.Println("name是谁?",name)
  109. if len(name) == 0 {
  110. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "姓名不能为空")
  111. return
  112. }
  113. content := dataBody["content"].(string)
  114. fmt.Println("content是谁?",content)
  115. if len(content) == 0 {
  116. this.ServeFailJsonSend(enums.ErrorCodeParamWrong,"内容不能为空")
  117. return
  118. }
  119. sort := dataBody["sort"].(string)
  120. sors, _ := strconv.ParseInt(sort, 10, 64)
  121. if sors <=0 {
  122. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "排序不能为空")
  123. return
  124. }
  125. fmt.Println("soert是谁?",sort)
  126. fmt.Println("姓名:",name,"内容",content,"排序",sort)
  127. timenow := time.Now().Unix()
  128. adminUserInfo := this.GetAdminUserInfo()
  129. userOrgID := adminUserInfo.CurrentOrgId
  130. category := models.ArticleCategory{
  131. Name: name,
  132. Summary: content,
  133. Order: sors,
  134. UserOrgId:userOrgID,
  135. Status: 1,
  136. Ctime: timenow,
  137. }
  138. fmt.Println(category)
  139. err = article_service.AddCategory(category)
  140. fmt.Println(err)
  141. if err !=nil{
  142. this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加分类失败")
  143. return
  144. }
  145. this.ServeSuccessJSON(map[string]interface{}{
  146. "category":category,
  147. })
  148. }
  149. func (this *ArticleManage) GetCategorys() {
  150. adminUserInfo := this.GetAdminUserInfo()
  151. userOrgID := adminUserInfo.CurrentOrgId
  152. page, _ := this.GetInt64("page", 1)
  153. limit, _ := this.GetInt64("limit", 10)
  154. if page <= 0 {
  155. page = 1
  156. }
  157. if limit <= 0 {
  158. limit = 10
  159. }
  160. fmt.Println("机构ID",userOrgID)
  161. categorys, total, err := article_service.GetCategorys(page, limit, userOrgID)
  162. fmt.Println("hhe",categorys,total,err)
  163. if err !=nil{
  164. this.ServeFailJsonSend(enums.ErrorCodeDataException, "获取文章分类列表失败")
  165. return
  166. }
  167. this.ServeSuccessJSON(map[string]interface{}{
  168. "category":categorys,
  169. "total":total,
  170. })
  171. return
  172. }
  173. func (this *ArticleManage) EditCategorys() {
  174. id, _ := this.GetInt64("MenuId", 0)
  175. fmt.Println("ID是多少",id)
  176. if id <= 0 {
  177. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误:id")
  178. return
  179. }
  180. adminUserInfo := this.GetAdminUserInfo()
  181. userOrgID := adminUserInfo.CurrentOrgId
  182. categorys, err := article_service.GetCategorysByID(userOrgID, id)
  183. fmt.Println("err是设么",err)
  184. if err != nil {
  185. this.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑类别失败:("+err.Error()+")")
  186. return
  187. }
  188. if categorys == nil {
  189. this.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑类别失败:(会员记录不存在)")
  190. return
  191. }
  192. timenow := time.Now().Unix()
  193. dataBody := make(map[string]interface{}, 0)
  194. err = json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
  195. if err != nil {
  196. utils.ErrorLog(err.Error())
  197. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
  198. return
  199. }
  200. name:= dataBody["Name"].(string)
  201. fmt.Println("name是谁?",name)
  202. if len(name) == 0 {
  203. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "姓名不能为空")
  204. return
  205. }
  206. content := dataBody["Summary"].(string)
  207. fmt.Println("content是谁?",content)
  208. if len(content) == 0 {
  209. this.ServeFailJsonSend(enums.ErrorCodeParamWrong,"内容不能为空")
  210. return
  211. }
  212. sort := dataBody["Order"].(string)
  213. sors, _ := strconv.ParseInt(sort, 10, 64)
  214. if sors <=0 {
  215. this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "排序不能为空")
  216. return
  217. }
  218. fmt.Println("soert是谁?",sort)
  219. fmt.Println("姓名:",name,"内容",content,"排序",sort)
  220. category := models.ArticleCategory{
  221. Name: name,
  222. Summary: content,
  223. Order: sors,
  224. UserOrgId:userOrgID,
  225. Status: 1,
  226. Mtime: timenow,
  227. }
  228. err = article_service.EditCategory(category)
  229. fmt.Println("err错误",err)
  230. if err != nil {
  231. this.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑类别失败:("+err.Error()+")")
  232. return
  233. }
  234. fmt.Println(category)
  235. this.ServeSuccessJSON(map[string]interface{}{
  236. "category":categorys,
  237. })
  238. }