OrderController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\express\services\DadaExpressServices;
  4. use bizHd\purchase\classes\PurchaseClass;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizGhs\order\classes\OrderClass;
  8. use bizGhs\order\services\OrderService;
  9. use bizHd\saas\services\RegionService;
  10. use bizGhs\product\classes\ProductClass;
  11. use common\components\dict;
  12. use common\components\dateUtil;
  13. use common\components\httpUtil;
  14. use Yii;
  15. use common\components\util;
  16. class OrderController extends BaseController
  17. {
  18. public $guestAccess = ['create-order', 'order-relate', 'fast-pay'];
  19. //订单列表 ssh 2021.1.20
  20. public function actionList()
  21. {
  22. $get = Yii::$app->request->get();
  23. $status = $get['status'] ?? 0;
  24. if (!empty($status)) {
  25. $where['status'] = $status;
  26. }
  27. $where['merchantId'] = $this->sjId;
  28. if (isset($get['shopId'])) {
  29. $shopId = $get['shopId'] ?? 0;
  30. if (!empty($shopId)) {
  31. $where['shopId'] = $this->shopId;
  32. }
  33. } else {
  34. $where['shopId'] = $this->shopId;
  35. }
  36. $searchTime = $get['searchTime'] ?? '';
  37. if (!empty($searchTime)) {
  38. $startTime = $get['startTime'] ?? '';
  39. $endTime = $get['endTime'] ?? '';
  40. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  41. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  42. }
  43. $name = $get['name'] ?? '';
  44. if (!empty($name)) {
  45. if (preg_match("/^[a-zA-Z\s]+$/", $name)) {
  46. $where['customNamePy'] = ['like', $name];
  47. } else {
  48. $where['customName'] = ['like', $name];
  49. }
  50. }
  51. $respond = OrderClass::getOrderList($where);
  52. $respond['shop'] = $this->shop->attributes;
  53. util::success($respond);
  54. }
  55. //商城下单操作 ssh 2021.1.14
  56. public function actionCreateOrder()
  57. {
  58. $post = Yii::$app->request->post();
  59. $shopId = $this->shopId;
  60. $customId = $post['customId'] ?? 0;
  61. $post['customId'] = $customId;
  62. //开单必须选客户
  63. if (empty($customId)) {
  64. util::fail('请选择客户');
  65. }
  66. $custom = CustomClass::getCustom($customId);
  67. if (empty($custom)) {
  68. util::fail('请选客户哦');
  69. }
  70. CustomClass::valid($custom, $this->shopId);
  71. $post['ghsId'] = $custom['ghsId'] ?? 0;
  72. $post['customMobile'] = $custom['mobile'] ?? '';
  73. $customName = $custom['name'] ?? '';
  74. $post['customName'] = $customName;
  75. $post['customNamePy'] = $custom['py'] ?? '';
  76. $post['customAvatar'] = $custom['shortSmallAvatar'] ?? '';
  77. $post['shopAdminId'] = $this->shopAdminId;
  78. $post['province'] = $custom['province'] ?? '';
  79. $post['city'] = $custom['city'] ?? '';
  80. $post['dist'] = $custom['dist'] ?? '';
  81. $post['address'] = $custom['address'] ?? '';
  82. $post['floor'] = $custom['floor'] ?? '';
  83. $post['fullAddress'] = $custom['fullAddress'] ?? '';
  84. $post['showAddress'] = $custom['showAddress'] ?? '';
  85. $shopAdmin = $this->shopAdmin->attributes;
  86. $post['shopAdminName'] = $shopAdmin['name'] ?? '';
  87. $post['merchantId'] = $this->sjId;
  88. $post['shopId'] = $shopId;
  89. $post['fromType'] = 1;
  90. $post['expressId'] = $post['expressId'] ?? 0;
  91. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  92. $post['payWay'] = dict::getDict('payWay', 'wxPay');
  93. //PC端开单
  94. if (isset($post['clearType'])) {
  95. $clearType = $post['clearType'];
  96. if ($clearType == OrderClass::CLEAR_DEBT) {
  97. $post['debt'] = OrderClass::DEBT_YES;
  98. $post['payWay'] = dict::getDict('payWay', 'debtPay');
  99. } else {
  100. $post['debt'] = OrderClass::DEBT_NO;
  101. }
  102. }
  103. //PC端开单
  104. if (isset($post['getType'])) {
  105. $getType = $post['getType'];
  106. //自取
  107. if ($getType == 1) {
  108. $post['sendType'] = OrderClass::SEND_TYPE_NO;
  109. } else {
  110. //选择配送时,在发货时让商家自己再确认一下用什么方式
  111. $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
  112. }
  113. }
  114. $productJson = $post['product'] ?? '';
  115. if (empty($productJson)) {
  116. util::fail('请选择花材');
  117. }
  118. $productList = json_decode($productJson, true);
  119. if (empty($productList)) {
  120. util::fail('请选择花材');
  121. }
  122. $connection = Yii::$app->db;//事务处理
  123. $transaction = $connection->beginTransaction();
  124. try {
  125. //判断花材有效性
  126. ProductClass::valid($productList, $this->shopId);
  127. $post['product'] = $productList;
  128. $return = OrderService::createOrder($post, $custom);
  129. $transaction->commit();
  130. util::success($return);
  131. } catch (\Exception $e) {
  132. $transaction->rollBack();
  133. Yii::info("下单失败原因:" . $e->getMessage());
  134. util::fail('下单失败');
  135. }
  136. }
  137. //延期支付 ssh 2021.1.19
  138. public function actionDebt()
  139. {
  140. $get = Yii::$app->request->get();
  141. $id = $get['id'] ?? 0;
  142. $info = OrderService::getOrderInfo($id);
  143. OrderClass::valid($info, $this->shopId);
  144. OrderClass::debt($info, $this->shop);
  145. util::complete();
  146. }
  147. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  148. public function actionWxPay()
  149. {
  150. ini_set('date.timezone', 'Asia/Shanghai');
  151. $post = Yii::$app->request->post();
  152. $couponId = $post['couponId'] ?? 0;
  153. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  154. $order = OrderClass::getByOrderSn($orderSn);
  155. if (empty($order)) {
  156. util::fail('订单号无效');
  157. }
  158. $id = $order['id'];
  159. //支付前验证订单有效性
  160. if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
  161. util::fail('没有权限操作');
  162. }
  163. $name = $order['orderName'] ?? '购买商品';
  164. $totalFee = $order['actPrice'];
  165. //强制使用小程序的miniOpenId
  166. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  167. if (empty($openId)) {
  168. util::fail('没有找到openId');
  169. }
  170. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  171. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  172. $wxPayType = 0;
  173. if (httpUtil::isMiniProgram()) {
  174. $wxPayType = 1;
  175. }
  176. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  177. //订单30分钟后过期
  178. $now = time();
  179. $expireTime = $now + 1800;
  180. $wx = Yii::getAlias("@vendor/weixin");
  181. require_once($wx . '/lib/WxPay.Api.php');
  182. require_once($wx . '/example/WxPay.JsApiPay.php');
  183. $input = new \WxPayUnifiedOrder();
  184. $input->SetBody($name);
  185. $input->SetOut_trade_no($orderSn);
  186. $input->SetTotal_fee($totalFee * 100);
  187. $input->SetTime_start(date("YmdHis", $now));
  188. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  189. $input->SetTime_expire(date("YmdHis", $expireTime));
  190. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  191. $input->SetTrade_type("JSAPI");
  192. //花卉宝代为申请的微信支付
  193. //$merchantExtend = $this->sjExtend->attributes;
  194. $merchantExtend = WxOpenClass::getGhsWxInfo();
  195. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  196. $input->SetSub_openid($openId);
  197. } else {
  198. $input->SetOpenid($openId);
  199. }
  200. //强制使用小程序的miniAppId
  201. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  202. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  203. $tools = new \JsApiPay();
  204. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  205. $newParams = json_decode($jsApiParameters, true);
  206. //微信不能修改价格
  207. $current = date("Y-m-d H:i:s");
  208. $updateData = ['deadline' => $current, 'modPrice' => 0];
  209. OrderClass::updateById($id, $updateData);
  210. util::success($newParams);
  211. }
  212. //获取订单详情 ssh 2021.1.21
  213. public function actionDetail()
  214. {
  215. $get = Yii::$app->request->get();
  216. $id = $get['id'] ?? 0;
  217. $info = OrderService::getOrderInfo($id, true, true, true, false);
  218. OrderClass::valid($info, $this->shopId);
  219. util::success($info);
  220. }
  221. //自取免发货流程处理 ssh 2021.1.22
  222. public function actionWithoutSend()
  223. {
  224. $get = Yii::$app->request->get();
  225. $id = $get['id'] ?? 0;
  226. $info = OrderService::getById($id, true);
  227. OrderClass::valid($info, $this->shopId);
  228. $purchaseId = $info->purchaseId;
  229. $purchase = PurchaseClass::getById($purchaseId, true);
  230. OrderService::withoutSend($info, $purchase);
  231. util::complete();
  232. }
  233. //本店送 ssh 2021.1.24
  234. public function actionSelfSend()
  235. {
  236. $get = Yii::$app->request->get();
  237. $id = isset($get['id']) ? $get['id'] : 0;
  238. $info = OrderClass::getById($id, true);
  239. OrderClass::valid($info->attributes, $this->shopId);
  240. $connection = Yii::$app->db;
  241. $transaction = $connection->beginTransaction();
  242. try {
  243. OrderClass::selfSend($info);
  244. $transaction->commit();
  245. } catch (\Exception $exception) {
  246. $transaction->rollBack();
  247. Yii::info("本店送操作报错:" . $exception->getMessage());
  248. util::fail('操作失败');
  249. }
  250. util::complete();
  251. }
  252. //运费计算 ssh 2021.1.24
  253. public function actionFreight()
  254. {
  255. //2021.3.28 接入达达
  256. $get = Yii::$app->request->get();
  257. $post = Yii::$app->request->post();
  258. if (empty($get)) {
  259. $get = $post;
  260. }
  261. $orderId = $get['id'] ?? 0; //订单ID
  262. $customId = $get['customId'] ?? 0;
  263. if (!empty($customId)) {
  264. util::success(['sedCost' => 27]);
  265. }
  266. if (empty($orderId)) {
  267. util::success(['sedCost' => 10]);
  268. }
  269. $province = $get['province'] ?? '';
  270. $city = $get['city'] ?? '';
  271. $address = $get['address'] ?? '';
  272. $floor = $get['floor'] ?? '';
  273. $customName = $get['customName'] ?? '';
  274. $customMobile = $get['customMobile'] ?? '';
  275. $fullAddress = $province . $city . $address . $floor;
  276. $sendTime = $get['sendTime'] ?? '';
  277. $lat = $get['lat'] ?? '';
  278. $lng = $get['long'] ?? '';
  279. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  280. util::fail('请选择配送地址');
  281. }
  282. if (!empty($sendTime)) {
  283. //配送时间不为空
  284. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  285. $sendTimeInt = strtotime($sendTime);
  286. $now = time();
  287. $diff = $sendTimeInt - $now;
  288. //因服务器写入时间,改为至少提前6分钟
  289. if ($diff <= 360) {
  290. util::fail('配送时间至少提前6分钟');
  291. }
  292. }
  293. //更新订单表
  294. $info = OrderService::getOrderInfo($orderId);
  295. OrderClass::valid($info, $this->shopId);
  296. $expressAddInfo = [
  297. 'customName' => $customName,
  298. 'customMobile' => $customMobile,
  299. 'province' => $province,
  300. 'city' => $city,
  301. 'address' => $address,
  302. 'floor' => $floor,
  303. 'lat' => $lat,
  304. 'long' => $lng,
  305. 'sendTime' => date('Y-m-d H:i', strtotime($sendTime)),
  306. ];
  307. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  308. //计算运费
  309. $info['province'] = $province;
  310. $info['city'] = $city;
  311. $info['address'] = $address;
  312. $info['floor'] = $floor;
  313. $info['fullAddress'] = $fullAddress;
  314. $info['lat'] = $lat;
  315. $info['long'] = $lng;
  316. $info['sendTime'] = $expressAddInfo['sendTime'];
  317. $res = DadaExpressServices::queryDeliverFee($info);
  318. util::success(['sedCost' => $res['fee']]);
  319. }
  320. //发快递和第三方配送 ssh 2021.1.24
  321. public function actionThirdSend()
  322. {
  323. $get = Yii::$app->request->get();
  324. $id = isset($get['id']) ? $get['id'] : 0;
  325. $info = OrderService::getById($id, true);
  326. OrderClass::valid($info->attributes, $this->shopId);
  327. $connection = Yii::$app->db;
  328. $transaction = $connection->beginTransaction();
  329. try {
  330. $respond = OrderClass::thirdSend($info, $get);
  331. $transaction->commit();
  332. util::success($respond);
  333. } catch (\Exception $exception) {
  334. $transaction->rollBack();
  335. Yii::info("发快递操作报错:" . $exception->getMessage());
  336. util::fail('操作失败' . $exception->getMessage());
  337. }
  338. }
  339. //确认送达 ssh 2021.1.24
  340. public function actionReach()
  341. {
  342. $get = Yii::$app->request->get();
  343. $id = isset($get['id']) ? $get['id'] : 0;
  344. $info = OrderClass::getById($id, true);
  345. OrderClass::valid($info->attributes, $this->shopId);
  346. $connection = Yii::$app->db;
  347. $transaction = $connection->beginTransaction();
  348. try {
  349. OrderService::reach($info);
  350. $transaction->commit();
  351. } catch (\Exception $exception) {
  352. $transaction->rollBack();
  353. Yii::info("送达操作报错:" . $exception->getMessage());
  354. util::fail('操作失败');
  355. }
  356. util::complete();
  357. }
  358. //下单要用到的相关信息 ssh 2019.12.6
  359. public function actionOrderRelate()
  360. {
  361. $regionTree = RegionService::tree();
  362. $shop = $this->shop->attributes;
  363. $freight = ['first' => 5, 'add' => 2];
  364. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  365. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  366. util::success($out);
  367. }
  368. //客户欠款的订单 ssh 2021.2.4
  369. public function actionDebtList()
  370. {
  371. $id = Yii::$app->request->get('id', 0);
  372. $info = CustomClass::getCustom($id);
  373. CustomClass::valid($info, $this->shopId);
  374. $where = ['customId' => $id, 'debt' => 1];
  375. $respond = OrderService::getDebtOrderList($where);
  376. $respond['customInfo'] = $info;
  377. util::success($respond);
  378. }
  379. }