MtController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. $hasGoodsId = $hasGoods->id ?? 0;
  231. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $appSpuCode, 'access_token' => $accessToken];
  232. $goodsInfo = $app->retail->getDetail($params);
  233. Yii::info('获取商品详情:' . $goodsInfo . ' ' . json_encode($params));
  234. $goodsInfo = json_decode($goodsInfo, true);
  235. if (isset($goodsInfo['error'])) {
  236. noticeUtil::push("美团修改商品,通知我们时出错了,没有找到商品图片", '15280215347');
  237. return Json::encode(['data' => 'ok']);
  238. }
  239. $imgString = $goodsInfo['data']['picture'] ?? '';
  240. if (empty($imgString)) {
  241. noticeUtil::push("美团修改商品,通知我们时出错了,没有商品图片...", '15280215347');
  242. return Json::encode(['data' => 'ok']);
  243. }
  244. $goodsName = $goodsInfo['data']['name'] ?? '';
  245. $goodsPrice = $goodsInfo['data']['price'] ?? '';
  246. $description = $goodsInfo['data']['description'] ?? '';
  247. $sh = strstr($description, '【鲜花】') !== false ? 1 : 0;
  248. $currentStr = md5($goodsName . '_' . $goodsPrice . '_' . $description . '_' . $sh . '_' . $imgString);
  249. $cacheKey = 'mt_goods_id_' . $hasGoodsId;
  250. $cacheStr = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  251. if ($cacheStr == $currentStr) {
  252. noticeUtil::push("美团修改商品,通知我们时发现商品没有更新哦", '15280215347');
  253. return Json::encode(['data' => 'ok']);
  254. } else {
  255. Yii::$app->redis->executeCommand('SET', [$cacheKey, $currentStr]);
  256. }
  257. noticeUtil::push("美团修改商品,通知我们,商品图片:" . $imgString, '15280215347');
  258. $shopImg = [];
  259. $cover = '';
  260. $imgArr = explode(',', $imgString);
  261. if (!empty($imgArr)) {
  262. foreach ($imgArr as $imgUrl) {
  263. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  264. $shopImg[] = $shortUrl;
  265. if ($cover == '') {
  266. $cover = $shortUrl;
  267. }
  268. }
  269. }
  270. $hasGoods->price = $goodsPrice;
  271. $hasGoods->name = $goodsName;
  272. $hasGoods->cover = $cover;
  273. $hasGoods->sh = $sh;
  274. $hasGoods->shopImg = json_encode($shopImg);
  275. $hasGoods->save();
  276. $transaction->commit();
  277. return Json::encode(['data' => 'ok']);
  278. } catch (\Exception $e) {
  279. $transaction->rollBack();
  280. $msg = $e->getMessage();
  281. noticeUtil::push("美团修改商品,通知我们时出错了,错误信息:{$msg}", '15280215347');
  282. return Json::encode(['data' => 'ok']);
  283. }
  284. }
  285. //已确认订单通知
  286. public function actionConfirmOrder()
  287. {
  288. //error_reporting(E_ALL);
  289. $post = Yii::$app->request->post();
  290. $mtOrderId = $post['order_id'] ?? 0;
  291. $thirdSn = $post['day_seq'] ?? 0;
  292. //4秒内不允许第三方重复通知
  293. $cacheKey = 'mt_confirm_order_' . $mtOrderId;
  294. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  295. if (!empty($has)) {
  296. Yii::info("美团已确认订单通知,出现重复通知,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}");
  297. return Json::encode(['data' => 'ok']);
  298. }
  299. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  300. // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  301. if (empty($post)) {
  302. return Json::encode(['data' => 'ok']);
  303. }
  304. Yii::info('confirmOrderData: ' . json_encode($post));
  305. $connection = Yii::$app->db;//事务处理
  306. $transaction = $connection->beginTransaction();
  307. try {
  308. $shopId = $post['app_poi_code'] ?? '';
  309. $shop = ShopClass::getById($shopId, true);
  310. if (empty($shop)) {
  311. noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  312. return Json::encode(['data' => 'ok']);
  313. }
  314. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  315. if (empty($shopExt)) {
  316. noticeUtil::push("美团确认订单报错,没有找到门店拓展信息,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  317. return Json::encode(['data' => 'ok']);
  318. }
  319. $mainId = $shop->mainId ?? 0;
  320. $sjId = $shop->sjId ?? 0;
  321. $customId = $shop->defaultCustomId ?? 0;
  322. if (empty($customId)) {
  323. noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  324. return Json::encode(['data' => 'ok']);
  325. }
  326. $custom = CustomClass::getById($customId);
  327. if (empty($custom)) {
  328. noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  329. return Json::encode(['data' => 'ok']);
  330. }
  331. foreach ($post as $k => $val) {
  332. $post[$k] = urldecode($val);
  333. }
  334. MtOrderClass::add($post);
  335. $detail = $post['detail'] ?? '';
  336. if (empty($detail)) {
  337. noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  338. return Json::encode(['data' => 'ok']);
  339. }
  340. $mtGoodsData = json_decode($detail, true);
  341. if (empty($mtGoodsData)) {
  342. noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  343. return Json::encode(['data' => 'ok']);
  344. }
  345. Yii::info('商品信息:' . json_encode($mtGoodsData));
  346. $productList = [];
  347. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  348. if (empty($mtCategory)) {
  349. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  350. }
  351. $catId = $mtCategory->id ?? 0;
  352. if (empty($catId)) {
  353. noticeUtil::push("美团确认订单报错,美团默认分类没有找到,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  354. return Json::encode(['data' => 'ok']);
  355. }
  356. $caution = isset($post['caution']) && !empty($post['caution']) ? $post['caution'] : '';
  357. $remark = $caution;
  358. $bookMobile = $post['order_phone_number'] ?? '';
  359. $receiveMobile = $post['recipient_phone'] ?? '';
  360. $receiveUserName = $post['recipient_name'] ?? '';
  361. $fullAddress = $post['recipient_address'] ?? '';
  362. $cover = '';
  363. $goodsNum = 0;
  364. foreach ($mtGoodsData as $mtKey => $mtGoods) {
  365. $mtGoodsSpu = $mtGoods['app_spu_code'] ?? '';
  366. if (empty($mtGoodsSpu)) {
  367. if (isset($mtGoods['app_food_code']) && !empty($mtGoods['app_food_code'])) {
  368. $mtGoodsSpu = $mtGoods['app_food_code'];
  369. }
  370. }
  371. $goodsPrice = $mtGoods['price'] ?? 0;
  372. $num = $mtGoods['quantity'] ?? 0;
  373. $goodsNum = bcadd($num, $goodsNum);
  374. $currentGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  375. if (empty($currentGoods)) {
  376. noticeUtil::push("美团确认订单报错,数据库没有相应商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  377. return Json::encode(['data' => 'ok']);
  378. }
  379. $id = $currentGoods->id ?? 0;
  380. $unitType = 0;
  381. $property = dict::getDict('property', 'goods');
  382. $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
  383. $productList = array_merge($goods, $productList);
  384. }
  385. $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
  386. if (empty($poi_receive_detail_yuan)) {
  387. noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  388. util::stop();
  389. }
  390. $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
  391. if (is_array($poi_receive_detail_yuan_data) == false) {
  392. noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  393. util::stop();
  394. }
  395. $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
  396. if ($onlinePayment == 0) {
  397. noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  398. util::stop();
  399. }
  400. $modifyPrice = $onlinePayment;
  401. //技术服务费
  402. $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0;
  403. $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? '';
  404. //履约服务费
  405. $performanceServiceFee = 0;
  406. if (!empty($reconciliationExtras)) {
  407. $extArr = json_decode($reconciliationExtras, true);
  408. $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0;
  409. }
  410. //运费
  411. $sendCost = $post['shipping_fee'] ?? 0;
  412. //打包费
  413. $labourCost = $post['package_bag_money_yuan'] ?? 0;
  414. //服务费和手续费
  415. $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2);
  416. $estimate_arrival_time = $post['estimate_arrival_time'] ?? 0;
  417. $reachPeriod = date("H:i", $estimate_arrival_time);
  418. $reachDate = date("Y-m-d", $estimate_arrival_time);
  419. $reachTime = $estimate_arrival_time;
  420. $orderData = [
  421. 'product' => $productList,
  422. 'fromType' => dict::getDict('fromType', 'mt'),
  423. 'labourCost' => $labourCost,
  424. 'sendCost' => $sendCost,
  425. 'shopId' => $shopId,
  426. 'mainId' => $mainId,
  427. 'sjId' => $sjId,
  428. 'modifyPrice' => $modifyPrice,
  429. 'serviceFee' => $serviceFee,
  430. 'thirdSn' => $thirdSn,
  431. 'cover' => $cover,
  432. 'reachTime' => $reachTime,
  433. 'reachDate' => $reachDate,
  434. 'reachPeriod' => $reachPeriod,
  435. 'remark' => $remark,
  436. 'goodsNum' => $goodsNum,
  437. 'bookMobile' => $bookMobile,
  438. 'receiveMobile' => $receiveMobile,
  439. 'receiveUserName' => $receiveUserName,
  440. 'fullAddress' => $fullAddress,
  441. 'thirdOrderId' => $mtOrderId,
  442. ];
  443. $hasPay = dict::getDict('hasPay', 'payed');
  444. $order = OrderService::createFinishOrder($orderData, $custom, $hasPay);
  445. $transaction->commit();
  446. //新制作单提示
  447. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  448. ShopExtClass::newWorkRemind($shopExt, $order);
  449. return Json::encode(['data' => 'ok']);
  450. } catch (\Exception $e) {
  451. $transaction->rollBack();
  452. $msg = $e->getMessage();
  453. noticeUtil::push("确认订单报错了,美团订单id:{$mtOrderId} 美团编号:{$thirdSn},错误信息:{$msg}", '15280215347');
  454. return Json::encode(['data' => 'ok']);
  455. }
  456. }
  457. //取消订单
  458. public function actionCancelOrder()
  459. {
  460. $post = Yii::$app->request->post();
  461. $mtOrderId = $post['order_id'] ?? 0;
  462. $shopId = $post['app_poi_code'] ?? '';
  463. if (empty($post)) {
  464. return Json::encode(['data' => 'ok']);
  465. }
  466. Yii::info('cancelOrderData: ' . json_encode($post));
  467. //4秒内不允许第三方重复通知
  468. $cacheKey = 'mt_cancel_order_' . $mtOrderId;
  469. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  470. if (!empty($has)) {
  471. Yii::info("美团取消订单通知,出现重复通知,orderId:{$mtOrderId}");
  472. return Json::encode(['data' => 'ok']);
  473. }
  474. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  475. $mt = dict::getDict('fromType', 'mt');
  476. $order = OrderClass::getByCondition(['fromType' => $mt, 'thirdOrderId' => $mtOrderId], true);
  477. if (empty($order)) {
  478. //没有接单时取消订单
  479. return Json::encode(['data' => 'ok']);
  480. }
  481. //只要制作单没有完成,则走取消订单流程。如果有制作单完成,则订单状态和制作单暂时先不管,后面要兼容。
  482. $orderId = $order->id ?? 0;
  483. $workList = WorkClass::getAllByCondition(['orderId' => $orderId], null, '*', null, true);
  484. $hasCompleteWork = false;
  485. if (!empty($workList)) {
  486. foreach ($workList as $work) {
  487. if ($work->status == 1) {
  488. $hasCompleteWork = true;
  489. }
  490. }
  491. }
  492. //取消订单和制作单
  493. if ($hasCompleteWork == false) {
  494. $order->status = 5;
  495. $order->save();
  496. if (!empty($workList)) {
  497. foreach ($workList as $work) {
  498. $work->status = 2;
  499. $work->save();
  500. }
  501. }
  502. }
  503. //如果制作单完成了还会出现取消订单情况,这个等于全部退款,这个后面再考虑兼容!!!!!
  504. //制作单取消通知
  505. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  506. ShopExtClass::orderCancelRemind($shopExt, $order);
  507. return Json::encode(['data' => 'ok']);
  508. }
  509. //推送已支付订单
  510. public function actionNewOrder()
  511. {
  512. return Json::encode(['data' => 'ok']);
  513. }
  514. //部分退款
  515. public function actionPartRefund()
  516. {
  517. return Json::encode(['data' => 'ok']);
  518. }
  519. //全部退款
  520. public function actionAllRefund()
  521. {
  522. return Json::encode(['data' => 'ok']);
  523. }
  524. }