OrderController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\express\services\DadaExpressServices;
  4. use bizHd\wx\classes\WxOpenClass;
  5. use bizGhs\custom\classes\CustomClass;
  6. use bizGhs\merchant\services\ShopService;
  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['payWay'] = $this->isWx == true ? 0 : 1;
  90. $post['fromType'] = 1;
  91. $post['expressId'] = $post['expressId'] ?? 0;
  92. $post['clearType'] = $post['clearType'] ?? OrderClass::CLEAR_DEBT;
  93. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  94. $productJson = $post['product'] ?? '';
  95. if (empty($productJson)) {
  96. util::fail('请选择花材');
  97. }
  98. $productList = json_decode($productJson, true);
  99. if (empty($productList)) {
  100. util::fail('请选择花材');
  101. }
  102. $connection = Yii::$app->db;//事务处理
  103. $transaction = $connection->beginTransaction();
  104. try {
  105. //判断花材有效性
  106. ProductClass::valid($productList, $this->shopId);
  107. $post['product'] = $productList;
  108. $return = OrderService::createOrder($post, $custom);
  109. $transaction->commit();
  110. util::success($return);
  111. } catch (\Exception $e) {
  112. $transaction->rollBack();
  113. Yii::info("下单失败原因:" . $e->getMessage());
  114. util::fail('下单失败');
  115. }
  116. }
  117. //延期支付 ssh 2021.1.19
  118. public function actionDebt()
  119. {
  120. $get = Yii::$app->request->get();
  121. $id = $get['id'] ?? 0;
  122. $info = OrderClass::getOrderInfo($id);
  123. OrderClass::valid($info, $this->shopId);
  124. OrderClass::debt($info, $this->shop);
  125. util::complete();
  126. }
  127. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  128. public function actionWxPay()
  129. {
  130. ini_set('date.timezone', 'Asia/Shanghai');
  131. $post = Yii::$app->request->post();
  132. $couponId = $post['couponId'] ?? 0;
  133. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  134. $order = OrderClass::getByOrderSn($orderSn);
  135. if (empty($order)) {
  136. util::fail('订单号无效');
  137. }
  138. $id = $order['id'];
  139. //支付前验证订单有效性
  140. if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
  141. util::fail('没有权限操作');
  142. }
  143. $name = $order['orderName'] ?? '购买商品';
  144. $totalFee = $order['actPrice'];
  145. //强制使用小程序的miniOpenId
  146. $openId = $this->admin['miniOpenId'];
  147. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  148. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  149. $wxPayType = 0;
  150. if (httpUtil::isMiniProgram()) {
  151. $wxPayType = 1;
  152. }
  153. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  154. //订单30分钟后过期
  155. $now = time();
  156. $expireTime = $now + 1800;
  157. $wx = Yii::getAlias("@vendor/weixin");
  158. require_once($wx . '/lib/WxPay.Api.php');
  159. require_once($wx . '/example/WxPay.JsApiPay.php');
  160. $input = new \WxPayUnifiedOrder();
  161. $input->SetBody($name);
  162. $input->SetOut_trade_no($orderSn);
  163. $input->SetTotal_fee($totalFee * 100);
  164. $input->SetTime_start(date("YmdHis", $now));
  165. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  166. $input->SetTime_expire(date("YmdHis", $expireTime));
  167. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  168. $input->SetTrade_type("JSAPI");
  169. //花卉宝代为申请的微信支付
  170. //$merchantExtend = $this->sjExtend;
  171. $merchantExtend = WxOpenClass::getGhsWxInfo();
  172. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  173. $input->SetSub_openid($openId);
  174. } else {
  175. $input->SetOpenid($openId);
  176. }
  177. //强制使用小程序的miniAppId
  178. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  179. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  180. $tools = new \JsApiPay();
  181. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  182. $newParams = json_decode($jsApiParameters, true);
  183. //微信不能修改价格
  184. $current = date("Y-m-d H:i:s");
  185. $updateData = ['deadline' => $current, 'modPrice' => 0];
  186. OrderClass::updateById($id, $updateData);
  187. util::success($newParams);
  188. }
  189. //获取订单详情 ssh 2021.1.21
  190. public function actionDetail()
  191. {
  192. $get = Yii::$app->request->get();
  193. $id = $get['id'] ?? 0;
  194. $info = OrderClass::getOrderInfo($id, true, true, true, false);
  195. OrderClass::valid($info, $this->shopId);
  196. util::success($info);
  197. }
  198. //自取和免发货流程处理 ssh 2021.1.22
  199. public function actionWithoutSend()
  200. {
  201. $get = Yii::$app->request->get();
  202. $id = $get['id'] ?? 0;
  203. $info = OrderClass::getOrderInfo($id);
  204. OrderClass::valid($info, $this->shopId);
  205. OrderClass::withoutSend($info);
  206. util::complete();
  207. }
  208. //本店送 ssh 2021.1.24
  209. public function actionSelfSend()
  210. {
  211. $get = Yii::$app->request->get();
  212. $id = isset($get['id']) ? $get['id'] : 0;
  213. $info = OrderClass::getOrderInfo($id);
  214. OrderClass::valid($info, $this->shopId);
  215. $connection = Yii::$app->db;
  216. $transaction = $connection->beginTransaction();
  217. try {
  218. OrderClass::selfSend($info);
  219. $transaction->commit();
  220. } catch (\Exception $exception) {
  221. $transaction->rollBack();
  222. Yii::info("本店送操作报错:" . $exception->getMessage());
  223. util::fail('操作失败');
  224. }
  225. util::complete();
  226. }
  227. //运费计算 ssh 2021.1.24
  228. public function actionFreight()
  229. {
  230. //2021.3.28 接入达达
  231. $get = Yii::$app->request->get();
  232. $post = Yii::$app->request->post();
  233. if (empty($get)) {
  234. $get = $post;
  235. }
  236. $orderId = $get['id'] ?? 0; //订单ID
  237. $customId = $get['customId'] ?? 0;
  238. if (!empty($customId)) {
  239. util::success(['sedCost' => 27]);
  240. }
  241. if (empty($orderId)) {
  242. util::success(['sedCost' => 10]);
  243. }
  244. $province = $get['province'] ?? '';
  245. $city = $get['city'] ?? '';
  246. $address = $get['address'] ?? '';
  247. $floor = $get['floor'] ?? '';
  248. $customName = $get['customName'] ?? '';
  249. $customMobile = $get['customMobile'] ?? '';
  250. $fullAddress = $province . $city . $address . $floor;
  251. $sendTime = $get['sendTime'] ?? '';
  252. $lat = $get['lat'] ?? '';
  253. $lng = $get['long'] ?? '';
  254. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  255. util::fail('请选择配送地址');
  256. }
  257. if (!empty($sendTime)) {
  258. //配送时间不为空
  259. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  260. $sendTimeInt = strtotime($sendTime);
  261. $now = time();
  262. $diff = $sendTimeInt - $now;
  263. //因服务器写入时间,改为至少提前6分钟
  264. if ($diff <= 360) {
  265. util::fail('配送时间至少提前6分钟');
  266. }
  267. }
  268. //更新订单表
  269. $info = OrderClass::getOrderInfo($orderId);
  270. OrderClass::valid($info, $this->shopId);
  271. $expressAddInfo = [
  272. 'customName' => $customName,
  273. 'customMobile' => $customMobile,
  274. 'province' => $province,
  275. 'city' => $city,
  276. 'address' => $address,
  277. 'floor' => $floor,
  278. 'lat' => $lat,
  279. 'long' => $lng,
  280. 'sendTime' => date('Y-m-d H:i', strtotime($sendTime)),
  281. ];
  282. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  283. //计算运费
  284. $info['province'] = $province;
  285. $info['city'] = $city;
  286. $info['address'] = $address;
  287. $info['floor'] = $floor;
  288. $info['fullAddress'] = $fullAddress;
  289. $info['lat'] = $lat;
  290. $info['long'] = $lng;
  291. $info['sendTime'] = $expressAddInfo['sendTime'];
  292. $res = DadaExpressServices::queryDeliverFee($info);
  293. util::success(['sedCost' => $res['fee']]);
  294. }
  295. //发快递和第三方配送 ssh 2021.1.24
  296. public function actionThirdSend()
  297. {
  298. $get = Yii::$app->request->get();
  299. $id = isset($get['id']) ? $get['id'] : 0;
  300. $info = OrderClass::getOrderInfo($id);
  301. OrderClass::valid($info, $this->shopId);
  302. $connection = Yii::$app->db;
  303. $transaction = $connection->beginTransaction();
  304. try {
  305. $respond = OrderClass::thirdSend($info, $get);
  306. $transaction->commit();
  307. util::success($respond);
  308. } catch (\Exception $exception) {
  309. $transaction->rollBack();
  310. Yii::info("发快递操作报错:" . $exception->getMessage());
  311. util::fail('操作失败' . $exception->getMessage());
  312. }
  313. }
  314. //确认送达 ssh 2021.1.24
  315. public function actionReach()
  316. {
  317. $get = Yii::$app->request->get();
  318. $id = isset($get['id']) ? $get['id'] : 0;
  319. $info = OrderClass::getOrderInfo($id);
  320. OrderClass::valid($info, $this->shopId);
  321. $connection = Yii::$app->db;
  322. $transaction = $connection->beginTransaction();
  323. try {
  324. OrderClass::reach($info);
  325. $transaction->commit();
  326. } catch (\Exception $exception) {
  327. $transaction->rollBack();
  328. Yii::info("送达操作报错:" . $exception->getMessage());
  329. util::fail('操作失败');
  330. }
  331. util::complete();
  332. }
  333. //下单要用到的相关信息 ssh 2019.12.6
  334. public function actionOrderRelate()
  335. {
  336. $regionTree = RegionService::tree();
  337. $shop = ShopService::getDefaultShop($this->sj);
  338. $freight = ['first' => 5, 'add' => 2];
  339. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  340. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $this->sj];
  341. util::success($out);
  342. }
  343. //客户欠款的订单 ssh 2021.2.4
  344. public function actionDebtList()
  345. {
  346. $id = Yii::$app->request->get('id', 0);
  347. $info = CustomClass::getCustom($id);
  348. CustomClass::valid($info, $this->shopId);
  349. $where = ['customId' => $id, 'debt' => 1];
  350. $respond = OrderService::getDebtOrderList($where);
  351. $respond['customInfo'] = $info;
  352. util::success($respond);
  353. }
  354. }