OrderController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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;
  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;
  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 = $this->admin['miniOpenId'];
  167. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  168. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  169. $wxPayType = 0;
  170. if (httpUtil::isMiniProgram()) {
  171. $wxPayType = 1;
  172. }
  173. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  174. //订单30分钟后过期
  175. $now = time();
  176. $expireTime = $now + 1800;
  177. $wx = Yii::getAlias("@vendor/weixin");
  178. require_once($wx . '/lib/WxPay.Api.php');
  179. require_once($wx . '/example/WxPay.JsApiPay.php');
  180. $input = new \WxPayUnifiedOrder();
  181. $input->SetBody($name);
  182. $input->SetOut_trade_no($orderSn);
  183. $input->SetTotal_fee($totalFee * 100);
  184. $input->SetTime_start(date("YmdHis", $now));
  185. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  186. $input->SetTime_expire(date("YmdHis", $expireTime));
  187. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  188. $input->SetTrade_type("JSAPI");
  189. //花卉宝代为申请的微信支付
  190. //$merchantExtend = $this->sjExtend;
  191. $merchantExtend = WxOpenClass::getGhsWxInfo();
  192. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  193. $input->SetSub_openid($openId);
  194. } else {
  195. $input->SetOpenid($openId);
  196. }
  197. //强制使用小程序的miniAppId
  198. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  199. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  200. $tools = new \JsApiPay();
  201. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  202. $newParams = json_decode($jsApiParameters, true);
  203. //微信不能修改价格
  204. $current = date("Y-m-d H:i:s");
  205. $updateData = ['deadline' => $current, 'modPrice' => 0];
  206. OrderClass::updateById($id, $updateData);
  207. util::success($newParams);
  208. }
  209. //获取订单详情 ssh 2021.1.21
  210. public function actionDetail()
  211. {
  212. $get = Yii::$app->request->get();
  213. $id = $get['id'] ?? 0;
  214. $info = OrderService::getOrderInfo($id, true, true, true, false);
  215. OrderClass::valid($info, $this->shopId);
  216. util::success($info);
  217. }
  218. //自取免发货流程处理 ssh 2021.1.22
  219. public function actionWithoutSend()
  220. {
  221. $get = Yii::$app->request->get();
  222. $id = $get['id'] ?? 0;
  223. $info = OrderService::getById($id, true);
  224. OrderClass::valid($info, $this->shopId);
  225. $purchaseId = $info->purchaseId;
  226. $purchase = PurchaseClass::getById($purchaseId, true);
  227. OrderService::withoutSend($info, $purchase);
  228. util::complete();
  229. }
  230. //本店送 ssh 2021.1.24
  231. public function actionSelfSend()
  232. {
  233. $get = Yii::$app->request->get();
  234. $id = isset($get['id']) ? $get['id'] : 0;
  235. $info = OrderClass::getById($id, true);
  236. OrderClass::valid($info->attributes, $this->shopId);
  237. $connection = Yii::$app->db;
  238. $transaction = $connection->beginTransaction();
  239. try {
  240. OrderClass::selfSend($info);
  241. $transaction->commit();
  242. } catch (\Exception $exception) {
  243. $transaction->rollBack();
  244. Yii::info("本店送操作报错:" . $exception->getMessage());
  245. util::fail('操作失败');
  246. }
  247. util::complete();
  248. }
  249. //运费计算 ssh 2021.1.24
  250. public function actionFreight()
  251. {
  252. //2021.3.28 接入达达
  253. $get = Yii::$app->request->get();
  254. $post = Yii::$app->request->post();
  255. if (empty($get)) {
  256. $get = $post;
  257. }
  258. $orderId = $get['id'] ?? 0; //订单ID
  259. $customId = $get['customId'] ?? 0;
  260. if (!empty($customId)) {
  261. util::success(['sedCost' => 27]);
  262. }
  263. if (empty($orderId)) {
  264. util::success(['sedCost' => 10]);
  265. }
  266. $province = $get['province'] ?? '';
  267. $city = $get['city'] ?? '';
  268. $address = $get['address'] ?? '';
  269. $floor = $get['floor'] ?? '';
  270. $customName = $get['customName'] ?? '';
  271. $customMobile = $get['customMobile'] ?? '';
  272. $fullAddress = $province . $city . $address . $floor;
  273. $sendTime = $get['sendTime'] ?? '';
  274. $lat = $get['lat'] ?? '';
  275. $lng = $get['long'] ?? '';
  276. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  277. util::fail('请选择配送地址');
  278. }
  279. if (!empty($sendTime)) {
  280. //配送时间不为空
  281. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  282. $sendTimeInt = strtotime($sendTime);
  283. $now = time();
  284. $diff = $sendTimeInt - $now;
  285. //因服务器写入时间,改为至少提前6分钟
  286. if ($diff <= 360) {
  287. util::fail('配送时间至少提前6分钟');
  288. }
  289. }
  290. //更新订单表
  291. $info = OrderService::getOrderInfo($orderId);
  292. OrderClass::valid($info, $this->shopId);
  293. $expressAddInfo = [
  294. 'customName' => $customName,
  295. 'customMobile' => $customMobile,
  296. 'province' => $province,
  297. 'city' => $city,
  298. 'address' => $address,
  299. 'floor' => $floor,
  300. 'lat' => $lat,
  301. 'long' => $lng,
  302. 'sendTime' => date('Y-m-d H:i', strtotime($sendTime)),
  303. ];
  304. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  305. //计算运费
  306. $info['province'] = $province;
  307. $info['city'] = $city;
  308. $info['address'] = $address;
  309. $info['floor'] = $floor;
  310. $info['fullAddress'] = $fullAddress;
  311. $info['lat'] = $lat;
  312. $info['long'] = $lng;
  313. $info['sendTime'] = $expressAddInfo['sendTime'];
  314. $res = DadaExpressServices::queryDeliverFee($info);
  315. util::success(['sedCost' => $res['fee']]);
  316. }
  317. //发快递和第三方配送 ssh 2021.1.24
  318. public function actionThirdSend()
  319. {
  320. $get = Yii::$app->request->get();
  321. $id = isset($get['id']) ? $get['id'] : 0;
  322. $info = OrderService::getById($id, true);
  323. OrderClass::valid($info->attributes, $this->shopId);
  324. $connection = Yii::$app->db;
  325. $transaction = $connection->beginTransaction();
  326. try {
  327. $respond = OrderClass::thirdSend($info, $get);
  328. $transaction->commit();
  329. util::success($respond);
  330. } catch (\Exception $exception) {
  331. $transaction->rollBack();
  332. Yii::info("发快递操作报错:" . $exception->getMessage());
  333. util::fail('操作失败' . $exception->getMessage());
  334. }
  335. }
  336. //确认送达 ssh 2021.1.24
  337. public function actionReach()
  338. {
  339. $get = Yii::$app->request->get();
  340. $id = isset($get['id']) ? $get['id'] : 0;
  341. $info = OrderClass::getById($id, true);
  342. OrderClass::valid($info->attributes, $this->shopId);
  343. $connection = Yii::$app->db;
  344. $transaction = $connection->beginTransaction();
  345. try {
  346. OrderService::reach($info);
  347. $transaction->commit();
  348. } catch (\Exception $exception) {
  349. $transaction->rollBack();
  350. Yii::info("送达操作报错:" . $exception->getMessage());
  351. util::fail('操作失败');
  352. }
  353. util::complete();
  354. }
  355. //下单要用到的相关信息 ssh 2019.12.6
  356. public function actionOrderRelate()
  357. {
  358. $regionTree = RegionService::tree();
  359. $shop = $this->shop;
  360. $freight = ['first' => 5, 'add' => 2];
  361. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  362. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  363. util::success($out);
  364. }
  365. //客户欠款的订单 ssh 2021.2.4
  366. public function actionDebtList()
  367. {
  368. $id = Yii::$app->request->get('id', 0);
  369. $info = CustomClass::getCustom($id);
  370. CustomClass::valid($info, $this->shopId);
  371. $where = ['customId' => $id, 'debt' => 1];
  372. $respond = OrderService::getDebtOrderList($where);
  373. $respond['customInfo'] = $info;
  374. util::success($respond);
  375. }
  376. }