OrderController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\merchant\services\MerchantExtendService;
  4. use bizGhs\admin\classes\AdminClass;
  5. use bizGhs\custom\classes\CustomClass;
  6. use bizGhs\merchant\services\ShopService;
  7. use bizGhs\order\classes\OrderClass;
  8. use bizGhs\order\services\OrderItemService;
  9. use bizGhs\order\services\OrderService;
  10. use biz\saas\services\RegionService;
  11. use biz\user\services\UserService;
  12. use bizGhs\product\classes\ProductClass;
  13. use common\components\configDict;
  14. use common\components\httpUtil;
  15. use common\services\xhPayToolService;
  16. use Yii;
  17. use common\components\util;
  18. use common\components\stringUtil;
  19. class OrderController extends BaseController
  20. {
  21. public $withoutLogin = ['order-relate', 'fast-pay'];
  22. //订单列表 shish 2021.1.20
  23. public function actionList()
  24. {
  25. $get = Yii::$app->request->get();
  26. $status = $get['status'] ?? 0;
  27. if (!empty($status)) {
  28. $where['status'] = $status;
  29. }
  30. $where['merchantId'] = $this->merchantId;
  31. if (isset($get['shopId'])) {
  32. $shopId = $get['shopId'] ?? 0;
  33. if (!empty($shopId)) {
  34. $where['shopId'] = $this->shopId;
  35. }
  36. } else {
  37. $where['shopId'] = $this->shopId;
  38. }
  39. $data = OrderClass::getOrderList($where);
  40. util::success($data);
  41. }
  42. //商城下单操作 shish 2021.1.14
  43. public function actionCreateOrder()
  44. {
  45. $post = Yii::$app->request->post();
  46. $adminId = $this->adminId;
  47. $shopId = $this->shopId;
  48. if (!empty($shopId)) {
  49. //供货商端 员工 开单
  50. $post['customId'] = $post['customId'] ?? 0;
  51. $post['adminId'] = $adminId;
  52. $post['userId'] = 0;
  53. } else {
  54. //供货商端 客户 开单
  55. $post['userId'] = $adminId;
  56. $post['adminId'] = 0;
  57. $post['customId'] = CustomClass::addCustom();
  58. }
  59. $post['payWay'] = $this->isWx == true ? 0 : 1;
  60. $post['merchantId'] = $this->merchantId;
  61. $post['shopId'] = 0;
  62. $post['deadline'] = time() + 1800;//订单30分钟后过期
  63. $post['fromType'] = 1;//商城
  64. $post['prePrice'] = 0.01;
  65. $post['actPrice'] = 0.01;
  66. $post['sendSide'] = $post['sendSide'] ?? 0;
  67. $productJson = $post['product'] ?? '';
  68. if (empty($productJson)) {
  69. util::fail('请选择花材');
  70. }
  71. $productList = json_decode($productJson, true);
  72. if (empty($productList)) {
  73. util::fail('请选择花材');
  74. }
  75. $productIds = array_column($productList, 'productId');
  76. ProductClass::valid($productIds, $this->merchantId);
  77. $post['product'] = $productList;
  78. $order = OrderClass::addOrder($post);
  79. $orderSn = $order['orderSn'] ?? '';
  80. $orderId = $order['id'] ?? 0;
  81. util::success([
  82. 'orderId' => $orderId,
  83. 'orderSn' => $orderSn,
  84. 'totalPrice' => 0.01,
  85. 'sendType' => '快递送',
  86. 'sendTime' => '15:00',
  87. 'qrCode' => Yii::$app->params['imgHost'] . '/hhb_small.png',
  88. ]);
  89. }
  90. //延期支付 shish 2021.1.19
  91. public function actionDebt()
  92. {
  93. $get = Yii::$app->request->get();
  94. $orderId = $get['orderId'] ?? 0;
  95. util::complete();
  96. }
  97. //获取商城下单要用的微信支付和小程序支付参数 shish 2019.21.3
  98. public function actionWxPay()
  99. {
  100. ini_set('date.timezone', 'Asia/Shanghai');
  101. $post = Yii::$app->request->post();
  102. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  103. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  104. $order = OrderClass::getByOrderSn($orderSn);
  105. $orderId = $order['id'];
  106. //支付前验证订单有效性
  107. OrderClass::valid($order, $this->adminId);
  108. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  109. $totalFee = $order['actPrice'];
  110. //小程序使用miniOpenId
  111. if (httpUtil::isMiniProgram()) {
  112. $openId = $this->user['miniOpenId'];
  113. } else {
  114. $openId = $this->user['openId'];
  115. }
  116. $typeList = configDict::getConfig('capitalType');
  117. $capitalType = $typeList['xhOrder']['id'];
  118. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  119. $wxPayType = 0;
  120. if (httpUtil::isMiniProgram()) {
  121. $wxPayType = 1;
  122. }
  123. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  124. //订单30分钟后过期
  125. $now = time();
  126. $expireTime = $now + 1800;
  127. $wx = Yii::getAlias("@vendor/weixin");
  128. require_once($wx . '/lib/WxPay.Api.php');
  129. require_once($wx . '/example/WxPay.JsApiPay.php');
  130. $input = new \WxPayUnifiedOrder();
  131. $input->SetBody($name);
  132. $input->SetOut_trade_no($orderSn);
  133. $input->SetTotal_fee($totalFee * 100);
  134. $input->SetTime_start(date("YmdHis", $now));
  135. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  136. $input->SetTime_expire(date("YmdHis", $expireTime));
  137. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  138. $input->SetTrade_type("JSAPI");
  139. //花卉宝代为申请的微信支付
  140. $merchantExtend = $this->merchantExtend;
  141. if ($merchantExtend['wxPayApply'] == 1) {
  142. $input->SetSub_openid($openId);
  143. } else {
  144. $input->SetOpenid($openId);
  145. }
  146. //小程序使用miniAppId
  147. if (httpUtil::isMiniProgram()) {
  148. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  149. }
  150. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  151. $tools = new \JsApiPay();
  152. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  153. $newParams = json_decode($jsApiParameters, true);
  154. //已请求微信不能修改价格
  155. $updateData = [];
  156. $updateData['modPrice'] = 0;
  157. if (empty($order['deadline'])) {
  158. $updateData['deadline'] = $expireTime;
  159. }
  160. OrderService::updateById($orderId, $updateData);
  161. util::success($newParams);
  162. }
  163. //已经支付
  164. public function actionHasPay()
  165. {
  166. util::complete();
  167. }
  168. //获取订单详情 shish 2021.1.21
  169. public function actionDetail()
  170. {
  171. $get = Yii::$app->request->get();
  172. $id = $get['id'] ?? 0;
  173. $info = OrderClass::getOrderInfo($id, true, true);
  174. OrderClass::valid($info, $this->shopId);
  175. util::success($info);
  176. }
  177. //重选花材 shish 2021.1.22
  178. public function actionReplaceProduct()
  179. {
  180. $post = Yii::$app->request->post();
  181. $id = $post['id'] ?? 0;
  182. $product = $post['product'] ?? '[]';
  183. $product = json_decode($product, true);
  184. //检查花材是否都是同家门店的
  185. ProductClass::valid($product, $this->shopId);
  186. $info = OrderClass::getOrderInfo($id);
  187. OrderClass::valid($info, $this->shopId);
  188. OrderItemService::replaceProduct($info, $product);
  189. util::complete();
  190. }
  191. }