|
@@ -9,6 +9,7 @@ use app\models\user\UserCoin;
|
|
|
use app\Request;
|
|
use app\Request;
|
|
|
use crmeb\services\{UtilService, JsonService};
|
|
use crmeb\services\{UtilService, JsonService};
|
|
|
use think\facade\Config;
|
|
use think\facade\Config;
|
|
|
|
|
+use think\facade\Cache;
|
|
|
|
|
|
|
|
class UserCoinController {
|
|
class UserCoinController {
|
|
|
public function status(Request $request) {
|
|
public function status(Request $request) {
|
|
@@ -22,12 +23,15 @@ class UserCoinController {
|
|
|
}
|
|
}
|
|
|
//
|
|
//
|
|
|
$defStatus = [
|
|
$defStatus = [
|
|
|
|
|
+ 'boot' => 0,
|
|
|
|
|
+ 'stop' => 0,
|
|
|
'progress'=> 0,
|
|
'progress'=> 0,
|
|
|
'symbol'=>$symbol,
|
|
'symbol'=>$symbol,
|
|
|
'icon'=>$icon,
|
|
'icon'=>$icon,
|
|
|
'price'=> 0.00,
|
|
'price'=> 0.00,
|
|
|
'total'=> 0,
|
|
'total'=> 0,
|
|
|
'step' => 0,
|
|
'step' => 0,
|
|
|
|
|
+ 'ts' => 0,
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
if (!$symbol) {
|
|
if (!$symbol) {
|
|
@@ -36,11 +40,22 @@ class UserCoinController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$uid = $request->uid();
|
|
$uid = $request->uid();
|
|
|
- $mymining = UserHash::mining_get($uid) ?? $defStatus;
|
|
|
|
|
- if ($mymining['progress'] > 0) {
|
|
|
|
|
- $mining = $this->calcMining($uid, $mymining);
|
|
|
|
|
- UserHash::mining_set($uid, $mining);
|
|
|
|
|
|
|
+ $mymining = UserHash::mining_get($uid);
|
|
|
|
|
+ if (!$mymining) {
|
|
|
|
|
+ $mymining = $defStatus;
|
|
|
}
|
|
}
|
|
|
|
|
+ if ($mymining['progress'] > 0.0) {
|
|
|
|
|
+ $mymining = $this->calcMining($uid, $mymining);
|
|
|
|
|
+ UserHash::mining_set($uid, $mymining);
|
|
|
|
|
+ }
|
|
|
|
|
+ return app('json')->successful([
|
|
|
|
|
+ 'symbol' => $mymining['symbol'],
|
|
|
|
|
+ 'icon' => $mymining['icon'],
|
|
|
|
|
+ 'price' => $mymining['price'],
|
|
|
|
|
+ 'step' => $mymining['step'],
|
|
|
|
|
+ 'progress' => $mymining['progress'],
|
|
|
|
|
+ 'total' => $mymining['total'],
|
|
|
|
|
+ ]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -52,37 +67,41 @@ class UserCoinController {
|
|
|
if ($p['progress'] <= 0) {
|
|
if ($p['progress'] <= 0) {
|
|
|
return $p;
|
|
return $p;
|
|
|
}
|
|
}
|
|
|
- if (!isset($p['ts'])) {
|
|
|
|
|
|
|
+ if (!isset($p['ts']) || !isset($p['boot']) || !isset($p['stop']) || !isset($p['step'])) {
|
|
|
|
|
+ Log::warning('error format.');
|
|
|
return $p;
|
|
return $p;
|
|
|
}
|
|
}
|
|
|
$now = time();
|
|
$now = time();
|
|
|
- $secs_passed = $now - $p['ts'];
|
|
|
|
|
- $secs_unit = Config::get('app.mining_sec_unit');
|
|
|
|
|
- $reward_unit = Config::get('app.mining_num_per_unit');
|
|
|
|
|
- $hours = Config::get('app.mining_time');
|
|
|
|
|
- if (count($hours) != 2 || $hours[0] > $hours[1]) {
|
|
|
|
|
- Log::warning('app.mining_time config error.');
|
|
|
|
|
- return $p;
|
|
|
|
|
- }
|
|
|
|
|
- $hour = random_int($hours[0], $hours[1]);
|
|
|
|
|
- $secs = $hour * 60 * 60;
|
|
|
|
|
|
|
+ $secs_passed = $now - $p['ts']; // 从上次到现在
|
|
|
//
|
|
//
|
|
|
- if ($secs_passed >= $secs) { // 挖矿结束
|
|
|
|
|
- $p['progress'] = 0;
|
|
|
|
|
-
|
|
|
|
|
|
|
+ if ($now >= $p['stop']) { // 挖矿结束
|
|
|
|
|
+ $secs_remain = $p['stop'] - $p['ts'];
|
|
|
|
|
+ if ($secs_remain < 0) {
|
|
|
|
|
+ $secs_remain = 0;
|
|
|
|
|
+ }
|
|
|
// 本次个数
|
|
// 本次个数
|
|
|
- $count = floatval(bcmul(bcdiv($secs, $secs_unit, 8), $reward_unit, 8));
|
|
|
|
|
- // save to database
|
|
|
|
|
- if (!UserCoinTransfer::addMining($uid, $p['order_id'], $p['symbol'], $count)) {
|
|
|
|
|
- Log::error("user<$uid> save transfer failed, amount<$count>");
|
|
|
|
|
|
|
+ $count = floatval(bcmul($p['step'], $secs_remain, 8));
|
|
|
|
|
+ $p['progress'] += floatval(bcadd($p['progress'], $count, 8));
|
|
|
|
|
+ // save to db
|
|
|
|
|
+ UserCoinTransfer::beginTrans();
|
|
|
|
|
+ $r1 = UserCoinTransfer::addMining($uid, $p['order_id'], $p['symbol'], $p['progress']);
|
|
|
|
|
+ $r2 = UserCoin::upsertCoin($uid, $p['symbol'], $p['progress']);
|
|
|
|
|
+ UserCoinTransfer::checkTrans($r1 && $r2);
|
|
|
|
|
+ if (!$r1 || !$r2) {
|
|
|
|
|
+ $amount = $p['progress'];
|
|
|
|
|
+ Log::error("user<$uid> save transfer failed, amount<$amount>");
|
|
|
|
|
+ return $p;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- $p['total'] += $count;
|
|
|
|
|
|
|
+ // -- save to db
|
|
|
|
|
+ $p['ts'] = $now;
|
|
|
|
|
+ $p['total'] = floatval(bcadd($p['total'], $p['progress'], 8));
|
|
|
|
|
+ $p['progress'] = 0;
|
|
|
return $p;
|
|
return $p;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- $count = floatval(bcmul(bcdiv($secs_passed, $secs_unit, 8), $reward_unit, 8));
|
|
|
|
|
- $p['progress'] = $count;
|
|
|
|
|
|
|
+ // 进行中
|
|
|
|
|
+ $count = floatval(bcmul($p['step'], $secs_passed, 8));
|
|
|
|
|
+ $p['progress'] = floatval(bcadd($p['progress'], $count, 8));
|
|
|
|
|
+ $p['ts'] = $now;
|
|
|
|
|
|
|
|
return $p;
|
|
return $p;
|
|
|
}
|
|
}
|
|
@@ -92,6 +111,7 @@ class UserCoinController {
|
|
|
*/
|
|
*/
|
|
|
public function boot(Request $request) {
|
|
public function boot(Request $request) {
|
|
|
$uid = $request->uid();
|
|
$uid = $request->uid();
|
|
|
|
|
+ $now = time();
|
|
|
// 是否开启活动
|
|
// 是否开启活动
|
|
|
$symbol = Config::get('app.mining_symbo');
|
|
$symbol = Config::get('app.mining_symbo');
|
|
|
if (!$symbol) {
|
|
if (!$symbol) {
|
|
@@ -100,7 +120,8 @@ class UserCoinController {
|
|
|
// 是否已经开启
|
|
// 是否已经开启
|
|
|
$mining = UserHash::mining_get($uid);
|
|
$mining = UserHash::mining_get($uid);
|
|
|
if ($mining) {
|
|
if ($mining) {
|
|
|
- if (isset($mining['progress']) && $mining['progress'] > 0.0) {
|
|
|
|
|
|
|
+ if (isset($mining['progress']) && $mining['progress'] > 0.0
|
|
|
|
|
+ && isset($mining['boot']) && isset($mining['stop']) && isset($mining['ts'])) {
|
|
|
return app('json')->fail('已启动');
|
|
return app('json')->fail('已启动');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -108,26 +129,37 @@ class UserCoinController {
|
|
|
// 是否有订单
|
|
// 是否有订单
|
|
|
$orderId = StoreOrderCartInfo::getMiningOrderId($uid);
|
|
$orderId = StoreOrderCartInfo::getMiningOrderId($uid);
|
|
|
if (!$orderId) {
|
|
if (!$orderId) {
|
|
|
- return app('json')->fail('下单后可启动');
|
|
|
|
|
|
|
+ return app('json')->fail('参加活动后可启动,请参看新手教程');
|
|
|
}
|
|
}
|
|
|
// 标记订单
|
|
// 标记订单
|
|
|
if (!StoreOrderCartInfo::setMining($orderId)) {
|
|
if (!StoreOrderCartInfo::setMining($orderId)) {
|
|
|
- return app('json')->fail('执行失败');
|
|
|
|
|
|
|
+ return app('json')->fail('启动失败');
|
|
|
}
|
|
}
|
|
|
// 开启
|
|
// 开启
|
|
|
$secs_unit = Config::get('app.mining_sec_unit');
|
|
$secs_unit = Config::get('app.mining_sec_unit');
|
|
|
$reward_unit = Config::get('app.mining_num_per_unit');
|
|
$reward_unit = Config::get('app.mining_num_per_unit');
|
|
|
- $progress = floatval(bcdiv($reward_unit, $secs_unit, 8));
|
|
|
|
|
|
|
+ $step = floatval(bcdiv($reward_unit, $secs_unit, 8));
|
|
|
|
|
+ $progress = $step;
|
|
|
$icon = DictCoin::getIcon($symbol);
|
|
$icon = DictCoin::getIcon($symbol);
|
|
|
$balance = UserCoin::where('uid', $uid)->where('symbol', $symbol)->value('balance') ?? 0.0;
|
|
$balance = UserCoin::where('uid', $uid)->where('symbol', $symbol)->value('balance') ?? 0.0;
|
|
|
|
|
+ $hours = Config::get('app.mining_time');
|
|
|
|
|
+ if (count($hours) != 2 || $hours[0] > $hours[1]) {
|
|
|
|
|
+ Log::warning('app.mining_time config error.');
|
|
|
|
|
+ return app('json')->fail('启动失败,请联系客服');;
|
|
|
|
|
+ }
|
|
|
|
|
+ $hour = random_int($hours[0], $hours[1]);
|
|
|
|
|
+ $stop = $now + $hour * 60 * 60;
|
|
|
|
|
|
|
|
$suc = UserHash::mining_set($uid, [
|
|
$suc = UserHash::mining_set($uid, [
|
|
|
|
|
+ 'boot' => $now,
|
|
|
|
|
+ 'step' => $step,
|
|
|
|
|
+ 'stop' => $stop,
|
|
|
'symbol'=> $symbol,
|
|
'symbol'=> $symbol,
|
|
|
'icon' => $icon,
|
|
'icon' => $icon,
|
|
|
'price' => 0,
|
|
'price' => 0,
|
|
|
'progress' => $progress,
|
|
'progress' => $progress,
|
|
|
'total' => $balance,
|
|
'total' => $balance,
|
|
|
- 'ts' => time(),
|
|
|
|
|
|
|
+ 'ts' => $now + 1,
|
|
|
'order_id' => $orderId,
|
|
'order_id' => $orderId,
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
@@ -137,7 +169,7 @@ class UserCoinController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return app('json')->successful([
|
|
return app('json')->successful([
|
|
|
- "symbol" => $symbol,
|
|
|
|
|
|
|
+ 'symbol' => $symbol,
|
|
|
'icon' => $icon,
|
|
'icon' => $icon,
|
|
|
'price' => 0,
|
|
'price' => 0,
|
|
|
'step' => $progress,
|
|
'step' => $progress,
|