VoteController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\api\controller;
  3. use app\models\trade\CashTradeOrder;
  4. use app\models\user\UserBill;
  5. use app\models\user\UserMoney;
  6. use app\models\user\UserMoneyOrder;
  7. use app\models\vote\UserVote;
  8. use app\models\vote\Vote;
  9. use app\models\vote\VoteSub;
  10. use app\Request;
  11. use crmeb\services\CacheService;
  12. use crmeb\services\UtilService;
  13. use think\db\exception\DataNotFoundException;
  14. use think\db\exception\DbException;
  15. use think\db\exception\ModelNotFoundException;
  16. class VoteController
  17. {
  18. /**
  19. * 投票列表
  20. * @param Request $request
  21. * @return mixed
  22. * @throws DataNotFoundException
  23. * @throws DbException
  24. * @throws ModelNotFoundException
  25. */
  26. public function lst(Request $request)
  27. {
  28. $uid = $request->uid();
  29. $page = $request->get('page', 1);
  30. $limit = $request->get('limit', 10);
  31. $data = Vote::ing()->page($page, $limit)->select()->each(function ($item) use ($request) {
  32. $item['sub_vote'] = Vote::votingSub($item['id'], $request->uid());
  33. });
  34. $count = Vote::ing()->count();
  35. $commission_type = sys_config('vote_commission_type', '');
  36. $commission_ratio = sys_config('vote_commission_ratio', 0);
  37. $all_commission = 0;
  38. $today_commission = 0;
  39. $vote_people = UserVote::group('uid')->count();
  40. if ($uid) {
  41. $all_commission = UserMoney::getComission($uid, 'USDT_ERC20');
  42. $today_commission = UserMoney::getComission($uid, 'USDT_ERC20', 0, 'today');
  43. }
  44. return app('json')->success('ok', compact('data', 'count', 'commission_type', 'commission_ratio', 'all_commission', 'today_commission', 'vote_people'));
  45. }
  46. /**
  47. * 投票详情
  48. * @param $id
  49. * @param Request $request
  50. * @return mixed
  51. * @throws DataNotFoundException
  52. * @throws DbException
  53. * @throws ModelNotFoundException
  54. */
  55. public function detail($id, Request $request)
  56. {
  57. $uid = $request->uid();
  58. $data = Vote::ing()->where('id', $id)->find()->toArray();
  59. $status = $request->get('status', '');
  60. if ($data) $data['sub_vote'] = Vote::votingSub($id, $request->uid(), $status);
  61. if ($uid) {
  62. $data['all_commission'] = UserMoney::getComission($uid, 'USDT_ERC20', $id);
  63. $data['today_commission'] = UserMoney::getComission($uid, 'USDT_ERC20', $id, 'today');
  64. }
  65. $data['commission_type'] = sys_config('vote_commission_type', '');
  66. $data['commission_ratio'] = sys_config('vote_commission_ratio', 0);
  67. return app('json')->success('ok', $data);
  68. }
  69. /**
  70. * 参加投票
  71. * @param Request $request
  72. * @return mixed
  73. */
  74. public function join(Request $request)
  75. {
  76. // CacheService::redisHandler()->set('1',10086);
  77. // CacheService::redisHandler()->get('1');
  78. $user = $request->user();
  79. $uid = $user['uid'];
  80. list($num, $id, , $captcha) = UtilService::postMore(
  81. [
  82. ['num', 0,],
  83. ['id', 0, '', '', 'not_empty_check', '请选择参与的投票'],
  84. ['trade_psw', '', '', '', ['not_empty_check', function ($item) use ($user) {
  85. // var_dump($user);
  86. return md5(md5($item)) == $user['trade_pwd'];
  87. }], ['请输入交易密码', '交易密码错误']],
  88. ['captcha', '']
  89. ], $request, true);
  90. $type = Vote::where('id', $id)->value('money_type');
  91. //TODO 写队列
  92. $price = 0;
  93. $list = sys_data('money_type');
  94. foreach ($list as $v) {
  95. if ($v['code'] == $type) {
  96. $price = $v['price'] ? $v['price'] : CashTradeOrder::averagePrice($v['code']);
  97. }
  98. }
  99. if (bcmul($num, $price, 2) >= 10000) {
  100. $verifyCode = CacheService::get('code_' . $user['account']);
  101. if (!$verifyCode)
  102. return app('json')->fail('请先获取验证码');
  103. $verifyCode = substr($verifyCode, 0, 6);
  104. if ($verifyCode != $captcha)
  105. return app('json')->fail('验证码错误');
  106. }
  107. $wait_id = md5($uid . $id . time() . rand(0, 99999));
  108. (new \crmeb\utils\Vote)->push(['uid' => $uid, 'num' => $num, 'id' => $id, 'wait_id' => $wait_id]);
  109. CacheService::set($wait_id, '投票中,请稍候……');
  110. return app('json')->success('已进入投票排队', ['wait_id' => $wait_id]);
  111. }
  112. public function voteResult($key, Request $request)
  113. {
  114. $res = CacheService::get($key);
  115. $res2 = CacheService::get($key . '_success', false);
  116. if ($res) return app('json')->success($res, ['status' => $res2 ? 1 : 0]);
  117. else return app('json')->fail('投票查询记录已失效或不存在');
  118. }
  119. /**
  120. * 我参与的投票列表
  121. * @param Request $request
  122. */
  123. public function my_list(Request $request)
  124. {
  125. $user = $request->user();
  126. $uid = $user['uid'];
  127. $page = $request->get('page', 1);
  128. $limit = $request->get('limit', 10);
  129. $status = $request->get('status', '');
  130. $where = [];
  131. if ($status != '') $where['status'] = explode(',', $status);
  132. $ids = UserVote::where('uid', $uid)->column('sub_vote_id');
  133. // var_dump($ids);
  134. $count = $ids ? VoteSub::where('id', 'in', $ids)->where($where)->count() : 0;
  135. // var_dump($count);
  136. $list = $ids ? VoteSub::where('id', 'in', $ids)->where($where)->order('add_time', 'desc')->page($page, $limit)->select()->each(function ($item) use ($uid) {
  137. $item['parent'] = Vote::where('id', $item['vote_id'])->find();
  138. $item['my'] = UserVote::where('sub_vote_id', $item['id'])->where('uid', $uid)->select();
  139. $item['now_voted'] = UserVote::where('sub_vote_id', $item['id'])->sum('vote_num');
  140. if ($uid) {
  141. $item['user_now_voted'] = UserVote::where('uid', $uid)->where('sub_vote_id', $item['id'])->sum('vote_num');
  142. $item['user_now_get'] = UserMoneyOrder::where('to_uid', $uid)->where('from_sub_vote', $item['id'])->sum('money');
  143. }
  144. $item['commission_type'] = sys_config('vote_commission_type', '');
  145. $item['commission_ratio'] = sys_config('vote_commission_ratio', 0);
  146. $item['_finish_time'] = date('Y-m-d H:i:s',$item['finish_time']);
  147. $item['_end_time'] = date('Y-m-d H:i:s',$item['end_time']);
  148. $item['_add_time'] = date('Y-m-d H:i:s',$item['add_time']);
  149. $item['_success_time'] = date('Y-m-d H:i:s',$item['success_time']);
  150. }) : [];
  151. $all_commission = 0;
  152. $today_commission = 0;
  153. if ($uid) {
  154. $all_commission = UserMoney::getComission($uid, 'USDT_ERC20');
  155. $today_commission = UserMoney::getComission($uid, 'USDT_ERC20', 0, 'today');
  156. }
  157. // var_dump(UserMoney::getLastSql());
  158. return app('json')->success('ok', compact('list', 'count', 'all_commission', 'today_commission'));
  159. }
  160. }