feat: 现货/合约转账市场价格转换
1. 添加合约账户为可选转账目标(原量化账户) 2. BTC/ETH 现货→合约:按市场价转为USDT存入合约账户 3. 合约→BTC/ETH 现货:USDT按市场价转为BTC/ETH 4. USDT 现货↔合约:1:1直接转移 5. get_transfer 返回 exchange 汇率字段
This commit is contained in:
@@ -539,17 +539,40 @@ class Wallet extends Api
|
||||
{
|
||||
$user_id = $this->auth->id;
|
||||
$curr_id = $this->request->post("id",1);
|
||||
$user_curr = Db::name("app_currency_user a")->field("a.curr_id,a.num,b.process,b.exchange")
|
||||
|
||||
// 查询指定币种的现货余额(num 字段)
|
||||
$user_curr = Db::name("app_currency_user a")->field("a.curr_id,a.num,b.process,b.exchange,b.name")
|
||||
->join("app_currency b","a.curr_id=b.id","left")
|
||||
->where("a.user_id",$user_id)
|
||||
->where("a.curr_id",$curr_id)
|
||||
->find();
|
||||
|
||||
// $apt_curr = Db::name("app_currency")->where("id",2)->find();
|
||||
// $apt_free = Config::get("site.apt_free");
|
||||
// $apt_process = sprintf("%.6f",$user_curr['process']*$user_curr['exchange']/$apt_curr['exchange']*$apt_free);
|
||||
|
||||
// 若用户尚无该币种记录,num 返回 0
|
||||
$spot_num = $user_curr ? $user_curr['num'] : 0;
|
||||
|
||||
// 按币种确定链选项与默认手续费
|
||||
// curr_id=1: USDT → ERC-20 + TRC-20,手续费 10
|
||||
// curr_id=3: BTC → BTC 链,手续费 0.0001
|
||||
// curr_id=4: ETH → ERC-20,手续费 0.005
|
||||
// 其他:沿用数据库 process 字段,默认链 TRC-20
|
||||
if($curr_id == 1){
|
||||
$lian = ["ERC-20","TRC-20"];
|
||||
$fee = 10;
|
||||
}elseif($curr_id == 3){
|
||||
// BTC:仅原生 BTC 链,无 ERC-20/TRC-20
|
||||
$lian = ["BTC"];
|
||||
$fee = 0.0001;
|
||||
}elseif($curr_id == 4){
|
||||
// ETH:仅 ERC-20,无 TRC-20
|
||||
$lian = ["ERC-20"];
|
||||
$fee = 0.005;
|
||||
}else{
|
||||
$lian = ["TRC-20"];
|
||||
$fee = $user_curr ? $user_curr['process'] : 0;
|
||||
}
|
||||
|
||||
$apt_process = 0;
|
||||
|
||||
|
||||
if($this->auth->secret){
|
||||
$is_google = true;
|
||||
}else{
|
||||
@@ -564,29 +587,22 @@ class Wallet extends Api
|
||||
$withdraw_text = Config::get("site.withdraw_text_en");
|
||||
break;
|
||||
}
|
||||
if($curr_id == 1){
|
||||
$lian = ["ERC-20","TRC-20"];
|
||||
}elseif($curr_id == 4){
|
||||
$lian = ["ERC-20"];
|
||||
}else{
|
||||
$lian = ["TRC-20"];
|
||||
}
|
||||
$data = array(
|
||||
"lian" => $lian,
|
||||
"curr_id" => $curr_id,
|
||||
"num" => $user_curr['num'],
|
||||
"process" => $user_curr['process'],
|
||||
"erc_process" => $user_curr['process'],
|
||||
"yhk_process" => 0,
|
||||
"apt_process" => $apt_process,
|
||||
"lian" => $lian,
|
||||
"curr_id" => $curr_id,
|
||||
"num" => $spot_num,
|
||||
"process" => $fee,
|
||||
"erc_process" => $fee,
|
||||
"yhk_process" => 0,
|
||||
"apt_process" => $apt_process,
|
||||
"withdraw_text" => $withdraw_text,
|
||||
"is_google" => $is_google,
|
||||
"m_prefix" => $this->auth->m_prefix,
|
||||
"mobile" => $this->auth->mobile,
|
||||
"email" => $this->auth->email,
|
||||
"apy_pay" => $this->auth->apy_pay,
|
||||
"is_google" => $is_google,
|
||||
"m_prefix" => $this->auth->m_prefix,
|
||||
"mobile" => $this->auth->mobile,
|
||||
"email" => $this->auth->email,
|
||||
"apy_pay" => $this->auth->apy_pay,
|
||||
);
|
||||
|
||||
|
||||
$this->success('success',$data);
|
||||
}
|
||||
|
||||
@@ -796,10 +812,10 @@ class Wallet extends Api
|
||||
$zhuanghu = array(
|
||||
['id'=>1,"name"=>__("现货账户")],
|
||||
['id'=>2,"name"=>__("法币账户")],
|
||||
// ['id'=>3,"name"=>__("量化账户")],
|
||||
['id'=>3,"name"=>__("合约账户")],
|
||||
);
|
||||
//获取可划转币种
|
||||
$curr = Db::name("app_currency a")->field("a.id as curr_id,a.name,b.num,b.num_fb,b.num_lh")
|
||||
//获取可划转币种 + 汇率用于实时价格转换
|
||||
$curr = Db::name("app_currency a")->field("a.id as curr_id,a.name,a.exchange,b.num,b.num_fb,b.num_lh")
|
||||
->join("app_currency_user b","a.id=b.curr_id","left")
|
||||
->where("a.is_tran",1)
|
||||
->where("b.user_id",$user_id)
|
||||
@@ -832,7 +848,7 @@ class Wallet extends Api
|
||||
$curr_id = $this->request->post("curr_id",1);
|
||||
$num = $this->request->post("num");
|
||||
$pay_pwd = $this->request->post("pay_pwd");
|
||||
|
||||
|
||||
if(!is_numeric($num) || $num <= 0){
|
||||
$this->error(__("请输入有效数量"));
|
||||
}
|
||||
@@ -844,11 +860,124 @@ class Wallet extends Api
|
||||
{
|
||||
$this->error(__('交易密码错误'));
|
||||
}
|
||||
|
||||
|
||||
$curr = Db::name("app_currency")->where("id",$curr_id)->where("is_tran",1)->find();
|
||||
if(empty($curr)){
|
||||
$this->error(__("改币种不支持划转"));
|
||||
$this->error(__("该币种不支持划转"));
|
||||
}
|
||||
|
||||
// ========== 合约账户特殊处理:BTC/ETH按市场价转USDT ==========
|
||||
// 合约账户只存USDT,非USDT币种需要按市场价转换
|
||||
$is_contract_transfer = ($left_id == 3 || $right_id == 3);
|
||||
$need_conversion = $is_contract_transfer && $curr_id != 1; // 非USDT需转换
|
||||
|
||||
if($need_conversion) {
|
||||
// 获取市场汇率
|
||||
$exchange_rate = floatval($curr['exchange']);
|
||||
if($exchange_rate <= 0) {
|
||||
$this->error(__("汇率异常"));
|
||||
}
|
||||
$usdt_amount = sprintf("%.6f", $num * $exchange_rate);
|
||||
|
||||
// 获取两个币种的用户记录
|
||||
$user_curr_coin = Db::name("app_currency_user")->where("curr_id",$curr_id)->where("user_id",$user_id)->find();
|
||||
$user_curr_usdt = Db::name("app_currency_user")->where("curr_id",1)->where("user_id",$user_id)->find();
|
||||
|
||||
if(empty($user_curr_coin) || empty($user_curr_usdt)){
|
||||
$this->error(__("账户数据异常"));
|
||||
}
|
||||
|
||||
if($right_id == 3) {
|
||||
// 现货 → 合约:扣现货BTC/ETH的num,加USDT的num_lh
|
||||
if($user_curr_coin['num'] < $num){
|
||||
$this->error(__("余额不足"));
|
||||
}
|
||||
$left_msg = "现货账户";
|
||||
$right_msg = "合约账户";
|
||||
|
||||
//redis防重复
|
||||
$symbol = "transfer_sub" . $this->auth->id;
|
||||
$submited = pushRedis($symbol);
|
||||
if (!$submited) { $this->error(__("操作频繁")); }
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 扣币种现货
|
||||
Db::name("app_currency_user")->where("id",$user_curr_coin['id'])
|
||||
->update(['num' => $user_curr_coin['num'] - $num]);
|
||||
// 加USDT合约
|
||||
Db::name("app_currency_user")->where("id",$user_curr_usdt['id'])
|
||||
->update(['num_lh' => $user_curr_usdt['num_lh'] + $usdt_amount]);
|
||||
// 记录
|
||||
Db::name("app_detailed")->insert([
|
||||
"user_id" => $user_id, "curr_id" => $curr_id, "price" => $num,
|
||||
"cart" => 2, "type" => 8, "description" => $curr['name']." ".$left_msg."转到".$right_msg."(≈".$usdt_amount." USDT)",
|
||||
"createtime" => time(), "before_num" => $user_curr_coin['num'], "after_num" => $user_curr_coin['num'] - $num,
|
||||
]);
|
||||
Db::name("app_detailed_lh")->insert([
|
||||
"user_id" => $user_id, "curr_id" => 1, "price" => $usdt_amount,
|
||||
"cart" => 1, "type" => 1, "description" => $curr['name']." ".$left_msg."转到".$right_msg."(按".$exchange_rate."转换)",
|
||||
"createtime" => time(), "before_num" => $user_curr_usdt['num_lh'], "after_num" => $user_curr_usdt['num_lh'] + $usdt_amount,
|
||||
]);
|
||||
Db::name("app_transfer_log")->insert([
|
||||
"user_id" => $user_id, "curr_id" => $curr_id, "form" => $left_id, "to" => $right_id,
|
||||
"num" => $num, "msg" => $num." ".$curr['name']." → ".$usdt_amount." USDT", "addtime" => time(),
|
||||
]);
|
||||
Db::commit();
|
||||
lopRedis($symbol);
|
||||
$this->success(__('划转成功')." (".$num." ".$curr['name']." ≈ ".$usdt_amount." USDT)");
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
lopRedis($symbol);
|
||||
$this->error(__('系统繁忙'));
|
||||
}
|
||||
} else {
|
||||
// 合约 → 现货:扣USDT的num_lh,加BTC/ETH的num
|
||||
// num参数此时是USDT数量
|
||||
$btc_amount = sprintf("%.8f", $num / $exchange_rate);
|
||||
if($user_curr_usdt['num_lh'] < $num){
|
||||
$this->error(__("合约账户余额不足"));
|
||||
}
|
||||
$left_msg = "合约账户";
|
||||
$right_msg = "现货账户";
|
||||
|
||||
$symbol = "transfer_sub" . $this->auth->id;
|
||||
$submited = pushRedis($symbol);
|
||||
if (!$submited) { $this->error(__("操作频繁")); }
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
Db::name("app_currency_user")->where("id",$user_curr_usdt['id'])
|
||||
->update(['num_lh' => $user_curr_usdt['num_lh'] - $num]);
|
||||
Db::name("app_currency_user")->where("id",$user_curr_coin['id'])
|
||||
->update(['num' => $user_curr_coin['num'] + $btc_amount]);
|
||||
Db::name("app_detailed_lh")->insert([
|
||||
"user_id" => $user_id, "curr_id" => 1, "price" => $num,
|
||||
"cart" => 2, "type" => 1, "description" => $left_msg."转到".$curr['name']." ".$right_msg,
|
||||
"createtime" => time(), "before_num" => $user_curr_usdt['num_lh'], "after_num" => $user_curr_usdt['num_lh'] - $num,
|
||||
]);
|
||||
Db::name("app_detailed")->insert([
|
||||
"user_id" => $user_id, "curr_id" => $curr_id, "price" => $btc_amount,
|
||||
"cart" => 1, "type" => 8, "description" => $num." USDT → ".$btc_amount." ".$curr['name']."(按".$exchange_rate."转换)",
|
||||
"createtime" => time(), "before_num" => $user_curr_coin['num'], "after_num" => $user_curr_coin['num'] + $btc_amount,
|
||||
]);
|
||||
Db::name("app_transfer_log")->insert([
|
||||
"user_id" => $user_id, "curr_id" => $curr_id, "form" => $left_id, "to" => $right_id,
|
||||
"num" => $num, "msg" => $num." USDT → ".$btc_amount." ".$curr['name'], "addtime" => time(),
|
||||
]);
|
||||
Db::commit();
|
||||
lopRedis($symbol);
|
||||
$this->success(__('划转成功')." (".$num." USDT ≈ ".$btc_amount." ".$curr['name'].")");
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
lopRedis($symbol);
|
||||
$this->error(__('系统繁忙'));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// ========== 普通划转(含USDT现货↔合约1:1转移)==========
|
||||
$user_curr = Db::name("app_currency_user")->where("curr_id",$curr_id)->where("user_id",$user_id)->find();
|
||||
switch ($left_id) {
|
||||
case 1:
|
||||
@@ -864,7 +993,7 @@ class Wallet extends Api
|
||||
$left_type = 1;
|
||||
break;
|
||||
case 3:
|
||||
$left_msg = "量化账户";
|
||||
$left_msg = "合约账户";
|
||||
$left_field = "num_lh";
|
||||
$left_table = "app_detailed_lh";
|
||||
$left_type = 1;
|
||||
@@ -887,7 +1016,7 @@ class Wallet extends Api
|
||||
$right_type = 1;
|
||||
break;
|
||||
case 3:
|
||||
$right_msg = "量化账户";
|
||||
$right_msg = "合约账户";
|
||||
$right_field = "num_lh";
|
||||
$right_table = "app_detailed_lh";
|
||||
$right_type = 1;
|
||||
@@ -905,7 +1034,7 @@ class Wallet extends Api
|
||||
if (!$submited) {
|
||||
$this->error(__("操作频繁"));
|
||||
}
|
||||
|
||||
|
||||
$left_detail = array(
|
||||
"user_id" => $user_id,
|
||||
"curr_id" => $curr_id,
|
||||
@@ -941,7 +1070,7 @@ class Wallet extends Api
|
||||
$left_field => $user_curr[$left_field] - $num,
|
||||
$right_field => $user_curr[$right_field] + $num,
|
||||
);
|
||||
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
Db::name("app_transfer_log")->insert($transfer_log);
|
||||
|
||||
Reference in New Issue
Block a user