MiningController.php 7.8 KB

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