UserNotificationController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\models\coin\UserCoinTransfer;
  4. use app\models\redis\SystemCarousel;
  5. use app\models\redis\UserHash;
  6. use app\models\system\DictCoin;
  7. use app\Request;
  8. use crmeb\services\JsonService;
  9. use think\facade\Config;
  10. use think\facade\Log;
  11. use think\facade\Cache;
  12. /**
  13. *
  14. * activities json 配置:
  15. mining:
  16. {
  17. "enabled": 1,
  18. "symbol": "btc",
  19. }
  20. * Class UserNotificationController
  21. * @package app\api\controller\user
  22. */
  23. class UserNotificationController {
  24. /**
  25. * 定时请求的状态接口
  26. * @param Request $request
  27. */
  28. public function snapshot(Request $request) {
  29. $uid = 1;
  30. // 未读消息
  31. $unread = UserHash::unread_get($uid);
  32. // 是否开启挖矿
  33. $symbol = Config::get('app.mining_symbo');
  34. $icon = Cache::get($symbol);
  35. if (!$icon) {
  36. $icon = DictCoin::where('symbol', $symbol)->value('icon');
  37. Cache::set($symbol, $icon);
  38. }
  39. $defStatus = [
  40. 'progress'=> 0,
  41. 'symbol'=>$symbol,
  42. 'icon'=>$icon,
  43. 'total'=> 0,
  44. 'ts' => time(),
  45. ];
  46. if ($symbol) {
  47. $mymining = UserHash::mining_get($uid) ?? $defStatus;
  48. if ($mymining['progress'] > 0) {
  49. $mining = $this->calcMining($uid, $mymining);
  50. UserHash::mining_set($uid, $mining);
  51. }
  52. }
  53. // 跑马灯
  54. $carousel = SystemCarousel::getFirst(20);
  55. return app('json')->successful(compact('unread', 'mining', 'carousel'));
  56. }
  57. /**
  58. * 根据上次挖矿状态, 和过去的时长, 计算当前的状态
  59. * @param $p
  60. * @return mixed
  61. */
  62. protected function calcMining($uid, $p) {
  63. if ($p['progress'] <= 0) {
  64. return $p;
  65. }
  66. if (!isset($p['ts'])) {
  67. return $p;
  68. }
  69. $now = time();
  70. $secs_passed = $now - $p['ts'];
  71. $secs_unit = Config::get('app.mining_sec_unit');
  72. $reward_unit = Config::get('app.mining_num_per_unit');
  73. $hours = Config::get('app.mining_time');
  74. if (count($hours) != 2 || $hours[0] > $hours[1]) {
  75. Log::warning('app.mining_time config error.');
  76. return $p;
  77. }
  78. $hour = random_int($hours[0], $hours[1]);
  79. $secs = $hour * 60 * 60;
  80. //
  81. if ($secs_passed >= $secs) { // 挖矿结束
  82. $p['progress'] = 0;
  83. // 本次个数
  84. $count = floatval(bcmul(bcdiv($secs, $secs_unit, 8), $reward_unit, 8));
  85. // save to database
  86. if (!UserCoinTransfer::addMining($uid, $p['order_id'], $p['symbol'], $count)) {
  87. Log::error("user<$uid> save transfer failed, amount<$count>");
  88. }
  89. $p['total'] += $count;
  90. return $p;
  91. }
  92. $count = floatval(bcmul(bcdiv($secs_passed, $secs_unit, 8), $reward_unit, 8));
  93. $p['progress'] = $count;
  94. return $p;
  95. }
  96. }