Пример #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)
}
Пример #3
0
func loginHandler(request *http.Request, resp http.ResponseWriter, redis *models.RedisLogger, form loginForm) {
	user := &models.Account{}
	var err error
	var reg bool
	token := Uuid() + "-" + strconv.FormatInt(time.Now().AddDate(0, 0, 30).Unix(), 10)

	switch form.Type {
	case "weibo":
		reg, user, err = weiboLogin(form.Userid, form.Password, redis)
	case "weixin":
		writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.UnimplementedError))
		return
	case "usrpass":
		fallthrough
	default:
		var find bool
		if find, err = user.FindByUserPass(strings.ToLower(form.Userid), Md5(form.Password)); !find {
			if err == nil {
				err = errors.NewError(errors.AuthError)
			}
		}
	}

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

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

	redis.SetOnlineUser(token, user.Id)

	data := map[string]interface{}{
		"access_token":    token,
		"userid":          user.Id,
		"register":        reg,
		"last_login_time": user.LastLogin.Unix(),
		"ExpEffect":       Awards{},
	}
	writeResponse(request.RequestURI, resp, data, nil)
}
Пример #4
0
func registerHandler(request *http.Request, resp http.ResponseWriter, redis *models.RedisLogger, form userRegForm) {
	user := &models.Account{}
	t := ""
	if phone, _ := strconv.ParseUint(form.Email, 10, 64); phone > 0 {
		user.Phone = form.Email
		t = models.AccountPhone
	} else {
		user.Email = strings.ToLower(form.Email)
		t = models.AccountEmail
	}

	if exists, _ := user.Exists(t); exists {
		writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.UserExistError))
		return
	}
	//user.Nickname = form.Nickname
	user.Password = Md5(form.Password)
	user.Role = t
	user.RegTime = time.Now()
	dbw, err := getNewWallet()
	if err != nil {
		writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.DbError, "wallet: "+err.Error()))
		return
	}
	user.Wallet = *dbw

	if err := user.Save(); err != nil {
		writeResponse(request.RequestURI, resp, nil, err)
	} else {
		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, t)
		redis.SetOnlineUser(token, user.Id)

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