UserLevelController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\models\store\StoreBargainUser;
  4. use app\models\store\StoreOrder;
  5. use app\models\store\StorePink;
  6. use app\models\system\SystemUserLevel;
  7. use app\models\system\SystemUserTask;
  8. use app\models\user\UserLevel;
  9. use app\models\user\UserLevelOrder;
  10. use app\Request;
  11. use crmeb\repositories\OrderRepository;
  12. use crmeb\services\UtilService;
  13. /**
  14. * 会员等级类
  15. * Class UserLevelController
  16. * @package app\api\controller\user
  17. */
  18. class UserLevelController
  19. {
  20. /**
  21. * 检测用户是否可以成为会员
  22. * @param Request $request
  23. * @return mixed
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. */
  27. public function detection(Request $request)
  28. {
  29. UserLevel::beginTrans();
  30. $res = UserLevel::setLevelComplete($request->uid());
  31. UserLevel::checkTrans($res);
  32. return app('json')->successful($res);
  33. }
  34. /**
  35. * 会员等级列表
  36. * @param Request $request
  37. * @return mixed
  38. */
  39. public function grade(Request $request)
  40. {
  41. return app('json')->successful(SystemUserLevel::getLevelList($request->uid()));
  42. }
  43. /**
  44. * 获取等级任务
  45. * @param Request $request
  46. * @param $id
  47. * @return mixed
  48. */
  49. public function task(Request $request, $id)
  50. {
  51. return app('json')->successful(SystemUserTask::getTashList($id, $request->uid()));
  52. }
  53. /**
  54. * 等级购买订单创建
  55. * @param Request $request
  56. * @param $key
  57. * @return mixed
  58. * @throws \think\Exception
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. * @throws \think\exception\DbException
  62. */
  63. public function create(Request $request, $level)
  64. {
  65. if (!$level) return app('json')->fail('参数错误!');
  66. $uid = $request->uid();
  67. // if (StoreOrder::be(['order_id|unique' => $key, 'uid' => $uid, 'is_del' => 0]))
  68. // return app('json')->status('extend_order', '订单已生成', ['orderId' => $key, 'key' => $key]);
  69. list($addressId, $couponId, $payType, $useIntegral, $mark, $combinationId, $pinkId, $seckill_id, $formId, $bargainId, $from, $shipping_type, $real_name, $phone, $storeId) = UtilService::postMore([
  70. 'addressId', 'couponId', 'payType', ['useIntegral', 0], 'mark', ['combinationId', 0], ['pinkId', 0], ['seckill_id', 0], ['formId', ''], ['bargainId', ''], ['from', 'weixin'],
  71. ['shipping_type', 1], ['real_name', ''], ['phone', ''], ['store_id', 0]
  72. ], $request, true);
  73. $payType = strtolower($payType);
  74. $isChannel = 1;
  75. if ($from == 'weixin')
  76. $isChannel = 0;
  77. elseif ($from == 'weixinh5')
  78. $isChannel = 2;
  79. $order = UserLevelOrder::CreateOrder($request->uid(), $addressId, $payType, (int)$useIntegral, $couponId, $mark, $combinationId, $pinkId, $seckill_id, $bargainId, false, $isChannel, $shipping_type, $real_name, $phone, $storeId);
  80. if ($order === false) return app('json')->fail(StoreOrder::getErrorInfo('订单生成失败'));
  81. $orderId = $order['order_id'];
  82. $info = compact('orderId', 'key');
  83. if ($orderId) {
  84. event('OrderCreated', [$order]); //订单创建成功事件
  85. event('ShortMssageSend', [$orderId, 'AdminPlaceAnOrder']);//发送管理员通知
  86. switch ($payType) {
  87. case "weixin":
  88. $orderInfo = StoreOrder::where('order_id', $orderId)->find();
  89. if (!$orderInfo || !isset($orderInfo['paid'])) return app('json')->fail('支付订单不存在!');
  90. $orderInfo = $orderInfo->toArray();
  91. if ($orderInfo['paid']) return app('json')->fail('支付已支付!');
  92. //支付金额为0
  93. if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
  94. //创建订单jspay支付
  95. $payPriceStatus = StoreOrder::jsPayPrice($orderId, $uid, $formId);
  96. if ($payPriceStatus)//0元支付成功
  97. return app('json')->status('success', '微信支付成功', $info);
  98. else
  99. return app('json')->status('pay_error', StoreOrder::getErrorInfo());
  100. } else {
  101. try {
  102. if ($from == 'routine') {
  103. $jsConfig = OrderRepository::jsPay($orderId); //创建订单jspay
  104. } else if ($from == 'weixinh5') {
  105. $jsConfig = OrderRepository::h5Pay($orderId);
  106. } else {
  107. $jsConfig = OrderRepository::wxPay($orderId);
  108. }
  109. } catch (\Exception $e) {
  110. return app('json')->status('pay_error', $e->getMessage(), $info);
  111. }
  112. $info['jsConfig'] = $jsConfig;
  113. if ($from == 'weixinh5') {
  114. return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
  115. } else {
  116. return app('json')->status('wechat_pay', '订单创建成功', $info);
  117. }
  118. }
  119. break;
  120. case 'yue':
  121. if (StoreOrder::yuePay($orderId, $request->uid(), $formId))
  122. return app('json')->status('success', '余额支付成功', $info);
  123. else {
  124. $errorinfo = StoreOrder::getErrorInfo();
  125. if (is_array($errorinfo))
  126. return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
  127. else
  128. return app('json')->status('pay_error', $errorinfo);
  129. }
  130. break;
  131. case 'offline':
  132. return app('json')->status('success', '订单创建成功', $info);
  133. break;
  134. }
  135. } else return app('json')->fail(StoreOrder::getErrorInfo('订单生成失败!'));
  136. }
  137. }