MtController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace console\controllers;
  3. use common\components\noticeUtil;
  4. use common\components\util;
  5. use yii\console\Controller;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopExtClass;
  8. use bizHd\goods\classes\CategoryClass;
  9. use bizHd\goods\classes\GoodsClass;
  10. use common\components\dict;
  11. use common\components\httpUtil;
  12. use common\components\mtUtil;
  13. use Shishaoqi\MeituanFlashPurchase\Application;
  14. class MtController extends Controller
  15. {
  16. //同步商品 ssh 20220801
  17. public function actionSyncGoods()
  18. {
  19. global $argv;
  20. ini_set('memory_limit', '2045M');
  21. set_time_limit(0);
  22. $shopId = $argv[2] ?? 0;
  23. if (empty($shopId)) {
  24. util::stop('请填写门店id');
  25. }
  26. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  27. if (empty($ext)) {
  28. util::stop('没有找到门店相信息');
  29. }
  30. $shop = ShopClass::getById($shopId, true);
  31. if (empty($shop)) {
  32. util::stop('没有找到门店');
  33. }
  34. $mainId = $shop->mainId ?? 0;
  35. $sjId = $shop->sjId ?? 0;
  36. $config = dict::getDict('mtConfig');
  37. $app = new Application($config);
  38. $accessToken = mtUtil::getAccessToken($ext);
  39. $params = ['app_poi_code' => $shopId, 'access_token' => $accessToken];
  40. $json = $app->retail->retailList($params);
  41. $arr = json_decode($json, true);
  42. if (isset($arr['data']) == false || empty($arr['data'])) {
  43. util::stop('没有商品');
  44. }
  45. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  46. if (empty($mtCategory)) {
  47. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  48. }
  49. $catId = $mtCategory->id ?? 0;
  50. if (empty($catId)) {
  51. util::stop('没有找到美团的商品分类');
  52. }
  53. $goodsList = $arr['data'];
  54. foreach ($goodsList as $key => $val) {
  55. $picture = $val['picture'] ?? '';
  56. $price = $val['price'] ?? 0;
  57. $name = $val['name'] ?? '';
  58. $mtGoodsSpu = $val['app_spu_code'] ?? '';
  59. $imgArr = explode(',', $picture);
  60. if (empty($mtGoodsSpu)) {
  61. noticeUtil::push('商家有商品没有spuId,' . $name, '15280215347');
  62. continue;
  63. }
  64. $current = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $mtGoodsSpu], true);
  65. if (!empty($current)) {
  66. } else {
  67. $shopImg = [];
  68. foreach ($imgArr as $imgUrl) {
  69. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  70. $shopImg[] = $shortUrl;
  71. }
  72. $cover = $shopImg[0];
  73. $goodsData = [
  74. 'spu' => $mtGoodsSpu,
  75. 'shopImg' => $shopImg,
  76. 'name' => $name,
  77. 'sjId' => $sjId,
  78. 'mainId' => $mainId,
  79. 'shopId' => $shopId,
  80. 'catId' => $catId,
  81. 'cover' => $cover,
  82. 'unitGoodsPrice' => $price,
  83. ];
  84. GoodsClass::orderCreateGoods($goodsData);
  85. }
  86. }
  87. }
  88. //同步商品 ssh 20220801
  89. public function actionBc()
  90. {
  91. global $argv;
  92. ini_set('memory_limit', '2045M');
  93. set_time_limit(0);
  94. $shopId = $argv[2] ?? 0;
  95. if (empty($shopId)) {
  96. util::stop('请填写门店id');
  97. }
  98. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  99. if (empty($ext)) {
  100. util::stop('没有找到门店相信息');
  101. }
  102. $shop = ShopClass::getById($shopId, true);
  103. if (empty($shop)) {
  104. util::stop('没有找到门店');
  105. }
  106. $mainId = $shop->mainId ?? 0;
  107. $sjId = $shop->sjId ?? 0;
  108. $config = dict::getDict('mtConfig');
  109. $app = new Application($config);
  110. $accessToken = mtUtil::getAccessToken($ext);
  111. $params = ['app_poi_code' => $shopId, 'access_token' => $accessToken];
  112. $json = $app->retail->retailList($params);
  113. $arr = json_decode($json, true);
  114. if (isset($arr['data']) == false || empty($arr['data'])) {
  115. util::stop('没有商品');
  116. }
  117. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  118. if (empty($mtCategory)) {
  119. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  120. }
  121. $catId = $mtCategory->id ?? 0;
  122. if (empty($catId)) {
  123. util::stop('没有找到美团的商品分类');
  124. }
  125. $goodsList = $arr['data'];
  126. foreach ($goodsList as $key => $val) {
  127. $picture = $val['picture'] ?? '';
  128. $price = $val['price'] ?? 0;
  129. $name = $val['name'] ?? '';
  130. $mtGoodsSpu = $val['app_spu_code'] ?? '';
  131. $imgArr = explode(',', $picture);
  132. if (empty($mtGoodsSpu)) {
  133. noticeUtil::push('商家有商品没有spuId,' . $name, '15280215347');
  134. continue;
  135. }
  136. $current = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $mtGoodsSpu], true);
  137. if (!empty($current)) {
  138. $current->price = $price;
  139. $current->save();
  140. }
  141. }
  142. }
  143. }