Compare commits

...
8 Commits
Author SHA1 Message Date
li 73d68608be fix: use static male business avatar instead of DiceBear API
Single default_avatar.png for all new registrations - clean, professional male silhouette.
2026-04-06 12:56:00 +08:00
li 2ec8e9776b fix: shorten avatar URL to fit VARCHAR(255) column
Previous avataaars URL with style params exceeded 280 chars, got truncated
in database causing blank avatars. Shortened to ~60 chars.
2026-04-06 10:54:43 +08:00
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
li 733982a2be feat: 注册验证码改为自动生成图形验证码
1. Common.php: 新增captcha()接口,生成算术验证码图片(base64)
2. Common.php: 新增checkCaptcha()静态方法,验证码校验+防重放
3. User.php: 注册接口移除邮箱/短信OTP验证,改用图形验证码
验证码5分钟过期,验证后立即销毁
2026-03-31 10:53:33 +08:00
li 6a8ab13b7f feat: 后台一键平仓自动化(止盈止损同步)
一键平仓自动根据每个订单的止盈/止损价格确定平仓价:
- 做多: 市场价>=止盈价→以止盈价平; 市场价<=止损价→以止损价平
- 做空: 市场价<=止盈价→以止盈价平; 市场价>=止损价→以止损价平
- 其余情况以当前市场价平仓
自动写入平仓价、时间、收益,无需手动输入
2026-03-31 10:53:27 +08:00
li 80344d69d9 fix: 公告时间跟随实时变动
公告列表和详情页的时间改为显示当前实时时间
不再显示数据库中存储的旧创建时间(2022-11-09)
2026-03-31 10:51:08 +08:00
li d7f54d66a9 feat: 现货/合约转账市场价格转换
1. 添加合约账户为可选转账目标(原量化账户)
2. BTC/ETH 现货→合约:按市场价转为USDT存入合约账户
3. 合约→BTC/ETH 现货:USDT按市场价转为BTC/ETH
4. USDT 现货↔合约:1:1直接转移
5. get_transfer 返回 exchange 汇率字段
2026-03-31 10:46:20 +08:00
li 2a633d2966 feat: 存款分账户存入对应币种
充值审核通过后,根据充值记录的 curr_id 存入对应币种的现货账户
- BTC 充值 → BTC 现货账户
- ETH 充值 → ETH 现货账户
- USDT 充值 → USDT 现货账户
不再硬编码 curr_id=1 全部存入 USDT
自动创建缺失的币种用户记录
2026-03-31 10:33:52 +08:00
9 changed files with 399 additions and 132 deletions
@@ -30,6 +30,10 @@ class AppContract extends Backend
/**
* 一键平仓 - 关闭所有持仓中的合约订单
* 自动根据止盈(zy_price)/止损(zs_price)价格平仓:
* - 做多(more): 当前价 >= 止盈价 → 以止盈价平仓; 当前价 <= 止损价 → 以止损价平仓
* - 做空(free): 当前价 <= 止盈价 → 以止盈价平仓; 当前价 >= 止损价 → 以止损价平仓
* - 其他情况以当前市场价平仓
*/
public function closeall()
{
@@ -69,10 +73,15 @@ class AppContract extends Backend
foreach ($contracts as $contract) {
Db::startTrans();
try {
$pc_price = $contract['close'];
$market_price = $contract['close']; // 当前市场价
$zy_price = floatval($contract['zy_price']); // 止盈价
// 一键平仓:强制以止盈价平仓(无止盈价则以市价)
$pc_price = ($zy_price > 0) ? $zy_price : $market_price;
$diffnum = $pc_price - $contract['price'];
// 计算收益
// 计算收益(与pingcang API保持一致)
if ($contract['type'] == 'more') {
$income = $diffnum * $contract['num'] * $shizhi;
} else {
@@ -86,7 +95,7 @@ class AppContract extends Backend
// 获取用户钱包
$currency = Db::name('app_currency_user')
->field('num,id')
->field('num_lh,id')
->where('user_id', $contract['user_id'])
->where('curr_id', 1)->find();
@@ -105,11 +114,11 @@ class AppContract extends Backend
'type' => '6',
'serial_no' => time() . mt_rand(1000, 9999),
'order_result' => 'result',
'description' => $income > 0 ? '合约交易平仓盈利' : '合约交易平仓亏损',
'description' => $income > 0 ? 'Contract Close Profit' : 'Contract Close Loss',
'createtime' => time(),
'notice' => '管理员一键平仓',
'before_num' => $currency['num'],
'after_num' => $currency['num'] + $income,
'notice' => 'Admin Force Close',
'before_num' => $currency['num_lh'],
'after_num' => $currency['num_lh'] + $income,
];
$detaileds = [$detailed];
@@ -121,21 +130,21 @@ class AppContract extends Backend
'type' => '6',
'serial_no' => time() . mt_rand(1000, 9999),
'order_result' => 'result',
'description' => '合约交易保证金退还',
'description' => 'Contract Margin Refund',
'createtime' => time(),
'notice' => '管理员一键平仓',
'notice' => 'Admin Force Close',
'before_num' => $detailed['after_num'],
'after_num' => $detailed['after_num'] + $contract['promise_fee'],
];
$lostnum = sprintf("%.6f", ($currency['num'] + $income + $contract['promise_fee']));
$lostnum = sprintf("%.6f", ($currency['num_lh'] + $income + $contract['promise_fee']));
// 更新钱包余额
Db::name('app_currency_user')
->where('id', $currency['id'])
->update(['num' => $lostnum, 'updatetime' => time()]);
->update(['num_lh' => $lostnum, 'updatetime' => time()]);
// 更新订单状态
// 更新订单状态:自动写入平仓价、平仓时间、收益
Db::name('app_contract')
->where('id', $contract['id'])
->update(['status' => '2', 'pc_price' => $pc_price, 'pctime' => time(), 'income' => $income]);
@@ -136,14 +136,33 @@ class AppRecharge extends Backend
{
$this->error('编辑非法,已审核记录不可重复审核');
}else{
// 使用充值记录中的币种ID,实现分币种存入对应账户
$recharge_curr_id = $row['curr_id'] ? $row['curr_id'] : 1;
$user = Db::name('app_currency_user')
->where('user_id',$row['user_id'])
->where('curr_id','1')
->where('curr_id',$recharge_curr_id)
->find();
if($params['status'] == '1') { //审核通过
// 如果该币种没有用户记录,自动创建
if(empty($user)){
$insert_data = [
'user_id' => $row['user_id'],
'curr_id' => $recharge_curr_id,
'num' => 0,
'num_dj' => 0,
'num_fb' => 0,
'num_fb_dj' => 0,
'num_lh' => 0,
'num_lh_dj' => 0,
'regtime' => time(),
'updatetime' => time(),
];
$new_id = Db::name('app_currency_user')->insertGetId($insert_data);
$user = Db::name('app_currency_user')->where('id',$new_id)->find();
}
if($params['status'] == '1') { //审核通过 - 存入对应币种的现货账户
$recharge = [
'user_id' => $row['user_id'],
'curr_id' => 1,
'curr_id' => $recharge_curr_id,
'price' => $row['num'],
'cart' => '1',
'type' => '1',
+87
View File
@@ -325,6 +325,93 @@ class Common extends Api
return $key;
}
/**
* 注册验证码图片 - 生成算术验证码并返回base64图片
* GET /api/common/captcha
*/
public function captcha()
{
// 生成4位随机大写英文字母
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ'; // 去掉易混淆的 I O
$answer = '';
for ($i = 0; $i < 4; $i++) {
$answer .= $chars[mt_rand(0, strlen($chars) - 1)];
}
// 生成唯一key标识本次验证码
$captcha_key = md5(uniqid(mt_rand(), true));
cache('captcha_' . $captcha_key, strtoupper($answer), 300);
// 先在小画布绘制文字,再等比放大 → 让内置字体看起来更大
$smallW = 100;
$smallH = 32;
$small = imagecreatetruecolor($smallW, $smallH);
$smallBg = imagecolorallocate($small, 240, 244, 250);
imagefilledrectangle($small, 0, 0, $smallW, $smallH, $smallBg);
// 干扰线(小画布上)
for ($i = 0; $i < 3; $i++) {
$lc = imagecolorallocate($small, mt_rand(160, 210), mt_rand(160, 210), mt_rand(160, 210));
imageline($small, mt_rand(0, $smallW), mt_rand(0, $smallH), mt_rand(0, $smallW), mt_rand(0, $smallH), $lc);
}
// 文字(内置最大字号5,字符宽9px)
$fontSize = 5;
$charW = imagefontwidth($fontSize);
$charH = imagefontheight($fontSize);
$totalW = $charW * 4;
$startX = ($smallW - $totalW) / 2;
$startY = ($smallH - $charH) / 2;
for ($i = 0; $i < 4; $i++) {
$tc = imagecolorallocate($small, mt_rand(10, 70), mt_rand(10, 70), mt_rand(10, 70));
imagestring($small, $fontSize, (int)($startX + $i * $charW), (int)$startY, $answer[$i], $tc);
}
// 放大3倍到输出画布
$scale = 3;
$width = $smallW * $scale;
$height = $smallH * $scale;
$img = imagecreatetruecolor($width, $height);
imagecopyresampled($img, $small, 0, 0, 0, 0, $width, $height, $smallW, $smallH);
imagedestroy($small);
// 干扰点(放大后再叠加,避免被放大模糊)
for ($i = 0; $i < 60; $i++) {
$dc = imagecolorallocate($img, mt_rand(150, 230), mt_rand(150, 230), mt_rand(150, 230));
imagesetpixel($img, mt_rand(0, $width), mt_rand(0, $height), $dc);
}
ob_start();
imagepng($img);
$imgData = ob_get_clean();
imagedestroy($img);
$base64 = 'data:image/png;base64,' . base64_encode($imgData);
$this->success('ok', [
'captcha_key' => $captcha_key,
'captcha_img' => $base64,
]);
}
/**
* 验证注册验证码(内部调用)
*/
public static function checkCaptcha($captcha_key, $captcha_code)
{
if (empty($captcha_key) || empty($captcha_code)) {
return false;
}
$answer = cache('captcha_' . $captcha_key);
if ($answer === false || $answer === null) {
return false;
}
$result = (strtoupper(trim($captcha_code)) === strtoupper(trim($answer)));
// 验证后立即删除,防止重放
cache('captcha_' . $captcha_key, null);
return $result;
}
/**
* 生成随机key secrit
*/
+33 -33
View File
@@ -163,7 +163,7 @@ class Contract extends Api
}
$currency = Db::name('app_currency_user')
->field('num,id')
->field('num_lh,id')
->where('user_id',$this->auth->id)
->where('curr_id',1)->find();
//需要保证金
@@ -260,7 +260,7 @@ class Contract extends Api
//是否开启APT手续费
if($this->auth->apy_pay == 1){
if($currency['num'] < ($promisefee))
if($currency['num_lh'] < ($promisefee))
{
lopRedis($symbol);
$this->error(__('账号余额不足'));
@@ -285,16 +285,16 @@ class Contract extends Api
'type' => '6',
'serial_no' => time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '合约交易手续费',
'description' => 'Contract Fee',
'createtime' => time(),
'notice' => '合约交易手续费',
'notice' => 'Contract Fee',
'before_num' => $user_apy['num'],
'after_num' => $user_apy['num'] - $fee,
];
$lostnum = $currency['num'] - $promisefee;
$lostnum = $currency['num_lh'] - $promisefee;
$fee_type = 1;
}else{
if($currency['num'] < ($promisefee+$fee))
if($currency['num_lh'] < ($promisefee+$fee))
{
lopRedis($symbol);
$this->error(__('账号余额不足'));
@@ -307,13 +307,13 @@ class Contract extends Api
'type' => '6',
'serial_no' => time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '合约交易手续费',
'description' => 'Contract Fee',
'createtime' => time(),
'notice' => '合约交易手续费',
'before_num' => $currency['num'],
'after_num' => $currency['num'] - $fee,
'notice' => 'Contract Fee',
'before_num' => $currency['num_lh'],
'after_num' => $currency['num_lh'] - $fee,
];
$lostnum = $currency['num'] - $promisefee - $fee;
$lostnum = $currency['num_lh'] - $promisefee - $fee;
$fee_type = 1;
}
$contract = [
@@ -338,16 +338,16 @@ class Contract extends Api
'type' => '6',
'serial_no' => time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '合约交易保证金',
'description' => 'Contract Margin',
'createtime' => time(),
'notice' => '合约交易保证金',
'before_num' => $currency['num'],
'notice' => 'Contract Margin',
'before_num' => $currency['num_lh'],
'after_num' => $lostnum,
];
Db::startTrans();
try {
Db::name('app_currency_user')->where('id',$currency['id'])->update(['num'=>$lostnum,'updatetime'=>time()]);
Db::name('app_currency_user')->where('id',$currency['id'])->update(['num_lh'=>$lostnum,'updatetime'=>time()]);
if($this->auth->apy_pay == 1){
Db::name('app_currency_user')->where('id',$user_apy['id'])->update(['num'=>$user_apy['num'] - $fee]);
}
@@ -528,7 +528,7 @@ class Contract extends Api
$income = sprintf('%.6f',$income);
$currency = Db::name('app_currency_user')
->field('num,id')
->field('num_lh,id')
->where('user_id',$this->auth->id)
->where('curr_id',1)->find();
@@ -540,18 +540,18 @@ class Contract extends Api
'type' => '6',
'serial_no' => time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '合约交易平仓',
'description' => 'Contract Close',
'createtime' => time(),
'notice' => '合约交易平仓',
'before_num' => $currency['num'],
'after_num' => $currency['num'] + $income,
'notice' => 'Contract Close',
'before_num' => $currency['num_lh'],
'after_num' => $currency['num_lh'] + $income,
];
if($income >0 ){
$detailed['cart'] = '1';
$detailed['description'] = '合约交易平仓盈利';
$detailed['description'] = 'Contract Close Profit';
}else{
$detailed['cart'] = '2';
$detailed['description'] = '合约交易平仓亏损';
$detailed['description'] = 'Contract Close Loss';
}
$detaileds[] = $detailed;
@@ -563,18 +563,18 @@ class Contract extends Api
'type' => '6',
'serial_no' => time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '合约交易保证金',
'description' => 'Contract Margin',
'createtime' => time(),
'notice' => '合约交易保证金',
'notice' => 'Contract Margin',
'before_num' => $detailed['after_num'],
'after_num' => $detailed['after_num'] + $contract['promise_fee'],
];
$lostnum = sprintf("%.6f",($currency['num'] + $income + $contract['promise_fee']));
$lostnum = sprintf("%.6f",($currency['num_lh'] + $income + $contract['promise_fee']));
Db::startTrans();
try {
$ret = Db::name('app_currency_user')
->where('id',$currency['id'])->update(['num'=>$lostnum,'updatetime'=>time()]);
->where('id',$currency['id'])->update(['num_lh'=>$lostnum,'updatetime'=>time()]);
if(!$ret)
{
lopRedis($symbol);
@@ -627,7 +627,7 @@ class Contract extends Api
$this->error(__("操作频繁"));
}
$currency = Db::name('app_currency_user')
->field('num,id')
->field('num_lh,id')
->where('user_id',$this->auth->id)
->where('curr_id',1)->find();
@@ -639,18 +639,18 @@ class Contract extends Api
'type' => '6',
'serial_no' => time().Random::build('numeric',4),
'order_result' => 'result',
'description' => '合约交易保证金及手续费',
'description' => 'Contract Margin & Fee Refund',
'createtime' => time(),
'notice' => '合约交易保证金及手续费',
'before_num' => $currency['num'],
'after_num' => $currency['num'] + $contract['promise_fee'] + $contract['fee'],
'notice' => 'Contract Margin & Fee Refund',
'before_num' => $currency['num_lh'],
'after_num' => $currency['num_lh'] + $contract['promise_fee'] + $contract['fee'],
];
$lostnum = sprintf("%.6f",($currency['num'] + $contract['promise_fee'] + $contract['fee']));
$lostnum = sprintf("%.6f",($currency['num_lh'] + $contract['promise_fee'] + $contract['fee']));
Db::startTrans();
try {
$ret = Db::name('app_currency_user')
->where('id',$currency['id'])->update(['num'=>$lostnum,'updatetime'=>time()]);
->where('id',$currency['id'])->update(['num_lh'=>$lostnum,'updatetime'=>time()]);
if(!$ret)
{
lopRedis($symbol);
+4 -2
View File
@@ -64,7 +64,8 @@ class Index extends Api
->order('id desc')
->paginate(20,false,['query' => request()->param()]);
foreach ($message as $key => $value) {
$value['createtime'] = date("Y-m-d H:i:s",$value['createtime']);
// 公告时间跟随实时变动,显示当前时间
$value['createtime'] = date("Y-m-d H:i:s", time());
switch ($lang) {
case "zh-cn":
$value['content'] = strip_tags($value['content']);
@@ -129,7 +130,8 @@ class Index extends Api
if(empty($message)){
$this->error(__("内容不存在"));
}
$message['createtime'] = date("Y-m-d H:i:s",$message['createtime']);
// 公告时间跟随实时变动
$message['createtime'] = date("Y-m-d H:i:s", time());
switch ($lang) {
case "zh-cn":
$message['content'] = str_replace("src=\"","src=\"".Config::get("site.image_url"), $message['content']);
+2 -1
View File
@@ -35,8 +35,9 @@ class My extends Api
$is_angel = false;
}
$data = [
'id' => $this->auth->id,
'user_id' => $this->auth->id,
'avatar' => Config::get('site.image_url').$this->auth->avatar,
'avatar' => (strpos($this->auth->avatar, 'http') === 0) ? $this->auth->avatar : Config::get('site.image_url').$this->auth->avatar,
'nickname' => $this->auth->nickname,
'email' => $this->auth->email,
'is_auth' => $auth['status'],
+20 -26
View File
@@ -47,7 +47,8 @@ class User extends Api
$ret = $this->auth->login($account, $password);
if ($ret) {
$userinfo = $this->auth->getUserinfo();
$userinfo['avatar'] = Config::get("site.image_url").$userinfo['avatar'];
$_av = $userinfo['avatar'];
$userinfo['avatar'] = (strpos($_av, 'http') === 0) ? $_av : Config::get("site.image_url").$_av;
$data = ['userinfo' => $userinfo];
//对接公链
@@ -141,21 +142,23 @@ class User extends Api
$mobile = $this->request->post('mobile',"");
$m_prefix = $this->request->post('m_prefix',"");
$code = $this->request->post('code');
$captcha_key = $this->request->post('captcha_key', '');
$pwd2 = $this->request->post('pay_pwd');
$referral_code = $this->request->post('referral_code', '');
// 验证图形验证码(替代邮箱/短信OTP)
if (!$captcha_key || !$code) {
$this->error(__('验证码错误'));
}
$captchaOk = \app\api\controller\Common::checkCaptcha($captcha_key, $code);
if (!$captchaOk) {
$this->error(__('验证码错误'));
}
if($mobile){
$ret = Sms::check($mobile, $code, 'register');
if (!$ret && $code!=157258) {
$this->error(__('验证码错误'));
}
$username = $mobile;
$extend['m_prefix'] = $m_prefix;
}else{
$ret = Ems::check($email, $code, 'register');
if (!$ret && $code!=157258) {
$this->error(__('验证码错误'));
}
$username = $email;
$extend['m_prefix'] = "";
}
@@ -172,7 +175,7 @@ class User extends Api
$this->error(__('请输入邀请码'));
}
$extend['nickname'] = "用户". rand(1000000, 9999999);
$extend['nickname'] = $email ? $email : $mobile;
// if($referral_code){ //判断上级推荐码和id
$w["referral_code"] = array( "eq" , $referral_code );
$p_info = Db::name('user')->field("id,path")->where($w)->find();
@@ -184,7 +187,8 @@ class User extends Api
}
// }
$extend['pay_pwd'] = strtoupper(md5(strtoupper(md5($pwd2.'skund'))));
$extend['avatar'] = Config::get("site.default_avatar");
// 默认男性商务头像(静态图片)
$extend['avatar'] = '/uploads/default_avatar.png';
$extend['referral_code'] = $this->create_invite_code(); //推荐码
//注册钱包eth
$ret = $this->auth->register($username, $password, $email, $mobile, $extend);
@@ -290,25 +294,15 @@ class User extends Api
{
$newpassword = $this->request->request("newpassword");
$captcha = $this->request->request("captcha");
$captcha_key = $this->request->request("captcha_key", "");
$types = $this->request->request("types");
$type = $this->request->request("type","mobile");//email
if (!$newpassword || !$captcha) {
$this->error(__('请输入完整信息'));
}
if($type == "email"){
$user = \app\common\model\User::getByEmail($this->auth->email);
$ret = Ems::check($user['email'], $captcha, 'resetpwd');
if (!$ret && $captcha!=157258) {
$this->error(__('验证码错误'));
}
Ems::flush($user['email'], 'resetpwd');
}else{
$user = \app\common\model\User::getByMobile($this->auth->mobile);
$ret = Sms::check($user['mobile'], $captcha, 'resetpwd');
if (!$ret && $captcha!=157258) {
$this->error(__('验证码错误'));
}
Sms::flush($user['mobile'], 'resetpwd');
// 使用图形验证码(4位大写字母)代替邮箱/短信OTP
$captchaOk = \app\api\controller\Common::checkCaptcha($captcha_key, $captcha);
if (!$captchaOk) {
$this->error(__('验证码错误'));
}
//模拟一次登录
+202 -47
View File
@@ -96,8 +96,21 @@ class Wallet extends Api
);
}
}
$paixu = array_column($curr_arr, "usdt_price");
array_multisort($paixu, SORT_DESC, $curr_arr);
// 前三位固定: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(
@@ -108,7 +121,20 @@ class Wallet extends Api
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),
@@ -127,11 +153,11 @@ class Wallet extends Api
"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,
// ),
"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);
}
@@ -539,15 +565,38 @@ 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){
@@ -564,27 +613,20 @@ 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);
@@ -721,7 +763,7 @@ class Wallet extends Api
'price' => $apy_num,
'cart' => '2',
'type' => '2',
'description' => '提币APY手续费',
'description' => 'Withdrawal APY Fee',
'createtime' => time(),
'before_num' => $user_apy['num'],
'after_num' => $user_apy['num'] - $apy_num,
@@ -759,7 +801,7 @@ class Wallet extends Api
'price' => $params['num'],
'cart' => '2',
'type' => '2',
'description' => '提币出账',
'description' => 'Withdrawal',
'createtime' => time(),
'before_num' => $user_curr['num'],
'after_num' => $user_curr['num'] - $params['num'],
@@ -794,12 +836,12 @@ class Wallet extends Api
{
$user_id = $this->auth->id;
$zhuanghu = array(
['id'=>1,"name"=>__("现货账户")],
['id'=>2,"name"=>__("法币账户")],
// ['id'=>3,"name"=>__("量化账户")],
['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,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)
@@ -847,24 +889,137 @@ class Wallet extends Api
$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 = "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 = "现货账户";
$left_msg = "Spot";
$left_field = "num";
$left_table = "app_detailed";
$left_type = 8;
break;
case 2:
$left_msg = "法币账户";
$left_msg = "Fiat";
$left_field = "num_fb";
$left_table = "app_detailed_fb";
$left_type = 1;
break;
case 3:
$left_msg = "量化账户";
$left_msg = "Contract";
$left_field = "num_lh";
$left_table = "app_detailed_lh";
$left_type = 1;
@@ -875,19 +1030,19 @@ class Wallet extends Api
}
switch ($right_id) {
case 1:
$right_msg = "现货账户";
$right_msg = "Spot";
$right_field = "num";
$right_table = "app_detailed";
$right_type = 8;
break;
case 2:
$right_msg = "法币账户";
$right_msg = "Fiat";
$right_field = "num_fb";
$right_table = "app_detailed_fb";
$right_type = 1;
break;
case 3:
$right_msg = "量化账户";
$right_msg = "Contract";
$right_field = "num_lh";
$right_table = "app_detailed_lh";
$right_type = 1;
@@ -912,7 +1067,7 @@ class Wallet extends Api
"price" => $num,
"cart" => 2,
"type" => $left_type,
"description" => $left_msg."转到".$right_msg,
"description" => $left_msg." to ".$right_msg,
"createtime" => time(),
"before_num" => $user_curr[$left_field],
"after_num" => $user_curr[$left_field] - $num,
@@ -923,7 +1078,7 @@ class Wallet extends Api
"price" => $num,
"cart" => 1,
"type" => $right_type,
"description" => $left_msg."转到".$right_msg,
"description" => $left_msg." to ".$right_msg,
"createtime" => time(),
"before_num" => $user_curr[$right_field],
"after_num" => $user_curr[$right_field] + $num,
@@ -934,7 +1089,7 @@ class Wallet extends Api
"form" => $left_id,
"to" => $right_id,
"num" => $num,
"msg" => $left_msg."转到".$right_msg,
"msg" => $left_msg." to ".$right_msg,
"addtime" => time(),
);
$curr_update = array(
Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB