Exemple #1
0
func txHandler(r *http.Request, w http.ResponseWriter,
	client *ApnClient, redis *models.RedisLogger, user *models.Account, p Parameter) {
	form := p.(txForm)

	if form.FromAddr == form.ToAddr {
		writeResponse(r.RequestURI, w, map[string]string{"txid": ""}, nil)
		return
	}

	if form.Value <= 0 {
		writeResponse(r.RequestURI, w, nil, errors.NewError(errors.AccessError, "无效的金额"))
		return
	}

	receiver := &models.Account{}
	if find, err := receiver.FindByWalletAddr(form.ToAddr); !find {
		e := errors.NewError(errors.NotFoundError, "无效的收款地址")
		if err != nil {
			e = errors.NewError(errors.DbError, "无效的钱包地址")
		}
		writeResponse(r.RequestURI, w, nil, e)
		return
	}

	if redis.Relationship(receiver.Id, user.Id) == models.RelBlacklist {
		writeResponse(r.RequestURI, w, nil,
			errors.NewError(errors.AccessError, "对方屏蔽了你!"))
		return
	}

	wal, err := getWallet(user.Wallet.Id, user.Wallet.Key)
	if err != nil {
		writeResponse(r.RequestURI, w, nil, errors.NewError(errors.DbError, "获取钱包失败"))
		return
	}

	outputs, amount, err := getUnspent(form.FromAddr, wal.Keys, form.Value)
	if err != nil {
		writeResponse(r.RequestURI, w, nil, errors.NewError(errors.DbError, "获取账户信息失败"))
		return
	}
	//log.Println("amount:", amount, "value:", form.Value)

	if form.Value > amount {
		writeResponse(r.RequestURI, w, nil, errors.NewError(errors.AccessError, "余额不足"))
		return
	}

	article := &models.Article{}
	if strings.ToLower(form.Type) == "reward" && len(form.Id) > 0 {
		if b, e := article.FindById(form.Id); !b {
			err := errors.NewError(errors.NotExistsError, "文章不存在!")
			if e != nil {
				err = errors.NewError(errors.DbError)
			}
			writeResponse(r.RequestURI, w, nil, err)
			return
		}
	}

	changeAddr := form.FromAddr
	if len(changeAddr) == 0 {
		changeAddr = wal.Keys[0].PubKey
	}
	rawtx, err := CreateRawTx2(outputs, amount, form.Value, form.ToAddr, changeAddr)
	if err != nil {
		writeResponse(r.RequestURI, w, nil, errors.NewError(errors.DbError))
		return
	}

	txid, err := sendRawTx(rawtx)
	if err != nil {
		writeResponse(r.RequestURI, w, nil, errors.NewError(errors.DbError))
		return
	}

	redis.Transaction(user.Id, receiver.Id, form.Value)
	// ws push
	event := &models.Event{
		Type: models.EventWallet,
		Time: time.Now().Unix(),
	}

	//log.Println("tx type:", strings.ToLower(form.Type))

	switch strings.ToLower(form.Type) {
	case "reward":

		article.Reward(user.Id, form.Value)

		event.Data = models.EventData{
			Type: models.EventReward,
			Id:   article.Id.Hex(),
			From: user.Id,
			To:   receiver.Id,
			Body: []models.MsgBody{
				{Type: "nikename", Content: user.Nickname},
				{Type: "image", Content: article.Image},
				{Type: "total_count", Content: strconv.FormatInt(article.TotalReward, 10)},
				//{Type: "new_count", Content: strconv.Itoa(models.EventCount(models.EventReward, article.Id.Hex()) + 1)},
			},
		}

	default:
		event.Data = models.EventData{
			Type: models.EventTx,
			Id:   user.Id + "-" + receiver.Id,
			From: user.Id,
			To:   receiver.Id,
			Body: []models.MsgBody{
				{Type: "nikename", Content: user.Nickname},
				{Type: "image", Content: user.Profile},
				{Type: "total_count", Content: strconv.FormatInt(form.Value, 10)},
			},
		}
	}

	if user.Id != receiver.Id {
		/*
			if err := event.Save(); err == nil {
				redis.IncrEventCount(receiver.Id, event.Data.Type, 1)
			}
		*/
		event.Save()
		event.Data.Body = append(event.Data.Body,
			models.MsgBody{Type: "new_count",
				Content: strconv.Itoa(models.EventCount(event.Data.Type, event.Data.Id, event.Data.To))})
		redis.PubMsg("wallet", receiver.Id, event.Bytes())
	}

	if receiver.Push {
		msg := user.Nickname + "通过转帐发送给你" +
			strconv.FormatFloat(float64(form.Value)/float64(models.Satoshi), 'f', 8, 64) + "个贝币"
		if event.Data.Type == models.EventReward {
			msg = user.Nickname + "给你的文章打赏了" +
				strconv.FormatFloat(float64(form.Value)/float64(models.Satoshi), 'f', 8, 64) + "个贝币"
		}
		go sendApn(client, msg, receiver.EventCount("", ""), receiver.Devs...)
	}

	writeResponse(r.RequestURI, w, map[string]string{"txid": txid}, nil)
}