123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- namespace app\api\controller\auction;
- use app\models\auction\Auction;
- use app\models\auction\AuctionBooking;
- use app\models\auction\AuctionOrder;
- use app\models\auction\AuctionProduct;
- use app\models\auction\AuctionSection;
- use app\models\user\User;
- use app\models\user\UserBill;
- use app\Request;
- use crmeb\services\SystemConfigService;
- use crmeb\services\UtilService;
- use think\facade\Db;
- class AuctionProductController
- {
- /**
- * 购买随机返回一张订单
- * @param Request $request
- * @return void
- */
- public function purchase(Request $request)
- {
- $data = UtilService::postMore([
- ['id'],
- ['s_id']
- ]);
- $auction = Auction::find($data['id']);
- if (!$auction) return app('json')->fail('场次不存在');
- if (!$data['s_id']) return app('json')->fail('数据传入错误');
- $section = AuctionSection::where('id', $data['s_id'])->find();
- $price = (float)SystemConfigService::get('product_hanging_price');
- $pro = AuctionProduct::where('hanging_price', '>=', $section['low'])->where('hanging_price','<=','high')->select()->toArray();
- if (empty($pro) and ($price < $section['low'] or $price > $section['high'])) {
- return app('json')->fail('该区间没有商品');
- }
- if (AuctionOrder::where([['auction_id', '=', $data['id']], ['frequency', '=', $auction['frequency']], ['uid', '=', $request->uid()]])->count() >= 1) return app('json')->fail('当前场次已购买商品');
- if ($request->user()['is_real'] < 1){
- $orderCount = AuctionOrder::where([['auction_id', '=', $data['id']], ['frequency', '=', $auction['frequency']]])->count(); // 查找出当前场次已派单多少
- $pd = AuctionBooking::where([['auction_id', '=', $data['id']], ['frequency', '=', $auction['frequency']]])->count(); // 当前预约人数
- $pds = ceil($pd * ($auction['dispatch']/100));
- if ($orderCount >= $pds) return app('json')->fail('商品已买完');
- }
- $show = AuctionProduct::random($data['id'], $request->uid(),$section);
- if ($show == 'false') return app('json')->fail('购买失败');
- return app('json')->successful($show);
- }
- /**
- * 用户商品
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function user_product(Request $request)
- {
- $data = UtilService::getMore([
- [['page', 'd'], 0],
- [['limit', 'd'], 0],
- ]);
- return app('json')->successful(AuctionProduct::user_product( $data,$request->uid()));
- }
- /**
- * 用户订单
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function user_auction_order(Request $request)
- {
- $data = UtilService::getMore([
- [['type', 'd'], 0],
- [['page', 'd'], 0],
- [['limit', 'd'], 0],
- ['order_id']
- ]);
- return app('json')->successful(AuctionOrder::userOrder($data,$request->uid()));
- }
- /**
- * 卖家显示订单
- * @param Request $request
- * @return void
- */
- public function seller(Request $request)
- {
- $data = UtilService::getMore([
- ['type', 0],
- [['page', 'd'], 0],
- [['limit', 'd'], 0],
- ['order_id']
- ]);
- return app('json')->successful(AuctionOrder::seller_list($data,$request->uid()));
- }
- /**
- * 支付订单
- * @param Request $request
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function zfpay(Request $request)
- {
- $data = UtilService::postMore([
- ['order_id'],
- ['pas']
- ]);
- if (empty($data['pas'])) return app('json')->fail('请填写支付密码');
- if ($request->user()['payment_pas'] != md5($data['pas'].'sxg')) return app('json')->fail('支付密码不正确');
- $model = new User();
- $order = AuctionOrder::where('order_id', $data['order_id'])->find();
- if (!$order) return app('json')->fail('订单不存在');
- if ($order['status'] == 0 or $order['status'] == 2) return app('json')->fail('订单状态不对');
- $user = $model->where('uid', $request->uid())->find();
- if ($user['now_money'] < $order['actual_price']) return app('json')->fail('余额不足');
- try {
- Db::startTrans();
- AuctionOrder::pay($order, $request->uid());
- Db::commit();
- return app('json')->successful('支付成功');
- } catch (\Exception $e) {
- Db::rollback();
- return app('json')->fail('支付失败');
- }
- }
- /**
- * 挂售详情
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function gsxq(Request $request)
- {
- $data = UtilService::postMore([
- ['id']
- ]);
- $product = AuctionProduct::where('id', $data['id'])->find();
- $data['aid_val'] = round($product['hanging_price'] * 0.0485, 2); // 需要的广告值
- $data['hanging_price'] = round($product['hanging_price'] + $product['hanging_price'] * 0.06, 2); // 第二天溢价
- return app('json')->successful($data);
- }
- /**
- * 挂售商品
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function hanging_sale(Request $request)
- {
- $data = UtilService::postMore([
- ['id'],
- ['pas']
- ]);
- if (empty($data['pas'])) return app('json')->fail('请填写支付密码');
- if ($request->user()['payment_pas'] != md5($data['pas'].'sxg')) return app('json')->fail('支付密码不正确');
- $product = AuctionProduct::where('id', $data['id'])->where('uid', $request->uid())->find();
- $recovery = (int)SystemConfigService::get('recovery');
- if ($product['hanging_price'] > $recovery) return app('json')->fail('到达最大阈值,不能挂售');
- if ($product['is_show'] == 1) return app('json')->fail('商品已挂售');
- $user = User::where('uid', $request->uid())->find();
- if (!$product) return app('json')->fail('商品不存在');
- $aid_val = round($product['hanging_price'] * 0.0485,2); // 需要的广告值
- $hanging_price = round($product['hanging_price'] + $product['hanging_price'] * 0.06, 2); // 第二天溢价
- if ($user['aid_val'] < $aid_val) return app('json')->fail('挂售需要广告值不足');
- $auction = Auction::where([['id', '=', $product['auction_id']]])->find();
- $bk = AuctionBooking::where([['uid', '=', $request->uid()], ['auction_id', '=', $auction['id']], ['frequency', '=', $auction['frequency']], ['status', '=', 1]])->find();
- if ($bk){
- $bk['status'] = 0; //解冻广告值
- $user['aid_val'] += $bk['advert']; // 退回用户广告值
- $bk->save();
- UserBill::income('广告值退回', $request->uid(), 'aid_val', 'add_aid_val', $bk['advert'], $user['spread_uid'], $user['aid_val'], '寄售商品成功退回广告值');
- }
- $product['hanging_price'] = $hanging_price;
- $product['is_show'] = 1;
- $product['gs_time'] = time();
- $user['aid_val'] -= $aid_val;
- try {
- User::beginTrans();
- $product->save();
- $user->save();
- UserBill::expend('挂售扣除广告值', $user['uid'], 'aid_val', 'sub_aid_val', $aid_val, $user['spread_uid'], $user['aid_val'], '挂售商品扣除广告值');
- User::commitTrans();
- return app('json')->successful('挂售成功');
- } catch (\Exception $e) {
- User::rollbackTrans();
- return app('json')->fail($e->getMessage());
- }
- }
- /**
- * 商品详情
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- *
- */
- public function product(Request $request)
- {
- $data = UtilService::getMore([
- ['id']
- ]);
- $product = AuctionProduct::where('id', $data['id'])->find();
- if (!$product) return app('json')->fail('没有此商品');
- $product['slider_image'] = is_string($product['slider_image']) ? json_decode($product['slider_image'], true) : [];
- $product['description'] = !empty($product['description']) ? html_entity_decode($product['description'], ENT_COMPAT) : '';
- $product = $product->toArray();
- return app('json')->successful($product);
- }
- }
|