ApiShopCard.Class.php 5.8 KB

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