MtController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. //美团里有的商品,但没同步到系统的进行同步;同时美团没有app_spu_code的,补充app_spu_code ssh 20220914
  17. // ./yii mt/sync-goods 36524
  18. public function actionSyncGoods()
  19. {
  20. global $argv;
  21. ini_set('memory_limit', '2045M');
  22. set_time_limit(0);
  23. $shopId = $argv[2] ?? 0;
  24. if (empty($shopId)) {
  25. util::stop('请填写门店id');
  26. }
  27. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  28. if (empty($ext)) {
  29. util::stop('没有找到门店相信息81');
  30. }
  31. $shop = ShopClass::getById($shopId, true);
  32. if (empty($shop)) {
  33. util::stop('没有找到门店82');
  34. }
  35. $mainId = $shop->mainId ?? 0;
  36. $sjId = $shop->sjId ?? 0;
  37. $config = dict::getDict('mtConfig');
  38. $app = new Application($config);
  39. $accessToken = mtUtil::getAccessToken($ext);
  40. $params = ['app_poi_code' => $shopId, 'access_token' => $accessToken];
  41. $json = $app->retail->retailList($params);
  42. $arr = json_decode($json, true);
  43. if (isset($arr['data']) == false || empty($arr['data'])) {
  44. util::stop('没有商品');
  45. }
  46. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  47. if (empty($mtCategory)) {
  48. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  49. }
  50. $catId = $mtCategory->id ?? 0;
  51. if (empty($catId)) {
  52. util::stop('没有找到美团的商品分类');
  53. }
  54. $goodsList = $arr['data'];
  55. foreach ($goodsList as $key => $val) {
  56. $picture = $val['picture'] ?? '';
  57. $price = $val['price'] ?? 0;
  58. $name = $val['name'] ?? '';
  59. $description = $val['description'] ?? '';
  60. $appSpuCode = $val['app_spu_code'] ?? '';
  61. if (!empty($appSpuCode)) {
  62. $current = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true);
  63. if (!empty($current)) {
  64. continue;
  65. }
  66. }
  67. $sh = strstr($description, '【鲜花】') !== false ? 1 : 0;
  68. $imgArr = explode(',', $picture);
  69. $shopImg = [];
  70. foreach ($imgArr as $imgUrl) {
  71. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  72. $shopImg[] = $shortUrl;
  73. }
  74. $cover = $shopImg[0];
  75. $catIds = [$catId];
  76. $addData = [
  77. 'name' => $name,
  78. 'flower' => 1,
  79. 'property' => 0,
  80. 'price' => $price,
  81. 'sjId' => $sjId,
  82. 'mainId' => $mainId,
  83. 'shopId' => $shopId,
  84. 'status' => 0,
  85. 'delStatus' => 0,
  86. 'shopImg' => $shopImg,
  87. 'catIds' => $catIds,
  88. 'cover' => $cover,
  89. 'spu' => $appSpuCode,
  90. 'sh' => $sh,
  91. ];
  92. $goods = GoodsClass::addGoods($addData);
  93. $goodsId = $goods->id ?? 0;
  94. if (empty($appSpuCode)) {
  95. $newSpuCode = 'hzg_' . $mainId . '_' . $goodsId;
  96. $end_category_code = $val['category_code'] ?? '';
  97. if (isset($val['secondary_category_code']) && !empty($val['secondary_category_code'])) {
  98. $end_category_code = $val['secondary_category_code'];
  99. }
  100. $name = $val['name'] ?? '';
  101. $params = ['app_poi_code' => $shopId, 'access_token' => $accessToken, 'name' => $name, 'category_code' => $end_category_code, 'app_spu_code' => $newSpuCode];
  102. $json = $app->retail->updateAppFoodCodeByNameAndSpec($params);
  103. $arr = json_decode($json, true);
  104. if (isset($arr['data']) == false || $arr['data'] != 'ok') {
  105. noticeUtil::push("同步商品 {$name} 到系统时,app_spu_code 没有生成,请确认", '15280215347');
  106. continue;
  107. }
  108. $goods->spu = $newSpuCode;
  109. $goods->save();
  110. }
  111. }
  112. }
  113. }