예제 #1
0
func loginHandlerV2(request *http.Request, resp http.ResponseWriter,
	redis *models.RedisLogger, form loginFormV2) {
	user := &models.Account{}
	if exists, _ := models.CheckUserExists(form.Id, form.Type); !exists {
		writeResponse(request.RequestURI, resp,
			nil, errors.NewError(errors.AuthError, "该用户还未注册"))
		return
	}
	user.FindPass(form.Id, form.Type, Md5(form.Password))
	if len(user.Id) == 0 {
		writeResponse(request.RequestURI, resp,
			nil, errors.NewError(errors.AuthError))
		return
	}

	if user.TimeLimit < 0 {
		writeResponse(request.RequestURI, resp,
			nil, errors.NewError(errors.AuthError, "账户已禁用"))
		return
	}

	token := Uuid() + "-" + strconv.FormatInt(time.Now().AddDate(0, 0, 30).Unix(), 10)
	redis.SetOnlineUser(token, user.Id)

	data := map[string]interface{}{
		"access_token": token,
		"userid":       user.Id,
		//"last_login_time": user.LastLogin.Unix(),
	}
	writeResponse(request.RequestURI, resp, data, nil)
}
예제 #2
0
func regHandlerV2(request *http.Request, resp http.ResponseWriter,
	redis *models.RedisLogger, form regFormV2) {

	exists, err := models.CheckUserExists(form.Id, form.Type)
	if err != nil {
		writeResponse(request.RequestURI, resp,
			nil, errors.NewError(errors.DbError))
		return
	}
	if exists {
		writeResponse(request.RequestURI, resp,
			nil, errors.NewError(errors.UserExistError))
		return
	}

	dbw, err := getNewWallet()
	if err != nil {
		writeResponse(request.RequestURI, resp,
			nil, errors.NewError(errors.DbError, "创建钱包失败"))
		return
	}

	user := &models.Account{
		Role:     form.Type,
		Password: Md5(form.Password),
		Nickname: form.Nickname,
		Profile:  form.Profile,
		Gender:   form.Gender,
		Birth:    form.Birthday,
		Wallet:   *dbw,
		RegTime:  time.Now(),
	}

	switch form.Type {
	case models.AccountPhone:
		user.Phone = form.Id
	case models.AccountWeibo:
		user.Weibo = form.Id
	case models.AccountEmail:
		fallthrough
	default:
		user.Email = form.Id
	}

	if err := user.Save(); err != nil {
		writeResponse(request.RequestURI, resp, nil, err)
		return
	}

	token := Uuid() + "-" + strconv.FormatInt(time.Now().AddDate(0, 0, 30).Unix(), 10)
	data := map[string]string{"access_token": token, "userid": user.Id}
	writeResponse(request.RequestURI, resp, data, nil)

	redis.LogRegister(user.Id, form.Type)
	redis.SetOnlineUser(token, user.Id)

	// ws push
	//regNotice(user.Id, redis)
}