MiningController.php 8.2 KB

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