Files
jyshd_mWtK2/application/api/controller/Wallet.php
T
li 7d7f53b356 feat: contract uses num_lh, male avatar, captcha upgrade, full i18n
- Contract buy/close/cancel all use num_lh (contract balance) not num (spot)
- Wallet API returns contract (lh) account data block
- My.php returns user id field for frontend display
- Avatar: DiceBear avataaars male business style
- Captcha: 3x scaled text for readability
- Transfer log descriptions in English
- All billing descriptions in English
2026-04-05 13:05:37 +08:00

1176 lines
46 KiB
PHP
Executable File

<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Config;
use app\common\library\Sms;
use app\common\library\Ems;
/**
* 公共接口
*/
class Wallet extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
/**
* 我的资产
*/
public function wallet_index()
{
$user_id = $this->auth->id;
$curr = Db::name("app_currency_user a")->field("a.id,a.curr_id,a.num,a.num_fb,a.num_lh,a.num_dj,a.num_fb_dj,a.num_lh_dj,b.name,b.full_name,b.exchange,b.thumb_image,b.is_reg,b.is_wid,b.is_tran,b.is_otc,a.tron_address,b.status")
->join("app_currency b","a.curr_id=b.id","left")
->where("a.user_id",$user_id)
->order("a.curr_id asc")
->select();
$count_xh_usdt = 0;//现货总U
$count_lh_num = 0;//量化总U
$count_fb_num = 0;//法币总U
$curr_arr = [];$lh_arr = [];$fb_arr = [];
$evt = 0;
foreach ($curr as $key => $value) {
if($value['curr_id'] == 20){
$evt = 1;
}
if($value['curr_id'] == 1){
$address = $value['tron_address'];
}
if($value['curr_id'] == 3){
$btc_price = $value['exchange'];
}
if(!$value['name']){
unset($curr[$key]);
continue;
}
//ICO排除
$usdt_num = sprintf("%.6f",($value['num']+$value['num_dj'])*$value['exchange']);
$usdt_lh_num = sprintf("%.6f",($value['num_lh']+$value['num_lh_dj'])*$value['exchange']);
$usdt_fb_num = sprintf("%.6f",($value['num_fb']+$value['num_fb_dj'])*$value['exchange']);
$count_xh_usdt = $count_xh_usdt+$usdt_num;
$count_lh_num = $count_lh_num+$usdt_lh_num;
$count_fb_num = $count_fb_num+$usdt_fb_num;
$curr_arr[] = array(
"name" => $value['name'],
"full_name" => $value['full_name'],
"thumb_image" => Config::get("site.image_url").$value['thumb_image'],
"curr_id" => $value['curr_id'],
"num" => $value['num'],
"num_dj" => $value['num_dj'],
"usdt_price" => sprintf("%.2f",$usdt_num),
"exchange" => $value['exchange'],
"is_reg" => $value['is_reg'],
"is_wid" => $value['is_wid'],
"is_tran" => $value['is_tran'],
"type" => 1,
);
if($value['curr_id'] == 1 || $value['curr_id'] == 2){
$lh_arr[] = array(
"name" => $value['name'],
"full_name" => $value['full_name'],
"thumb_image" => Config::get("site.image_url").$value['thumb_image'],
"curr_id" => $value['curr_id'],
"num" => $value['num_lh'],
"num_dj" => $value['num_lh_dj'],
"usdt_price" => sprintf("%.2f",$usdt_lh_num),
"exchange" => $value['exchange'],
"type" => 3,
);
$fb_arr[] = array(
"name" => $value['name'],
"full_name" => $value['full_name'],
"thumb_image" => Config::get("site.image_url").$value['thumb_image'],
"curr_id" => $value['curr_id'],
"num" => $value['num_fb'],
"num_dj" => $value['num_fb_dj'],
"usdt_price" => sprintf("%.2f",$usdt_fb_num),
"exchange" => $value['exchange'],
"is_otc" => $value['is_otc'],
"type" => 2,
);
}
}
// 前三位固定:USDT > BTC > ETH,其余按持仓价值降序
$pin_order = ['USDT' => 0, 'BTC' => 1, 'ETH' => 2];
$pinned = [null, null, null];
$rest = [];
foreach ($curr_arr as $item) {
$uname = strtoupper($item['name']);
if (isset($pin_order[$uname])) {
$pinned[$pin_order[$uname]] = $item;
} else {
$rest[] = $item;
}
}
$paixu = array_column($rest, "usdt_price");
array_multisort($paixu, SORT_DESC, $rest);
$curr_arr = array_merge(array_filter($pinned, function($v){ return $v !== null; }), $rest);
//添加EVT数据
if($evt == 0){
$inser = array(
"user_id" => $user_id,
"curr_id" => 20,
"tron_address" => $address,
);
Db::name("app_currency_user")->insert($inser);
}
$all_usdt = $count_xh_usdt+$count_lh_num+$count_fb_num;
// 合约交易总利润(盈 - 亏)
$profit_in = Db::name('app_detailed')
->where('user_id', $user_id)
->where('description', 'Contract Close Profit')
->sum('price');
$profit_out = Db::name('app_detailed')
->where('user_id', $user_id)
->where('description', 'Contract Close Loss')
->sum('price');
$total_profit = sprintf("%.2f", ($profit_in ?: 0) - ($profit_out ?: 0));
$data = array(
"total_profit" => $total_profit,
"all" => array(
"count_btc" => sprintf("%.8f",$all_usdt/$btc_price),
"count_usdt" => sprintf("%.2f",$all_usdt),
"count_xh_usdt" => sprintf("%.2f",$count_xh_usdt),
"count_lh_usdt" => sprintf("%.2f",$count_lh_num),
"count_fb_usdt" => sprintf("%.2f",$count_fb_num),
'usdt_num' => Db::name("app_currency_user")->where("user_id",$this->auth->id)->where("curr_id",1)->value("num"),
),
"xh" => array(
"count_btc" => sprintf("%.8f",$count_xh_usdt/$btc_price),
"count_usdt" => sprintf("%.2f",$count_xh_usdt),
"xh_arr" => $curr_arr,
),
"fb" => array(
"count_btc" => sprintf("%.8f",$count_fb_num/$btc_price),
"count_usdt" => sprintf("%.2f",$count_fb_num),
"fb_arr" => $fb_arr,
),
"lh" => array(
"count_btc" => sprintf("%.8f",$count_lh_num/$btc_price),
"count_usdt" => sprintf("%.2f",$count_lh_num),
"lh_arr" => $lh_arr,
),
);
$this->success("ok",$data);
}
/**
* 最近明细
*/
public function index_detail()
{
$data = Db::name("app_detailed a")->field('a.price,a.cart,a.description,a.createtime,b.name')
->join("app_currency b","a.curr_id=b.id","left")
->where('a.user_id',$this->auth->id)
->order('a.id','desc')
->paginate(10,false,['query' => request()->param()]);
foreach ($data as $key=>$value)
{
$value['description'] = __($value['description']);
$value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
$data[$key] = $value;
}
$this->success('success',$data);
}
//充值内容
// public function get_wallet_address()
// {
// $user_id = $this->auth->id;
// $curr_id = $this->request->post("id",1);
// $user_curr = Db::name("app_currency_user")->where("user_id",$user_id)->where("curr_id",$curr_id)->find();
//
// $data = [];
// if($curr_id == 1){
// //生成二维码
// $file_name = 'uploads/recharge/usdt_erc_'.$curr_id."_".$this->auth->id;
// $ercqr = controller("common")->qrcode_s( $user_curr['address'] , $file_name );
// $data[] = array(
// "lian" => "ERC-20",
// "address" => $user_curr['address'],
// "qrcode" => Config::get('site.image_url').$ercqr,
// );
// }
// if($curr_id == 20){
// if(!$user_curr['tron_address']){
// $user_usdt = Db::name("app_currency_user")->where("user_id",$user_id)->where("curr_id",1)->find();
// $user_curr['tron_address'] = $user_usdt['tron_address'];
// Db::name("app_currency_user")->where("id",$user_curr['id'])->update(['tron_address'=> $user_usdt['tron_address']]);
// }
// }
// if($curr_id == 55){
// if(!$user_curr['tron_address']){
// $user_usdt = Db::name("app_currency_user")->where("user_id",$user_id)->where("curr_id",1)->find();
// $user_curr['tron_address'] = $user_usdt['tron_address'];
// Db::name("app_currency_user")->where("id",$user_curr['id'])->update(['tron_address'=> $user_usdt['tron_address']]);
// }
// }
//
// $file_name = 'uploads/recharge/usdt_trc_'.$curr_id."_".$this->auth->id;
// $trcqr = controller("common")->qrcode_s( $user_curr['tron_address'] , $file_name );
// $data[] = array(
// "lian" => "TRC-20",
// "address" => $user_curr['tron_address'],
// "qrcode" => Config::get('site.image_url').$trcqr,
// );
// Db::name("app_currency_user")->where("id",$user_curr['id'])->update(['regtime'=> time()]);
// $this->success('success',$data);
// }
/**
* 充值内容
*/
public function get_wallet_address()
{
$data = [];
$curr_id = $this->request->post("curr_id",1);
$file_name = 'uploads/recharge/usdt_erc_'.$curr_id;
$erc_address = Config::get("site.erc_reg_address");
$ercqr = controller("common")->qrcode_s( $erc_address , $file_name );
$data[] = array(
"lian" => "ERC-20",
"address" => $erc_address,
"qrcode" => Config::get('site.image_url').$ercqr,
);
$file_name = 'uploads/recharge/usdt_trc_'.$curr_id;
$trc_address = Config::get("site.trc_reg_address");
$trcqr = controller("common")->qrcode_s( $trc_address , $file_name );
$data[] = array(
"lian" => "TRC-20",
"address" => $trc_address,
"qrcode" => Config::get('site.image_url').$trcqr,
);
//银行卡类型
// $bank_user = Config::get("site.bank_user");
// $bank_name = Config::get("site.bank_name");
// $card_num = Config::get("site.card_num");
// $bank = array(
// "bank_user" => $bank_user,
// "bank_name" => $bank_name,
// "card_num" => $card_num,
// );
$bank = Db::name("app_recharge_bank")->where("status",1)->select();
foreach ($bank as $key => $value) {
$value['logo_image'] = Config::get("site.image_url").$value['logo_image'];
$bank[$key] = $value;
}
$this->success('success',['curr'=>$data,'bank'=>$bank]);
}
/**
* 可充值币种返回
*/
public function reg_curr()
{
$curr = Db::name("app_currency")->field("id,name")->where("is_reg",1)->select();
$this->success('success',$curr);
}
// //充值记录
// public function get_recharge_log()
// {
// $user_id = $this->auth->id;
// //监听
// $user = Db::name('app_currency_user')
// ->where('address', '<>', '')
// ->where('curr_id', 1)
// ->where('user_id',$user_id)
// ->where("updatetime","<", time()-60)
// ->select();
// foreach ($user as $key => $value) {
// $txid = Db::name('app_detailed')
// ->where('cart', '1')
// ->where('type', '1')
// ->where('user_id', $user_id)
// ->select();
// $txidarr = array_column($txid, 'id', 'hash');
// $lowrecharge = 0.01;
// $transferdata = [
// 'command' => "getTransferRecord",
// 'code' => "tronapi2021.1",
// 'address' => $value['address'],
// 'contract' => 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t' //TK
// ];
// $result = json_decode(http_curl(Config::get('site.rpc_ip'), 'post', $transferdata), true);
//
// // var_dump($result);exit;
// if ($result['code'] == 1 && !empty($result['data'])) {
// $recharge = [];
// $amount = 0;
// foreach ($result['data'] as $kk => $val) {
// if (!isset($txidarr[$val['transaction_id']]) && $val['value'] > 100) {
// $nums = sprintf("%.8f", $val['value'] / 1000000);
// //存入充值表记录
// $recharge[] = [
// 'user_id' => $value['user_id'],
// 'curr_id' => $value['curr_id'],
// 'price' => $nums,
// 'cart' => '1',
// 'type' => '1',
// 'description' => 'trc20-USDT充值',
// 'hash' => $val['transaction_id'],
// 'before' => $value['num'],
// 'after' => $value['num'] + $nums,
// 'is_outer' => '0',
// 'createtime' => time(),
// ];
// $amount += $nums;
// }
// }
// if ($amount > $lowrecharge) {
// Db::name('app_detailed')->insertAll($recharge);
// $changeamount = $value['num'] + $amount;
// Db::name('app_currency_user')->where('id', $value['id'])
// ->update(['num' => $changeamount, 'updatetime' => time()]);
// }
// }else{
// Db::name('app_currency_user')->where('id', $value['id'])
// ->update([ 'updatetime' => time()]);
// }
// }
//
// //监听
// $user = Db::name('app_currency_user')
// ->where('address', '<>', '')
// ->where('curr_id', 2)
// ->where('user_id',$user_id)
// ->where("updatetime","<", time()-60)
// ->select();
// foreach ($user as $key => $value) {
// $txid = Db::name('app_detailed')
// ->where('cart', '1')
// ->where('type', '1')
// ->where('user_id', $user_id)
// ->select();
// $txidarr = array_column($txid, 'id', 'hash');
// $lowrecharge = 0.01;
// $transferdata = [
// 'command' => "getTransferRecord",
// 'code' => "tronapi2021.1",
// 'address' => $value['address'],
// 'contract' => 'TCsUiQVkHv5HjRug7R9UEausTg1kitMGBD' //TK
// ];
// $result = json_decode(http_curl(Config::get('site.rpc_ip'), 'post', $transferdata), true);
//
// // var_dump($result);exit;
// if ($result['code'] == 1 && !empty($result['data'])) {
// $recharge = [];
// $amount = 0;
// foreach ($result['data'] as $kk => $val) {
// if (!isset($txidarr[$val['transaction_id']]) && $val['value'] > 100) {
// $nums = sprintf("%.8f", $val['value'] / 1000000);
// //存入充值表记录
// $recharge[] = [
// 'user_id' => $value['user_id'],
// 'curr_id' => $value['curr_id'],
// 'price' => $nums,
// 'cart' => '1',
// 'type' => '1',
// 'description' => 'trc20-APT',
// 'hash' => $val['transaction_id'],
// 'before' => $value['num'],
// 'after' => $value['num'] + $nums,
// 'is_outer' => '0',
// 'createtime' => time(),
// ];
// $amount += $nums;
// }
// }
// if ($amount > $lowrecharge) {
// Db::name('app_detailed')->insertAll($recharge);
// $changeamount = $value['num'] + $amount;
// Db::name('app_currency_user')->where('id', $value['id'])
// ->update(['num' => $changeamount, 'updatetime' => time()]);
// }
// }else{
// Db::name('app_currency_user')->where('id', $value['id'])
// ->update([ 'updatetime' => time()]);
// }
// }
//
// $size = 20;
// $data = Db::name('app_detailed a')->field('a.id,a.price,a.description,a.createtime,b.name')
// ->join("app_currency b","a.curr_id=b.id","left")
// ->where('a.user_id',$this->auth->id)
// ->where('a.cart','1')
// ->where('a.type','1')
// ->order('a.id','desc')
// ->paginate($size,false,['query' => request()->param()]);
// foreach ($data as $key=>$value)
// {
// $value['description'] = __($value['description']);
// $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
// $data[$key] = $value;
// }
// $this->success('success',$data);
// }
//充值记录
public function get_recharge_log()
{
$user_id = $this->auth->id;
$size = 20;
$data = Db::name('app_recharge a')->field('a.id,a.num as price,a.addtime as createtime,a.status,b.name')
->join("app_currency b","a.curr_id=b.id","left")
->where('a.user_id',$user_id)
->order('a.id','desc')
->paginate($size,false,['query' => request()->param()]);
foreach ($data as $key=>$value)
{
// $value['description'] = __($value['description']);
$value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
$data[$key] = $value;
}
$this->success('success',$data);
}
/**
* 充提币详情
*/
public function recharge_con()
{
$user_id = $this->auth->id;
$cart = $this->request->post("cart",1);
$id = $this->request->post("id");
if($cart == 1){
$res = Db::name("app_recharge a")->field('a.id,a.num as price,a.addtime as createtime,a.hash,a.address,a.status,a.type,a.bank_name,a.card_num,a.voucher_image')
->where("a.id",$id)
->where("a.user_id",$user_id)
->find();
$res['name'] = "USDT";
$res['createtime'] = date('Y-m-d H:i:s',$res['createtime']);
$res['cart'] = 1;
if($res['voucher_image']){
$res['voucher_image'] = Config::get("site.image_url").$res['voucher_image'];
}
}else{
$res = Db::name("app_widthdraw a")->field('a.id,a.num as price,a.add_time as createtime,b.name,a.hash,a.status,a.bank_name,a.bank_user,a.card_num')
->join("app_currency b","a.curr_id=b.id","left")
->where("a.id",$id)
->where("a.user_id",$user_id)
->find();
$res['createtime'] = date('Y-m-d H:i:s',$res['createtime']);
$res['cart'] = 2;
}
$this->success('success',$res);
}
/**
* 充币提交
*/
public function recharge_sub()
{
$user_id = $this->auth->id;
$curr_id = $this->request->post("curr_id",1);
$type = $this->request->post("type",1);
$address = $this->request->post("address","");
$hash = $this->request->post("hash","");
$num = $this->request->post("num","");
$pay_pwd = $this->request->post("pay_pwd","");
$bank_name = $this->request->post("bank_name","");
$card_num = $this->request->post("card_num","");
$voucher_image = $this->request->post("voucher_image","");
$bank_id = $this->request->post("bank_id","");
if($num<=0){
$this->error(__("请填写充值数量"));
}
if($type == 1){
$price = 0;
if(!$address){
$this->error(__("请填写转账地址"));
}
if(!$hash){
$this->error(__("请填写交易ID(hash)"));
}
}else{
// if(!$bank_name){
// $this->error(__("请填写开户行"));
// }
// if(!$card_num){
// $this->error(__("请填写银行卡号"));
// }
$bank = Db::name("app_recharge_bank")->where("id",$bank_id)->where("status",1)->find();
if(empty($bank)){
$this->error(__("请选择银行"));
}
$price = $num;
$num = sprintf("%.4f",$num*$bank['exchange']);
}
//判断交易密码
if(strtoupper(md5(strtoupper(md5($pay_pwd.'skund')))) != $this->auth->pay_pwd)
{
$this->error(__('交易密码错误'));
}
$curr = Db::name("app_currency")->where("id",$curr_id)->find();
if(empty($curr) || $curr['is_reg'] != 1){
$this->error(__("币种不存在"));
}
$rech = Db::name("app_recharge")->where("user_id",$user_id)->where("status",2)->find();
if($rech){
$this->error(__("您有一笔充值正在审核中"));
}
//redis防重复点击
$symbol = "recharge_sub" . $this->auth->id;
$submited = pushRedis($symbol);
if (!$submited) {
$this->error( __( "操作频繁" ) );
}
$data = array(
"user_id" => $user_id,
"curr_id" => $curr_id,
"type" => $type,
"num" => $num,
"address" => $address,
"hash" => $hash,
"bank_name" => $bank_name,
"card_num" => $card_num,
"price" => $price,
"voucher_image" => $voucher_image,
"status" => 2,
"addtime" => time(),
);
Db::startTrans();
try {
Db::name("app_recharge")->insert($data);
Db::commit();
lopRedis($symbol);
$this->success( __("提交成功") );
} catch (Exception $e) {
Db::rollback();
lopRedis($symbol);
$this->error( __( "提交失败" ) );
}
}
//获取提币数据
public function get_withdraw()
{
$user_id = $this->auth->id;
$curr_id = $this->request->post("id",1);
// 查询指定币种的现货余额(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();
// 若用户尚无该币种记录,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{
$is_google = false;
}
$lang = $this->request->request("lang");
switch ($lang) {
case "zh-cn":
$withdraw_text = Config::get("site.withdraw_text");
break;
default:
$withdraw_text = Config::get("site.withdraw_text_en");
break;
}
$data = array(
"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,
);
$this->success('success',$data);
}
/**
* 可提现币种返回
*/
public function wid_curr()
{
$curr = Db::name("app_currency")->field("id,name")->where("is_wid",1)->select();
$this->success('success',$curr);
}
/**
* 提现记录
*/
public function get_width_log()
{
$size = 20;
$data = Db::name('app_widthdraw a')->field('a.id,a.num as price,a.add_time as createtime,b.name,a.status,a.description')
->join("app_currency b","a.curr_id=b.id","left")
->where('a.user_id',$this->auth->id)
->order('a.id','desc')
->paginate($size,false,['query' => request()->param()]);
foreach ($data as $key=>$value)
{
$value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
$data[$key] = $value;
}
$this->success('success',$data);
}
//提交提币
public function withdraw()
{
$params = $this->request->post();
$curr_id = $this->request->post("id",1);
$type = $this->request->post("type",1);
$coll_id = $this->request->post("coll_id",1);
$address = $this->request->post("address","");
$lian = $this->request->post("lian","TRC-20");
if($type == 1){
if(!isset($address) || empty($address))
{
$this->error(__('请输入正确的地址'));
}
$bank_name = "";
$bank_user = "";
$card_num = "";
}else{
$collection = Db::name("app_collection")->where("id",$coll_id)->where("user_id", $this->auth->id)->find();
if(empty($collection)){
$this->error("请选择提现的银行卡");
}
$bank_name = $collection['bank_name'];
$bank_user = $collection['username'];
$card_num = $collection['card_num'];
}
if(!isset($params['num']) || empty($params['num']) || $params['num'] <= 0 || !is_numeric($params['num']))
{
$this->error(__('请输入正确的数量'));
}
if(!isset($params['pay_pwd']) || empty($params['pay_pwd']))
{
$this->error(__('请输入交易密码'));
}
$curr = Db::name('app_currency')->where('id',$curr_id)->find();
if(empty($curr)){
$this->error(__("请选择有效币种"));
}
if($curr['is_wid'] != 1){
$this->error(__("该币种暂未开启提币"));
}
$authre = Db::name('app_auth')->where('user_id',$this->auth->id)->where('status','1')->find();
if(!$authre)
{
$this->error(__('请完成实名'));
}
//判断交易密码
if(strtoupper(md5(strtoupper(md5($params['pay_pwd'].'skund')))) != $this->auth->pay_pwd)
{
$this->error(__('交易密码错误'));
}
//判断验证码
// if ($this->auth->email && !Ems::check($this->auth->email, $params['code'], 'withdraw')) {
// $this->error(__("邮箱验证失败"));
// }
//手机验证
if ($this->auth->mobile && !Sms::check($this->auth->mobile, $params['code_m'], 'withdraw')) {
// $this->error(__("手机号验证失败"));
}
//谷歌验证
if($this->auth->secret && !controller("Google")->check_code($params['code_g'])){
// $this->error(__("谷歌验证失败"));
}
//redis防重复点击
$symbol = "withdraw" . $this->auth->id;
$submited = pushRedis($symbol);
if (!$submited) {
$this->error(__("操作频繁"));
}
if($params['num'] > $curr['max_widthdraw'] || $params['num'] < $curr['min_widthdraw'])
{
lopRedis($symbol);
$this->error(__('提币限额为:').$curr['min_widthdraw'].'-'.$curr['max_widthdraw']);
}
$user_curr = Db::name('app_currency_user')->where('curr_id',$curr_id)->where('user_id',$this->auth->id)->find();
if($user_curr['num'] < $params['num'])
{
lopRedis($symbol);
$this->error(__('余额不足'));
}
if($this->auth->apy_pay == 1){
//开启apt手续费支付
$apt_free = Config::get("site.apt_free");
$apy_curr = Db::name("app_currency")->where("id",2)->find();
$apy_num = sprintf("%.6f",$curr['process']*$curr['exchange']/$apy_curr['exchange']*$apt_free);
$fee = $apy_num;
$realnum = $params['num'];
$user_apy = Db::name('app_currency_user')->where('curr_id',2)->where('user_id',$this->auth->id)->find();
if($user_apy['num'] < $fee)
{
lopRedis($symbol);
$this->error('手续费不足');
}
$detailed[] = [
'user_id' => $this->auth->id,
'curr_id' => 2,
'price' => $apy_num,
'cart' => '2',
'type' => '2',
'description' => 'Withdrawal APY Fee',
'createtime' => time(),
'before_num' => $user_apy['num'],
'after_num' => $user_apy['num'] - $apy_num,
];
}else{
if($curr['process'] < 1 && $curr['process'] > 0){
$fee = sprintf("%.4f",$params['num']*$curr['process']);
}else{
$fee = $curr['process'];
}
// if($lian == "ERC-20"){
// $fee = 15;
// }
if($type != 1){
$fee = 0;
}
$realnum = sprintf('%.6f',$params['num'] - $fee);
}
$widthdata = [
'user_id' => $this->auth->id,
'address' => $address,
'bank_name' => $bank_name,
'bank_user' => $bank_user,
'card_num' => $card_num,
'curr_id' => $curr_id,
'num' => $params['num'],
'fee' => $fee,
'real_num' => $realnum,
'status' => '2',
'add_time' => time()
];
$detailed[] = [
'user_id' => $this->auth->id,
'curr_id' => $curr_id,
'price' => $params['num'],
'cart' => '2',
'type' => '2',
'description' => 'Withdrawal',
'createtime' => time(),
'before_num' => $user_curr['num'],
'after_num' => $user_curr['num'] - $params['num'],
];
Db::startTrans();
try {
Db::name("app_widthdraw")->insert($widthdata);
Db::name("app_detailed")->insertAll($detailed);
Db::name("app_currency_user")->where("id",$user_curr['id'])->update(['num'=>$user_curr['num'] - $params['num']]);
if($this->auth->apy_pay == 1){
Db::name("app_currency_user")->where("id",$user_apy['id'])->update(['num'=>$user_apy['num'] - $apy_num]);
}
Db::commit();
if($this->auth->email){
$send_data = array("num"=>$params['num'],"curr_name"=>$curr['name'],"address"=>$address);
controller("Ems")->api_send($this->auth->email, "width_succ",$send_data);
}
lopRedis($symbol);
$this->success(__('提交成功'));
} catch (Exception $e) {
Db::rollback();
lopRedis($symbol);
$this->error(__('系统繁忙'));
}
}
/**
* 划转内容
*/
public function get_transfer()
{
$user_id = $this->auth->id;
$zhuanghu = array(
['id'=>1,"name"=>"Spot"],
['id'=>2,"name"=>"Fiat"],
['id'=>3,"name"=>"Contract"],
);
//获取可划转币种 + 汇率用于实时价格转换
$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)
->select();
$lang = $this->request->request("lang");
switch ($lang) {
case "zh-cn":
$transfer_text = Config::get("site.transfer_text");
break;
default:
$transfer_text = Config::get("site.transfer_text_en");
break;
}
$data = array(
"zhuanghu" => $zhuanghu,
"curr" => $curr,
"transfer_text" => $transfer_text,
);
$this->success("ok",$data);
}
/**
* 划转提交
*/
public function transfer_sub()
{
$user_id = $this->auth->id;
$left_id = $this->request->post("left_id",1);
$right_id = $this->request->post("right_id",2);
$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(__("请输入有效数量"));
}
if($left_id == $right_id){
$this->error(__("不可同类型划转"));
}
//判断交易密码
if(strtoupper(md5(strtoupper(md5($pay_pwd.'skund')))) != $this->auth->pay_pwd)
{
$this->error(__('交易密码错误'));
}
$curr = Db::name("app_currency")->where("id",$curr_id)->where("is_tran",1)->find();
if(empty($curr)){
$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 = "Spot";
$right_msg = "Contract";
//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." to ".$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." to ".$right_msg." (rate:".$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 = "Contract";
$right_msg = "Spot";
$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." to ".$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']." (rate:".$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:
$left_msg = "Spot";
$left_field = "num";
$left_table = "app_detailed";
$left_type = 8;
break;
case 2:
$left_msg = "Fiat";
$left_field = "num_fb";
$left_table = "app_detailed_fb";
$left_type = 1;
break;
case 3:
$left_msg = "Contract";
$left_field = "num_lh";
$left_table = "app_detailed_lh";
$left_type = 1;
break;
default:
$this->error(__("请选择正确划转类型"));
break;
}
switch ($right_id) {
case 1:
$right_msg = "Spot";
$right_field = "num";
$right_table = "app_detailed";
$right_type = 8;
break;
case 2:
$right_msg = "Fiat";
$right_field = "num_fb";
$right_table = "app_detailed_fb";
$right_type = 1;
break;
case 3:
$right_msg = "Contract";
$right_field = "num_lh";
$right_table = "app_detailed_lh";
$right_type = 1;
break;
default:
$this->error(__("请选择正确划转类型"));
break;
}
if($user_curr[$left_field] < $num){
$this->error(__("余额不足"));
}
//redis防重复点击
$symbol = "transfer_sub" . $this->auth->id;
$submited = pushRedis($symbol);
if (!$submited) {
$this->error(__("操作频繁"));
}
$left_detail = array(
"user_id" => $user_id,
"curr_id" => $curr_id,
"price" => $num,
"cart" => 2,
"type" => $left_type,
"description" => $left_msg." to ".$right_msg,
"createtime" => time(),
"before_num" => $user_curr[$left_field],
"after_num" => $user_curr[$left_field] - $num,
);
$right_detail = array(
"user_id" => $user_id,
"curr_id" => $curr_id,
"price" => $num,
"cart" => 1,
"type" => $right_type,
"description" => $left_msg." to ".$right_msg,
"createtime" => time(),
"before_num" => $user_curr[$right_field],
"after_num" => $user_curr[$right_field] + $num,
);
$transfer_log = array(
"user_id" => $user_id,
"curr_id" => $curr_id,
"form" => $left_id,
"to" => $right_id,
"num" => $num,
"msg" => $left_msg." to ".$right_msg,
"addtime" => time(),
);
$curr_update = array(
$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);
Db::name($left_table)->insert($left_detail);
Db::name($right_table)->insert($right_detail);
Db::name("app_currency_user")->where("id",$user_curr['id'])->update($curr_update);
if($right_id == 3 && $this->auth->lh_act == 0){
Db::name("user")->where("id",$user_id)->update(['lh_act'=>1]);
}
Db::commit();
lopRedis($symbol);
$this->success(__('划转成功'));
} catch (Exception $e) {
Db::rollback();
lopRedis($symbol);
$this->error(__('系统繁忙'));
}
}
/**
* 划转记录
*/
public function transfer_log()
{
$size = 20;
$data = Db::name('app_transfer_log a')->field('a.id,a.num as price,a.addtime as createtime,b.name,a.msg as description')
->join("app_currency b","a.curr_id=b.id","left")
->where('a.user_id',$this->auth->id)
->order('a.id','desc')
->paginate($size,false,['query' => request()->param()]);
foreach ($data as $key=>$value)
{
$value['description'] = __($value['description']);
$value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
$data[$key] = $value;
}
$this->success('success',$data);
}
/**
* 收支明细
*/
public function detail_log()
{
$type = $this->request->post("type",1);
$curr_id = $this->request->post("curr_id",1);
switch ($type) {
case 1:
$table = "app_detailed";
break;
case 2:
$table = "app_detailed_fb";
break;
case 3:
$table = "app_detailed_lh";
break;
default:
$this->error(__("类型错误"));
break;
}
$data = Db::name($table)
->field('price,cart,description,createtime')
->where('user_id',$this->auth->id)
->where('curr_id',$curr_id)
->order('id','desc')
->paginate(20,false,['query' => request()->param()]);
foreach ($data as $key=>$value)
{
$value['description'] = __($value['description']);
$value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
$data[$key] = $value;
}
$this->success('success',$data);
}
}