| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- namespace app\api\controller\vote;
- use app\admin\model\vote\Vote;
- use app\admin\model\vote\VoteJoin;
- use app\admin\model\vote\VoteOrder;
- use app\Request;
- use crmeb\repositories\OrderRepository;
- use crmeb\services\UtilService;
- class VoteController
- {
- public function lst(Request $request)
- {
- $where = UtilService::getMore([['page', 1], ['limit', 10]], $request);
- return app('json')->success('ok', Vote::getList($where));
- }
- public function detail($id, Request $request)
- {
- $info = Vote::valid()->find($id);
- if ($info) {
- $info->visit = $info->visit + 1;
- $info->save();
- $info = $info->toArray();
- $info['join_num'] = VoteJoin::valid()->where('vid', $id)->count();
- $info['vote_num'] = VoteJoin::valid()->where('vid', $id)->sum('vote');
- $info['my_status'] = VoteJoin::where('vid', $id)->where('uid', $request->uid())->find();
- if ($request->uid()) {
- $info['today_free_vote_num'] = VoteOrder::valid()->where('uid', $request->uid())->where('vid', $id)->where('pay_type', 'free')->whereTime('pay_time', 'today')->sum('vote_num');
- }
- }
- return app('json')->success('ok', $info);
- }
- public function join_list($id, Request $request)
- {
- $where = UtilService::getMore([['page', 1], ['limit', 10], ['key_word', ''], ['job', ''], ['rank', 0]], $request);
- $list = VoteJoin::getValidList($id, $where);
- return app('json')->success('ok', $list);
- }
- public function join_detail($id, Request $request)
- {
- $list = VoteJoin::getValidDetail($id);
- return app('json')->success('ok', ['data' => $list]);
- }
- public function join($id, Request $request)
- {
- $info = Vote::valid()->find($id);
- if (!$info) {
- return app('json')->fail('活动不存在');
- }
- // if ($info['start_time'] > time()) {
- // return app('json')->fail('活动未开始');
- // }
- if ($info['end_time'] < time()) {
- return app('json')->fail('活动已结束');
- }
- if (VoteJoin::be(['vid' => $id, 'uid' => $request->uid(), 'status' => [1, 0]])) {
- return app('json')->fail('已有报名申请或已经参加活动');
- }
- $data = UtilService::postMore([
- ['type', 1, '', '', 'not_empty_check', '请选择报名类型'],
- ['job', '', '', '', 'not_empty_check', '请填写职业'],
- ['name', '', '', '', 'not_empty_check', '请填写姓名'],
- ['phone', '', '', '', 'not_empty_check', '请填写联系方式话'],
- ['address', '', '', '', 'not_empty_check', '请填写地址'],
- ['avatar', '', '', '', 'not_empty_check', '请上传参赛头像'],
- ['detail', '', '', '', 'not_empty_check', '请填写个人介绍'],
- ['company', ''],
- ], $request);
- $data['vid'] = $id;
- $data['uid'] = $request->uid();
- if ($data['type'] == 2) {
- if (!$data['company']) return app('json')->fail('企业报名请输入企业名称');
- } else {
- unset($data['company']);
- }
- if (VoteJoin::where('vid', $id)->where('uid', $request->uid())->find()) {
- $data['status'] = 0;
- $data['mark'] = '';
- $res = VoteJoin::where('vid', $id)->where('uid', $request->uid())->update($data);
- } else {
- $data['add_time'] = time();
- $res = VoteJoin::create($data);
- }
- if ($res) {
- return app('json')->success('报名成功');
- } else {
- return app('json')->fail('报名失败');
- }
- }
- public function vote($id, Request $request)
- {
- if (!$id) return app('json')->fail('参数错误!');
- $uid = $request->uid();
- if (!VoteJoin::valid()->find($id)) return app('json')->fail('参数错误!');
- list($num, $payType, $from) = UtilService::postMore([
- 'num', 'payType', ['from', 'weixin']
- ], $request, true);
- $payType = strtolower($payType);
- if ($num <= 0) return app('json')->fail('投票张数不小于1!');
- $order = VoteOrder::createOrder($request->uid(), $id, $num, $payType);
- if ($order === false) return app('json')->fail(VoteOrder::getErrorInfo('订单生成失败'));
- $orderId = $order['order_id'];
- $info = ['order_id' => $orderId];
- if ($orderId) {
- switch ($payType) {
- case "weixin":
- $orderInfo = VoteOrder::where('order_id', $orderId)->find();
- if (!$orderInfo || !isset($orderInfo['paid'])) return app('json')->fail('支付订单不存在!');
- $orderInfo = $orderInfo->toArray();
- if ($orderInfo['paid']) return app('json')->fail('订单已支付!');
- //支付金额为0
- if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
- //创建订单jspay支付
- $payPriceStatus = VoteOrder::jsPayPrice($orderId, $uid);
- if ($payPriceStatus)//0元支付成功
- return app('json')->status('success', '微信支付成功');
- else
- return app('json')->status('pay_error', VoteOrder::getErrorInfo());
- } else {
- try {
- if ($from == 'routine') {
- $jsConfig = OrderRepository::jsVotePay($orderId); //创建订单jspay
- } else if ($from == 'weixinh5') {
- $jsConfig = OrderRepository::h5VotePay($orderId);
- } else {
- $jsConfig = OrderRepository::wxVotePay($orderId);
- }
- } catch (\Exception $e) {
- return app('json')->status('pay_error', $e->getMessage());
- }
- $info['jsConfig'] = $jsConfig;
- if ($from == 'weixinh5') {
- return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
- } else {
- return app('json')->status('wechat_pay', '订单创建成功', $info);
- }
- }
- case 'yue':
- if (VoteOrder::yuePay($orderId, $request->uid()))
- return app('json')->status('success', '余额支付成功', $info);
- else {
- $errorinfo = VoteOrder::getErrorInfo();
- if (is_array($errorinfo))
- return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
- else
- return app('json')->status('pay_error', $errorinfo);
- }
- break;
- case 'free':
- if (VoteOrder::freePay($orderId, $request->uid()))
- return app('json')->status('success', '投票成功', $info);
- else {
- $errorinfo = VoteOrder::getErrorInfo();
- if (is_array($errorinfo))
- return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
- else
- return app('json')->status('pay_error', $errorinfo);
- }
- break;
- case 'offline':
- return app('json')->status('success', '订单创建成功', $info);
- break;
- }
- } else return app('json')->fail(VoteOrder::getErrorInfo('订单生成失败!'));
- }
- }
|