OrderController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. util::complete();
  95. }
  96. //已经支付
  97. public function actionHasPay()
  98. {
  99. $get = Yii::$app->request->get();
  100. util::complete();
  101. }
  102. //获取商城下单要用的微信支付和小程序支付参数 shish 2019.21.3
  103. public function actionWxPay()
  104. {
  105. ini_set('date.timezone', 'Asia/Shanghai');
  106. $post = Yii::$app->request->post();
  107. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  108. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  109. $order = OrderClass::getByOrderSn($orderSn);
  110. $orderId = $order['id'];
  111. //支付前验证订单有效性
  112. OrderClass::valid($order, $this->adminId);
  113. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  114. $totalFee = $order['actPrice'];
  115. //小程序使用miniOpenId
  116. if (httpUtil::isMiniProgram()) {
  117. $openId = $this->user['miniOpenId'];
  118. } else {
  119. $openId = $this->user['openId'];
  120. }
  121. $typeList = configDict::getConfig('capitalType');
  122. $capitalType = $typeList['xhOrder']['id'];
  123. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  124. $wxPayType = 0;
  125. if (httpUtil::isMiniProgram()) {
  126. $wxPayType = 1;
  127. }
  128. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  129. //订单30分钟后过期
  130. $now = time();
  131. $expireTime = $now + 1800;
  132. $wx = Yii::getAlias("@vendor/weixin");
  133. require_once($wx . '/lib/WxPay.Api.php');
  134. require_once($wx . '/example/WxPay.JsApiPay.php');
  135. $input = new \WxPayUnifiedOrder();
  136. $input->SetBody($name);
  137. $input->SetOut_trade_no($orderSn);
  138. $input->SetTotal_fee($totalFee * 100);
  139. $input->SetTime_start(date("YmdHis", $now));
  140. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  141. $input->SetTime_expire(date("YmdHis", $expireTime));
  142. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  143. $input->SetTrade_type("JSAPI");
  144. //花卉宝代为申请的微信支付
  145. $merchantExtend = $this->merchantExtend;
  146. if ($merchantExtend['wxPayApply'] == 1) {
  147. $input->SetSub_openid($openId);
  148. } else {
  149. $input->SetOpenid($openId);
  150. }
  151. //小程序使用miniAppId
  152. if (httpUtil::isMiniProgram()) {
  153. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  154. }
  155. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  156. $tools = new \JsApiPay();
  157. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  158. $newParams = json_decode($jsApiParameters, true);
  159. //已请求微信不能修改价格
  160. $updateData = [];
  161. $updateData['modPrice'] = 0;
  162. if (empty($order['deadline'])) {
  163. $updateData['deadline'] = $expireTime;
  164. }
  165. OrderService::updateById($orderId, $updateData);
  166. util::success($newParams);
  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 actionChooseProduct()
  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. //添加修改花材
  189. OrderItemService::replace($info, $product);
  190. util::complete();
  191. }
  192. //自取 shish 2021.1.22
  193. public function actionSelfGet()
  194. {
  195. $get = Yii::$app->request->get();
  196. $id = $get['id'] ?? 0;
  197. util::complete();
  198. }
  199. }