IntraCityController.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\wx\classes\WxMessageClass;
  4. use biz\wx\classes\WxOpenClass;
  5. use bizGhs\admin\classes\AdminClass;
  6. use bizGhs\express\classes\GhsExpressOrderClass;
  7. use bizGhs\shop\classes\ShopAdminClass;
  8. use bizHd\order\classes\OrderGoodsClass;
  9. use bizGhs\order\classes\OrderItemClass;
  10. use bizHd\purchase\classes\PurchaseClass;
  11. use common\components\dict;
  12. use common\components\imgUtil;
  13. use common\components\orderSn;
  14. use hd\models\IntraCity\StoreFeeForm;
  15. // 以上类来自零售端
  16. use bizGhs\order\classes\OrderClass;
  17. use biz\shop\classes\MainClass;
  18. use biz\shop\classes\ShopClass;
  19. use common\components\noticeUtil;
  20. use common\components\util;
  21. use common\components\IntraCityExpress;
  22. use yii\helpers\Json;
  23. use yii\db\Query;
  24. use Yii;
  25. /**
  26. * 同城配送控制器
  27. * 提供同城配送相关的API接口
  28. */
  29. class IntraCityController extends BaseController
  30. {
  31. public $guestAccess = ['callback'];
  32. //申请开通跑腿 ssh
  33. public function actionApply()
  34. {
  35. IntraCityExpress::applyStore();
  36. }
  37. /**
  38. * 创建门店
  39. * POST /intra-city/create-store
  40. */
  41. public function actionCreateStore()
  42. {
  43. $shop = $this->shop;
  44. $shopName = $shop->shopName ?? '';
  45. $sjName = $shop->merchantName ?? '';
  46. $name = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  47. if (empty($shop->lat) || empty($shop->long)) {
  48. util::fail('没有经纬度,请修改地址');
  49. }
  50. if (empty($shop->address)) {
  51. util::fail('地址缺失,请修改');
  52. }
  53. $adminId = $this->adminId;
  54. util::checkRepeatCommit($adminId, 3);
  55. $storeData = [
  56. 'out_store_id' => $this->mainId, //自定义门店编号
  57. 'store_name' => $name,
  58. 'address_info' => [
  59. 'province' => $shop->province,
  60. 'city' => $shop->city,
  61. 'area' => $shop->dist,
  62. 'street' => '',
  63. 'house' => $shop->address . $shop->floor,
  64. 'lat' => $shop->lat,
  65. 'lng' => $shop->long,
  66. 'phone' => $shop->telephone
  67. ]
  68. ];
  69. // 验证必填参数
  70. $requiredFields = ['out_store_id', 'store_name', 'address_info'];
  71. foreach ($requiredFields as $field) {
  72. if (empty($storeData[$field])) {
  73. util::fail("缺少必填参数:{$field}");
  74. }
  75. }
  76. // 创建门店前,检查是否已经 开通门店权限 (有wxStoreId,即说明开通了)
  77. $main = $this->main;
  78. if (!empty($main->wxStoreId)) {
  79. util::fail('已经创建门店,请刷新');
  80. }
  81. $result = IntraCityExpress::createStore($storeData);
  82. if ($result['errcode'] === 0) {
  83. // 保存微信门店编号(wx_store_id)
  84. $main->wxStoreId = $result['wx_store_id'];
  85. $main->save();
  86. util::success($result, "门店创建成功");
  87. } else {
  88. $errMsg = $result['errmsg'] ?? '';
  89. Yii::error("门店创建失败:" . $errMsg);
  90. noticeUtil::push("跑腿创建门店报错:" . $errMsg, '15280215347');
  91. util::fail('创建失败,请联系客服');
  92. }
  93. }
  94. /**
  95. * 查询门店创建情况
  96. */
  97. public function actionStore()
  98. {
  99. $shopId = $this->shopId;
  100. // 首先从门店扩展表查询是否已经创建
  101. $main = MainClass::getById($this->mainId, true, 'id, wxStoreId');
  102. if ($main->wxStoreId == '') { // 不存在,则从微信接口查询
  103. $result = IntraCityExpress::getStore($this->mainId);
  104. if ($result['errcode'] === 0) {
  105. $store = $result['store_list'][0];
  106. // 保存 wx_store_id
  107. $main->wxStoreId = $store['wx_store_id'];
  108. $main->save();
  109. util::success($store, "门店查询成功");
  110. } else {
  111. Yii::error("门店查询失败:" . $result['errmsg']);
  112. util::error(-1, '门店查询失败');
  113. }
  114. } else {
  115. $field = 'province, city, dist, lat, long, floor, address';
  116. $shop = ShopClass::getById($shopId, false, $field);
  117. $data = array_merge($shop, $main->toArray());
  118. util::success($data, "门店查询成功");
  119. }
  120. }
  121. /**
  122. * 更新门店
  123. */
  124. public function actionUpdateStore()
  125. {
  126. // 首先从门店扩展表查询是否已经创建
  127. $main = $this->main;
  128. $wxStoreId = $main->wxStoreId;
  129. if (empty($wxStoreId)) {
  130. util::fail('未创建门店,请先创建');
  131. }
  132. $shop = $this->shop;
  133. $shopName = $shop->shopName ?? '';
  134. $sjName = $shop->merchantName ?? '';
  135. $name = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  136. $adminId = $this->adminId;
  137. util::checkRepeatCommit($adminId, 3);
  138. if (empty($shop->lat) || empty($shop->long)) {
  139. util::fail('没有经纬度,请修改地址');
  140. }
  141. if (empty($shop->address)) {
  142. util::fail('地址缺失,请修改');
  143. }
  144. $storeData = [
  145. 'keys' => ['wx_store_id' => $wxStoreId],
  146. 'content' => [
  147. 'store_name' => $name,
  148. 'address_info' => [
  149. 'province' => $shop->province,
  150. 'city' => $shop->city,
  151. 'area' => $shop->dist,
  152. 'street' => '',
  153. 'house' => $shop->address . $shop->floor,
  154. 'lat' => $shop->lat,
  155. 'lng' => $shop->long,
  156. 'phone' => $shop->telephone
  157. ],
  158. //"order_pattern" => 2,
  159. //"service_trans_prefer" => "SFTC" //order_pattern = 2时必填
  160. ]
  161. ];
  162. $result = IntraCityExpress::updateStore($storeData);
  163. if ($result['errcode'] === 0) {
  164. util::success($result, "门店更新成功");
  165. } else {
  166. $errMsg = $result['errmsg'] ?? '';
  167. Yii::error("门店更新失败:" . $errMsg);
  168. noticeUtil::push("销花宝,跑腿修改门店报错:" . $errMsg, '15280215347');
  169. util::fail('更新失败,请联系客服');
  170. }
  171. }
  172. /**
  173. * 门店运费充值
  174. */
  175. public function actionStoreCharge()
  176. {
  177. $post = Yii::$app->request->post();
  178. $amount = floatval($post['amount']);
  179. $amountTurnFen = $amount * 100;
  180. if ($amountTurnFen < 5000) {
  181. util::fail('50元起充');
  182. }
  183. $expressId = $post['expressId'];
  184. $serviceTransIds = ['SFTC', 'DADA'];
  185. if (!in_array($expressId, $serviceTransIds)) {
  186. util::fail('快递动力ID出错');
  187. }
  188. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  189. if ($shopExt['wxStoreId'] == '') {
  190. util::fail('微信门店编号为空');
  191. }
  192. $wxStoreId = $shopExt['wxStoreId'];
  193. $result = IntraCityExpress::storeCharge($wxStoreId, $expressId, $amountTurnFen);
  194. if ($result['errcode'] === 0) {
  195. util::success($result, "门店余额充值单已生成");
  196. } else {
  197. Yii::error("门店余额充值失败:" . $result['errmsg']);
  198. util::fail('门店余额充值失败');
  199. }
  200. }
  201. /**
  202. * 门店余额查询
  203. */
  204. public function actionStoreBalance()
  205. {
  206. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  207. if ($shopExt['wxStoreId'] == '') {
  208. util::error(-1, '微信门店编号为空');
  209. }
  210. $wxStoreId = $shopExt['wxStoreId'];
  211. $result = IntraCityExpress::getBalance($wxStoreId);
  212. if ($result['errcode'] === 0) {
  213. $data = ['all_balance' => $result['all_balance'] / 100.0];
  214. foreach ($result['balance_detail'] as $item) {
  215. if ($item['service_trans_id'] == 'DADA') {
  216. $data['dada_balance'] = $item['balance'] / 100.0;
  217. }
  218. if ($item['service_trans_id'] == 'SFTC') {
  219. $data['sf_balance'] = $item['balance'] / 100.0;
  220. }
  221. }
  222. util::success($data, "门店余额查询成功");
  223. } else {
  224. Yii::error("门店余额查询失败:" . $result['errmsg']);
  225. util::fail('门店余额查询失败');
  226. }
  227. }
  228. /**
  229. * 查询运费
  230. */
  231. public function actionStoreFee()
  232. {
  233. $post = Yii::$app->request->post();
  234. // 创建表单验证模型
  235. $form = new StoreFeeForm();
  236. $form->setScenario('store_fee');
  237. $form->load($post, ''); // 直接从 post 数据加载,不使用模型名作为前缀
  238. // 执行表单验证
  239. $form->validateForm();
  240. $orderId = $post['orderId'];
  241. $order = OrderClass::getById($orderId);
  242. if (empty($order)) {
  243. util::fail('订单出错');
  244. }
  245. if ($order['mainId'] != $this->mainId) {
  246. util::fail('订单出错');
  247. }
  248. $sn = $order['orderSn'];
  249. $shopId = intval($this->shopId);
  250. if (empty($shopId)) {
  251. util::fail("门店不存在");
  252. }
  253. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  254. if (empty($shopExt)) {
  255. util::fail("微信门店不存在");
  256. }
  257. $itemInfoList = OrderItemClass::getAllByCondition(['orderSn' => $sn], null, 'id, name, cover, num');
  258. $orderType = 2; // 默认是 2。说明:1花束订单 2花材订单 3花束和花材都有
  259. if (empty($itemInfoList)) {
  260. $orderType = 1;
  261. }
  262. $cargoName = '花材'; // 商品名称,默认是花材
  263. $goodsInfoList = OrderGoodsClass::getListBySn($sn);
  264. if (count($goodsInfoList) == 1) {
  265. $goodsInfo = $goodsInfoList[0];
  266. $cargoName = $goodsInfo['name'];
  267. } else {
  268. if ($orderType != 2) {
  269. util::fail("订单商品数量不正确");
  270. }
  271. }
  272. $cargo = [
  273. 'cargo_name' => $cargoName, // 商品名称 -- $order 中没有,要在 goodsInfo 中取
  274. 'cargo_weight' => intval($post['weight'] * 1000), // 单位:克 -- 把千克转换为克
  275. 'cargo_price' => intval($post['price'] * 100), // 单位:分 -- 把元转换为分
  276. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  277. 'cargo_num' => intval($post['packageNum'])
  278. ];
  279. $originalLng = doubleval($post['user_lng']);
  280. $originalLat = doubleval($post['user_lat']);
  281. $orderData = [
  282. //'out_store_id' => $shopId,
  283. 'wx_store_id' => $shopExt['wxStoreId'],
  284. 'user_name' => $post['user_name'],
  285. 'user_phone' => $post['user_phone'],
  286. 'user_lng' => $originalLng,
  287. 'user_lat' => $originalLat,
  288. 'user_address' => $post['user_address'],
  289. 'cargo' => $cargo,
  290. // 'use_sandbox' => getenv('YII_ENV') == 'production' ? 0 : 1, //正式环境不能传这个参数,会报错
  291. ];
  292. //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  293. $result = IntraCityExpress::preAddOrder($orderData);
  294. if ($result['errcode'] === 0) {
  295. util::success($result, "查询成功");
  296. } else {
  297. Yii::error("运费查询失败:" . json_encode($result));
  298. util::fail('运费查询失败,请确认是否超过20公斤,编号56698');
  299. }
  300. }
  301. //根据订单查询运费 ssh
  302. public function actionGetOrderFee()
  303. {
  304. $post = Yii::$app->request->post();
  305. $orderId = $post['id'];
  306. $order = OrderClass::getById($orderId, true);
  307. if (empty($order)) {
  308. util::fail('没有找到订单');
  309. }
  310. if ($order->mainId != $this->mainId) {
  311. util::fail('不是你的订单');
  312. }
  313. $main = $this->main;
  314. $wxStoreId = $main->wxStoreId ?? '';
  315. if (empty($wxStoreId)) {
  316. util::fail('你还没有开通跑腿');
  317. }
  318. $weight = $post['weight'] ?? 0;
  319. if ($weight <= 0) {
  320. util::fail('请填写重量');
  321. }
  322. $packageNum = $post['packageNum'] ?? 1;
  323. $price = $order->actPrice ?? 0;
  324. $cargo = [
  325. 'cargo_name' => '鲜花花材', // 商品名称 -- $order 中没有,要在 goodsInfo 中取
  326. 'cargo_weight' => intval($weight * 1000), // 单位:克 -- 把千克转换为克
  327. 'cargo_price' => intval($price * 100), // 单位:分 -- 把元转换为分
  328. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  329. 'cargo_num' => intval($packageNum)
  330. ];
  331. $originalLng = $order->long;
  332. $originalLat = $order->lat;
  333. if (empty($originalLng) || empty($originalLat)) {
  334. util::fail('客户地址不完整');
  335. }
  336. $customName = $order->customName ?? '';
  337. $customMobile = $order->customMobile ?? '';
  338. if (empty($customMobile)) {
  339. util::fail('客户手机缺失');
  340. }
  341. $address = $order->address ?? '';
  342. if (empty($address)) {
  343. util::fail('客户地址缺失');
  344. }
  345. //根据环境变量判断是否使用沙盒环境
  346. // $useSandBox = getenv('YII_ENV') == 'production' ? 0 : 1;
  347. $orderData = [
  348. 'wx_store_id' => $wxStoreId,
  349. 'user_name' => $customName,
  350. 'user_phone' => $customMobile,
  351. 'user_lng' => $originalLng,
  352. 'user_lat' => $originalLat,
  353. 'user_address' => $address,
  354. 'cargo' => $cargo,
  355. // 'use_sandbox' => $useSandBox,//正式环境不能传这个参数,会报错
  356. ];
  357. //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  358. $result = IntraCityExpress::preAddOrder($orderData);
  359. if ($result['errcode'] === 0) {
  360. util::success($result, "查询成功");
  361. } else {
  362. $errMsg = $result['errmsg'] ?? '';
  363. $errCode = $result['errcode'] ?? '';
  364. noticeUtil::push("运费查询失败了:" . $errMsg . ",编号:" . $errCode, '15280215347');
  365. util::fail('查询失败,请确认量否超过20公斤,编号8930');
  366. }
  367. }
  368. /**
  369. * 创建订单
  370. * POST /intra-city/create-order
  371. */
  372. public function actionCreateOrder()
  373. {
  374. $post = Yii::$app->request->post();
  375. $adminId = $this->adminId;
  376. util::checkRepeatCommit($adminId, 4);
  377. $orderId = intval($post['orderId']);
  378. $order = OrderClass::getById($orderId, true);
  379. if (empty($order)) {
  380. util::fail('没有找到订单');
  381. }
  382. if ($order->mainId != $this->mainId) {
  383. util::fail('不是你的订单');
  384. }
  385. $packageNum = $post['packageNum'] ?? 0;
  386. if ($packageNum <= 0) {
  387. util::fail('请填写包裹数量');
  388. }
  389. $weight = $post['weight'] ?? 0;
  390. if ($weight <= 0) {
  391. util::fail('请填写重量');
  392. }
  393. $actPrice = $order->actPrice ?? 0;
  394. if ($actPrice <= 0) {
  395. util::fail('订单没有价格,不能发跑腿');
  396. }
  397. if ($order->status != 2) {
  398. util::fail('待发货订单才能发跑腿,请刷新页面');
  399. }
  400. //如果不是没鹛跑腿和取消了,则不能再叫跑腿,有多处,关键词no_call_express
  401. $notCanCreateSendStatus = [-2, 3];
  402. if (!in_array($order->sendStatus, $notCanCreateSendStatus)) {
  403. util::fail('不能叫跑腿,请刷新页面,编号800');
  404. }
  405. $cgId = $order->purchaseId ?? 0;
  406. $cgInfo = PurchaseClass::getById($cgId, true);
  407. if (empty($cgInfo)) {
  408. util::fail('没有采购信息');
  409. }
  410. $sn = $order->orderSn;
  411. $itemInfo = OrderItemClass::getByCondition(['orderSn' => $sn], null, 'id, name, cover, num');
  412. $num = intval($order->itemNum);
  413. $itemList[] = [
  414. 'item_name' => '花材',
  415. 'item_pic_url' => imgUtil::groupImg($itemInfo['cover']) . "?x-oss-process=image/resize,m_fill,h_130,w_130",
  416. 'count' => $num,
  417. ];
  418. $main = $this->main;
  419. $wxStoreId = $main->wxStoreId ?? '';
  420. if (empty($wxStoreId)) {
  421. util::fail('你还没有开通跑腿功能');
  422. }
  423. $cgId = $order->purchaseId ?? 0;
  424. if (empty($cgId)) {
  425. util::fail('没有找到采购信息');
  426. }
  427. $miniOpenId = '';
  428. $customShopAdminId = $order->customShopAdminId ?? 0;
  429. if (!empty($customShopAdminId)) {
  430. $customStaff = ShopAdminClass::getById($customShopAdminId, true);
  431. $customStaffAdminId = $customStaff->adminId ?? '';
  432. if (!empty($customStaffAdminId)) {
  433. $customAdmin = AdminClass::getById($customStaffAdminId, true);
  434. $miniOpenId = $customAdmin->miniOpenId ?? '';
  435. }
  436. }
  437. if (empty($miniOpenId)) {
  438. $admin = $this->admin;
  439. $miniOpenId = $admin->miniOpenId ?? '';
  440. }
  441. if (empty($miniOpenId)) {
  442. util::fail('没有找到微信登录信息,无法呼叫跑腿');
  443. }
  444. $cargoName = '鲜花花材';//商品名称
  445. $cargo = [
  446. 'cargo_name' => $cargoName,
  447. 'cargo_weight' => intval($weight * 1000),
  448. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  449. 'cargo_num' => intval($packageNum),
  450. 'cargo_price' => intval($actPrice * 100), // $order 中有各个价格,请注意
  451. 'item_list' => $itemList,
  452. ];
  453. $shopId = $this->shopId;
  454. $mainId = $this->mainId;
  455. $snData = ['shopId' => $shopId, 'mainId' => $mainId];
  456. $storeOrderId = orderSn::getGhsExpressSn($snData);
  457. $sendNum = $order->sendNum ?? '';
  458. $customName = $order->customName ?? '';
  459. if (empty($customName)) {
  460. util::fail('客户名称无效');
  461. }
  462. $customMobile = $order->customMobile ?? '';
  463. if (empty($customMobile)) {
  464. util::fail('客户手机号无效');
  465. }
  466. $long = $order->long ?? '';
  467. $lat = $order->lat ?? '';
  468. if (empty($long) || empty($lat)) {
  469. util::fail('客户地址无效');
  470. }
  471. $address = $order['address'] ?? '';
  472. if (empty($address)) {
  473. util::fail('客户地址无效呢');
  474. }
  475. $floor = $order['floor'] ?? '';
  476. $fullAddress = $address . $floor;
  477. $callbackUrl = Yii::$app->params['ghsHost'] . '/intra-city/callback';
  478. $orderData = [
  479. 'wx_store_id' => $wxStoreId,
  480. 'store_order_id' => $storeOrderId, //同一个门店订单编号要保证唯一,相同的订单号会重入
  481. 'user_openid' => $miniOpenId,
  482. 'user_lng' => (float)$long,
  483. 'user_lat' => (float)$lat,
  484. 'user_address' => $fullAddress,
  485. 'user_name' => $customName,
  486. 'user_phone' => $customMobile,
  487. 'order_seq' => $sendNum, // 用于配送员快速寻找到匹配的商品(非必传) -- 对应 xhOrder 表的 sendNum
  488. 'verify_code_type' => 0,
  489. 'order_detail_path' => '/pagesPurchase/purDetails?id=' . $cgId,
  490. 'callback_url' => $callbackUrl, // 订单状态回调地址(非必传)
  491. // 'use_sandbox' => 0,//正式环境不能传这个参数,会报错
  492. 'cargo' => $cargo
  493. ];
  494. $result = IntraCityExpress::createOrder($orderData);
  495. if (isset($result['errcode']) && $result['errcode'] === 0) {
  496. noticeUtil::push(json_encode($result), '15280215347');
  497. // 保存进 xhGhsExpressOrder 表
  498. $data = [
  499. 'mainId' => $this->mainId,
  500. 'shopId' => $this->shopId,
  501. 'deliveryId' => $result['service_trans_id'] ?? '',
  502. 'wxOrderId' => $result['wx_order_id'] ?? '',
  503. 'wxStoreId' => $result['wx_store_id'] ?? '',
  504. 'storeOrderId' => $result['store_order_id'] ?? '',
  505. 'orderId' => $orderId,
  506. 'transOrderId' => $result['trans_order_id'] ?? '',
  507. 'distance' => $result['distance'] ?? 0,
  508. 'fee' => $result['fee'] ?? 0,
  509. 'fetchCode' => $result['fetch_code'] ?? '',
  510. 'orderSeq' => $result['order_seq'] ?? '',
  511. 'status' => 1,//有订单可以取消
  512. 'orderStatus' => 0,//已提交申请,待确认
  513. ];
  514. GhsExpressOrderClass::add($data);
  515. $sendDistance = $data['distance'] ?? 0;
  516. $deliveryId = $data['deliveryId'] ?? '';
  517. //更新订单
  518. $order->sendDistance = $sendDistance;
  519. $order->sendStatus = -1;//已提交申请,待确认
  520. $order->deliveryId = $deliveryId;
  521. $order->sendType = 2;
  522. $order->save();
  523. $cgInfo->sendDistance = $sendDistance;
  524. $cgInfo->sendStatus = -1;//已提交申请,待确认
  525. $cgInfo->deliveryId = $deliveryId;
  526. $cgInfo->sendType = 2;
  527. $cgInfo->save();
  528. $params = [
  529. 'fhType' => 0,
  530. 'fhWl' => '',
  531. 'fhWlNo' => '',
  532. 'staffId' => 0,
  533. 'staffName' => '',
  534. ];
  535. //确认发货,花店会增加路上库存
  536. OrderClass::orderSend($order, $params);
  537. $totalFee = $result['fee'] ?? 0;
  538. $ghsName = $cgInfo->ghsName ?? '';
  539. noticeUtil::push("批发订单,发跑跑腿,获取运费:{$totalFee} 重量:{$weight} 距离:{$sendDistance} 订单号:{$orderId} 供货商:{$ghsName} 客户:{$customName} 客户手机:{$customMobile} 收货地址:{$fullAddress}", '15280215347');
  540. util::success(['fee' => $data['fee']], '订单创建成功');
  541. } else {
  542. noticeUtil::push("批发 -- " . $this->mainId . ",订单创建失败:" . json_encode($result));
  543. Yii::error("订单创建失败:" . ($result['errmsg'] ?? '未知错误'), 'intraCity');
  544. util::fail('提交失败,请确认余额是否充足');
  545. }
  546. }
  547. /**
  548. * 查询订单
  549. * GET /intra-city/get-order
  550. */
  551. public function actionGetOrder()
  552. {
  553. try {
  554. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  555. if (empty($shopExt)) {
  556. util::fail("微信门店不存在");
  557. }
  558. $post = Yii::$app->request->post();
  559. $wxOrderId = isset($post['wx_order_id']) ? $post['wx_order_id'] : '';
  560. $orderId = $post['orderId'];
  561. $wxStoreId = isset($post['wx_store_id']) ? $post['wx_store_id'] : $shopExt['wxStoreId'];
  562. if (empty($wxOrderId) && (empty($orderId) || empty($wxStoreId))) {
  563. util::fail("参数不足:wx_order_id 或 (order_id + wx_store_id) 必须提供一组");
  564. }
  565. $order = OrderClass::getById($orderId, false, 'id, mainId, sendStatus');
  566. if ($order['mainId'] != $this->mainId) {
  567. util::fail('订单未找到');
  568. }
  569. // 检查是否存在重复的订单:如果存在,则使用最后一个订单的 store_order_id
  570. $storeOrderId = $orderId; // ------------------------------------- 注意区分:$storeOrderId , $orderId 。它们有可能不相同
  571. $query = (new Query())
  572. ->from('xhGhsExpressOrder')
  573. ->where(['orderId' => $orderId])
  574. ->andWhere(['mainId' => $this->mainId]);
  575. $esDatas = $query->all();
  576. $count = count($esDatas);
  577. if ($count > 0) {
  578. $es = $esDatas[0];
  579. $storeOrderId = $es['storeOrderId'];
  580. }
  581. $result = IntraCityExpress::queryOrder($wxStoreId, $storeOrderId, $wxOrderId);
  582. if (isset($result['errcode']) && $result['errcode'] === 0) {
  583. util::success($result, "查询成功");
  584. } else {
  585. Yii::error("订单查询失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  586. util::fail('订单查询失败: ' . $result['errcode']);
  587. }
  588. } catch (\Exception $e) {
  589. Yii::error("订单查询异常:" . $e->getMessage(), 'intracity');
  590. util::fail("系统出错");
  591. }
  592. }
  593. /**
  594. * 取消订单
  595. * POST /intra-city/cancel-order
  596. */
  597. public function actionCancelOrder()
  598. {
  599. try {
  600. $post = Yii::$app->request->post();
  601. $adminId = $this->adminId;
  602. util::checkRepeatCommit($adminId, 4);
  603. $orderId = $post['orderId'] ?? 0;
  604. $order = OrderClass::getById($orderId, true);//todo 这时必须取全部字段,下面发货要用到,否则会出错,请勿修改
  605. if (empty($order)) {
  606. util::fail('没有找到订单');
  607. }
  608. if ($order->mainId != $this->mainId) {
  609. util::fail('不是你的订单');
  610. }
  611. if ($order->status != 3) {
  612. util::fail('不能取消');
  613. }
  614. if (in_array($order->sendStatus, [-2, 2, 3])) {
  615. //订单 没叫跑腿、已送达、已取消
  616. util::fail('不能取消哈,编号666');
  617. }
  618. $cgId = $order->purchaseId ?? 0;
  619. $cg = PurchaseClass::getById($cgId, true);
  620. if (empty($cg)) {
  621. util::fail('没有采购单信息');
  622. }
  623. $main = $this->main;
  624. $wxStoreId = $main->wxStoreId ?? '';
  625. $express = GhsExpressOrderClass::getByCondition(['orderId' => $orderId, 'status' => 1], true);
  626. if (empty($express)) {
  627. util::fail('没有找到有效配送单');
  628. }
  629. $wxOrderId = $express->wxOrderId ?? '';
  630. $storeOrderId = $express->storeOrderId ?? '';
  631. $result = IntraCityExpress::cancelOrder($wxOrderId, $storeOrderId, $wxStoreId);
  632. if (isset($result['errcode']) && $result['errcode'] === 0) {
  633. // 更新订单状态
  634. $order->sendStatus = 3;
  635. $order->save();
  636. $cg->sendStatus = 3;
  637. $cg->save();
  638. $params = ['staffId' => 0, 'staffName' => ''];
  639. OrderClass::cancelOrderSend($order, $params);
  640. $deductFee = $result['deductfee'] ?? 0;
  641. $express->status = 0;
  642. $express->cancelTime = date('Y-m-d H:i:s');
  643. $express->deductfee = $deductFee;
  644. $express->save();
  645. util::success($result, "订单取消成功");
  646. } else {
  647. Yii::error("订单取消失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  648. util::fail('取消失败,编号6693');
  649. }
  650. } catch (\Exception $e) {
  651. Yii::error("订单取消异常:" . $e->getMessage(), 'intraCity');
  652. util::fail("系统出错");
  653. }
  654. }
  655. /**
  656. * 微信回调接口
  657. */
  658. public function actionCallback()
  659. {
  660. $callbackData = Yii::$app->request->post();
  661. if (empty($callbackData)) {
  662. $postStr = file_get_contents('php://input');
  663. $callbackData = json_decode($postStr, true);
  664. if (empty($callbackData)) {
  665. util::fail('回调请求的数据为空');
  666. }
  667. }
  668. // 从配置中获取安全token
  669. // $token = Yii::$app->params['wx_intracity_token'] ?? 'your_token_here';
  670. // $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
  671. // 记录回调日志
  672. Yii::info('同城配送回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'intraCity');
  673. $storeOrderId = $callbackData['store_order_id'] ?? 0;
  674. $wxStoreId = $callbackData['wx_store_id'] ?? ''; //微信门店编号
  675. if (empty($storeOrderId) || empty($wxStoreId)) {
  676. Yii::error("配送单回调接口的请求参数出错,storeOrderId = $storeOrderId, wxStoreId = $wxStoreId");
  677. return $this->asJson(['return_code' => 1, 'return_msg' => '请求参数不正确']);
  678. }
  679. try {
  680. // 使用 storeOrderId 与 wxStoreId 去查询 xhExpressOrder 表
  681. $eo = GhsExpressOrderClass::getByCondition(['storeOrderId' => $storeOrderId, 'wxStoreId' => $wxStoreId], true, false);
  682. if (empty($eo)) {
  683. Yii::error('订单不存在. store_order_id: ' . $storeOrderId . ',wx_store_id: ' . $wxStoreId);
  684. return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误']);
  685. }
  686. $orderId = $eo->orderId ?? 0;
  687. if (empty($orderId)) {
  688. $errRemind = '订单号为空,store_order_id: ' . $storeOrderId . ',wx_store_id: ' . $wxStoreId;
  689. Yii::error($errRemind, 'intracity');
  690. noticeUtil::push($errRemind, '15280215347');
  691. return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误2']);
  692. }
  693. $order = OrderClass::getById($orderId, true);//todo 注意,这里必须取全部字段,否则下面发错会出错!!!!!!!!
  694. if (empty($order)) {
  695. $errRemind = '没有找到订单,store_order_id: ' . $storeOrderId . ',wx_store_id: ' . $wxStoreId;
  696. Yii::error($errRemind, 'intracity');
  697. noticeUtil::push($errRemind, '15280215347');
  698. return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误3']);
  699. }
  700. $cgId = $order->purchaseId ?? 0;
  701. $cg = PurchaseClass::getById($cgId, true);
  702. if (empty($cg)) {
  703. $errRemind = '没有找到采购单,store_order_id: ' . $storeOrderId . ',wx_store_id: ' . $wxStoreId;
  704. Yii::error($errRemind, 'intracity');
  705. noticeUtil::push($errRemind, '15280215347');
  706. return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误4']);
  707. }
  708. $orderStatus = intval($callbackData['order_status']);
  709. //同个配送单,同个状态的,5秒内不允许重复请求,重要勿删
  710. $uniqueId = $storeOrderId . '_' . $orderStatus;
  711. util::checkRepeatCommit($uniqueId, 5);
  712. // ====================== 开启事务
  713. $tx = Yii::$app->db->beginTransaction();
  714. $msg = ''; // 发通知的内容(默认为空,根据情况生成。没有内容则不会发通知)
  715. switch ($orderStatus) {
  716. // 订单创建成功,已叫跑腿,等跑腿接单
  717. case IntraCityExpress::ORDER_STATUS_CREATED:
  718. if ($order->status == 3) {
  719. $eo->orderStatus = 1;//已叫跑腿,等跑腿接单
  720. $eo->save();
  721. $order->sendStatus = 0;//已叫跑腿,等跑腿接单
  722. $order->save();
  723. $cg->sendStatus = 0;//已叫跑腿,等跑腿接单
  724. $cg->save();
  725. }
  726. break;
  727. // 配送员接单
  728. case IntraCityExpress::ORDER_STATUS_ACCEPTED:
  729. if ($order->status == 3) {
  730. $eo->orderStatus = 4;//跑腿已接单
  731. $eo->save();
  732. $order->sendStatus = 1;//跑腿已接单
  733. $order->save();
  734. $cg->sendStatus = 1;//跑腿已接单
  735. $cg->save();
  736. }
  737. break;
  738. // 配送员到店
  739. case IntraCityExpress::ORDER_STATUS_ARRIVED:
  740. if ($order->status == 3) {
  741. $eo->orderStatus = 5;//跑腿到店
  742. $eo->save();
  743. $order->sendStatus = 4;//跑腿到店
  744. $order->save();
  745. $cg->sendStatus = 4;//跑腿到店
  746. $cg->save();
  747. }
  748. break;
  749. // 配送中
  750. case IntraCityExpress::ORDER_STATUS_DELIVERING:
  751. if ($order->status == 3) {
  752. $eo->orderStatus = 6;//跑腿配送中
  753. $eo->save();
  754. $order->sendStatus = 5;//跑腿配送中
  755. $order->save();
  756. $cg->sendStatus = 5;//跑腿配送中
  757. $cg->save();
  758. }
  759. break;
  760. // 配送完成
  761. case IntraCityExpress::ORDER_STATUS_COMPLETED:
  762. if ($order->status == 3) {
  763. $eo->orderStatus = 8;//配送完成
  764. $eo->save();
  765. $order->sendStatus = 2;//已送达
  766. $order->save();
  767. $cg->sendStatus = 2;//已送达
  768. $cg->save();
  769. $params = ['staffId' => 0, 'staffName' => ''];
  770. OrderClass::confirmReach($order, $params);
  771. }
  772. break;
  773. // 商家取消订单
  774. case IntraCityExpress::ORDER_STATUS_CANCELED_BY_MERCHANT:
  775. //取消已发货,回滚到待发货
  776. if ($order->status == 3) {
  777. $eo->orderStatus = 2;
  778. $eo->save();
  779. $order->sendStatus = 3;
  780. $order->save();
  781. $cg->sendStatus = 3;
  782. $cg->save();
  783. $params = ['staffId' => 0, 'staffName' => ''];
  784. OrderClass::cancelOrderSend($order, $params);
  785. $msg = '取消配送,订单编号 ';
  786. }
  787. break;
  788. // 配送方取消订单
  789. case IntraCityExpress::ORDER_STATUS_CANCELED_BY_DELIVERY:
  790. //取消已发货,回滚到待发货
  791. if ($order->status == 3) {
  792. $eo->orderStatus = 3;
  793. $eo->status = 0;
  794. $eo->save();
  795. $order->sendStatus = 3;
  796. $order->save();
  797. $cg->sendStatus = 3;
  798. $cg->save();
  799. $params = ['staffId' => 0, 'staffName' => ''];
  800. OrderClass::cancelOrderSend($order, $params);
  801. $msg = '配送方取消配送,订单编号 ';
  802. }
  803. break;
  804. // 配送员撤单
  805. case IntraCityExpress::ORDER_STATUS_WITHDRAWN:
  806. //todo 配送员撤单,目前我们认为会重新派单,先不要有任何操作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  807. $msg = '跑腿取消配送,订单编号 ';
  808. break;
  809. // 配送异常
  810. case IntraCityExpress::ORDER_STATUS_EXCEPTION:
  811. //取消已发货,回滚到待发货
  812. if ($order->status == 3) {
  813. $eo->orderStatus = 9;
  814. $eo->status = 0;
  815. $eo->save();
  816. $order->sendStatus = 3;
  817. $order->save();
  818. $cg->sendStatus = 3;
  819. $cg->save();
  820. $params = ['staffId' => 0, 'staffName' => ''];
  821. OrderClass::cancelOrderSend($order, $params);
  822. $msg = '订单配送异常,订单编号 ';
  823. }
  824. break;
  825. }
  826. // ====================== 事务提交
  827. $tx->commit();
  828. if (!empty($msg)) {
  829. $order = OrderClass::getById($orderId, true, 'id, shopId, customName, sendNum');
  830. $shop = ShopClass::getById($order->shopId, true, 'id, ptStyle, mainId');
  831. $result = $msg . $order->sendNum;
  832. WxMessageClass::expressErrorInform($shop, $order->customName, $orderId, $result);
  833. }
  834. // 成功状态的返回数据
  835. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  836. } catch (\Exception $e) {
  837. Yii::error('同城配送回调处理异常:' . $e->getMessage());
  838. return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误']);
  839. }
  840. }
  841. //模拟接口回调 ssh 20250826
  842. public function actionMockNotify()
  843. {
  844. if (getenv('YII_ENV') != 'production') {
  845. $post = Yii::$app->request->post();
  846. $orderStatus = $post['orderStatus'] ?? 0;
  847. $wxOrderId = $post['wxOrderId'] ?? '';
  848. $ptStyle = dict::getDict('ptStyle', 'hd');
  849. $merchant = WxOpenClass::getWxInfo();
  850. $ret = IntraCityExpress::mockNotify($orderStatus, $wxOrderId, '', '', $merchant, $ptStyle);
  851. util::success($ret);
  852. }
  853. util::complete();
  854. }
  855. }