MtController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace console\controllers;
  3. use common\components\util;
  4. use yii\console\Controller;
  5. use biz\shop\classes\ShopClass;
  6. use biz\shop\classes\ShopExtClass;
  7. use bizHd\goods\classes\CategoryClass;
  8. use bizHd\goods\classes\GoodsClass;
  9. use common\components\dict;
  10. use common\components\httpUtil;
  11. use common\components\mtUtil;
  12. use Shishaoqi\MeituanFlashPurchase\Application;
  13. class MtController extends Controller
  14. {
  15. //美团里有的商品,但没同步到系统的进行同步;同时美团没有app_spu_code的,补充app_spu_code ssh 20220914
  16. // ./yii mt/sync-goods
  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. $appSpuCode = $val['app_spu_code'] ?? '';
  59. if (!empty($appSpuCode)) {
  60. $current = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true);
  61. if (!empty($current)) {
  62. continue;
  63. }
  64. }
  65. $imgArr = explode(',', $picture);
  66. $shopImg = [];
  67. foreach ($imgArr as $imgUrl) {
  68. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  69. $shopImg[] = $shortUrl;
  70. }
  71. $cover = $shopImg[0];
  72. $catIds = [$catId];
  73. $addData = [
  74. 'name' => $name,
  75. 'flower' => 1,
  76. 'property' => 0,
  77. 'price' => $price,
  78. 'sjId' => $sjId,
  79. 'mainId' => $mainId,
  80. 'shopId' => $shopId,
  81. 'status' => 0,
  82. 'delStatus' => 0,
  83. 'shopImg' => $shopImg,
  84. 'catIds' => $catIds,
  85. 'cover' => $cover,
  86. 'spu' => $appSpuCode,
  87. ];
  88. $goods = GoodsClass::addGoods($addData);
  89. $goodsId = $goods->id ?? 0;
  90. if (empty($appSpuCode)) {
  91. $newSpuCode = 'hzg_' . $mainId . '_' . $goodsId;
  92. $end_category_code = $val['category_code'] ?? '';
  93. if (isset($val['secondary_category_code']) && !empty($val['secondary_category_code'])) {
  94. $end_category_code = $val['secondary_category_code'];
  95. }
  96. $name = $val['name'] ?? '';
  97. $params = ['app_poi_code' => $shopId, 'access_token' => $accessToken, 'name' => $name, 'category_code' => $end_category_code, 'app_spu_code' => $newSpuCode];
  98. $app->retail->updateAppFoodCodeByNameAndSpec($params);
  99. $goods->spu = $newSpuCode;
  100. $goods->save();
  101. }
  102. }
  103. }
  104. }