//新增频道 func (this *Article) Create() { //Get方法 if this.methodGet { this.Data["token"] = this.token() //所属频道选项 this.Data["chs"] = channels.GetChannelSelectItems(0 /*, utils.ChNews*/) this.TplNames = this.getTplFileName("create") this.Render() return } //Post方法 //签名错误,返回重复提交错误 if this.invalidToken() { this.renderLoseToken() return } //数据模型 m := new(models.Articles) models.Extend(m, this.xm) m.Channelid, _ = this.GetInt("channelid") m.Title = this.GetString("title") m.Subtitle = this.GetString("subtitle") m.Intro = this.GetString("intro") m.Content = this.GetString("content") m.Keywords = this.GetString("keywords") m.Description = this.GetString("description") m.Author = this.GetString("author") if this.GetString("status") == "on" { m.Status = 1 } else { m.Status = 0 } if t, err := beego.DateParse(this.GetString("published"), "Y-n-j H:i:s"); err == nil { m.Published = utils.Millisecond(t) } else { m.Published = utils.Millisecond(time.Now()) } //数据合法性检验 if data, inv := this.invalidModel(m); inv { this.renderJson(data) return } //提交DDL var data interface{} _, err := articles.Add(m) if err != nil { data = utils.JsonMessage(false, "", err.Error()) } else { data = utils.JsonMessage(true, "", "") } this.renderJson(data) }
//允许新的请求,数据通用字段初始信息,附带验证用户是否合法(err), func (this *base) allowRequest() (ok bool) { currentuser, ok := this.validUser() field := new(models.Field) if ok { field.Status = 0 field.Deleted = 0 field.Updator = currentuser.Id field.Updated = utils.Millisecond(time.Now()) field.Ip = utils.GetIp(this.Ctx.Request.RemoteAddr) field.Role = currentuser.Role field.Name = currentuser.Name } this.xm = field return }
//修改账户信息 func (this *Article) Edit() { //Get方法 if this.methodGet { this.Data["token"] = this.token() id, err := this.getParamsInt64(":id") if err != nil { this.errorHandle(utils.JsonMessage(false, "invalidRequestParams", this.lang("invalidRequestParams"))) return } c, err := articles.Get(id) if err != nil { this.errorHandle(utils.JsonMessage(false, "", err.Error())) return } this.Data["article"] = c //所属频道选项 //this.Data["chs"] = channels.GetChannelSelectItems(-1, utils.ChNews, c.Channelid) this.Data["chs"] = channels.GetChannelSelectItems(0, -1, c.Channelid) this.TplNames = this.getTplFileName("edit") this.Render() return } //Post方法 //签名错误,返回重复提交错误 if this.invalidToken() { this.renderLoseToken() return } //提交DDL var data interface{} id, err := this.GetInt("id") if err != nil || id == 0 { this.errorHandle(utils.JsonMessage(false, "invalidRequestParams", this.lang("invalidRequestParams"))) return } //获取原始数据模型 m, err := articles.Get(id) if err != nil { this.errorHandle(utils.JsonMessage(false, "", err.Error())) return } //赋值 m.Channelid, _ = this.GetInt("channelid") m.Title = this.GetString("title") m.Subtitle = this.GetString("subtitle") m.Intro = this.GetString("intro") m.Content = this.GetString("content") m.Keywords = this.GetString("keywords") m.Description = this.GetString("description") m.Author = this.GetString("author") m.Updated = this.xm.Updated m.Updator = this.xm.Updator m.Ip = this.xm.Ip if this.GetString("status") == "on" { m.Status = 1 } else { m.Status = 0 } if t, err := beego.DateParse(this.GetString("published"), "Y-n-j"); err == nil { m.Published = utils.Millisecond(t) } else { m.Published = utils.Millisecond(time.Now()) } //数据合法性检验 if data, inv := this.invalidModel(m); inv { this.renderJson(data) return } //提交DDL _, err = articles.Update(m) if err != nil { data = utils.JsonMessage(false, "", err.Error()) } else { data = utils.JsonMessage(true, "", "") } this.renderJson(data) }