|
@@ -1,12 +1,15 @@
|
|
|
<?php
|
|
<?php
|
|
|
namespace app\api\controller\user;
|
|
namespace app\api\controller\user;
|
|
|
|
|
|
|
|
|
|
+use app\models\coin\UserCoinTransfer;
|
|
|
|
|
+use app\models\redis\SystemCarousel;
|
|
|
|
|
+use app\models\redis\UserHash;
|
|
|
|
|
+use app\models\system\DictCoin;
|
|
|
use app\Request;
|
|
use app\Request;
|
|
|
-use crmeb\services\UtilService;
|
|
|
|
|
-use crmeb\utils\Redis;
|
|
|
|
|
|
|
+use crmeb\services\JsonService;
|
|
|
use think\facade\Config;
|
|
use think\facade\Config;
|
|
|
use think\facade\Log;
|
|
use think\facade\Log;
|
|
|
-
|
|
|
|
|
|
|
+use think\facade\Cache;
|
|
|
/**
|
|
/**
|
|
|
*
|
|
*
|
|
|
* activities json 配置:
|
|
* activities json 配置:
|
|
@@ -27,61 +30,35 @@ class UserNotificationController {
|
|
|
*/
|
|
*/
|
|
|
public function snapshot(Request $request) {
|
|
public function snapshot(Request $request) {
|
|
|
$uid = 1;
|
|
$uid = 1;
|
|
|
- $redis = Redis::instance();
|
|
|
|
|
// 未读消息
|
|
// 未读消息
|
|
|
- $unread = intval(Redis::hGet('user:'.$uid, 'unread')??0);
|
|
|
|
|
|
|
+ $unread = UserHash::unread_get($uid);
|
|
|
|
|
|
|
|
// 是否开启挖矿
|
|
// 是否开启挖矿
|
|
|
- $symbo = Config::get('app.mining_symbo');
|
|
|
|
|
- $icon = Config::get('app.mining_symbo_icon');
|
|
|
|
|
-
|
|
|
|
|
- /*
|
|
|
|
|
- 最近挖矿状态
|
|
|
|
|
- hash key user:<uid> field: mining
|
|
|
|
|
-
|
|
|
|
|
- 格式:
|
|
|
|
|
- {
|
|
|
|
|
- "progress": 10,
|
|
|
|
|
- "symbol": "etc",
|
|
|
|
|
- "icon": "http://x.png",
|
|
|
|
|
- "price": 20,
|
|
|
|
|
- "total": 20
|
|
|
|
|
- }
|
|
|
|
|
- */
|
|
|
|
|
-
|
|
|
|
|
|
|
+ $symbol = Config::get('app.mining_symbo');
|
|
|
|
|
+ $icon = Cache::get($symbol);
|
|
|
|
|
+ if (!$icon) {
|
|
|
|
|
+ $icon = DictCoin::where('symbol', $symbol)->value('icon');
|
|
|
|
|
+ Cache::set($symbol, $icon);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$defStatus = [
|
|
$defStatus = [
|
|
|
'progress'=> 0,
|
|
'progress'=> 0,
|
|
|
- 'symbol'=>$symbo,
|
|
|
|
|
|
|
+ 'symbol'=>$symbol,
|
|
|
'icon'=>$icon,
|
|
'icon'=>$icon,
|
|
|
'total'=> 0,
|
|
'total'=> 0,
|
|
|
'ts' => time(),
|
|
'ts' => time(),
|
|
|
];
|
|
];
|
|
|
- if ($symbo) {
|
|
|
|
|
- $smymining = Redis::hGet('user:'.$uid, 'mining');
|
|
|
|
|
- $mymining = json_decode($smymining, true) ?? $defStatus;
|
|
|
|
|
|
|
+ if ($symbol) {
|
|
|
|
|
+ $mymining = UserHash::mining_get($uid) ?? $defStatus;
|
|
|
if ($mymining['progress'] > 0) {
|
|
if ($mymining['progress'] > 0) {
|
|
|
- $mymining = $this->calcMining($mymining);
|
|
|
|
|
|
|
+ $mining = $this->calcMining($uid, $mymining);
|
|
|
|
|
+ UserHash::mining_set($uid, $mining);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ // 跑马灯
|
|
|
|
|
+ $carousel = SystemCarousel::getFirst(20);
|
|
|
|
|
|
|
|
- /*
|
|
|
|
|
- 跑马灯
|
|
|
|
|
- 列表: 键 sys:carousel
|
|
|
|
|
-
|
|
|
|
|
- 格式:
|
|
|
|
|
- {
|
|
|
|
|
- "text": "<span style=\"color:2343;\"></span>",
|
|
|
|
|
- "uri": "page/boards",
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- */
|
|
|
|
|
- $arrStr = Redis::lRange('sys:carousel', 0, 10)??[];
|
|
|
|
|
- $carousel = [];
|
|
|
|
|
- foreach ($arrStr as $str) {
|
|
|
|
|
- array_push($carousel, json_decode($str, true));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return app('json')->successful(compact('unread', 'mymining', 'carousel'));
|
|
|
|
|
|
|
+ return JsonService::successful(compact('unread', 'mining', 'carousel'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -89,7 +66,7 @@ class UserNotificationController {
|
|
|
* @param $p
|
|
* @param $p
|
|
|
* @return mixed
|
|
* @return mixed
|
|
|
*/
|
|
*/
|
|
|
- protected function calcMining(&$p) {
|
|
|
|
|
|
|
+ protected function calcMining($uid, $p) {
|
|
|
if ($p['progress'] <= 0) {
|
|
if ($p['progress'] <= 0) {
|
|
|
return $p;
|
|
return $p;
|
|
|
}
|
|
}
|
|
@@ -98,26 +75,31 @@ class UserNotificationController {
|
|
|
}
|
|
}
|
|
|
$now = time();
|
|
$now = time();
|
|
|
$secs_passed = $now - $p['ts'];
|
|
$secs_passed = $now - $p['ts'];
|
|
|
- $reward_unit = Config::get('app.mining_num_per_5_sec');
|
|
|
|
|
|
|
+ $secs_unit = Config::get('app.mining_sec_unit');
|
|
|
|
|
+ $reward_unit = Config::get('app.mining_num_per_unit');
|
|
|
$hours = Config::get('app.mining_time');
|
|
$hours = Config::get('app.mining_time');
|
|
|
- if (count($hours) != 2 || $hours[1] < $hours[0]) {
|
|
|
|
|
|
|
+ if (count($hours) != 2 || $hours[0] > $hours[1]) {
|
|
|
Log::warning('app.mining_time config error.');
|
|
Log::warning('app.mining_time config error.');
|
|
|
return $p;
|
|
return $p;
|
|
|
}
|
|
}
|
|
|
- $hour = random_int($hours[1], $hours[0]);
|
|
|
|
|
|
|
+ $hour = random_int($hours[0], $hours[1]);
|
|
|
$secs = $hour * 60 * 60;
|
|
$secs = $hour * 60 * 60;
|
|
|
//
|
|
//
|
|
|
if ($secs_passed >= $secs) { // 挖矿结束
|
|
if ($secs_passed >= $secs) { // 挖矿结束
|
|
|
$p['progress'] = 0;
|
|
$p['progress'] = 0;
|
|
|
|
|
|
|
|
// 本次个数
|
|
// 本次个数
|
|
|
- $count = floatval(bcmul(bcdiv($secs, 5.0, 2), $reward_unit, 2));
|
|
|
|
|
- // TODO save to database
|
|
|
|
|
|
|
+ $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>");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$p['total'] += $count;
|
|
$p['total'] += $count;
|
|
|
return $p;
|
|
return $p;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $count = floatval(bcmul(bcdiv($secs_passed, 5.0, 2), $reward_unit, 2));
|
|
|
|
|
|
|
+ $count = floatval(bcmul(bcdiv($secs_passed, $secs_unit, 8), $reward_unit, 8));
|
|
|
$p['progress'] = $count;
|
|
$p['progress'] = $count;
|
|
|
|
|
|
|
|
return $p;
|
|
return $p;
|