UserLevelController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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);
  80. if ($order === false) return app('json')->fail(UserLevelOrder::getErrorInfo('订单生成失败'));
  81. $orderId = $order['order_id'];
  82. // $info = compact('orderId', 'key');
  83. $info = $orderId;
  84. if ($orderId) {
  85. event('OrderCreated', [$order]); //订单创建成功事件
  86. // event('ShortMssageSend', [$orderId, 'AdminPlaceAnOrder']);//发送管理员通知
  87. switch ($payType) {
  88. case "weixin":
  89. $orderInfo = UserLevelOrder::where('order_id', $orderId)->find();
  90. if (!$orderInfo || !isset($orderInfo['paid'])) return app('json')->fail('支付订单不存在!');
  91. $orderInfo = $orderInfo->toArray();
  92. if ($orderInfo['paid']) return app('json')->fail('支付已支付!');
  93. //支付金额为0
  94. if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
  95. //创建订单jspay支付
  96. $payPriceStatus = UserLevelOrder::jsPayPrice($orderId, $uid, $formId);
  97. if ($payPriceStatus)//0元支付成功
  98. return app('json')->status('success', '微信支付成功', $info);
  99. else
  100. return app('json')->status('pay_error', UserLevelOrder::getErrorInfo());
  101. } else {
  102. try {
  103. if ($from == 'routine') {
  104. $jsConfig = OrderRepository::jsPay($orderId); //创建订单jspay
  105. } else if ($from == 'weixinh5') {
  106. $jsConfig = OrderRepository::h5Pay($orderId);
  107. } else {
  108. $jsConfig = OrderRepository::wxPay($orderId);
  109. }
  110. } catch (\Exception $e) {
  111. return app('json')->status('pay_error', $e->getMessage(), $info);
  112. }
  113. $info['jsConfig'] = $jsConfig;
  114. if ($from == 'weixinh5') {
  115. return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
  116. } else {
  117. return app('json')->status('wechat_pay', '订单创建成功', $info);
  118. }
  119. }
  120. break;
  121. case 'yue':
  122. if (UserLevelOrder::yuePay($orderId, $request->uid(), $formId))
  123. return app('json')->status('success', '余额支付成功', $info);
  124. else {
  125. $errorinfo = UserLevelOrder::getErrorInfo();
  126. if (is_array($errorinfo))
  127. return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
  128. else
  129. return app('json')->status('pay_error', $errorinfo);
  130. }
  131. break;
  132. case 'offline':
  133. return app('json')->status('success', '订单创建成功', $info);
  134. break;
  135. }
  136. } else return app('json')->fail(UserLevelOrder::getErrorInfo('订单生成失败!'));
  137. }
  138. }