Example #1
0
/**
注册帐号
name	帐号
pwd		密码
*/
func RegisterUser(session *server.Session, response *server.Response, request server.Request) {
	logger.Printf("regist user: %v", request)
	//dao.CheckUserExist("aa")
	name, hasName := request.Get("name")
	pwd, hasPwd := request.Get("pwd")

	if !hasName || !hasPwd {
		response.Put("state", server.STATE_FAILD)
		response.Put("msg", "参数异常!")
		return
	}

	tx, err := dao.Begin()
	defer tx.Close()

	isExist, err := tx.CheckUserExist(name)

	if err != nil {
		utils.CheckErr(err)
		response.Put("state", server.STATE_FAILD)
		response.Put("msg", "处理异常!")

		return
	}

	if isExist {
		response.Put("state", server.STATE_FAILD)
		response.Put("msg", "帐号已经存在!")

		return
	}

	err = tx.RegistUser(name, pwd)
	if err != nil {
		utils.CheckErr(err)
		response.Put("state", server.STATE_FAILD)
		response.Put("msg", "处理异常!")
		return

	}

	tx.Commit()
	response.Put("state", server.STATE_OK)

}
Example #2
0
/**
登录帐号
name	帐号
pwd		密码
*/
func UserLogin(session *server.Session, response *server.Response, request server.Request) {
	name, hasName := request.Get("name")
	pwd, hasPwd := request.Get("pwd")

	if !hasName || !hasPwd {
		response.Put("state", server.STATE_FAILD)
		response.Put("msg", "参数异常!")
		return
	}

	tx, err := dao.Begin()
	defer tx.Close()

	userId, err := tx.QueryUser(name, pwd)
	if err != nil {
		utils.CheckErr(err)
		response.Put("state", server.STATE_FAILD)
		response.Put("msg", "程序执行异常!")
		return
	}

	// 帐号不存在,或者密码错误
	if userId == 0 {
		response.Put("state", server.STATE_FAILD)
		response.Put("msg", "请检查帐号密码是否正确!")
		return
	}

	// 帐号检查成功
	// 存储userId
	session.Put(server.ATTR_USER_ID, userId)
	//
	session.Put(server.ATTR_IS_LOGIN, true)

	response.Put("state", server.STATE_OK)
}