Exemple #1
0
func CreateAdmin(r render.Render, req *http.Request) {
	if len(cache.Users) > 0 {
		r.Error(404)
		return
	}
	if req.Method == "GET" {
		r.HTML(200, "user_add", map[string]interface{}{})
		return
	}

	req.ParseForm()
	values := req.Form
	m := &model.User{}
	m.Account = values.Get("account")
	m.Password = model.Md5(values.Get("password"))
	m.Info = values.Get("info")
	m.Name = values.Get("name")
	if !checkNull([]string{m.Account, m.Password, m.Info, m.Name}...) {
		service.Logs.Error("args err")
		return
	}
	m.Status = model.UserStatusAdmin
	_, err := dao.AddUser(m)
	if err != nil {
		service.Logs.Error("dao.InsertUser err(%v)", err)
		return
	}
	r.Redirect("/", 302)
	return
}
Exemple #2
0
func Login(r render.Render, req *http.Request, session sessions.Session) {
	if req.Method == "GET" {
		r.HTML(200, "login", "")
		return
	}
	req.ParseForm()
	values := req.Form
	account := values.Get("account")
	fmt.Println(values.Get("pass"))
	pass := model.Md5(values.Get("pass"))
	fmt.Println(pass)
	fmt.Println(account)
	returnData := make(map[string]interface{})
	returnData["status"] = 0
	//returnData["msg"] = "登录失败"
	var errMsg string
	if account == "" || pass == "" {
		errMsg = "pass 或者  account 为空"
		service.Logs.Error(errMsg)
		returnData["msg"] = errMsg
		returnData["status"] = 1
		r.HTML(200, "login", returnData)
		return
	}
	userInfo, err := dao.GetAdminInfoByName(account, pass)
	if err != nil {
		errMsg = "登陆失败,账号或密码错误,或者账号已经禁用!"
		service.Logs.Error(fmt.Sprintf("%s", err))
		returnData["msg"] = errMsg
		returnData["status"] = 1
		r.JSON(200, returnData)
		return
	}

	session.Set("login", "true")
	session.Set("UserName", userInfo.Name)
	//	r.JSON(200, map[string]interface{}{"error": "10001", "msg": "Data Error"})
	//	return

	returnData["msg"] = "登陆成功!"
	r.JSON(200, returnData)
	return
}