|
|
@@ -4,27 +4,122 @@ namespace app\api\controller\user;
|
|
|
use app\Request;
|
|
|
use crmeb\services\UtilService;
|
|
|
use crmeb\utils\Redis;
|
|
|
+use think\facade\Config;
|
|
|
+use think\facade\Log;
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * activities json 配置:
|
|
|
+
|
|
|
+ mining:
|
|
|
+ {
|
|
|
+ "enabled": 1,
|
|
|
+ "symbol": "btc",
|
|
|
+ }
|
|
|
+
|
|
|
+ * Class UserNotificationController
|
|
|
+ * @package app\api\controller\user
|
|
|
+ */
|
|
|
class UserNotificationController {
|
|
|
- public function snapshot() {
|
|
|
+ /**
|
|
|
+ * 定时请求的状态接口
|
|
|
+ * @param Request $request
|
|
|
+ */
|
|
|
+ public function snapshot(Request $request) {
|
|
|
$uid = 1;
|
|
|
$redis = Redis::instance();
|
|
|
- // unread
|
|
|
- $unread = Redis::hGet('user:'.$uid, 'unread')??0;
|
|
|
- echo intval($unread);
|
|
|
- // is mining enabled
|
|
|
- $sconf = Redis::hGet('activities', 'mining')??'{"enabled":0}';
|
|
|
- $conf = json_decode($sconf, true);
|
|
|
- print_r($conf);
|
|
|
- // last time mining
|
|
|
- $smymining = Redis::hGet('user:'.$uid, 'mining')??'';
|
|
|
- $mymining = json_decode($smymining, true);
|
|
|
- if ($mymining['progress'] > 0) {
|
|
|
- $mymining = $this->calcMining($mymining);
|
|
|
+ // 未读消息
|
|
|
+ $unread = intval(Redis::hGet('user:'.$uid, 'unread')??0);
|
|
|
+
|
|
|
+ // 是否开启挖矿
|
|
|
+ $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
|
|
|
+ }
|
|
|
+ */
|
|
|
+
|
|
|
+ $defStatus = [
|
|
|
+ 'progress'=> 0,
|
|
|
+ 'symbol'=>$symbo,
|
|
|
+ 'icon'=>$icon,
|
|
|
+ 'total'=> 0,
|
|
|
+ 'ts' => time(),
|
|
|
+ ];
|
|
|
+ if ($symbo) {
|
|
|
+ $smymining = Redis::hGet('user:'.$uid, 'mining');
|
|
|
+ $mymining = json_decode($smymining, true) ?? $defStatus;
|
|
|
+ if ($mymining['progress'] > 0) {
|
|
|
+ $mymining = $this->calcMining($mymining);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ 跑马灯
|
|
|
+ 列表: 键 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'));
|
|
|
}
|
|
|
|
|
|
- protected function calcMining($p) {
|
|
|
+ /**
|
|
|
+ * 根据上次挖矿状态, 和过去的时长, 计算当前的状态
|
|
|
+ * @param $p
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ protected function calcMining(&$p) {
|
|
|
+ if ($p['progress'] <= 0) {
|
|
|
+ return $p;
|
|
|
+ }
|
|
|
+ if (!isset($p['ts'])) {
|
|
|
+ return $p;
|
|
|
+ }
|
|
|
+ $now = time();
|
|
|
+ $secs_passed = $now - $p['ts'];
|
|
|
+ $reward_unit = Config::get('app.mining_num_per_5_sec');
|
|
|
+ $hours = Config::get('app.mining_time');
|
|
|
+ if (count($hours) != 2 || $hours[1] < $hours[0]) {
|
|
|
+ Log::warning('app.mining_time config error.');
|
|
|
+ return $p;
|
|
|
+ }
|
|
|
+ $hour = random_int($hours[1], $hours[0]);
|
|
|
+ $secs = $hour * 60 * 60;
|
|
|
+ //
|
|
|
+ if ($secs_passed >= $secs) { // 挖矿结束
|
|
|
+ $p['progress'] = 0;
|
|
|
+
|
|
|
+ // 本次个数
|
|
|
+ $count = floatval(bcmul(bcdiv($secs, 5.0, 2), $reward_unit, 2));
|
|
|
+ // TODO save to database
|
|
|
+ $p['total'] += $count;
|
|
|
+ return $p;
|
|
|
+ }
|
|
|
+
|
|
|
+ $count = floatval(bcmul(bcdiv($secs_passed, 5.0, 2), $reward_unit, 2));
|
|
|
+ $p['progress'] = $count;
|
|
|
+
|
|
|
return $p;
|
|
|
}
|
|
|
}
|