Ejemplo n.º 1
0
/*
编辑心灵鸡汤内容
*/
func (this *AdminController) Editshuo() {
	this.TplNames = "message/editshuo.html"
	//判断是新增内容还是修改内容
	op := this.Input().Get("op")
	content := this.Input().Get("content")
	if op == "add" {
		//添加操作
		uid := this.GetSession("sessionuid")
		userinfo, err := models.Userinfowithuid(uid)
		if err != nil {
			return
		}
		//执行添加
		err = models.Addshuo(content, userinfo.Id)
		if err == nil {
			this.Redirect("/admin/shuo", 301)
		}
	} else {
		//更新操作
		uid := this.GetSession("sessionuid")
		userinfo, err := models.Userinfowithuid(uid)
		if err != nil {
			return
		}
		//执行更新
		err = models.Updateshuo(content, userinfo.Id)
		if err != nil {
			this.Redirect("/admin/shuo", 301)
		}
	}
}
Ejemplo n.º 2
0
/*
个人信息页面
*/
func (this *AdminController) Profile() {
	//判断是管理员还是普通用户
	sessionuid := this.GetSession("sessionuid")
	// cookieaccount := this.Ctx.GetCookie("cookieaccount")
	sessionaid := this.GetSession("sessionaid")
	cookieadmin := this.Ctx.GetCookie("cookieadmin")
	//管理员
	if sessionaid != nil {
		userinfo, err := models.Admininfowithaid(sessionaid)
		if err != nil {
			return
		}
		this.Data["userinfo"] = userinfo
		this.Data["user"] = false
	} else if len(cookieadmin) > 0 {

	} else if sessionuid != nil {
		//普通用户
		userinfo, err := models.Userinfowithuid(sessionuid)
		if err != nil {
			return
		}
		this.Data["userinfo"] = userinfo
		this.Data["user"] = true
	} else {

	}
	this.TplNames = "account/profile.html"
}
Ejemplo n.º 3
0
/*
文章显示,每浏览一次浏览量增加1
*/
func (this *IndexController) New() {
	uid := this.GetSession("sessionuid")

	/*
		输出文章信息
	*/
	article1, _ := models.Articleread(uid, 0)
	this.Data["article1"] = article1

	/*
		文章推荐信息
	*/
	articletuijian, _ := models.Articletuijian()
	this.Data["articletuijian"] = articletuijian

	/*
		随机文章信息
	*/
	articlerand, _ := models.Articlerand()
	this.Data["articlerand"] = articlerand

	articleid1 := this.Input().Get("articleid")
	articleid, _ := strconv.Atoi(articleid1)
	// this.Data["test"] = articleid
	// this.TplNames = "article/testarticle.html"
	// return
	//浏览量加1
	err := models.Articleviewsadd(articleid)
	if err != nil {
		return
	}
	article, _ := models.Articlefindwithaid(articleid1)
	userid := this.GetSession("sessionuid")
	var userinfo *models.User
	userinfo, err = models.Userinfowithuid(userid)
	if err != nil {
		return
	}
	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.Data["userinfo"] = userinfo
	this.Data["article"] = article
	this.TplNames = "blog/news.html"
}
Ejemplo n.º 4
0
/*
个人资料页面
*/
func (this *AdminController) Userinfo() {
	/*
		读取数据库信息显示到页面(读取两张表数据,分别是user表和about表)
	*/
	uid := this.GetSession("sessionuid")
	userinfo, err := models.Userinfowithuid(uid)
	if err != nil {
		this.TplNames = "message/userinfo.html"
		return
	}
	this.TplNames = "message/userinfo.html"
	this.Data["userinfo"] = userinfo
}
Ejemplo n.º 5
0
/*
分类显示
*/
func (this *IndexController) Category() {
	//获取分类
	category := this.GetString("tags")
	//输出文章,显示在左侧
	article1, err := models.Articlecategory(category)
	if err != nil {
		return
	}
	this.Data["article1"] = article1
	this.Data["tags"] = category

	this.Data["status"] = false
	/*
		输出文章信息,用于右侧显示
	*/
	uid := this.GetSession("sessionuid")
	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/learn.html"

}
Ejemplo n.º 6
0
func (this *IndexController) Riji() {
	uid := this.GetSession("sessionuid")
	/*
		输出文章信息
	*/
	article, _ := models.Articleread(uid, 0)
	this.Data["article"] = article

	/*
		文章推荐信息
	*/
	articletuijian, _ := models.Articletuijian()
	this.Data["articletuijian"] = articletuijian

	/*
		随机文章信息
	*/
	articlerand, _ := models.Articlerand()
	this.Data["articlerand"] = articlerand

	//获取用户信息
	userinfo, _ := models.Userinfowithuid(uid)
	if userinfo.Job != "" {
		this.Data["job"] = userinfo.Job
	} else {
		this.Data["job"] = "暂未填写"
	}
	if userinfo.Brief != "" {
		this.Data["brief"] = userinfo.Brief
	} else {
		this.Data["brief"] = "暂未填写"
	}
	//获取日记信息
	diary, _, count := models.Dirayfindwithuserid(uid)
	if count == 0 {
		this.Data["nodiary"] = true
	} else {
		this.Data["nodiary"] = false
	}
	this.Data["diary"] = diary
	this.Data["userinfo"] = userinfo
	this.TplNames = "blog/riji.html"
}
Ejemplo n.º 7
0
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)
	}

}
Ejemplo n.º 8
0
/*
学无止境
*/
func (this *IndexController) Xuewuzhijing() {
	/*
		输出文章信息
	*/
	uid := this.GetSession("sessionuid")
	article, _ := models.Articleread(uid, 0)
	this.Data["article"] = article

	/*
		文章推荐信息
	*/
	articletuijian, _ := models.Articletuijian()
	this.Data["articletuijian"] = articletuijian

	/*
		随机文章信息
	*/
	articlerand, _ := models.Articlerand()
	this.Data["articlerand"] = articlerand
	this.Data["status"] = true
	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/learn.html"
}
Ejemplo n.º 9
0
/*
上传头像处理
*/
func (this *AdminController) Postimage() {
	_, f, err := this.GetFile("uploadimage")
	if err != nil {
		this.Data["shangchuan"] = "请上传文件!"
		this.Data["errmsg"] = true
		this.TplNames = "upload/image.html"
		return
	}
	if f == nil {
		this.Data["shangchuan"] = "请上传文件!"
		this.Data["errmsg"] = true
		this.TplNames = "upload/image.html"
		return
	}
	filename := f.Filename
	houzhui := strings.LastIndex(filename, ".")
	//后缀名
	realhouzhui := filename[houzhui:]
	// this.Data["test"] = realhouzhui
	// this.TplNames = "article/testarticle.html"
	// return
	// curtime := time.Now().Unix()
	// formatcurtime := this.Timetoformat(curtime)
	//获取当前用户名
	userid := this.GetSession("sessionuid")
	if userid == nil {
		//session不存在
		return
	}
	var userinfo *models.User
	userinfo, err = models.Userinfowithuid(userid)
	if err != nil {
		return
	}
	username := userinfo.Username
	//头像保存路径
	path := "static/uploadheadimage/" + username + "/"
	err = os.MkdirAll(path, os.ModePerm)
	if err != nil {
		return
	}
	err = this.SaveToFile("uploadimage", path+"original"+realhouzhui)
	if err != nil {
		return
	}
	//图片保存成功后对图片生成缩略图,传递图片路径,图片名称,和缩略图大小和缩略图类型
	name := "original" + realhouzhui
	var size1 uint = 230
	var size2 uint = 120
	//生成大头像
	err = this.Thumbnail(path, name, size1, true)
	if err != nil {
		return
	}
	//生成小头像
	err = this.Thumbnail(path, name, size2, false)
	if err != nil {
		return
	}

	//获取上传后头像路径
	facebig := path + "facebig" + realhouzhui
	facesmall := path + "facesmall" + realhouzhui
	//先判断数据库是否存在头像路径,存在就删除头像文件,之后再更新数据库
	// if len(userinfo.Facebig) > 0 && len(userinfo.Facesmall) > 0 {
	// 	oldpath := userinfo.Facebig
	// 	oldpathmulu := oldpath[:strings.LastIndex(oldpath, "/")]
	// 	err := os.Remove(oldpathmulu)
	// 	if err != nil {
	// 		return
	// 	}
	// }
	err = models.Updateuserface(userid, facebig, facesmall)
	if err != nil {
		return
	}
	this.Data["success"] = true
	this.TplNames = "upload/image.html"
}
Ejemplo n.º 10
0
/*
修改个人密码
*/
func (this *AdminController) Modifypassword() {
	style, _ := this.GetInt32("type")
	oldpassword := this.Input().Get("password")
	newpassword := this.Input().Get("newpassword")
	newpassword2 := this.Input().Get("newpassword2")

	if style == 0 {
		//普通用户修改密码
		userid := this.GetSession("sessionuid")
		password := md5.New()
		salt1 := beego.AppConfig.String("salt1")
		salt2 := beego.AppConfig.String("salt2")
		io.WriteString(password, salt1)
		io.WriteString(password, oldpassword)
		io.WriteString(password, salt2)
		testpassword := fmt.Sprintf("%x", password.Sum(nil))
		//比较加密后的旧密码和数据库中密码是否一致
		userinfo, _ := models.Userinfowithuid(userid)
		if oldpassword == "" {
			this.Data["errmsg"] = "当前密码不能为空!"
			this.Profile()
		} else if newpassword == "" {
			this.Data["errmsg"] = "新密码不能为空!"
			this.Profile()
		} else if newpassword2 == "" {
			this.Data["errmsg"] = "请确认密码!"
			this.Profile()
		} else if userinfo.Password != testpassword {
			this.Data["errmsg"] = "原密码不正确!"
			this.Profile()
		} else if newpassword != newpassword2 {
			this.Data["errmsg"] = "两次输入的新密码不相同!"
			this.Profile()

		} else {
			//加密
			password1 := md5.New()
			io.WriteString(password1, salt1)
			io.WriteString(password1, newpassword)
			io.WriteString(password1, salt2)
			password1final := fmt.Sprintf("%x", password1.Sum(nil))
			err := models.Updateuserpassword(userid, password1final)
			if err != nil {
				return
			}
			this.Data["errmsg"] = "修改成功!"
		}
		this.Profile()
		// this.Redirect("/admin/modifypassword", 301)
	} else {
		//管理员修改密码
		adminid := this.GetSession("sessionaid")
		password := md5.New()
		salt1 := beego.AppConfig.String("salt1")
		salt2 := beego.AppConfig.String("salt2")
		io.WriteString(password, salt1)
		io.WriteString(password, oldpassword)
		io.WriteString(password, salt2)
		testpassword := fmt.Sprintf("%x", password.Sum(nil))
		//比较加密后的旧密码和数据库中密码是否一致
		userinfo, _ := models.Admininfowithaid(adminid)
		if oldpassword == "" {
			this.Data["errmsg"] = "当前密码不能为空!"
			this.Profile()
		} else if newpassword == "" {
			this.Data["errmsg"] = "新密码不能为空!"
			this.Profile()
		} else if newpassword2 == "" {
			this.Data["errmsg"] = "请确认密码!"
			this.Profile()
		} else if userinfo.Password != testpassword {
			this.Data["errmsg"] = "原密码不正确!"
			this.Profile()
		} else if newpassword != newpassword2 {
			this.Data["errmsg"] = "两次输入的新密码不相同!"
			this.Profile()

		} else {
			//加密
			password1 := md5.New()
			io.WriteString(password1, salt1)
			io.WriteString(password1, newpassword)
			io.WriteString(password1, salt2)
			password1final := fmt.Sprintf("%x", password1.Sum(nil))
			err := models.Updateadminpassword(adminid, password1final)
			if err != nil {
				return
			}
			this.Data["errmsg"] = "修改成功!"
		}
		this.Profile()

	}
}
Ejemplo n.º 11
0
/*
主页面输出
*/
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)
	}
}
Ejemplo n.º 12
0
//保存
func (this *ArticleController) Save() {
	var (
		id      int    = 0
		title   string = strings.TrimSpace(this.GetString("title"))
		content string = this.GetString("content")
		tags    string = strings.TrimSpace(this.GetString("tags"))
		color   string = strings.TrimSpace(this.GetString("color"))
		picture string = strings.TrimSpace(this.GetString("articleimage"))
		brief   string = strings.TrimSpace(this.GetString("brief"))
		// timestr int    = time.Now().Unix()
		status1 int = 0
		istop1  int = 0
		// article models.Article
	)
	if title == "" {
		//标题不能为空
		return
	}
	if brief == "" {
		return
	}

	id1 := this.GetString("id")
	id2, _ := strconv.ParseInt(id1, 10, 64)
	id = int(id2)
	status1, _ = this.GetInt("status")
	status := int8(status1)
	istop1, _ = this.GetInt("istop")
	istop := int8(istop1)
	if istop1 == 1 {
		istop = 1
	} else {
		istop = 0
	}

	if status != 1 && status != 2 {
		status = 0
	}

	addtags := make([]string, 0)
	//标签过滤
	if tags != "" {
		tagarr := strings.Split(tags, ",")
		for _, v := range tagarr {
			if tag := strings.TrimSpace(v); tag != "" {
				exists := false
				for _, vv := range addtags {
					if vv == tag {
						exists = true
						break
					}
				}
				if !exists {
					addtags = append(addtags, tag)
				}
			}
		}
	}

	if id < 1 {
		//id 小于1为添加文章操作
		uid := this.GetSession("sessionuid")
		userinfo, err := models.Userinfowithuid(uid)
		if err != nil {
			return
		}

		//插入操作
		author := userinfo.Username
		var articleid int
		articleid, err = models.Articleadd(author, title, content, tags, color, picture, brief, status, istop)
		if err != nil {
			return
		}

		/*
			处理上传文章封面图片,保存在static/articleimage/用户名/文章ID/封面图片
		*/
		_, f, _ := this.GetFile("articleimage")
		if f != nil {
			filename := f.Filename
			houzhui := strings.LastIndex(filename, ".")
			//后缀名
			realhouzhui := filename[houzhui:]
			userid := this.GetSession("sessionuid")
			userinfo, err = models.Userinfowithuid(userid)

			if err != nil {
				return
			}
			username := userinfo.Username
			articleid1 := strconv.Itoa(articleid)
			//保存路径
			path := "static/articleimage/" + username + "/" + articleid1 + "/"
			err = os.MkdirAll(path, os.ModePerm)
			err = this.SaveToFile("articleimage", path+"original"+realhouzhui)
			if err != nil {
				return
			}
			// this.Redirect("/admin/articlelist", 301)
			//图片保存成功后对图片生成缩略图,传递图片路径,图片名称和缩略图大小
			name := "original" + realhouzhui
			var size1 uint = 200
			var size2 uint = 123
			err = this.Thumbnailarticle(path, name, size1, size2)
			if err != nil {
				return
			}
			// this.Redirect("/admin/articlelist", 301)

			//获取保存后路径
			articleimage := path + "articleimage" + realhouzhui
			//更新数据库信息
			err = models.Articleimageupdate(articleid, articleimage)
			if err != nil {
				return
			}
		}

		this.Redirect("/admin/articlelist", 301)
	} else {
		//id大于等于1时为编辑文章操作
		// uid := this.GetSession("sessionuid")
		// userinfo, err := models.Userinfowithuid(uid)
		// if err != nil {
		// 	return
		// }

		//表单传递的时间格式转换为时间戳

		timevalue := this.GetString("posttime")
		timestampvalue := int(Datetotimestamp(timevalue))

		//文章编辑操作
		err := models.Articleedit(title, color, tags, content, brief, status, istop, timestampvalue, id)
		if err != nil {
			return
		}

		/*
			处理上传文章封面图片,保存在static/articleimage/用户名/文章ID/封面图片
		*/
		//如果编辑页面重新上传了新的封面,就进行封面更新,否则不处理

		_, f, _ := this.GetFile("articleimage")
		if f != nil {
			// this.Redirect("/admin", 301)
			filename := f.Filename
			houzhui := strings.LastIndex(filename, ".")
			//后缀名
			realhouzhui := filename[houzhui:]
			userid := this.GetSession("sessionuid")
			userinfo, err := models.Userinfowithuid(userid)

			if err != nil {
				return
			}
			username := userinfo.Username
			//保存路径
			articleid := strconv.Itoa(id)
			path := "static/articleimage/" + username + "/" + articleid + "/"
			err = os.MkdirAll(path, os.ModePerm)
			err = this.SaveToFile("articleimage", path+"original"+realhouzhui)
			if err != nil {
				return
			}
			// this.Redirect("/admin/articlelist", 301)
			//图片保存成功后对图片生成缩略图,传递图片路径,图片名称和缩略图大小
			name := "original" + realhouzhui
			var size1 uint = 200
			var size2 uint = 123
			err = this.Thumbnailarticle(path, name, size1, size2)
			if err != nil {
				return
			}
			// this.Redirect("/admin/articlelist", 301)

			//获取保存后路径
			articleimage := path + "articleimage" + realhouzhui
			//更新数据库信息
			err = models.Articleimageupdate(id, articleimage)
			if err != nil {
				return
			}
		}

		this.Redirect("/admin/articlelist", 301)
	}
	// uid := this.GetSession("uid")
	// userinfo, _ := models.Userinfowithuid(uid)
	// err := models.Articleadd(title, color, content)
	// if err != nil {
	// 	return
	// }
	this.Redirect("/admin/userinfo", 301)
}
Ejemplo n.º 13
0
/*
上传头像处理
*/
func (this *ImageController) Postimage() {
	_, f, err := this.GetFile("uploadimage")
	if err != nil {
		this.Data["shangchuan"] = "请上传文件!"
		this.Data["errmsg"] = true
		this.TplNames = "upload/image.html"
		return
	}
	if f == nil {
		this.Data["shangchuan"] = "请上传文件!"
		this.Data["errmsg"] = true
		this.TplNames = "upload/image.html"
		return
	}
	filename := f.Filename
	houzhui := strings.LastIndex(filename, ".")
	//后缀名
	realhouzhui := filename[houzhui:]
	// this.Data["test"] = realhouzhui
	// this.TplNames = "article/testarticle.html"
	// return
	curtime := time.Now().Unix()
	formatcurtime := this.Timetoformat(curtime)
	//获取当前用户名
	userid := this.GetSession("sessionuid")
	if userid == nil {
		//session不存在
		return
	}
	var userinfo *models.User
	userinfo, err = models.Userinfowithuid(userid)
	if err != nil {
		return
	}
	username := userinfo.Username
	//头像保存路径
	path := "static/uploadimage/" + username + "/" + formatcurtime + "/"
	err = os.MkdirAll(path, os.ModePerm)
	if err != nil {
		return
	}
	err = this.SaveToFile("uploadimage", path+"original"+realhouzhui)
	if err != nil {
		return
	}
	//获取原图片的宽度和高度
	name1 := "original" + realhouzhui
	width, height := this.Getimageheight(path, name1)

	//根据获取的宽度和高度压缩比例,宽度默认压缩为200
	height1 := 200 * height / width
	height2 := uint(height1)
	//图片保存成功后对图片生成缩略图,传递图片路径,图片名称,和缩略图大小和缩略图类型
	name := "original" + realhouzhui
	err = this.Thumbnail1(path, name, height2, curtime)
	if err != nil {
		return
	}
	// this.Redirect("/admin", 301)
	//移除原图

	//获取上传后图片路径
	curtime32 := int(curtime)
	curtimestring := strconv.Itoa(curtime32)
	imagepath := path + curtimestring + realhouzhui

	//获取上传后图片高度
	name2 := curtimestring + realhouzhui
	_, height3 := this.Getimageheight(path, name2)

	//插入数据到图片表
	userid1 := userid.(int)
	err = models.Insertpicture(userid1, height3, imagepath)
	if err != nil {
		return
	}
	this.Data["success"] = true
	this.TplNames = "upload/uploadimage.html"
}
Ejemplo n.º 14
0
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)
	}

}
Ejemplo n.º 15
0
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)
	}

}