Beispiel #1
0
//判断是否登陆
func CheckAdminLogin(c *beego.Controller, t int64) {
	id, _ := c.GetSession("adminid").(int)
	username, _ := c.GetSession("adminname").(string)
	adminrole, _ := c.GetSession("adminrole").(int)
	if id == 0 || username == "" || adminrole == 0 {
		switch t {
		case 0:
			c.Redirect("/admin/login", 301)
		case 1:
			//返回JSON
			json := &models.TipJSON{}
			json.Status = models.TipError
			json.Message = "请先登录,再执行此操作"
			json.ReturnUrl = "/admin/login"

			c.Data["json"] = json
			c.ServeJson()
			c.StopRun()
		}

	}
	c.Data["AdminName"] = username
	c.Data["AdminId"] = id
	c.Layout = "admin/layout.tpl"
}
Beispiel #2
0
//信息提示,如果异步,返回json、如果同步,则页面提示并返回
func EchoTip(c *beego.Controller, json *models.TipJSON) {
	if c.IsAjax() {
		//异步提交
		c.Data["json"] = json
		c.ServeJson()
		c.StopRun()
	} else {
		tpl := "admin/error.tpl"
		if json.Status == models.TipSuccess {
			tpl = "admin/success.tpl"
		}
		c.Data["Tip"] = json
		c.Layout = tpl
		//此处不能用 c.StopRun() 返回,否则会空白页面!
		//c.StopRun()
		return
	}
}
Beispiel #3
0
/* 后台操作数据表 */
func Restful(this BaseInterface, contro *beego.Controller) {
	ok, data := func() (bool, interface{}) {
		switch contro.GetString("type") {
		case "add": //增
			if err := contro.ParseForm(this); err != nil {
				return false, err.Error()
			}

			//数据验证
			valid := validation.Validation{}
			b, err := valid.Valid(this)
			if err != nil {
				return false, "验证参数解析失败"
			}

			if !b { //验证失败
				for _, err := range valid.Errors {
					return false, err.Key + " " + err.Message
				}
			}

			// 额外解析数组参数
			if _ok, _data := this.BaseFormStrings(contro); !_ok {
				return false, _data
			}

			if err := this.BaseInsert(); err != nil {
				return false, err.Error()
			}

			return true, "" // 插入成功
		case "delete": //删
			if _ok, _data := this.BaseSetId(contro.GetString("_id")); !_ok {
				return false, _data
			}

			err := this.BaseDelete()

			if err != nil {
				return false, err
			}

			return true, nil
		case "update": //改
			//根据id查询对象
			if _ok, _data := this.BaseFind(contro.GetString("_id")); !_ok {
				return false, _data
			}

			//解析请求参数->对象
			if err := contro.ParseForm(this); err != nil {
				return false, err.Error()
			}

			// 额外解析数组参数
			if _ok, _data := this.BaseFormStrings(contro); !_ok {
				return false, _data
			}

			//验证
			valid := validation.Validation{}
			b, err := valid.Valid(this)
			if err != nil {
				return false, "验证参数解析失败"
			}

			if !b { //验证失败
				for _, err := range valid.Errors {
					return false, err.Key + " " + err.Message
				}
			}

			if err := this.BaseUpdate(); err != nil {
				return false, err.Error()
			}
			return true, nil
		case "get": //查
			from, _ := contro.GetInt("from")
			number, _ := contro.GetInt("number")

			if number > 100 {
				return false, "最多查询100条"
			}

			if contro.GetString("like") != "" && contro.GetString("likeKey") != "" { // 模糊搜索
				var result []BaseInterface
				var count int

				if contro.GetString("likeMethod") == "like" {
					result = this.BaseSelectLike(from, number, contro.GetString("sort"), contro.GetString("likeKey"), contro.GetString("like"))
					count = this.BaseLikeCount(contro.GetString("likeKey"), contro.GetString("like"))
				} else if contro.GetString("likeMethod") == "accuracy" {
					result = this.BaseSelectAccuracy(from, number, contro.GetString("sort"), contro.GetString("likeKey"), contro.GetString("like"))
					count = this.BaseAccuracyCount(contro.GetString("likeKey"), contro.GetString("like"))
				}

				return true, map[string]interface{}{
					"lists": result,
					"count": count,
				}
			} else { // 普通查询
				if contro.GetString("filter") != "" {
					filter, _ := contro.GetInt("filter")
					count := this.BaseFilterCount(contro.GetString("filterKey"), filter)
					result := this.BaseFilterSelect(from, number, contro.GetString("sort"), contro.GetString("filterKey"), filter)

					return true, map[string]interface{}{
						"lists": result,
						"count": count,
					}
				} else {
					count := this.BaseCount()
					result := this.BaseSelect(from, number, contro.GetString("sort"))

					return true, map[string]interface{}{
						"lists": result,
						"count": count,
					}
				}
			}
		}
		return true, nil
	}()

	contro.Data["json"] = map[string]interface{}{
		"ok":   ok,
		"data": data,
	}
	contro.ServeJson()
}