IntraCityController.php 38 KB

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