MtController.php 26 KB

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