AuctionProductController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace app\api\controller\auction;
  3. use app\models\auction\Auction;
  4. use app\models\auction\AuctionBooking;
  5. use app\models\auction\AuctionOrder;
  6. use app\models\auction\AuctionProduct;
  7. use app\models\auction\AuctionSection;
  8. use app\models\user\User;
  9. use app\models\user\UserBill;
  10. use app\Request;
  11. use crmeb\services\SystemConfigService;
  12. use crmeb\services\UtilService;
  13. use think\facade\Db;
  14. class AuctionProductController
  15. {
  16. /**
  17. * 购买随机返回一张订单
  18. * @param Request $request
  19. * @return void
  20. */
  21. public function purchase(Request $request)
  22. {
  23. $data = UtilService::postMore([
  24. ['id'],
  25. ['s_id']
  26. ]);
  27. $auction = Auction::find($data['id']);
  28. if (!$auction) return app('json')->fail('场次不存在');
  29. if (!$data['s_id']) return app('json')->fail('数据传入错误');
  30. $section = AuctionSection::where('id', $data['s_id'])->find();
  31. $price = (float)SystemConfigService::get('product_hanging_price');
  32. $pro = AuctionProduct::where('hanging_price', '>=', $section['low'])->where('hanging_price','<=','high')->select()->toArray();
  33. if (empty($pro) and ($price < $section['low'] or $price > $section['high'])) {
  34. return app('json')->fail('该区间没有商品');
  35. }
  36. if (AuctionOrder::where([['auction_id', '=', $data['id']], ['frequency', '=', $auction['frequency']], ['uid', '=', $request->uid()]])->count() >= 1) return app('json')->fail('当前场次已购买商品');
  37. if ($request->user()['is_real'] < 1){
  38. $orderCount = AuctionOrder::where([['auction_id', '=', $data['id']], ['frequency', '=', $auction['frequency']]])->count(); // 查找出当前场次已派单多少
  39. $pd = AuctionBooking::where([['auction_id', '=', $data['id']], ['frequency', '=', $auction['frequency']]])->count(); // 当前预约人数
  40. $pds = ceil($pd * ($auction['dispatch']/100));
  41. if ($orderCount >= $pds) return app('json')->fail('商品已买完');
  42. }
  43. $show = AuctionProduct::random($data['id'], $request->uid(),$section);
  44. if ($show == 'false') return app('json')->fail('购买失败');
  45. return app('json')->successful($show);
  46. }
  47. /**
  48. * 用户商品
  49. * @param Request $request
  50. * @return mixed
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function user_product(Request $request)
  56. {
  57. $data = UtilService::getMore([
  58. [['page', 'd'], 0],
  59. [['limit', 'd'], 0],
  60. ]);
  61. return app('json')->successful(AuctionProduct::user_product( $data,$request->uid()));
  62. }
  63. /**
  64. * 用户订单
  65. * @param Request $request
  66. * @return mixed
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\DbException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. */
  71. public function user_auction_order(Request $request)
  72. {
  73. $data = UtilService::getMore([
  74. [['type', 'd'], 0],
  75. [['page', 'd'], 0],
  76. [['limit', 'd'], 0],
  77. ['order_id']
  78. ]);
  79. return app('json')->successful(AuctionOrder::userOrder($data,$request->uid()));
  80. }
  81. /**
  82. * 卖家显示订单
  83. * @param Request $request
  84. * @return void
  85. */
  86. public function seller(Request $request)
  87. {
  88. $data = UtilService::getMore([
  89. ['type', 0],
  90. [['page', 'd'], 0],
  91. [['limit', 'd'], 0],
  92. ['order_id']
  93. ]);
  94. return app('json')->successful(AuctionOrder::seller_list($data,$request->uid()));
  95. }
  96. /**
  97. * 支付订单
  98. * @param Request $request
  99. * @return void
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. */
  104. public function zfpay(Request $request)
  105. {
  106. $data = UtilService::postMore([
  107. ['order_id'],
  108. ['pas']
  109. ]);
  110. if (empty($data['pas'])) return app('json')->fail('请填写支付密码');
  111. if ($request->user()['payment_pas'] != md5($data['pas'].'sxg')) return app('json')->fail('支付密码不正确');
  112. $model = new User();
  113. $order = AuctionOrder::where('order_id', $data['order_id'])->find();
  114. if (!$order) return app('json')->fail('订单不存在');
  115. if ($order['status'] == 0 or $order['status'] == 2) return app('json')->fail('订单状态不对');
  116. $user = $model->where('uid', $request->uid())->find();
  117. if ($user['now_money'] < $order['actual_price']) return app('json')->fail('余额不足');
  118. try {
  119. Db::startTrans();
  120. AuctionOrder::pay($order, $request->uid());
  121. Db::commit();
  122. return app('json')->successful('支付成功');
  123. } catch (\Exception $e) {
  124. Db::rollback();
  125. return app('json')->fail('支付失败');
  126. }
  127. }
  128. /**
  129. * 挂售详情
  130. * @param Request $request
  131. * @return mixed
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. public function gsxq(Request $request)
  137. {
  138. $data = UtilService::postMore([
  139. ['id']
  140. ]);
  141. $product = AuctionProduct::where('id', $data['id'])->find();
  142. $data['aid_val'] = round($product['hanging_price'] * 0.0485, 2); // 需要的广告值
  143. $data['hanging_price'] = round($product['hanging_price'] + $product['hanging_price'] * 0.06, 2); // 第二天溢价
  144. return app('json')->successful($data);
  145. }
  146. /**
  147. * 挂售商品
  148. * @param Request $request
  149. * @return mixed
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. public function hanging_sale(Request $request)
  155. {
  156. $data = UtilService::postMore([
  157. ['id'],
  158. ['pas']
  159. ]);
  160. if (empty($data['pas'])) return app('json')->fail('请填写支付密码');
  161. if ($request->user()['payment_pas'] != md5($data['pas'].'sxg')) return app('json')->fail('支付密码不正确');
  162. $product = AuctionProduct::where('id', $data['id'])->where('uid', $request->uid())->find();
  163. $recovery = (int)SystemConfigService::get('recovery');
  164. if ($product['hanging_price'] > $recovery) return app('json')->fail('到达最大阈值,不能挂售');
  165. if ($product['is_show'] == 1) return app('json')->fail('商品已挂售');
  166. $user = User::where('uid', $request->uid())->find();
  167. if (!$product) return app('json')->fail('商品不存在');
  168. $aid_val = round($product['hanging_price'] * 0.0485,2); // 需要的广告值
  169. $hanging_price = round($product['hanging_price'] + $product['hanging_price'] * 0.06, 2); // 第二天溢价
  170. if ($user['aid_val'] < $aid_val) return app('json')->fail('挂售需要广告值不足');
  171. $auction = Auction::where([['id', '=', $product['auction_id']]])->find();
  172. $bk = AuctionBooking::where([['uid', '=', $request->uid()], ['auction_id', '=', $auction['id']], ['frequency', '=', $auction['frequency']], ['status', '=', 1]])->find();
  173. if ($bk){
  174. $bk['status'] = 0; //解冻广告值
  175. $user['aid_val'] += $bk['advert']; // 退回用户广告值
  176. $bk->save();
  177. UserBill::income('广告值退回', $request->uid(), 'aid_val', 'add_aid_val', $bk['advert'], $user['spread_uid'], $user['aid_val'], '寄售商品成功退回广告值');
  178. }
  179. $product['hanging_price'] = $hanging_price;
  180. $product['is_show'] = 1;
  181. $product['gs_time'] = time();
  182. $user['aid_val'] -= $aid_val;
  183. try {
  184. User::beginTrans();
  185. $product->save();
  186. $user->save();
  187. UserBill::expend('挂售扣除广告值', $user['uid'], 'aid_val', 'sub_aid_val', $aid_val, $user['spread_uid'], $user['aid_val'], '挂售商品扣除广告值');
  188. User::commitTrans();
  189. return app('json')->successful('挂售成功');
  190. } catch (\Exception $e) {
  191. User::rollbackTrans();
  192. return app('json')->fail($e->getMessage());
  193. }
  194. }
  195. /**
  196. * 商品详情
  197. * @param Request $request
  198. * @return mixed
  199. * @throws \think\db\exception\DataNotFoundException
  200. * @throws \think\db\exception\DbException
  201. * @throws \think\db\exception\ModelNotFoundException
  202. *
  203. */
  204. public function product(Request $request)
  205. {
  206. $data = UtilService::getMore([
  207. ['id']
  208. ]);
  209. $product = AuctionProduct::where('id', $data['id'])->find();
  210. if (!$product) return app('json')->fail('没有此商品');
  211. $product['slider_image'] = is_string($product['slider_image']) ? json_decode($product['slider_image'], true) : [];
  212. $product['description'] = !empty($product['description']) ? html_entity_decode($product['description'], ENT_COMPAT) : '';
  213. $product = $product->toArray();
  214. return app('json')->successful($product);
  215. }
  216. }