MtController.php 31 KB

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