MiningController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\api\controller\mining;
  3. use app\models\mining\MiningMachine;
  4. use app\models\mining\UserMining;
  5. use app\models\mining\UserMiningMachine;
  6. use app\models\trade\CashTradeOrder;
  7. use app\Request;
  8. use crmeb\services\CacheService;
  9. use crmeb\services\UtilService;
  10. use think\db\exception\DataNotFoundException;
  11. use think\db\exception\DbException;
  12. use think\db\exception\ModelNotFoundException;
  13. class MiningController
  14. {
  15. public function calculator(Request $request)
  16. {
  17. $machine = MiningMachine::valid()->where('get_money_type', 'in', ['XCH'])->find();
  18. if ($machine) {
  19. $machine_price = $machine['cost_money'];
  20. $machine_price_type = init_money_type()[$machine['cost_money_type']];
  21. $machine_get = $machine['day_get'];
  22. $machine_service = $machine['service'];
  23. $time = $machine['first_step_time'] + $machine['second_step_time'] + $machine['third_step_time'];
  24. } else {
  25. $machine_price = 0;
  26. $machine_price_type = 'FIL';
  27. $machine_get = 0;
  28. $machine_service = 0;
  29. $time = 0;
  30. }
  31. $money_type = sys_data('money_type');
  32. foreach ($money_type as $v) {
  33. if (explode('_', $v['code'])[0] == "USDT") {
  34. if ($v['price'] <= 0) {
  35. //计算前一天成交的平均价格
  36. $v['price'] = CashTradeOrder::averagePrice($v['code']);
  37. }
  38. $usdt_price = $v['price'];
  39. }
  40. if (explode('_', $v['code'])[0] == "XCH") {
  41. if ($v['price'] <= 0) {
  42. //计算前一天成交的平均价格
  43. $v['price'] = CashTradeOrder::averagePrice($v['code']);
  44. }
  45. $fli_price = $v['price'];
  46. }
  47. if (explode('_', $v['code'])[0] == $machine_price_type) {
  48. if ($v['price'] <= 0) {
  49. //计算前一天成交的平均价格
  50. $v['price'] = CashTradeOrder::averagePrice($v['code']);
  51. }
  52. $cost_price = $v['price'];
  53. }
  54. }
  55. $fli_usdt = (isset($usdt_price) && $usdt_price > 0) ? bcdiv($fli_price, $usdt_price, 8) : 0;//fli对usdt价格
  56. $cost_fli = (isset($fli_price) && $fli_price > 0) ? bcdiv($cost_price, $fli_price, 8) : 0;
  57. $cost_fli_price = (isset($cost_fli) && $cost_fli > 0) ? bcdiv($machine_price, $cost_fli, 8) : 0;
  58. $get_back = (isset($cost_fli_price) && $cost_fli_price > 0) ? bcdiv($cost_fli_price, $machine_get, 8) : 0;//回本天数
  59. $month_back = bcmul(bcmul($machine_get, 30, 8), bcsub(1, bcdiv($machine_service, 100, 2), 2), 8);
  60. $year = bcmul(bcmul($machine_get, 365, 8), bcsub(1, bcdiv($machine_service, 100, 2), 2), 8);
  61. $year_get = (isset($cost_fli_price) && $cost_fli_price > 0) ? bcdiv($year, $cost_fli_price, 8) : 0;
  62. return app('json')->success('ok', compact('fli_usdt', 'machine_price', 'machine_price_type', 'machine_get', 'time', 'get_back', 'month_back', 'year_get'));
  63. }
  64. public function mining_index(Request $request)
  65. {
  66. $all = UserMiningMachine::where('get_money_type', 'in', 'XCH')->sum('num');
  67. $doing = UserMiningMachine::where('get_money_type', 'in', 'XCH')->where('status', 'in', [1, 2])->sum('num');
  68. $stand = UserMiningMachine::where('get_money_type', 'in', 'XCH')->where('status', 0)->sum('num');
  69. $all_mining = UserMining::where('get_money_type', 'in', 'XCH')->sum('get_money');
  70. $all_lock = UserMining::where('get_money_type', 'in', 'XCH')->sum('lock_money');
  71. return app('json')->success('ok', compact('all', 'doing', 'stand', 'all_mining', 'all_lock'));
  72. }
  73. /**
  74. * 算力产品
  75. * @param Request $request
  76. * @return mixed
  77. */
  78. public function lst(Request $request)
  79. {
  80. $page = $request->get('page', 1);
  81. $limit = $request->get('limit', 10);
  82. $get_money_type = $request->get('get_money_type', '');
  83. $type = $request->get('type', '');
  84. return app('json')->success('ok', MiningMachine::getList($page, $limit, ['get_money_type' => $get_money_type, 'type' => $type]));
  85. }
  86. /**
  87. * @param $id
  88. * @param Request $request
  89. * @return mixed
  90. * @throws DataNotFoundException
  91. * @throws DbException
  92. * @throws ModelNotFoundException
  93. */
  94. public function detail($id, Request $request)
  95. {
  96. $res = MiningMachine::valid()->where('id', $id)->find()->toArray();
  97. $money_type = init_money_type();
  98. $res['_day_get'] = $res['day_get'] . $money_type[$res['get_money_type']] . '/T';
  99. $res['_cost_money'] = $res['cost_money'] . $money_type[$res['cost_money_type']];
  100. $res['_stand_money'] = $res['stand_money'] . $money_type[$res['get_money_type']];
  101. $res['_cost_money_type'] = $money_type[$res['cost_money_type']];
  102. return app('json')->success('ok', $res);
  103. }
  104. public function buy($id, Request $request)
  105. {
  106. $user = $request->user();
  107. list($num,) = UtilService::postMore(
  108. [
  109. ['num', 0,],
  110. ['trade_psw', '', '', '', ['not_empty_check', function ($item) use ($user) {
  111. // var_dump($user);
  112. return md5(md5($item)) == $user['trade_pwd'];
  113. }], ['请输入交易密码', '交易密码错误']],
  114. ], $request, true);
  115. $res = MiningMachine::buyMachine($id, $request->uid(), $num);
  116. if ($res) {
  117. return app('json')->success('购买成功');
  118. } else {
  119. return app('json')->fail(MiningMachine::getErrorInfo());
  120. }
  121. }
  122. public function my(Request $request)
  123. {
  124. $where = UtilService::getMore([
  125. ['page', 1],
  126. ['limit', 10],
  127. ]);
  128. $where['uid'] = $request->uid();
  129. return app('json')->success('ok', UserMiningMachine::getList($where));
  130. }
  131. }