Beispiel #1
0
//handleAddSched URL: /sched/add
func handleAddSched(cookie UserCookie, schedForm Sched, r render.Render, logger *log.Logger, config Config) {
	if !cookie.Validate() {
		r.JSON(http.StatusOK, J{"data": nil})
		return
	}
	if !schedForm.Validate() {
		r.JSON(http.StatusOK, J{"data": nil, "err": J{"code": 101, "msg": "invalid form"}})
		return
	}
	db, err := gorm.Open(config.DB.Type, config.DB.Uri)
	if err != nil {
		r.JSON(http.StatusInternalServerError, J{"data": nil})
		logger.Error(err)
		return
	}
	defer db.Close()
	var count int
	db.Table("users").Where(&cookie).Count(&count)
	if count == 0 {
		r.JSON(http.StatusOK, J{"data": nil})
		return
	}
	err = db.Table("scheds").Create(&schedForm).Error
	if err != nil {
		r.JSON(http.StatusOK, J{"data": nil, "err": J{"code": 300, "msg": err.Error()}})
		return
	}

	r.JSON(http.StatusOK, J{"data": "insert OK"})

}
Beispiel #2
0
//GetBkimg url: /user/bkimg/get
func GetBkimg(cookie UserCookie, config Config, logger *log.Logger, r render.Render) {
	if !cookie.Validate() {
		r.JSON(http.StatusOK, J{"data": nil})
		logger.Info("Fail to auth whith cookie:", cookie)
		return
	}

	db, err := gorm.Open(config.DB.Type, config.DB.Uri)
	if err != nil {
		r.JSON(http.StatusInternalServerError, J{"data": nil, "err": J{"code": 201, "msg": "database open error."}})
		return
	}
	var BkimgName string

	row := db.Table("users").Where(&cookie).Select("bkimg").Row()

	if row == nil {
		r.JSON(http.StatusInternalServerError, J{"data": nil})
		logger.Error(err)
		return
	}

	row.Scan(&BkimgName)
	r.JSON(http.StatusOK, J{"data": BkimgName, "err": nil})

}
Beispiel #3
0
//RegisterHandler url: /user/register
func RegisterHandler(w http.ResponseWriter, config Config, form UserRegisterForm, r render.Render, logger *log.Logger) {
	if !form.Validate() {
		r.JSON(http.StatusOK, J{"data": nil, "err": J{"code": 100, "msg": "invalid name"}})
		return
	}

	priv := util.GetRandomString(10)
	p1 := util.Md5(form.Pwd, priv)
	SecKey := util.Md5(p1, config.AuthConfig.ConstSalt)
	db, err := gorm.Open(config.DB.Type, config.DB.Uri)
	if err != nil {
		r.JSON(http.StatusInternalServerError, J{"data": nil, "err": J{"code": 201, "msg": "database open error."}})
		return
	}
	defer db.Close()

	NewUser := User{
		Name:   form.Name,
		SecKey: SecKey,
		Priv:   priv,
	}
	//把新用户插入users表中
	err = db.Table("users").Create(&NewUser).Error
	if err != nil {
		r.JSON(http.StatusInternalServerError, J{"data": nil, "err": J{"code": 202, "msg": "database insert error."}})
		logger.Error(err)
		return
	}

	r.JSON(http.StatusOK, J{"data": NewUser.Name, "err": nil})

}
Beispiel #4
0
//handleGetSched URL:/sched/all 获取日程表数据
func handleGetSched(r render.Render, logger *log.Logger, config Config, cookie UserCookie) {

	if !cookie.Validate() {
		logger.Info("Fail to auth whith cookie:", cookie)
		r.JSON(http.StatusOK, J{"data": nil})
		return
	}

	//type表示数据库的类型,如mysql,sqlite3等
	//uri为需要打开的数据库连接,格式为user:password@/dbname?charset=utf8
	//两者都定义在config.ini中
	db, err := gorm.Open(config.DB.Type, config.DB.Uri)
	if err != nil {
		r.JSON(http.StatusInternalServerError, J{"data": nil})
		logger.Error(err)
		return
	}
	defer db.Close()
	var count int
	db.Table("users").Where(&cookie).Count(&count)
	if count == 0 {
		r.JSON(http.StatusOK, J{"data": nil})
		return
	}

	var sched []Sched
	db.Table("scheds").Select("*").Where("user=?", cookie.Name).Find(&sched)

	r.JSON(http.StatusOK, J{"data": sched})

	logger.Info("Schedule items total", len(sched), "in JSON,", "with cookie:", cookie)
}
Beispiel #5
0
//UploadBkimg url: /user/bkimg/upload
func UploadBkimg(img Bkimg, r render.Render, cookie UserCookie, config Config, logger *log.Logger) {
	//检查cookie的有效性
	if !cookie.Validate() {
		r.Redirect("/", http.StatusUnauthorized)
		logger.Info("Fail to auth whith cookie:", cookie)
		return
	}
	//打开上传文件
	file, err := img.Content.Open()
	if err != nil {
		r.Redirect("/", http.StatusInternalServerError)
		return
	}

	//将文件内容全被读出来
	b, err := ioutil.ReadAll(file)
	if err != nil {
		r.Redirect("/", http.StatusInternalServerError)
		return
	}

	//检查该图片文件的类型,如果不是图片文件的话那么上传失败,返回。
	_, format, err := image.Decode(bytes.NewReader(b))
	switch err {
	case image.ErrFormat:
		r.Redirect("/", http.StatusOK)
		return
	case nil:
		break
	default:
		r.Redirect("/", http.StatusInternalServerError)
		logger.Info(err.Error())
		return
	}

	//计算文件的md5,作为唯一表示以及文件名。
	fileMd5 := util.Md5(b)
	fileName := fileMd5 + "." + format
	fileFullName := filepath.Join(config.Server.StaticHome, "img/bk", fileName)

	//如果该文件存在那么直接跳到接入数据库
	if fi, _ := os.Stat(fileFullName); fi != nil {
		r.Redirect("/", http.StatusFound)
		logger.Info("file exists:", fileFullName)
		goto CommitToDB
	}
	err = ioutil.WriteFile(fileFullName, b, 0600)
	if err != nil {
		r.Redirect("/", http.StatusInternalServerError)
		return
	}

	//将该图片文件的文件名存入users表的bkimg字段中。
CommitToDB:
	db, err := gorm.Open(config.DB.Type, config.DB.Uri)
	if err != nil {
		r.Redirect("/", http.StatusInternalServerError)
		return
	}

	if err = db.Table("users").Where(&cookie).Update("bkimg", fileName).Error; err != nil {
		r.Redirect("/", http.StatusInternalServerError)
		return
	}
	r.Redirect("/", http.StatusFound)

}