| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace console\controllers;
- use common\components\noticeUtil;
- use common\components\util;
- use yii\console\Controller;
- use biz\shop\classes\ShopClass;
- use biz\shop\classes\ShopExtClass;
- use bizHd\goods\classes\CategoryClass;
- use bizHd\goods\classes\GoodsClass;
- use common\components\dict;
- use common\components\httpUtil;
- use common\components\mtUtil;
- use Shishaoqi\MeituanFlashPurchase\Application;
- class MtController extends Controller
- {
- //美团里有的商品,但没同步到系统的进行同步;同时美团没有app_spu_code的,补充app_spu_code ssh 20220914
- // ./yii mt/sync-goods 36524
- public function actionSyncGoods()
- {
- global $argv;
- ini_set('memory_limit', '2045M');
- set_time_limit(0);
- $shopId = $argv[2] ?? 0;
- if (empty($shopId)) {
- util::stop('请填写门店id');
- }
- $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
- if (empty($ext)) {
- util::stop('没有找到门店相信息81');
- }
- $shop = ShopClass::getById($shopId, true);
- if (empty($shop)) {
- util::stop('没有找到门店82');
- }
- $mainId = $shop->mainId ?? 0;
- $sjId = $shop->sjId ?? 0;
- $config = dict::getDict('mtConfig');
- $app = new Application($config);
- $accessToken = mtUtil::getAccessToken($ext);
- $params = ['app_poi_code' => $shopId, 'access_token' => $accessToken];
- $json = $app->retail->retailList($params);
- $arr = json_decode($json, true);
- if (isset($arr['data']) == false || empty($arr['data'])) {
- util::stop('没有商品');
- }
- $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
- if (empty($mtCategory)) {
- $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
- }
- $catId = $mtCategory->id ?? 0;
- if (empty($catId)) {
- util::stop('没有找到美团的商品分类');
- }
- $goodsList = $arr['data'];
- foreach ($goodsList as $key => $val) {
- $picture = $val['picture'] ?? '';
- $price = $val['price'] ?? 0;
- $name = $val['name'] ?? '';
- $description = $val['description'] ?? '';
- $appSpuCode = $val['app_spu_code'] ?? '';
- if (!empty($appSpuCode)) {
- $current = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true);
- if (!empty($current)) {
- continue;
- }
- }
- $sh = strstr($description, '【鲜花】') !== false ? 1 : 0;
- $imgArr = explode(',', $picture);
- $shopImg = [];
- foreach ($imgArr as $imgUrl) {
- $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
- $shopImg[] = $shortUrl;
- }
- $cover = $shopImg[0];
- $catIds = [$catId];
- $addData = [
- 'name' => $name,
- 'flower' => 1,
- 'property' => 0,
- 'price' => $price,
- 'sjId' => $sjId,
- 'mainId' => $mainId,
- 'shopId' => $shopId,
- 'status' => 0,
- 'delStatus' => 0,
- 'shopImg' => $shopImg,
- 'catIds' => $catIds,
- 'cover' => $cover,
- 'spu' => $appSpuCode,
- 'sh' => $sh,
- ];
- $goods = GoodsClass::addGoods($addData);
- $goodsId = $goods->id ?? 0;
- if (empty($appSpuCode)) {
- $newSpuCode = 'hzg_' . $mainId . '_' . $goodsId;
- $end_category_code = $val['category_code'] ?? '';
- if (isset($val['secondary_category_code']) && !empty($val['secondary_category_code'])) {
- $end_category_code = $val['secondary_category_code'];
- }
- $name = $val['name'] ?? '';
- $params = ['app_poi_code' => $shopId, 'access_token' => $accessToken, 'name' => $name, 'category_code' => $end_category_code, 'app_spu_code' => $newSpuCode];
- $json = $app->retail->updateAppFoodCodeByNameAndSpec($params);
- $arr = json_decode($json, true);
- if (isset($arr['data']) == false || $arr['data'] != 'ok') {
- noticeUtil::push("同步商品 {$name} 到系统时,app_spu_code 没有生成,请确认", '15280215347');
- continue;
- }
- $goods->spu = $newSpuCode;
- $goods->save();
- }
- }
- }
- }
|