/* 删除用户 method:post params:ids */ func (this *Account) Delete() { var data interface{} id := this.getParamsString(":id") // ids := this.GetStrings("ids") if id != "" { ids = append(ids, id) } //不能自我重置 if utils.StringsContains(ids, fmt.Sprintf("%d", this.xm.Updator)) { data = utils.JsonMessage(false, "denyOneself", this.lang("denyOneself")) } else { var err error _, err = users.Delete(ids, this.xm) if err == nil { data = utils.JsonMessage(true, "", "") } else { data = utils.JsonMessage(false, "deleteFail", this.lang("deleteFail")) } } this.renderJson(data) }
//新增图片 func (this *Image) Create() { //Get方法 if this.methodGet { id, err := this.getParamsInt64(":id") if err != nil { this.errorHandle(utils.JsonMessage(false, "invalidRequestParams", this.lang("invalidRequestParams"))) return } this.Data["articleid"] = id this.Data["token"] = this.token() this.TplNames = this.getTplFileName("create") this.Render() return } //Post方法 //签名错误,返回重复提交错误 if this.invalidToken() { this.renderLoseToken() return } //数据模型 m := new(models.Images) models.Extend(m, this.xm) m.Articleid, _ = this.GetInt("articleid") m.Title = this.GetString("title") m.Url = this.GetString("url") if this.GetString("status") == "on" { m.Status = 1 } else { m.Status = 0 } files, err := this.upload("file") if err == nil { m.Path = files[0].Path m.Ext = files[0].Ext m.Srcfilename = files[0].Name m.Size = files[0].Size } //数据合法性检验 if data, inv := this.invalidModel(m); inv { this.renderJson(data) return } //提交DDL var data interface{} _, err = images.Add(m) if err != nil { data = utils.JsonMessage(false, "", err.Error()) } else { data = utils.JsonResult(true, "", files) } this.renderJson(data) }
//新增频道 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) }
/* 数据对象合法性验证 args:要检验的元素,长度为0,则检验全部元素 */ func (this *base) invalidModel(m interface{}, args ...interface{}) (data interface{}, invalid bool) { valid := validation.Validation{} b, err := valid.Valid(m) if err != nil { data = utils.JsonMessage(false, "", err.Error()) invalid = true return } if !b { var errstr string //检验元素 if n := len(args); n > 0 { for _, err := range valid.Errors { if utils.ListContains(args, err.Key[0:strings.Index(err.Key, ".")]) { errstr += fmt.Sprintf("%s %s;", err.Key, err.Message) } } } else { for _, err := range valid.Errors { errstr += fmt.Sprintf("%s %s;", err.Key, err.Message) } } if errstr == "" { invalid = false } else { data = utils.JsonMessage(false, "", errstr) invalid = true } return } return }
//修改账户信息 func (this *Account) 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 } u, err := users.Get(id) if err != nil { this.errorHandle(utils.JsonMessage(false, "", err.Error())) return } this.Data["user"] = u this.TplNames = this.getTplFileName("edit") this.Render() return } //Post方法 //签名错误,返回重复提交错误 if this.invalidToken() { this.renderLoseToken() return } //数据模型 u := new(models.Users) models.Extend(u, this.xm) u.Id, _ = this.GetInt("id") u.Loginname = this.GetString("loginname") u.Relname = this.GetString("relname") //数据合法性检验 // valid := validation.Validation // valid.Required(u.Loginname, "loginname") // valid.Required(u.Relname, "relname") if data, inv := this.invalidModel(u, "Loginname", "Relname"); inv { this.renderJson(data) return } //提交DDL var data interface{} _, err := users.Update(u) if err != nil { data = utils.JsonMessage(false, "", err.Error()) } else { data = utils.JsonMessage(true, "", "") } this.renderJson(data) }
//新增频道 func (this *Channel) Create() { //Get方法 if this.methodGet { this.Data["token"] = this.token() //所属频道选项 this.Data["chs"] = channels.GetChannelSelectItems(0, utils.ChNavigation) //频道类型 this.Data["types"] = channels.GetTypeSelectItems(0) this.TplNames = this.getTplFileName("create") this.Render() return } //Post方法 //签名错误,返回重复提交错误 if this.invalidToken() { this.renderLoseToken() return } //数据模型 m := new(models.Channels) models.Extend(m, this.xm) m.Pid, _ = this.GetInt("pid") m.Name = this.GetString("name") m.Enname = this.GetString("enname") t, _ := this.GetInt("children") m.Children = int(t) t, _ = this.GetInt("type") m.Type = int8(t) if this.GetString("status") == "on" { m.Status = 1 } else { m.Status = 0 } //数据合法性检验 if data, inv := this.invalidModel(m); inv { this.renderJson(data) return } //提交DDL var data interface{} _, err := channels.Add(m) if err != nil { data = utils.JsonMessage(false, "", err.Error()) } else { data = utils.JsonMessage(true, "", "") } this.renderJson(data) }
//频道列表 func (this *Article) GetAll() { var data interface{} // page := new(models.Pagination) if v, err := this.GetInt("index"); err == nil { page.Index = int(v) } if v, err := this.GetInt("size"); err == nil { page.Size = int(v) } //参数错误 if page.Index <= 0 { page.Index = 1 } if page.Size <= 0 { //data = utils.JsonMessage(false, "invalidRequestParams", this.lang("invalidRequestParams")) page.Size = 15 } channelid, _ := this.GetInt("channelid") // us, err := articles.GetAll(channelid, page) if err == nil { data = utils.JsonResult(true, fmt.Sprintf("%d", page.Count), us) } else { data = utils.JsonMessage(false, "", "") } this.renderJson(data) }
/* 重置图片 method:post params:id */ func (this *Image) Reset() { var data interface{} // id, err := this.GetInt("id") if err != nil { data = utils.JsonMessage(false, "invalidRequestParams", err.Error()) } else { // _, err = images.Reset(id, this.xm) if err == nil { data = utils.JsonMessage(true, "", "") } else { data = utils.JsonMessage(false, "resetFail", err.Error()) } } this.renderJson(data) }
/* 是否存在重名用户 */ func (this *Account) Exist() { var data interface{} name := this.GetString("loginname") if len(name) > 0 { if ok := users.Exist(name); ok { data = utils.JsonMessage(true, "sameNameAccount", this.lang("sameNameAccount")) } else { data = utils.JsonMessage(false, "", "") } } else { data = utils.JsonMessage(false, "invalidRequestParams", this.lang("invalidRequestParams")) } this.renderJson(data) }
//重新排列图片顺序 func (this *Image) Sequence() { var data interface{} // id, err1 := this.GetInt("id") sq, err2 := this.GetInt("sq") if err1 != nil || err2 != nil { data = utils.JsonMessage(false, "invalidRequestParams", err1.Error()+"\n"+err2.Error()) } else { this.xm.Sequence = int(sq) _, err := images.SetSequence(id, this.xm) if err == nil { data = utils.JsonMessage(true, "", "") } else { data = utils.JsonMessage(false, "resetFail", err.Error()) } } this.renderJson(data) }
/* 删除图片 method:post params:ids */ func (this *Image) Delete() { var data interface{} // id := this.getParamsString(":id") // ids := this.GetStrings("ids") if id != "" { ids = append(ids, id) } _, err := images.Delete(ids, this.xm) if err == nil { data = utils.JsonMessage(true, "", "") } else { data = utils.JsonMessage(false, "deleteFail", this.lang("deleteFail")) } this.renderJson(data) }
//新增账户 func (this *Account) Create() { //Get方法 if this.methodGet { this.Data["token"] = this.token() this.Data["password"] = beego.AppConfig.String("DefaultPassword") this.TplNames = this.getTplFileName("create") this.Render() return } //Post方法 //签名错误,返回重复提交错误 if this.invalidToken() { this.renderLoseToken() return } //数据模型 u := new(models.Users) models.Extend(u, this.xm) u.Loginname = this.GetString("loginname") u.Password = this.GetString("password") u.Relname = this.GetString("relname") r, _ := this.GetInt("role") u.Role = int8(r) //数据合法性检验 if data, inv := this.invalidModel(u); inv { this.renderJson(data) return } //提交DDL var data interface{} _, err := users.Add(u) if err != nil { data = utils.JsonMessage(false, "", err.Error()) } else { data = utils.JsonMessage(true, "", "") } this.renderJson(data) }
//图片列表 func (this *Image) GetAll() { us, err := images.GetAll() var data interface{} if err == nil { data = utils.JsonResult(true, "", us) } else { data = utils.JsonMessage(false, "", "") } this.renderJson(data) }
/* 重置用户 method:post params:id */ func (this *Account) Reset() { var data interface{} // id, err := this.GetInt("id") if err != nil { data = utils.JsonMessage(false, "invalidRequestParams", err.Error()) } else { //不能自我重置 if id == this.xm.Updator { data = utils.JsonMessage(false, "denyOneself", this.lang("denyOneself")) } else { _, err = users.Reset(id, this.xm) if err == nil { data = utils.JsonMessage(true, "", "") } else { data = utils.JsonMessage(false, "resetFail", err.Error()) } } } this.renderJson(data) }
//频道列表 func (this *Channel) GetAll() { t, err := this.GetInt("type") if err != nil { t = -1 } us, err := channels.GetAll(int(t)) var data interface{} if err == nil { data = utils.JsonResult(true, "", us) } else { data = utils.JsonMessage(false, "", err.Error()) } this.renderJson(data) }
//首页 func (this *Image) Index() { id, err := this.getParamsInt64(":id") if err != nil { this.errorHandle(utils.JsonMessage(false, "invalidRequestParams", this.lang("invalidRequestParams"))) return } article, err := articles.Get(id) if err == nil { this.Data["article"] = article is, _ := images.GetAll(id) this.Data["images"] = is } this.TplNames = this.getTplFileName("index") this.Render() }
//登录页 func (this *Front) Login() { loginName := this.GetString("loginname") //登录成功后转向地址 returnUrl := this.GetString("returnurl") //Get if this.methodGet { this.Layout = "" if returnUrl == "" { returnUrl = this.Ctx.Request.Referer() } if returnUrl == "" { returnUrl = "/" } if _, ok := this.validUser(); ok { this.Redirect(returnUrl, 302) return } //fmt.Println(returnUrl, this.Ctx.Request.Referer()) this.Data["loginname"] = loginName this.Data["returnurl"] = returnUrl this.Data["token"] = this.token() this.page.Title = "登录" this.TplNames = this.getTplFileName("login") return } //Post password := this.GetString("password") always := this.GetString("always") == "on" //登录成功后转向地址 if returnUrl == "" { returnUrl = "/" } var users = &models.Users{} u, err := users.Login(loginName, password, this.xm) if err != nil { //this.Redirect(fmt.Sprintf("/home/login?loginname=%s&returnurl=%s", loginName, returnUrl), 302) data := utils.JsonMessage(false, err.Error(), this.lang(err.Error())) this.renderJson(data) return } //登录状态存入cookie,缺省时间是1年:365*24*60*60 var cookieDuration interface{} if always { cookieDuration, _ = beego.AppConfig.Int("CookieDuration") } else { cookieDuration = "" } this.Ctx.SetCookie(beego.AppConfig.String("CookieName"), utils.CookieEncode(fmt.Sprintf("%d|%s|%d", u.Id, u.Relname, u.Role)), cookieDuration, "/") this.Redirect(returnUrl, 302) }
//响应签名丢失错误 func (this *base) renderLoseToken() { data := utils.JsonMessage(false, "invalidFormToken", this.lang("invalidFormToken")) this.renderJson(data) }
//修改账户信息 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) }
//修改账户信息 func (this *Channel) 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 := channels.Get(id) if err != nil { this.errorHandle(utils.JsonMessage(false, "", err.Error())) return } this.Data["channel"] = c //所属频道选项 this.Data["chs"] = channels.GetChannelSelectItems(-1, utils.ChNavigation, c.Pid) //频道类型 this.Data["types"] = channels.GetTypeSelectItems(c.Type) 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 := channels.Get(id) if err != nil { this.errorHandle(utils.JsonMessage(false, "", err.Error())) return } //赋值 m.Pid, _ = this.GetInt("pid") m.Name = this.GetString("name") m.Enname = this.GetString("enname") t, _ := this.GetInt("children") m.Children = int(t) t, _ = this.GetInt("type") m.Type = int8(t) 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 data, inv := this.invalidModel(m); inv { this.renderJson(data) return } //提交DDL _, err = channels.Update(m) if err != nil { data = utils.JsonMessage(false, "", err.Error()) } else { data = utils.JsonMessage(true, "", "") } this.renderJson(data) }
//修改账户信息 func (this *Image) Edit() { //Get方法 if this.methodGet { id, err := this.getParamsInt64(":id") if err != nil { this.errorHandle(utils.JsonMessage(false, "invalidRequestParams", this.lang("invalidRequestParams"))) return } c, err := images.Get(id) if err != nil { this.errorHandle(utils.JsonMessage(false, "", err.Error())) return } this.Data["image"] = c this.Data["token"] = this.token() 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 := images.Get(id) if err != nil { this.errorHandle(utils.JsonMessage(false, "", err.Error())) return } //赋值 m.Id = id m.Articleid, _ = this.GetInt("articleid") m.Title = this.GetString("title") m.Url = this.GetString("url") 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 } files, err := this.upload("file") if err == nil && len(files) > 0 { m.Path = files[0].Path m.Ext = files[0].Ext m.Srcfilename = files[0].Name m.Size = files[0].Size } //数据合法性检验 if data, inv := this.invalidModel(m); inv { this.renderJson(data) return } //提交 _, err = images.Update(m) if err != nil { data = utils.JsonMessage(false, "", err.Error()) } else { data = utils.JsonResult(true, "", files) } this.renderJson(data) }