動態獲取以太坊,以太經典實時價格介面,以及轉換匯率(區塊鏈)
WebRequest myWebRequest = WebRequest.Create("http://api.coindog.com/api/v1/currency/ranks");
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream ReceiveStream = myWebResponse.GetResponseStream();
string responseStr = "";
if (ReceiveStream != null)
{
StreamReader reader = new StreamReader(ReceiveStream, Encoding.UTF8);
responseStr = reader.ReadToEnd();
responseStr = responseStr.Replace("[", "").Replace("]", "").Replace(" ", "").
Replace("\n", "").Replace("\r", "").Replace("{", "").Replace("},", "|");
//JArray jsonArray = JArray.Parse(responseStr.Trim());
//dynamic data = JObject.Parse(jsonArray[0].ToString());
string[] arr = responseStr.Split('|');
foreach (var v in arr)
{
string[] ar = v.Split(',');
string[] n = ar[0].Split(':');
string currye = n[1].Replace("\"", "");
if (currye == "ETC")
p1 = Convert.ToDecimal(ar[4].Split(':')[1]);
else if (currye == "ET````")
p2 = Convert.ToDecimal(ar[4].Split(':')[1]);
usdp1 = Convert.ToDouble(p1) / rate;
usdp2 = Convert.ToDouble(p2) / rate;
usdp3 = p3 / rate;
}
}
<script type="text/javascript">
$(document).ready(function () {
zcount = function () {
var seldz = $('#dzs').val();
var selyz = $('#yzs').val();
var num = $("#dz").val();
var reg = /^\d+$/;
if (num.length <= 0) {
if (!reg.test(num)) {
if (num < 0) num = 1;
}
}
var js = seldz / selyz;
$("#yz").val(js * num);
}
$('#dz').bind('input propertychange', function () {
zcount();
});
$("#dzs").change(function () {
zcount();
});
$('#yz').bind('input propertychange', function () {
zcount();
});
$("#yzs").change(function () {
zcount();
});
});
if (!base.IsPostBack)
{
string val1 = Request["type"];
if (val1 == "1")
{
NewMethod();
}
}
}
private void NewMethod()
{
int id = Convert.ToInt32(Session["User_ID"]);
//獲取三種幣的餘額
DataTable currencyTable = crud.QueryTable(" select top 1 U_Account_ETH,U_Account_ETC,U_Account_ETES from DS_User where ID="+id+"");
double ETHcurrency = Convert.ToDouble(currencyTable.Rows[0]["U_Account_ETH"]);
double ETCcurrency = Convert.ToDouble(currencyTable.Rows[0]["U_Account_ETC"]);
double ETEScurrency = Convert.ToDouble(currencyTable.Rows[0]["U_Account_ETES"]);
double val2 = Convert.ToDouble(Request["value1"]);//輸入要準備兌換的幣
string dzs = Request["dzs"];//選擇準備兌換的幣
double val4 = Convert.ToDouble(Request["value3"]);
string yzs = Request["yzs"];
string sqlName1 = "";
string sqlName2 = "";
if (dzs == "ETES" && val2 < ETEScurrency)
{
sqlName1 = "U_Account_ETES";
}
else if (dzs == "ETH" && val2 < ETHcurrency)
{
sqlName1 = "U_Account_ETH";
}
else if (dzs == "ETC" && val2 < ETCcurrency)
{
sqlName1 = "U_Account_ETC";
}
else
{
Response.Write( "0" );
Response.End();
}
if (yzs == "ETC"&&dzs!="ETC")
{
sqlName2 = "U_Account_ETC";
}
else if (yzs == "ETH"&&dzs!="ETH")
{
sqlName2 = "U_Account_ETH";
}
else if (yzs == "ETES" && dzs != "ETES")
{
sqlName2 = "U_Account_ETES";
}
else
{
Response.Write("2");
Response.End();
}
crud.ExecuteSQL(" update DS_User set " + sqlName1 + "=" + sqlName1 + "- " + val2 + ", " + sqlName2 + "=" + sqlName2 + "+ " + val4 + " where ID='" + id + "'");
Response.Write("1");
Response.End();
}
以下為轉換JS
function sDD() {
var options=$("#dzs option:selected");
var options1 = $("#yzs option:selected");
$.ajax({
url: "Lock_prize.aspx",
type: "post",
data: {
"value1": $("#dz").val(),
"dzs": options.text(),
"value3": $("#yz").val(),
"yzs": options1.text(),
"type": 1,
},
success: function (data) {
if (data == "1")
{
alert("兌換成功");
}
else if (data == "0") {
alert("餘額不足,請充值");
}
else if(data=="2")
{
alert("請選擇要兌換的幣");
}
}
});