OrderController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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\classes\ShopClass;
  7. use bizGhs\merchant\services\ShopService;
  8. use bizGhs\order\classes\OrderClass;
  9. use bizGhs\order\services\OrderItemService;
  10. use bizGhs\order\services\OrderService;
  11. use biz\saas\services\RegionService;
  12. use biz\user\services\UserService;
  13. use bizGhs\product\classes\ProductClass;
  14. use common\components\configDict;
  15. use common\components\httpUtil;
  16. use common\services\xhPayToolService;
  17. use Yii;
  18. use common\components\util;
  19. use common\components\stringUtil;
  20. class OrderController extends BaseController
  21. {
  22. public $withoutLogin = ['order-relate', 'fast-pay'];
  23. //订单列表 shish 2021.1.20
  24. public function actionList()
  25. {
  26. $get = Yii::$app->request->get();
  27. $status = $get['status'] ?? 0;
  28. if (!empty($status)) {
  29. $where['status'] = $status;
  30. }
  31. $where['merchantId'] = $this->merchantId;
  32. if (isset($get['shopId'])) {
  33. $shopId = $get['shopId'] ?? 0;
  34. if (!empty($shopId)) {
  35. $where['shopId'] = $this->shopId;
  36. }
  37. } else {
  38. $where['shopId'] = $this->shopId;
  39. }
  40. $data = OrderClass::getOrderList($where);
  41. util::success($data);
  42. }
  43. //商城下单操作 shish 2021.1.14
  44. public function actionCreateOrder()
  45. {
  46. $post = Yii::$app->request->post();
  47. $adminId = $this->adminId;
  48. $shopId = $this->shopId;
  49. if (!empty($shopId)) {
  50. //供货商端 员工 开单
  51. $post['customId'] = $post['customId'] ?? 0;
  52. $post['adminId'] = $adminId;
  53. $post['userId'] = 0;
  54. $post['shopId'] = $shopId;
  55. } else {
  56. //供货商端 客户 开单
  57. $post['userId'] = $adminId;
  58. $post['adminId'] = 0;
  59. $shopId = $post['shopId'] ?? 0;
  60. if (empty($shopId)) {
  61. util::fail('请选择供货商');
  62. }
  63. $post['shopId'] = $shopId;
  64. $shop = ShopClass::getById($shopId);
  65. if (empty($shop)) {
  66. util::fail('没有找到供货商');
  67. }
  68. }
  69. $post['payWay'] = $this->isWx == true ? 0 : 1;
  70. $post['merchantId'] = $this->merchantId;
  71. $post['fromType'] = 1;//商城
  72. $post['prePrice'] = 0.01;
  73. $post['actPrice'] = 0.01;
  74. $post['sendSide'] = $post['sendSide'] ?? 0;
  75. $productJson = $post['product'] ?? '';
  76. if (empty($productJson)) {
  77. util::fail('请选择花材');
  78. }
  79. $productList = json_decode($productJson, true);
  80. if (empty($productList)) {
  81. util::fail('请选择花材');
  82. }
  83. $orderSn = '';
  84. $orderId = 0;
  85. $connection = Yii::$app->db;//事务处理
  86. $transaction = $connection->beginTransaction();
  87. try {
  88. $productIds = array_column($productList, 'productId');
  89. ProductClass::valid($productIds, $this->shopId);
  90. $post['product'] = $productList;
  91. $order = OrderClass::addOrder($post);
  92. $orderSn = $order['orderSn'] ?? '';
  93. $orderId = $order['id'] ?? 0;
  94. $transaction->commit();
  95. } catch (Exception $e) {
  96. $transaction->rollBack();
  97. util::fail('下单失败');
  98. }
  99. util::success([
  100. 'orderId' => $orderId,
  101. 'orderSn' => $orderSn,
  102. 'totalPrice' => 0.01,
  103. 'sendType' => '快递送',
  104. 'sendTime' => '15:00',
  105. 'qrCode' => Yii::$app->params['imgHost'] . '/hhb_small.png',
  106. ]);
  107. }
  108. //延期支付 shish 2021.1.19
  109. public function actionDebt()
  110. {
  111. $get = Yii::$app->request->get();
  112. util::complete();
  113. }
  114. //已经支付
  115. public function actionHasPay()
  116. {
  117. $get = Yii::$app->request->get();
  118. util::complete();
  119. }
  120. //获取商城下单要用的微信支付和小程序支付参数 shish 2019.21.3
  121. public function actionWxPay()
  122. {
  123. ini_set('date.timezone', 'Asia/Shanghai');
  124. $post = Yii::$app->request->post();
  125. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  126. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  127. $order = OrderClass::getByOrderSn($orderSn);
  128. $orderId = $order['id'];
  129. //支付前验证订单有效性
  130. OrderClass::valid($order, $this->adminId);
  131. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  132. $totalFee = $order['actPrice'];
  133. //小程序使用miniOpenId
  134. if (httpUtil::isMiniProgram()) {
  135. $openId = $this->user['miniOpenId'];
  136. } else {
  137. $openId = $this->user['openId'];
  138. }
  139. $typeList = configDict::getConfig('capitalType');
  140. $capitalType = $typeList['xhOrder']['id'];
  141. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  142. $wxPayType = 0;
  143. if (httpUtil::isMiniProgram()) {
  144. $wxPayType = 1;
  145. }
  146. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  147. //订单30分钟后过期
  148. $now = time();
  149. $expireTime = $now + 1800;
  150. $wx = Yii::getAlias("@vendor/weixin");
  151. require_once($wx . '/lib/WxPay.Api.php');
  152. require_once($wx . '/example/WxPay.JsApiPay.php');
  153. $input = new \WxPayUnifiedOrder();
  154. $input->SetBody($name);
  155. $input->SetOut_trade_no($orderSn);
  156. $input->SetTotal_fee($totalFee * 100);
  157. $input->SetTime_start(date("YmdHis", $now));
  158. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  159. $input->SetTime_expire(date("YmdHis", $expireTime));
  160. $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
  161. $input->SetTrade_type("JSAPI");
  162. //花卉宝代为申请的微信支付
  163. $merchantExtend = $this->merchantExtend;
  164. if ($merchantExtend['wxPayApply'] == 1) {
  165. $input->SetSub_openid($openId);
  166. } else {
  167. $input->SetOpenid($openId);
  168. }
  169. //小程序使用miniAppId
  170. if (httpUtil::isMiniProgram()) {
  171. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  172. }
  173. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  174. $tools = new \JsApiPay();
  175. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  176. $newParams = json_decode($jsApiParameters, true);
  177. //已请求微信不能修改价格
  178. $updateData = [];
  179. $updateData['modPrice'] = 0;
  180. if (empty($order['deadline'])) {
  181. $updateData['deadline'] = $expireTime;
  182. }
  183. OrderService::updateById($orderId, $updateData);
  184. util::success($newParams);
  185. }
  186. //获取订单详情 shish 2021.1.21
  187. public function actionDetail()
  188. {
  189. $get = Yii::$app->request->get();
  190. $id = $get['id'] ?? 0;
  191. $info = OrderClass::getOrderInfo($id, true, true);
  192. OrderClass::valid($info, $this->shopId);
  193. util::success($info);
  194. }
  195. //重选花材 shish 2021.1.22
  196. public function actionChooseProduct()
  197. {
  198. $post = Yii::$app->request->post();
  199. $id = $post['id'] ?? 0;
  200. $product = $post['product'] ?? '[]';
  201. $product = json_decode($product, true);
  202. //检查花材是否都是同家门店的
  203. ProductClass::valid($product, $this->shopId);
  204. $info = OrderClass::getOrderInfo($id);
  205. OrderClass::valid($info, $this->shopId);
  206. //添加修改花材
  207. OrderItemService::replace($info, $product);
  208. util::complete();
  209. }
  210. //自取和免发货流程处理 shish 2021.1.22
  211. public function actionWithoutSend()
  212. {
  213. $get = Yii::$app->request->get();
  214. $id = $get['id'] ?? 0;
  215. $info = OrderClass::getOrderInfo($id);
  216. OrderClass::valid($info, $this->shopId);
  217. OrderClass::withoutSend($info);
  218. util::complete();
  219. }
  220. //本店送 shish 2021.1.24
  221. public function actionSelfSend()
  222. {
  223. $get = Yii::$app->request->get();
  224. $id = isset($get['id']) ? $get['id'] : 0;
  225. $info = OrderClass::getOrderInfo($id);
  226. OrderClass::valid($info, $this->shopId);
  227. OrderClass::selfSend($info);
  228. util::complete();
  229. }
  230. //运费计算 shish 2021.1.24
  231. public function actionFreight()
  232. {
  233. util::success(['sndCost' => 20]);
  234. }
  235. //发快递和第三方配送 shish 2021.1.24
  236. public function actionThirdSend()
  237. {
  238. $respond = [
  239. 'sendSide' => '达达',
  240. 'distance' => 5.5,
  241. 'sendCost' => 20,
  242. 'status' => 1,
  243. 'tip' => 5,
  244. ];
  245. util::success($respond);
  246. }
  247. //确认送达 shish 2021.1.24
  248. public function actionReach()
  249. {
  250. $get = Yii::$app->request->get();
  251. $id = isset($get['id']) ? $get['id'] : 0;
  252. $info = OrderClass::getOrderInfo($id);
  253. OrderClass::valid($info, $this->shopId);
  254. util::complete();
  255. }
  256. }