func (this *AdminController) Get() { /* 获取session和cookie,管理员和用户的session和cookie都要获取,并进行分类处理输出对应模板 */ //普通用户 sessionuid := this.GetSession("sessionuid") cookieaccount := this.Ctx.GetCookie("cookieaccount") //管理员 sessionaid := this.GetSession("sessionaid") cookieadmin := this.Ctx.GetCookie("cookieadmin") //判断用户类型 if len(cookieadmin) > 0 { //管理员 this.Data["admin"] = true this.TplNames = "index/index.html" return } else if sessionaid != nil { //管理员 this.Data["admin"] = true this.TplNames = "index/index.html" } else if len(cookieaccount) > 0 { //普通用户,读取用户名 account := DecodeCookie(cookieaccount) userinfo, _ := models.Userinfo(account) this.Data["username"] = userinfo.Username this.Data["admin"] = false this.TplNames = "index/index.html" } else if sessionuid != nil { //普通用户,读取用户uid userinfo, _ := models.Userinfowithuid(sessionuid) this.Data["username"] = userinfo.Username this.Data["admin"] = false this.TplNames = "index/index.html" } else { //返回登录界面 this.Redirect("/", 301) } }
/* 主页面输出 */ func (this *AdminController) Main() { /* 获取session和cookie,管理员和用户的session和cookie都要获取,并进行分类处理输出对应模板 */ //普通用户 sessionuid := this.GetSession("sessionuid") cookieaccount := this.Ctx.GetCookie("cookieaccount") //管理员 sessionaid := this.GetSession("sessionaid") cookieadmin := this.Ctx.GetCookie("cookieadmin") //判断用户类型 if len(cookieadmin) > 0 { //管理员 this.Data["hostname"], _ = os.Hostname() this.Data["goversion"] = runtime.Version() this.Data["os"] = runtime.GOOS this.Data["cpunum"] = runtime.NumCPU() this.Data["arch"] = runtime.GOARCH this.Data["admin"] = true this.TplNames = "layout.html" return } else if sessionaid != nil { //管理员 this.Data["hostname"], _ = os.Hostname() this.Data["goversion"] = runtime.Version() this.Data["os"] = runtime.GOOS this.Data["cpunum"] = runtime.NumCPU() this.Data["arch"] = runtime.GOARCH //输出网站内容统计信息 article0, _ := models.Articletypesumwithstatus(0) article1, _ := models.Articletypesumwithstatus(1) article2, _ := models.Articletypesumwithstatus(2) diary0, _ := models.Diarytypesumwithstatic(0) diary1, _ := models.Diarytypesumwithstatic(1) diary2, _ := models.Diarytypesumwithstatic(2) shuo, _ := models.Shuosumwithall() picture0, _ := models.Imagereadcountwithstatic(0) picture1, _ := models.Imagereadcountwithstatic(1) usersum, _ := models.Usersum() this.Data["article0"] = article0 this.Data["article1"] = article1 this.Data["article2"] = article2 this.Data["diary0"] = diary0 this.Data["diary1"] = diary1 this.Data["diary2"] = diary2 this.Data["shuo"] = shuo this.Data["picture0"] = picture0 this.Data["picture1"] = picture1 this.Data["usersum"] = usersum this.Data["admin"] = true this.TplNames = "layout.html" } else if len(cookieaccount) > 0 { //普通用户 account := DecodeCookie(cookieaccount) userinfo, _ := models.Userinfo(account) //输出文章数量,日记数量,碎言碎语数量,照片数量 userid := userinfo.Id article0, _ := models.Articletypesum(userid, 0) article1, _ := models.Articletypesum(userid, 1) article2, _ := models.Articletypesum(userid, 2) diary0, _ := models.Diarytypesum(userid, 0) diary1, _ := models.Diarytypesum(userid, 1) diary2, _ := models.Diarytypesum(userid, 2) shuo, _ := models.Shuosum(userid) picture0, _ := models.Imagereadcount(userid, 0) picture1, _ := models.Imagereadcount(userid, 1) this.Data["article0"] = article0 this.Data["article1"] = article1 this.Data["article2"] = article2 this.Data["diary0"] = diary0 this.Data["diary1"] = diary1 this.Data["diary2"] = diary2 this.Data["shuo"] = shuo this.Data["picture0"] = picture0 this.Data["picture1"] = picture1 this.Data["admin"] = false this.TplNames = "layout.html" } else if sessionuid != nil { //普通用户,读取用户uid userinfo, _ := models.Userinfowithuid(sessionuid) //输出文章数量,日记数量,碎言碎语数量,照片数量 userid := userinfo.Id article0, _ := models.Articletypesum(userid, 0) article1, _ := models.Articletypesum(userid, 1) article2, _ := models.Articletypesum(userid, 2) diary0, _ := models.Diarytypesum(userid, 0) diary1, _ := models.Diarytypesum(userid, 1) diary2, _ := models.Diarytypesum(userid, 2) shuo, _ := models.Shuosum(userid) picture0, _ := models.Imagereadcount(userid, 0) picture1, _ := models.Imagereadcount(userid, 1) this.Data["article0"] = article0 this.Data["article1"] = article1 this.Data["article2"] = article2 this.Data["diary0"] = diary0 this.Data["diary1"] = diary1 this.Data["diary2"] = diary2 this.Data["shuo"] = shuo this.Data["picture0"] = picture0 this.Data["picture1"] = picture1 this.Data["admin"] = false this.TplNames = "layout.html" } else { //返回登录界面 this.Redirect("/", 301) } }
/* 登录页面 */ func (this *LoginController) Post() { this.TplNames = "account/prologin.html" if this.GetString("dosubmit") == "yes" { //获取表单传递数据 account := strings.TrimSpace(this.Input().Get("account")) password := strings.TrimSpace(this.Input().Get("password")) //remember用于判断是否创建cookie remember := strings.TrimSpace(this.Input().Get("remember")) //加密用户密码,用于判断密码是否正确 passwordinfo := md5.New() salt1 := beego.AppConfig.String("salt1") salt2 := beego.AppConfig.String("salt2") io.WriteString(passwordinfo, salt1) io.WriteString(passwordinfo, password) io.WriteString(passwordinfo, salt2) passwordinfofinal := fmt.Sprintf("%x", passwordinfo.Sum(nil)) //查找用户名是否存在和用户密码是否正确 searchaccount := models.SearchAccount(account) searchaccountandpwd := models.SearchAccountAndPwd(account, passwordinfofinal) userinfo1, _ := models.Searchuser(account) if len(account) == 0 { this.Data["errmsg"] = "账号不能为空!" } else if len(password) == 0 { this.Data["errmsg"] = "密码不能为空!" } else if searchaccount != nil { this.Data["errmsg"] = "账号不存在!" } else if searchaccountandpwd != nil { this.Data["errmsg"] = "密码不正确!" } else if userinfo1.Lock == 1 { this.Data["errmsg"] = "该用户已锁定!" } else { /* 判断是否记录密码一周 */ if remember == "yes" { //记住密码一周 account1 := beego.AppConfig.String("salt1") + " " + account + " " + beego.AppConfig.String("salt2") account2 := []byte(account1) //base64加密,需要使用byte类型 cookieval := base64.StdEncoding.EncodeToString(account2) this.Ctx.SetCookie("cookieaccount", cookieval, 7*24*3600, "/") } /* 只把当前账号存到session中,登录时不记住密码一周默认设置session,不设置cookie */ userinfo, _ := models.Userinfo(account) uid := userinfo.Id sessionuid := this.GetSession("sessionuid") if sessionuid == nil { //如果session不存在,就创建session this.SetSession("sessionuid", uid) //记录最新登录时间,根据userid更新 ip := this.GetClientIp() err := models.Updateuserlogintime(uid, ip) if err != nil { return } this.Redirect("/index", 301) } else { /* 如果session存在,判断是否是本人博客,URL中获取用户博客名称,然后从 数据库中读取用户账号,加密后判断是否和session数值一致,一致表示是本人 博客,显示本人博客全部功能,不一致表示是其他人博客,只显示博客内容,不显示 博客管理功能! */ //记录最新登录时间,根据userid更新 ip := this.GetClientIp() err := models.Updateuserlogintime(uid, ip) if err != nil { return } this.Redirect("/index", 301) return } } } }
/* 注册页面 */ func (this *LoginController) Register() { this.TplNames = "account/proregister.html" if this.GetString("dosubmit") == "yes" { account := strings.TrimSpace(this.Input().Get("account")) password := strings.TrimSpace(this.Input().Get("password")) password1 := strings.TrimSpace(this.Input().Get("password1")) username := strings.TrimSpace(this.Input().Get("username")) /* 查找数据库是否存在该账号和博客名称 */ searchaccount := models.SearchAccount(account) searchusername := models.SearchUsername(username) /* 判断输入是否合法 */ if len(account) == 0 { this.Data["errmsg"] = "请输入账号!" } else if len(password) == 0 { this.Data["errmsg"] = "请输入密码!" } else if len(password1) == 0 { this.Data["errmsg"] = "请确认密码!" } else if len(username) == 0 { this.Data["errmsg"] = "请输入博客名称!" } else if searchaccount == nil { this.Data["errmsg"] = "该账号已被注册!" } else if searchusername == nil { this.Data["errmsg"] = "该博客名称已被注册!" } else if password1 != password { this.Data["errmsg"] = "两次输入的密码不一致!" } else { /* 加密注册密码 */ hpassword := md5.New() io.WriteString(hpassword, beego.AppConfig.String("salt1")) io.WriteString(hpassword, password) io.WriteString(hpassword, beego.AppConfig.String("salt2")) hpasswordfinal := fmt.Sprintf("%x", hpassword.Sum(nil)) //记录ip地址 ip := this.GetClientIp() err := models.Register(account, hpasswordfinal, username, ip) if err != nil { this.Ctx.WriteString("注册失败!") } else { /* 注册时默认设置session,不设置cookie,根据account字段查找user表主码id */ userinfo, _ := models.Userinfo(account) uid := userinfo.Id //普通用户sessionuid sessionuid := this.GetSession("sessionuid") if sessionuid == nil { //如果session不存在,就创建session this.SetSession("sessionuid", uid) this.Redirect("/index", 301) } else { /* 如果session存在,判断是否是本人博客,URL中获取用户博客名称,然后从 数据库中读取用户账号,加密后判断是否和session数值一致,一致表示是本人 博客,显示本人博客全部功能,不一致表示是其他人博客,只显示博客内容,不显示 博客管理功能! */ this.Redirect("/index", 301) return } } } } }
func (this *IndexController) Shuo() { //获取uid,判断是否非法访问 cookieaccount := this.Ctx.GetCookie("cookieaccount") uid := this.GetSession("sessionuid") if len(cookieaccount) > 0 { if uid == nil { //获取用户账号account account := DecodeCookie(cookieaccount) //获取用户uid userinfo, _ := models.Userinfo(account) uid = userinfo.Id this.SetSession("sessionuid", uid) } //读取数据 // userinfo, _ := models.Userinfowithuid(uid) // chicken, err := models.Selectshuo(userinfo.Id) // if err != nil { // return // } /* 定义分页方法 */ var page int var pagesize int = 10 var list []*models.Chicken_soup var chicken models.Chicken_soup if page, _ = this.GetInt("page"); page < 1 { page = 1 } offset := (page - 1) * pagesize count, _ := chicken.Query(uid).Count() if count > 0 { chicken.Query(uid).OrderBy("-id").Limit(pagesize, offset).All(&list) } //判断count是否为0 if count == 0 { this.Data["noshuo"] = true } else { this.Data["noshuo"] = false } this.Data["count"] = count this.Data["list"] = list this.Data["pagebar"] = util.NewPager(page, int(count), pagesize, "/index/shuo", true).ToString() this.TplNames = "blog/shuo.html" } else if uid != nil { //读取数据 // userinfo, _ := models.Userinfowithuid(uid) // chicken, err := models.Selectshuo(userinfo.Id) // if err != nil { // return // } // this.Data["chicken"] = chicken // this.TplNames = "blog/shuo.html" /* 定义分页方法 */ var page int var pagesize int = 10 var list []*models.Chicken_soup var chicken models.Chicken_soup if page, _ = this.GetInt("page"); page < 1 { page = 1 } offset := (page - 1) * pagesize count, _ := chicken.Query(uid).Count() if count > 0 { chicken.Query(uid).OrderBy("-id").Limit(pagesize, offset).All(&list) } //判断count是否为0 if count == 0 { this.Data["noshuo"] = true } else { this.Data["noshuo"] = false } this.Data["count"] = count this.Data["list"] = list this.Data["pagebar"] = util.NewPager(page, int(count), pagesize, "/index/shuo", true).ToString() this.TplNames = "blog/shuo.html" } else { this.Redirect("/", 301) } }
func (this *IndexController) Get() { //获取cookie和session值 cookieaccount := this.Ctx.GetCookie("cookieaccount") uid := this.GetSession("sessionuid") /* 如果存在cookie,判断session是否存在,不存在就创建session */ if len(cookieaccount) > 0 { if uid == nil { //获取用户账号account account := DecodeCookie(cookieaccount) //获取用户uid userinfo, _ := models.Userinfo(account) uid = userinfo.Id this.SetSession("sessionuid", uid) } /* 输出文章信息 */ article, _ := models.Articleread(uid, 0) this.Data["article"] = article /* 文章推荐信息 */ articletuijian, _ := models.Articletuijian() this.Data["articletuijian"] = articletuijian userinfo, err := models.Userinfowithuid(uid) if err != nil { return } this.Data["userinfo"] = userinfo if userinfo.Job != "" { this.Data["job"] = userinfo.Job } else { this.Data["job"] = "暂未填写" } if userinfo.Brief != "" { this.Data["brief"] = userinfo.Brief } else { this.Data["brief"] = "暂未填写" } this.TplNames = "blog/index.html" return } else if uid != nil { /* 输出文章信息 */ article, _ := models.Articleread(uid, 0) this.Data["article"] = article /* 文章推荐信息 */ articletuijian, _ := models.Articletuijian() this.Data["articletuijian"] = articletuijian /* 随机文章信息 */ articlerand, _ := models.Articlerand() this.Data["articlerand"] = articlerand userinfo, err := models.Userinfowithuid(uid) if err != nil { return } this.Data["userinfo"] = userinfo if userinfo.Job != "" { this.Data["job"] = userinfo.Job } else { this.Data["job"] = "暂未填写" } if userinfo.Brief != "" { this.Data["brief"] = userinfo.Brief } else { this.Data["brief"] = "暂未填写" } this.TplNames = "blog/index.html" return } else { this.Redirect("/", 301) } }
func (this *IndexController) About() { //获取user表中的introduce字段值 uid := this.GetSession("sessionuid") cookieaccount := this.Ctx.GetCookie("cookieaccount") if len(cookieaccount) > 0 { if uid == nil { //获取用户账号account account := DecodeCookie(cookieaccount) //获取用户uid userinfo, _ := models.Userinfo(account) uid = userinfo.Id this.SetSession("sessionuid", uid) } /* 输出文章信息 */ article, _ := models.Articleread(uid, 0) this.Data["article"] = article /* 文章推荐信息 */ articletuijian, _ := models.Articletuijian() this.Data["articletuijian"] = articletuijian /* 随机文章信息 */ articlerand, _ := models.Articlerand() this.Data["articlerand"] = articlerand //读取数据 userinfo, err := models.Userinfowithuid(uid) if err != nil { return } this.Data["userinfo"] = userinfo if userinfo.Job != "" { this.Data["job"] = userinfo.Job } else { this.Data["job"] = "暂未填写" } if userinfo.Brief != "" { this.Data["brief"] = userinfo.Brief } else { this.Data["brief"] = "暂未填写" } if userinfo.Introduce != "" { this.Data["introduce"] = userinfo.Introduce this.TplNames = "blog/about.html" } else { this.Data["introduce"] = "暂无内容!" this.TplNames = "blog/about.html" } } else if uid != nil { /* 输出文章信息 */ article, _ := models.Articleread(uid, 0) this.Data["article"] = article /* 文章推荐信息 */ articletuijian, _ := models.Articletuijian() this.Data["articletuijian"] = articletuijian /* 随机文章信息 */ articlerand, _ := models.Articlerand() this.Data["articlerand"] = articlerand userinfo, err := models.Userinfowithuid(uid) if err != nil { return } this.Data["userinfo"] = userinfo if userinfo.Job != "" { this.Data["job"] = userinfo.Job } else { this.Data["job"] = "暂未填写" } if userinfo.Brief != "" { this.Data["brief"] = userinfo.Brief } else { this.Data["brief"] = "暂未填写" } if userinfo.Introduce != "" { this.Data["introduce"] = userinfo.Introduce this.TplNames = "blog/about.html" } else { this.Data["introduce"] = "暂无内容!" this.TplNames = "blog/about.html" } } else { this.Redirect("/", 301) } }