package article import ( "github.com/astaxie/beego" "fmt" "SCRM/controllers" "SCRM/service/article_service" "SCRM/enums" "SCRM/models" "time" "SCRM/utils" "encoding/json" "strconv" ) func ArticleRouters() { beego.Router("/api/acticle/createacticle",&ArticleManage{},"Get:CreateArticle") beego.Router("/api/acticle/getArticleType",&ArticleManage{},"Get:GetArticleType") beego.Router("/api/acticle/getAllArticles",&ArticleManage{},"Get:GetAllArticles") beego.Router("/api/article/addCategory",&ArticleManage{},"Post:AddCategory") beego.Router("/api/acticle/getCategorys",&ArticleManage{},"Get:GetCategorys") beego.Router("/api/acticle/edit",&ArticleManage{},"Put:EditCategorys") } type ArticleManage struct { controllers.BaseAuthAPIController } func (this *ArticleManage) CreateArticle(){ adminUserInfo := this.GetAdminUserInfo() userOrgID := int64(adminUserInfo.CurrentOrgId) actname := this.GetString("act_name") actcontent := this.GetString("act_content") orglogo := this.GetString("org_logo") acttype, _ := this.GetInt64("act_type") timenow := time.Now().Unix() fmt.Println("姓名:",actname,"文章内容",actcontent,"图片",orglogo,"文章类型",acttype,userOrgID) articles := models.Articles{ Title: actname, Content: actcontent, Imgs: orglogo, ClassId: acttype, UserOrgId:userOrgID, Ctime:timenow, Status:1, } err := article_service.AddAritcle(articles) if err !=nil{ this.ServeFailJsonSend(enums.ErrorCodeDataException, "插入文章失败") return } } func (this *ArticleManage) GetArticleType(){ adminUserInfo := this.GetAdminUserInfo() userOrgID := adminUserInfo.CurrentOrgId category, err := article_service.FindArticleCategoryType(userOrgID) fmt.Println("文章分类列表",category,err) if err !=nil{ this.ServeFailJsonSend(enums.ErrorCodeDataException, "获取文章分类列表失败") return } this.ServeSuccessJSON(map[string]interface{}{ "category":category, }) return } func (this *ArticleManage) GetAllArticles() { page, _ := this.GetInt64("page", 1) limit, _ := this.GetInt64("limit", 10) searchKey := this.GetString("search", "") classId,_ := this.GetInt64("classid",0) fmt.Println("页面",page,"限制",limit,"关键字",searchKey,"分类号",classId) if page <= 0 { page = 1 } if limit <= 0 { limit = 10 } adminUserInfo := this.GetAdminUserInfo() userOrgID := adminUserInfo.CurrentOrgId articles, total, err := article_service.FindAllArticle(userOrgID, page,limit, searchKey, classId) fmt.Println("文章内容是是么",articles) fmt.Println("total",total) fmt.Println("err",err) if err !=nil{ this.ServeFailJsonSend(enums.ErrorCodeDataException, "获取文章列表失败") return } category, err := article_service.FindCategoryList(userOrgID) fmt.Println("category是傻",category,err) if err !=nil{ this.ServeFailJsonSend(enums.ErrorCodeDataException, "获取文章分类列表失败") return } this.ServeSuccessJSON(map[string]interface{}{ "articles":articles, "total":total, "category":category, }) return } func (this *ArticleManage) AddCategory(){ dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody) fmt.Println("err是什么呢",err) if err != nil { utils.ErrorLog(err.Error()) this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误") return } name:= dataBody["name"].(string) fmt.Println("name是谁?",name) if len(name) == 0 { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "姓名不能为空") return } content := dataBody["content"].(string) fmt.Println("content是谁?",content) if len(content) == 0 { this.ServeFailJsonSend(enums.ErrorCodeParamWrong,"内容不能为空") return } sort := dataBody["sort"].(string) sors, _ := strconv.ParseInt(sort, 10, 64) if sors <=0 { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "排序不能为空") return } fmt.Println("soert是谁?",sort) fmt.Println("姓名:",name,"内容",content,"排序",sort) timenow := time.Now().Unix() adminUserInfo := this.GetAdminUserInfo() userOrgID := adminUserInfo.CurrentOrgId category := models.ArticleCategory{ Name: name, Summary: content, Order: sors, UserOrgId:userOrgID, Status: 1, Ctime: timenow, } fmt.Println(category) err = article_service.AddCategory(category) fmt.Println(err) if err !=nil{ this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加分类失败") return } this.ServeSuccessJSON(map[string]interface{}{ "category":category, }) } func (this *ArticleManage) GetCategorys() { adminUserInfo := this.GetAdminUserInfo() userOrgID := adminUserInfo.CurrentOrgId page, _ := this.GetInt64("page", 1) limit, _ := this.GetInt64("limit", 10) if page <= 0 { page = 1 } if limit <= 0 { limit = 10 } fmt.Println("机构ID",userOrgID) categorys, total, err := article_service.GetCategorys(page, limit, userOrgID) fmt.Println("hhe",categorys,total,err) if err !=nil{ this.ServeFailJsonSend(enums.ErrorCodeDataException, "获取文章分类列表失败") return } this.ServeSuccessJSON(map[string]interface{}{ "category":categorys, "total":total, }) return } func (this *ArticleManage) EditCategorys() { id, _ := this.GetInt64("MenuId", 0) fmt.Println("ID是多少",id) if id <= 0 { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误:id") return } adminUserInfo := this.GetAdminUserInfo() userOrgID := adminUserInfo.CurrentOrgId categorys, err := article_service.GetCategorysByID(userOrgID, id) fmt.Println("err是设么",err) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑类别失败:("+err.Error()+")") return } if categorys == nil { this.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑类别失败:(会员记录不存在)") return } timenow := time.Now().Unix() dataBody := make(map[string]interface{}, 0) err = json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误") return } name:= dataBody["Name"].(string) fmt.Println("name是谁?",name) if len(name) == 0 { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "姓名不能为空") return } content := dataBody["Summary"].(string) fmt.Println("content是谁?",content) if len(content) == 0 { this.ServeFailJsonSend(enums.ErrorCodeParamWrong,"内容不能为空") return } sort := dataBody["Order"].(string) sors, _ := strconv.ParseInt(sort, 10, 64) if sors <=0 { this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "排序不能为空") return } fmt.Println("soert是谁?",sort) fmt.Println("姓名:",name,"内容",content,"排序",sort) category := models.ArticleCategory{ Name: name, Summary: content, Order: sors, UserOrgId:userOrgID, Status: 1, Mtime: timenow, } err = article_service.EditCategory(category) fmt.Println("err错误",err) if err != nil { this.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑类别失败:("+err.Error()+")") return } fmt.Println(category) this.ServeSuccessJSON(map[string]interface{}{ "category":categorys, }) }