MtController.php 4.1 KB

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