MtController.php 25 KB

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