Explorar el Código

add: notifications 协议实现

joe hace 4 años
padre
commit
08928fa6a1
Se han modificado 2 ficheros con 117 adiciones y 15 borrados
  1. 109 14
      app/api/controller/user/UserNotificationController.php
  2. 8 1
      config/app.php

+ 109 - 14
app/api/controller/user/UserNotificationController.php

@@ -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;
     }
 }

+ 8 - 1
config/app.php

@@ -47,5 +47,12 @@ return [
     // 亚里士多得
     'qy_weixin_robot_aristotle' => 'a17b9d56-b566-404b-904e-4476b95dc8d7',
     // 自动发消息的消息发送者
-    'notice_sender' => '运营管理员'
+    'notice_sender' => '运营管理员',
+    // 挖矿配置, 每次更新币种后需要重新配置
+    // 单次挖矿时长
+    'mining_symbo' => 'doge',
+    'mining_symbo_icon' => '',
+    'mining_time' => [12, 24],
+    // 每 5 sec 挖币个数
+    'mining_num_per_5_sec' => 0.01,
 ];