MtController.php 27 KB

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