ApiShopCard.Class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace JinDouYun\Controller\Shop;
  3. use JinDouYun\Controller\BaseController;
  4. use JinDouYun\Dao\BaseDao;
  5. use JinDouYun\Dao\Customer\DCustomerCardNum;
  6. use JinDouYun\Model\Customer\MCustomer;
  7. use JinDouYun\Model\Customer\MCustomerCard;
  8. use JinDouYun\Model\Shop\MShopCard;
  9. use JinDouYun\Model\Shop\MShopCardBind;
  10. use JinDouYun\Model\Shop\MShopCardOrder;
  11. use JinDouYun\Model\Shop\MShopProject;
  12. use JinDouYun\Model\Shop\MShopRostering;
  13. use JinDouYun\Model\Shop\MShopSubscribe;
  14. use Mall\Framework\Core\ErrorCode;
  15. use Mall\Framework\Core\Ding;
  16. use Mall\Framework\Factory;
  17. class ApiShopCard extends BaseController
  18. {
  19. private $obj;
  20. private $objCardBind;
  21. private $objCardOrder;
  22. private $objCardNum;
  23. private $objProject;
  24. private $objCustomer;
  25. public function __construct($isCheckAcl = true, $isMustLogin = false, $checkToken = true, $getAreaCode = false, $checkShopToken = true, $checkSupplierToken = false)
  26. {
  27. parent::__construct($isCheckAcl, $isMustLogin, $checkToken, $getAreaCode, $checkShopToken, $checkSupplierToken);
  28. $authorization = $this->request->getServerParam('HTTP_AUTHORIZATION');
  29. if (!empty($authorization)) {
  30. self::getUserIdByAuthorization();
  31. }
  32. $this->obj = new MShopCard($this->onlineEnterpriseId, $this->onlineUserId);
  33. $this->objCardBind = new MShopCardBind($this->onlineEnterpriseId);
  34. $this->objCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId);
  35. $this->objCardOrder = new MShopCardOrder($this->onlineEnterpriseId, $this->onlineUserId);
  36. $this->objProject = new MShopProject($this->onlineEnterpriseId, $this->onlineUserId);
  37. $this->objCardNum = new DCustomerCardNum('default');
  38. }
  39. /**
  40. * 购买会员卡
  41. * @return void
  42. */
  43. public function buy_card()
  44. {
  45. $id = $this->request->param('id');
  46. $pay_type = $this->request->param('pay_type');
  47. $source = $this->request->param('source');
  48. $card = $this->obj->details(['id' => $id]);
  49. if (empty($this->onlineUserId)) parent::sendOutput('未登录', ErrorCode::$dberror);
  50. if (!$card->isSuccess()) parent::sendOutput('该优惠卡不存在', ErrorCode::$dberror);
  51. $card = $card->getData();
  52. $base = new BaseDao();
  53. $base->beginTransaction();
  54. $customer = $this->objCustomer->get(['userCenterId' => $this->onlineUserId]);
  55. $orderInsert = [
  56. 'enterprise_id' => $this->onlineEnterpriseId,
  57. 'card_id' => $card['id'],
  58. 'customer_id' => $customer['id'],
  59. 'order_id' => 'car'.$customer['id'].time().$this->getRandPass(),
  60. 'ot_price' => $card['ot_price'],
  61. 'pay_price' => $card['price'],
  62. 'pay_type' => $pay_type,
  63. ];
  64. $order = $this->objCardOrder->insert($orderInsert);
  65. if (!$order->isSuccess()){
  66. parent::sendOutput('订单生成失败', ErrorCode::$dberror);
  67. }
  68. if ($pay_type == 1){
  69. //余额支付
  70. $res = $this->objCardOrder->yuePay($orderInsert);
  71. if (!$res->isSuccess()){
  72. parent::sendOutput($res->getData(), ErrorCode::$dberror);
  73. }
  74. $base->commit();
  75. parent::sendOutput(['status' => 'complete', 'data' => '支付成功']);
  76. }elseif ($pay_type == 2){
  77. //微信支付
  78. $res = $this->objCardOrder->wxPay($orderInsert, $this->request->get_onlineip(), $source);
  79. $base->commit();
  80. parent::sendOutput(['status' => 'weixinPay', 'data' => $res->getData()]);
  81. }elseif ($pay_type == 3){
  82. //支付宝支付
  83. parent::sendOutput('支付方式错误', ErrorCode::$dberror);
  84. }
  85. $base->rollBack();
  86. parent::sendOutput('支付方式错误', ErrorCode::$dberror);
  87. }
  88. /**用户优惠卡订单
  89. * @return void
  90. */
  91. public function card_order()
  92. {
  93. $uid = $this->onlineUserId;
  94. if (empty($uid)) parent::sendOutput('未登录', 1002);
  95. $params = $this->request->getRawJson();
  96. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  97. $selectParams['limit'] = $pageParams['limit'];
  98. $selectParams['offset'] = $pageParams['offset'];
  99. $customer = $this->objCustomer->get(['userCenterId' => $uid]);
  100. $selectParams['customer_id'] = $customer['id'];
  101. $selectParams['paid'] = 1;
  102. $list = $this->objCardOrder->list($selectParams)->getData();
  103. $pageData = [
  104. 'pageIndex' => $params['page'],
  105. 'pageSize' => $params['pageSize'],
  106. 'pageTotal' => $list['total'],
  107. ];
  108. parent::sendOutput($list['data'], 0, $pageData);
  109. }
  110. /**
  111. * 用户拥有优惠券列表
  112. * @return void
  113. */
  114. public function userCard()
  115. {
  116. $params = $this->request->getRawJson();
  117. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  118. $limit = $pageParams['limit'];
  119. $offset = $pageParams['offset'];
  120. $uid = $this->onlineUserId;
  121. if (empty($uid)) parent::sendOutput('未登录', 1002);
  122. $customer = $this->objCustomer->get(['userCenterId' => $uid]);
  123. $selectParams['customer_id'] = $customer['id'];
  124. $selectParams['enterprise_id'] = $this->onlineEnterpriseId;
  125. $list = $this->objCardNum->select($selectParams, '*', 'id DESC', $limit, $offset);
  126. $count = $this->objCardNum->count($selectParams);
  127. foreach ($list as &$item){
  128. $project = $this->objProject->details(['id' => $item['project_id']])->getData();
  129. $item['name'] = $project['name'];
  130. $item['image'] = $project['image'];
  131. }
  132. $pageData = [
  133. 'pageIndex' => $params['page'],
  134. 'pageSize' => $params['pageSize'],
  135. 'pageTotal' => $count,
  136. ];
  137. parent::sendOutput($list, 0, $pageData);
  138. }
  139. }