OrderController.php 14 KB

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