123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace app\api\controller;
- use app\models\trade\CashTradeOrder;
- use app\models\user\UserBill;
- use app\models\user\UserMoney;
- use app\models\user\UserMoneyOrder;
- use app\models\vote\UserVote;
- use app\models\vote\Vote;
- use app\models\vote\VoteSub;
- use app\Request;
- use crmeb\services\CacheService;
- use crmeb\services\UtilService;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- class VoteController
- {
- /**
- * 投票列表
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function lst(Request $request)
- {
- $uid = $request->uid();
- $page = $request->get('page', 1);
- $limit = $request->get('limit', 10);
- $data = Vote::ing()->page($page, $limit)->select()->each(function ($item) use ($request) {
- $item['sub_vote'] = Vote::votingSub($item['id'], $request->uid());
- });
- $count = Vote::ing()->count();
- $commission_type = sys_config('vote_commission_type', '');
- $commission_ratio = sys_config('vote_commission_ratio', 0);
- $all_commission = 0;
- $today_commission = 0;
- $vote_people = UserVote::group('uid')->count();
- if ($uid) {
- $all_commission = UserMoney::getComission($uid, 'USDT_ERC20');
- $today_commission = UserMoney::getComission($uid, 'USDT_ERC20', 0, 'today');
- }
- return app('json')->success('ok', compact('data', 'count', 'commission_type', 'commission_ratio', 'all_commission', 'today_commission', 'vote_people'));
- }
- /**
- * 投票详情
- * @param $id
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function detail($id, Request $request)
- {
- $uid = $request->uid();
- $data = Vote::ing()->where('id', $id)->find()->toArray();
- $status = $request->get('status', '');
- if ($data) $data['sub_vote'] = Vote::votingSub($id, $request->uid(), $status);
- if ($uid) {
- $data['all_commission'] = UserMoney::getComission($uid, 'USDT_ERC20', $id);
- $data['today_commission'] = UserMoney::getComission($uid, 'USDT_ERC20', $id, 'today');
- }
- $data['commission_type'] = sys_config('vote_commission_type', '');
- $data['commission_ratio'] = sys_config('vote_commission_ratio', 0);
- return app('json')->success('ok', $data);
- }
- /**
- * 参加投票
- * @param Request $request
- * @return mixed
- */
- public function join(Request $request)
- {
- // CacheService::redisHandler()->set('1',10086);
- // CacheService::redisHandler()->get('1');
- $user = $request->user();
- $uid = $user['uid'];
- list($num, $id, , $captcha) = UtilService::postMore(
- [
- ['num', 0,],
- ['id', 0, '', '', 'not_empty_check', '请选择参与的投票'],
- ['trade_psw', '', '', '', ['not_empty_check', function ($item) use ($user) {
- // var_dump($user);
- return md5(md5($item)) == $user['trade_pwd'];
- }], ['请输入交易密码', '交易密码错误']],
- ['captcha', '']
- ], $request, true);
- $type = Vote::where('id', $id)->value('money_type');
- //TODO 写队列
- $price = 0;
- $list = sys_data('money_type');
- foreach ($list as $v) {
- if ($v['code'] == $type) {
- $price = $v['price'] ? $v['price'] : CashTradeOrder::averagePrice($v['code']);
- }
- }
- if (bcmul($num, $price, 2) >= 10000) {
- $verifyCode = CacheService::get('code_' . $user['account']);
- if (!$verifyCode)
- return app('json')->fail('请先获取验证码');
- $verifyCode = substr($verifyCode, 0, 6);
- if ($verifyCode != $captcha)
- return app('json')->fail('验证码错误');
- }
- $wait_id = md5($uid . $id . time() . rand(0, 99999));
- (new \crmeb\utils\Vote)->push(['uid' => $uid, 'num' => $num, 'id' => $id, 'wait_id' => $wait_id]);
- CacheService::set($wait_id, '投票中,请稍候……');
- return app('json')->success('已进入投票排队', ['wait_id' => $wait_id]);
- }
- public function voteResult($key, Request $request)
- {
- $res = CacheService::get($key);
- $res2 = CacheService::get($key . '_success', false);
- if ($res) return app('json')->success($res, ['status' => $res2 ? 1 : 0]);
- else return app('json')->fail('投票查询记录已失效或不存在');
- }
- /**
- * 我参与的投票列表
- * @param Request $request
- */
- public function my_list(Request $request)
- {
- $user = $request->user();
- $uid = $user['uid'];
- $page = $request->get('page', 1);
- $limit = $request->get('limit', 10);
- $status = $request->get('status', '');
- $where = [];
- if ($status != '') $where['status'] = explode(',', $status);
- $ids = UserVote::where('uid', $uid)->column('sub_vote_id');
- // var_dump($ids);
- $count = $ids ? VoteSub::where('id', 'in', $ids)->where($where)->count() : 0;
- // var_dump($count);
- $list = $ids ? VoteSub::where('id', 'in', $ids)->where($where)->order('add_time', 'desc')->page($page, $limit)->select()->each(function ($item) use ($uid) {
- $item['parent'] = Vote::where('id', $item['vote_id'])->find();
- $item['my'] = UserVote::where('sub_vote_id', $item['id'])->where('uid', $uid)->select();
- $item['now_voted'] = UserVote::where('sub_vote_id', $item['id'])->sum('vote_num');
- if ($uid) {
- $item['user_now_voted'] = UserVote::where('uid', $uid)->where('sub_vote_id', $item['id'])->sum('vote_num');
- $item['user_now_get'] = UserMoneyOrder::where('to_uid', $uid)->where('from_sub_vote', $item['id'])->sum('money');
- }
- $item['commission_type'] = sys_config('vote_commission_type', '');
- $item['commission_ratio'] = sys_config('vote_commission_ratio', 0);
- $item['_finish_time'] = date('Y-m-d H:i:s',$item['finish_time']);
- $item['_end_time'] = date('Y-m-d H:i:s',$item['end_time']);
- $item['_add_time'] = date('Y-m-d H:i:s',$item['add_time']);
- $item['_success_time'] = date('Y-m-d H:i:s',$item['success_time']);
- }) : [];
- $all_commission = 0;
- $today_commission = 0;
- if ($uid) {
- $all_commission = UserMoney::getComission($uid, 'USDT_ERC20');
- $today_commission = UserMoney::getComission($uid, 'USDT_ERC20', 0, 'today');
- }
- // var_dump(UserMoney::getLastSql());
- return app('json')->success('ok', compact('list', 'count', 'all_commission', 'today_commission'));
- }
- }
|