MtController.php 31 KB

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