MtController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizHd\goods\classes\CategoryClass;
  6. use bizHd\goods\classes\GoodsCategoryClass;
  7. use bizHd\goods\classes\GoodsClass;
  8. use bizHd\meituan\classes\MtOrderClass;
  9. use bizHd\order\classes\OrderClass;
  10. use bizHd\order\services\OrderService;
  11. use bizHd\shop\classes\ShopClass;
  12. use bizHd\work\classes\WorkClass;
  13. use common\components\dict;
  14. use common\components\httpUtil;
  15. use common\components\mtUtil;
  16. use common\components\noticeUtil;
  17. use common\components\oss;
  18. use common\components\util;
  19. use Yii;
  20. use Shishaoqi\MeituanFlashPurchase\Application;
  21. use yii\helpers\Json;
  22. class MtController extends PublicController
  23. {
  24. public $guestAccess = [];
  25. public function actionGoodsAdd()
  26. {
  27. $post = Yii::$app->request->post();
  28. $connection = Yii::$app->db;//事务处理
  29. $transaction = $connection->beginTransaction();
  30. try {
  31. if (isset($post['retail_data']) == false) {
  32. noticeUtil::push("美团添加商品,通知我们时出错了,没有获取到相应信息", '15280215347');
  33. return Json::encode(['data' => 'ok']);
  34. }
  35. $retailData = $post['retail_data'];
  36. $retail = urldecode(urldecode($retailData));
  37. $info = json_decode($retail);
  38. $obj = $info[0] ?? null;
  39. $shopId = $obj->app_poi_code ?? 0;
  40. $shop = ShopClass::getById($shopId, true);
  41. if (empty($shop)) {
  42. noticeUtil::push("美团添加商品,通知我们时出错了,没有找到门店", '15280215347');
  43. return Json::encode(['data' => 'ok']);
  44. }
  45. $sjId = $shop->sjId ?? 0;
  46. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  47. if (empty($ext)) {
  48. noticeUtil::push("美团添加商品,通知我们时出错了,没有找到门店拓展信息", '15280215347');
  49. return Json::encode(['data' => 'ok']);
  50. }
  51. $mainId = $shop->mainId ?? 0;
  52. if (empty($mainId)) {
  53. noticeUtil::push("美团添加商品,通知我们时出错了,没有main信息", '15280215347');
  54. return Json::encode(['data' => 'ok']);
  55. }
  56. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  57. if (empty($mtCategory)) {
  58. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  59. }
  60. $catId = $mtCategory->id ?? 0;
  61. if (empty($catId)) {
  62. noticeUtil::push("美团添加商品,通知我们时出错了,没有美团的分类id", '15280215347');
  63. return Json::encode(['data' => 'ok']);
  64. }
  65. $config = dict::getDict('mtConfig');
  66. $app = new Application($config);
  67. $accessToken = mtUtil::getAccessToken($ext);
  68. $appSpuCode = $obj->app_spu_code ?? '';
  69. $hasGoods = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true);
  70. if (!empty($hasGoods)) {
  71. noticeUtil::push("美团添加商品,通知我们时,发现已经有这个商品了,mainId:{$mainId} spu:{$appSpuCode}", '15280215347');
  72. return Json::encode(['data' => 'ok']);
  73. }
  74. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $appSpuCode, 'access_token' => $accessToken];
  75. $goodsInfo = $app->retail->getDetail($params);
  76. Yii::info('获取商品详情:' . $goodsInfo . ' ' . json_encode($params));
  77. $goodsInfo = json_decode($goodsInfo, true);
  78. if (isset($goodsInfo['error'])) {
  79. noticeUtil::push("美团添加商品,通知我们时出错了,没有找到商品图片", '15280215347');
  80. return Json::encode(['data' => 'ok']);
  81. }
  82. $imgString = $goodsInfo['data']['picture'] ?? '';
  83. if (empty($imgString)) {
  84. noticeUtil::push("美团添加商品,通知我们时出错了,没有商品图片...", '15280215347');
  85. return Json::encode(['data' => 'ok']);
  86. }
  87. noticeUtil::push("美团添加商品,通知我们,商品图片:" . $imgString, '15280215347');
  88. $goodsName = $goodsInfo['data']['name'] ?? '';
  89. $goodsPrice = $goodsInfo['data']['price'] ?? '';
  90. $shopImg = [];
  91. $cover = '';
  92. $imgArr = explode(',', $imgString);
  93. if (!empty($imgArr)) {
  94. foreach ($imgArr as $imgUrl) {
  95. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  96. $shopImg[] = $shortUrl;
  97. if ($cover == '') {
  98. $cover = $shortUrl;
  99. }
  100. }
  101. }
  102. $catIds = [$catId];
  103. $addData = [
  104. 'name' => $goodsName,
  105. 'flower' => 1,
  106. 'property' => 0,
  107. 'price' => $goodsPrice,
  108. 'sjId' => $sjId,
  109. 'mainId' => $mainId,
  110. 'shopId' => $shopId,
  111. 'status' => 0,
  112. 'delStatus' => 0,
  113. 'shopImg' => $shopImg,
  114. 'catIds' => $catIds,
  115. 'cover' => $cover,
  116. 'spu' => $appSpuCode,
  117. ];
  118. GoodsClass::addGoods($addData);
  119. $transaction->commit();
  120. return Json::encode(['data' => 'ok']);
  121. } catch (\Exception $e) {
  122. $transaction->rollBack();
  123. $msg = $e->getMessage();
  124. noticeUtil::push("美团添加商品,通知我们时出错了,错误信息:{$msg}", '15280215347');
  125. return Json::encode(['data' => 'ok']);
  126. }
  127. }
  128. //删除商品 ssh 20220914
  129. public function actionGoodsDel()
  130. {
  131. $post = Yii::$app->request->post();
  132. if (isset($post['retail_data']) == false) {
  133. noticeUtil::push("美团删除商品,通知我们时出错了,没有获取到相应信息", '15280215347');
  134. return Json::encode(['data' => 'ok']);
  135. }
  136. $retailData = $post['retail_data'];
  137. $retail = urldecode(urldecode($retailData));
  138. $info = json_decode($retail);
  139. $obj = $info[0] ?? null;
  140. $shopId = $obj->app_poi_code ?? 0;
  141. $shop = ShopClass::getById($shopId, true);
  142. if (empty($shop)) {
  143. noticeUtil::push("美团删除商品,通知我们时出错了,没有找到门店", '15280215347');
  144. return Json::encode(['data' => 'ok']);
  145. }
  146. $sjId = $shop->sjId ?? 0;
  147. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  148. if (empty($ext)) {
  149. noticeUtil::push("美团删除商品,通知我们时出错了,没有找到门店拓展信息", '15280215347');
  150. return Json::encode(['data' => 'ok']);
  151. }
  152. $mainId = $shop->mainId ?? 0;
  153. if (empty($mainId)) {
  154. noticeUtil::push("美团删除商品,通知我们时出错了,没有main信息", '15280215347');
  155. return Json::encode(['data' => 'ok']);
  156. }
  157. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  158. if (empty($mtCategory)) {
  159. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  160. }
  161. $catId = $mtCategory->id ?? 0;
  162. if (empty($catId)) {
  163. noticeUtil::push("美团删除商品,通知我们时出错了,没有美团的分类id", '15280215347');
  164. return Json::encode(['data' => 'ok']);
  165. }
  166. $appSpuCode = $obj->app_spu_code ?? '';
  167. $hasGoods = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true);
  168. if (empty($hasGoods)) {
  169. return Json::encode(['data' => 'ok']);
  170. }
  171. $hasGoods->delStatus = 1;
  172. $hasGoods->save();
  173. $goodsId = $hasGoods->id ?? 0;
  174. GoodsCategoryClass::updateByCondition(['gId' => $goodsId], ['delStatus' => 1]);
  175. return Json::encode(['data' => 'ok']);
  176. }
  177. //修改商品 ssh 20220914
  178. public function actionGoodsUpdate()
  179. {
  180. $post = Yii::$app->request->post();
  181. $connection = Yii::$app->db;//事务处理
  182. $transaction = $connection->beginTransaction();
  183. try {
  184. if (isset($post['retail_data']) == false) {
  185. noticeUtil::push("美团修改商品,通知我们时出错了,没有获取到相应信息", '15280215347');
  186. return Json::encode(['data' => 'ok']);
  187. }
  188. $retailData = $post['retail_data'];
  189. $retail = urldecode(urldecode($retailData));
  190. $info = json_decode($retail);
  191. $obj = $info[0] ?? null;
  192. $shopId = $obj->app_poi_code ?? 0;
  193. $shop = ShopClass::getById($shopId, true);
  194. if (empty($shop)) {
  195. noticeUtil::push("美团修改商品,通知我们时出错了,没有找到门店", '15280215347');
  196. return Json::encode(['data' => 'ok']);
  197. }
  198. $sjId = $shop->sjId ?? 0;
  199. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  200. if (empty($ext)) {
  201. noticeUtil::push("美团修改商品,通知我们时出错了,没有找到门店拓展信息", '15280215347');
  202. return Json::encode(['data' => 'ok']);
  203. }
  204. $mainId = $shop->mainId ?? 0;
  205. if (empty($mainId)) {
  206. noticeUtil::push("美团修改商品,通知我们时出错了,没有main信息", '15280215347');
  207. return Json::encode(['data' => 'ok']);
  208. }
  209. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  210. if (empty($mtCategory)) {
  211. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  212. }
  213. $catId = $mtCategory->id ?? 0;
  214. if (empty($catId)) {
  215. noticeUtil::push("美团修改商品,通知我们时出错了,没有美团的分类id", '15280215347');
  216. return Json::encode(['data' => 'ok']);
  217. }
  218. $config = dict::getDict('mtConfig');
  219. $app = new Application($config);
  220. $accessToken = mtUtil::getAccessToken($ext);
  221. $appSpuCode = $obj->app_spu_code ?? '';
  222. $hasGoods = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true);
  223. if (empty($hasGoods)) {
  224. noticeUtil::push("美团修改商品,通知我们时,没有发现这个商品,mainId:{$mainId} spu:{$appSpuCode}", '15280215347');
  225. return Json::encode(['data' => 'ok']);
  226. }
  227. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $appSpuCode, 'access_token' => $accessToken];
  228. $goodsInfo = $app->retail->getDetail($params);
  229. Yii::info('获取商品详情:' . $goodsInfo . ' ' . json_encode($params));
  230. $goodsInfo = json_decode($goodsInfo, true);
  231. if (isset($goodsInfo['error'])) {
  232. noticeUtil::push("美团修改商品,通知我们时出错了,没有找到商品图片", '15280215347');
  233. return Json::encode(['data' => 'ok']);
  234. }
  235. $imgString = $goodsInfo['data']['picture'] ?? '';
  236. if (empty($imgString)) {
  237. noticeUtil::push("美团修改商品,通知我们时出错了,没有商品图片...", '15280215347');
  238. return Json::encode(['data' => 'ok']);
  239. }
  240. noticeUtil::push("美团修改商品,通知我们,商品图片:" . $imgString, '15280215347');
  241. $goodsName = $goodsInfo['data']['name'] ?? '';
  242. $goodsPrice = $goodsInfo['data']['price'] ?? '';
  243. $shopImg = [];
  244. $cover = '';
  245. $imgArr = explode(',', $imgString);
  246. if (!empty($imgArr)) {
  247. foreach ($imgArr as $imgUrl) {
  248. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  249. $shopImg[] = $shortUrl;
  250. if ($cover == '') {
  251. $cover = $shortUrl;
  252. }
  253. }
  254. }
  255. $hasGoods->price = $goodsPrice;
  256. $hasGoods->name = $goodsName;
  257. $hasGoods->cover = $cover;
  258. $hasGoods->shopImg = json_encode($shopImg);
  259. $hasGoods->save();
  260. $transaction->commit();
  261. return Json::encode(['data' => 'ok']);
  262. } catch (\Exception $e) {
  263. $transaction->rollBack();
  264. $msg = $e->getMessage();
  265. noticeUtil::push("美团修改商品,通知我们时出错了,错误信息:{$msg}", '15280215347');
  266. return Json::encode(['data' => 'ok']);
  267. }
  268. }
  269. //已确认订单通知
  270. public function actionConfirmOrder()
  271. {
  272. //error_reporting(E_ALL);
  273. $post = Yii::$app->request->post();
  274. $mtOrderId = $post['order_id'] ?? 0;
  275. $thirdSn = $post['day_seq'] ?? 0;
  276. //4秒内不允许第三方重复通知
  277. $cacheKey = 'mt_confirm_order_' . $mtOrderId;
  278. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  279. if (!empty($has)) {
  280. Yii::info("美团已确认订单通知,出现重复通知,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}");
  281. return Json::encode(['data' => 'ok']);
  282. }
  283. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  284. // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  285. if (empty($post)) {
  286. return Json::encode(['data' => 'ok']);
  287. }
  288. Yii::info('confirmOrderData: ' . json_encode($post));
  289. $connection = Yii::$app->db;//事务处理
  290. $transaction = $connection->beginTransaction();
  291. try {
  292. $shopId = $post['app_poi_code'] ?? '';
  293. $shop = ShopClass::getById($shopId, true);
  294. if (empty($shop)) {
  295. noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  296. return Json::encode(['data' => 'ok']);
  297. }
  298. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  299. if (empty($shopExt)) {
  300. noticeUtil::push("美团确认订单报错,没有找到门店拓展信息,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  301. return Json::encode(['data' => 'ok']);
  302. }
  303. $mainId = $shop->mainId ?? 0;
  304. $sjId = $shop->sjId ?? 0;
  305. $customId = $shop->defaultCustomId ?? 0;
  306. if (empty($customId)) {
  307. noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  308. return Json::encode(['data' => 'ok']);
  309. }
  310. $custom = CustomClass::getById($customId);
  311. if (empty($custom)) {
  312. noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  313. return Json::encode(['data' => 'ok']);
  314. }
  315. foreach ($post as $k => $val) {
  316. $post[$k] = urldecode($val);
  317. }
  318. MtOrderClass::add($post);
  319. $detail = $post['detail'] ?? '';
  320. if (empty($detail)) {
  321. noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  322. return Json::encode(['data' => 'ok']);
  323. }
  324. $mtGoodsData = json_decode($detail, true);
  325. if (empty($mtGoodsData)) {
  326. noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  327. return Json::encode(['data' => 'ok']);
  328. }
  329. Yii::info('商品信息:' . json_encode($mtGoodsData));
  330. $productList = [];
  331. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  332. if (empty($mtCategory)) {
  333. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  334. }
  335. $catId = $mtCategory->id ?? 0;
  336. if (empty($catId)) {
  337. noticeUtil::push("美团确认订单报错,美团默认分类没有找到,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  338. return Json::encode(['data' => 'ok']);
  339. }
  340. $caution = isset($post['caution']) && !empty($post['caution']) ? $post['caution'] : '';
  341. $remark = $caution;
  342. $bookMobile = $post['order_phone_number'] ?? '';
  343. $receiveMobile = $post['recipient_phone'] ?? '';
  344. $receiveUserName = $post['recipient_name'] ?? '';
  345. $fullAddress = $post['recipient_address'] ?? '';
  346. $cover = '';
  347. $goodsNum = 0;
  348. foreach ($mtGoodsData as $mtKey => $mtGoods) {
  349. $mtGoodsSpu = $mtGoods['app_spu_code'] ?? '';
  350. if (empty($mtGoodsSpu)) {
  351. if (isset($mtGoods['app_food_code']) && !empty($mtGoods['app_food_code'])) {
  352. $mtGoodsSpu = $mtGoods['app_food_code'];
  353. }
  354. }
  355. $goodsPrice = $mtGoods['price'] ?? 0;
  356. $num = $mtGoods['quantity'] ?? 0;
  357. $goodsNum = bcadd($num, $goodsNum);
  358. $currentGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  359. if (empty($currentGoods)) {
  360. noticeUtil::push("美团确认订单报错,数据库没有相应商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  361. return Json::encode(['data' => 'ok']);
  362. }
  363. $id = $currentGoods->id ?? 0;
  364. $unitType = 0;
  365. $property = dict::getDict('property', 'goods');
  366. $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
  367. $productList = array_merge($goods, $productList);
  368. }
  369. $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
  370. if (empty($poi_receive_detail_yuan)) {
  371. noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  372. util::stop();
  373. }
  374. $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
  375. if (is_array($poi_receive_detail_yuan_data) == false) {
  376. noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  377. util::stop();
  378. }
  379. $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
  380. if ($onlinePayment == 0) {
  381. noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  382. util::stop();
  383. }
  384. $modifyPrice = $onlinePayment;
  385. //技术服务费
  386. $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0;
  387. $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? '';
  388. //履约服务费
  389. $performanceServiceFee = 0;
  390. if (!empty($reconciliationExtras)) {
  391. $extArr = json_decode($reconciliationExtras, true);
  392. $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0;
  393. }
  394. //运费
  395. $sendCost = $post['shipping_fee'] ?? 0;
  396. //打包费
  397. $labourCost = $post['package_bag_money_yuan'] ?? 0;
  398. //服务费和手续费
  399. $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2);
  400. $estimate_arrival_time = $post['estimate_arrival_time'] ?? 0;
  401. $reachPeriod = date("H:i", $estimate_arrival_time);
  402. $reachDate = date("Y-m-d", $estimate_arrival_time);
  403. $reachTime = $estimate_arrival_time;
  404. $orderData = [
  405. 'product' => $productList,
  406. 'fromType' => dict::getDict('fromType', 'mt'),
  407. 'labourCost' => $labourCost,
  408. 'sendCost' => $sendCost,
  409. 'shopId' => $shopId,
  410. 'mainId' => $mainId,
  411. 'sjId' => $sjId,
  412. 'modifyPrice' => $modifyPrice,
  413. 'serviceFee' => $serviceFee,
  414. 'thirdSn' => $thirdSn,
  415. 'cover' => $cover,
  416. 'reachTime' => $reachTime,
  417. 'reachDate' => $reachDate,
  418. 'reachPeriod' => $reachPeriod,
  419. 'remark' => $remark,
  420. 'goodsNum' => $goodsNum,
  421. 'bookMobile' => $bookMobile,
  422. 'receiveMobile' => $receiveMobile,
  423. 'receiveUserName' => $receiveUserName,
  424. 'fullAddress' => $fullAddress,
  425. 'thirdOrderId' => $mtOrderId,
  426. ];
  427. $hasPay = dict::getDict('hasPay', 'payed');
  428. $order = OrderService::createFinishOrder($orderData, $custom, $hasPay);
  429. $transaction->commit();
  430. //新制作单提示
  431. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  432. ShopExtClass::newWorkRemind($shopExt, $order);
  433. return Json::encode(['data' => 'ok']);
  434. } catch (\Exception $e) {
  435. $transaction->rollBack();
  436. $msg = $e->getMessage();
  437. noticeUtil::push("确认订单报错了,美团订单id:{$mtOrderId} 美团编号:{$thirdSn},错误信息:{$msg}", '15280215347');
  438. return Json::encode(['data' => 'ok']);
  439. }
  440. }
  441. //取消订单
  442. public function actionCancelOrder()
  443. {
  444. $post = Yii::$app->request->post();
  445. $mtOrderId = $post['order_id'] ?? 0;
  446. $shopId = $post['app_poi_code'] ?? '';
  447. if (empty($post)) {
  448. return Json::encode(['data' => 'ok']);
  449. }
  450. Yii::info('cancelOrderData: ' . json_encode($post));
  451. //4秒内不允许第三方重复通知
  452. $cacheKey = 'mt_cancel_order_' . $mtOrderId;
  453. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  454. if (!empty($has)) {
  455. Yii::info("美团取消订单通知,出现重复通知,orderId:{$mtOrderId}");
  456. return Json::encode(['data' => 'ok']);
  457. }
  458. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  459. $mt = dict::getDict('fromType', 'mt');
  460. $order = OrderClass::getByCondition(['fromType' => $mt, 'thirdOrderId' => $mtOrderId], true);
  461. if (empty($order)) {
  462. //没有接单时取消订单
  463. return Json::encode(['data' => 'ok']);
  464. }
  465. //只要制作单没有完成,则走取消订单流程。如果有制作单完成,则订单状态和制作单暂时先不管,后面要兼容。
  466. $orderId = $order->id ?? 0;
  467. $workList = WorkClass::getAllByCondition(['orderId' => $orderId], null, '*', null, true);
  468. $hasCompleteWork = false;
  469. if (!empty($workList)) {
  470. foreach ($workList as $work) {
  471. if ($work->status == 1) {
  472. $hasCompleteWork = true;
  473. }
  474. }
  475. }
  476. //取消订单和制作单
  477. if ($hasCompleteWork == false) {
  478. $order->status = 5;
  479. $order->save();
  480. if (!empty($workList)) {
  481. foreach ($workList as $work) {
  482. $work->status = 2;
  483. $work->save();
  484. }
  485. }
  486. }
  487. //如果制作单完成了还会出现取消订单情况,这个等于全部退款,这个后面再考虑兼容!!!!!
  488. //制作单取消通知
  489. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  490. ShopExtClass::orderCancelRemind($shopExt, $order);
  491. return Json::encode(['data' => 'ok']);
  492. }
  493. //推送已支付订单
  494. public function actionNewOrder()
  495. {
  496. return Json::encode(['data' => 'ok']);
  497. }
  498. //部分退款
  499. public function actionPartRefund()
  500. {
  501. return Json::encode(['data' => 'ok']);
  502. }
  503. //全部退款
  504. public function actionAllRefund()
  505. {
  506. return Json::encode(['data' => 'ok']);
  507. }
  508. }