/** * 根据 goods_id 得到一个商品的图片集 * * @param $f3 */ public function GalleryThumb($f3) { // 参数验证 $validator = new Validator($f3->get('GET')); $errorMessage = ''; $goods_id = $validator->required()->digits()->min(1)->filter('ValidatorIntValue')->validate('goods_id'); if (!$this->validate($validator)) { $errorMessage = implode('|', $this->flashMessageArray); goto out_fail; } $goodsGalleryService = new GoodsGalleryService(); $galleryArray = $goodsGalleryService->fetchGoodsGalleryArrayByGoodsId($goods_id); $thumImageList = array(); foreach ($galleryArray as $galleryItem) { $thumImageList[] = array('img_id' => $galleryItem['img_id'], 'thumb_url' => RouteHelper::makeImageUrl($galleryItem['thumb_url'])); } out: Ajax::header(); echo Ajax::buildResult(null, null, $thumImageList); return; out_fail: // 失败,返回出错信息 Ajax::header(); echo Ajax::buildResult(-1, $errorMessage, null); }
/** * 抓取商品的图片到本地,并且自动生成缩率图 * */ function fetchGoodsImage($goods_id, $imageUrl) { global $f3; printLog('start to fetch goods_id [' . $goods_id . '] imageUrl[' . $imageUrl . ']'); // 抓取图片,伪装成浏览器防止被某些服务器阻止 $webInstance = \Web::instance(); $webInstance->engine('curl'); $request = $webInstance->request($imageUrl, array('user_agent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)')); if (!$request || isset($request['http_code']) && 200 != $request['http_code']) { // 抓取失败,什么都不做 printLog('can not fetch [' . $imageUrl . ']', 'fetchGoodsImage', \Core\Log\Base::ERROR); goto out_release_res; } // 上传目录 $dataPathRoot = $f3->get('sysConfig[data_path_root]'); $saveFilePath = $dataPathRoot . '/upload/image/' . date("Y/m/d"); if (!file_exists($saveFilePath)) { if (!mkdir($saveFilePath, 0755, true)) { printLog('can not mkdir [' . $saveFilePath . ']', 'fetchGoodsImage', \Core\Log\Base::ERROR); goto out_release_res; } } //保存文件 $saveFilePath .= '/' . date("YmdHis") . '_' . rand(1, 10000) . strtolower(strrchr($imageUrl, '.')); file_put_contents($saveFilePath, $request['body']); printLog('save to image : ' . $saveFilePath); // 保存 goods_gallery 记录 $imageFileRelativeName = str_replace($dataPathRoot . '/', '', $saveFilePath); $pathInfoArray = pathinfo($imageFileRelativeName); $imageThumbFileRelativeName = $pathInfoArray['dirname'] . '/' . $pathInfoArray['filename'] . '_' . $f3->get('sysConfig[image_thumb_width]') . 'x' . $f3->get('sysConfig[image_thumb_height]') . '.jpg'; //生成缩略图 StorageImageHelper::resizeImage($dataPathRoot, $imageFileRelativeName, $imageThumbFileRelativeName, $f3->get('sysConfig[image_thumb_width]'), $f3->get('sysConfig[image_thumb_height]')); //保存 goods_gallery 记录 $goodsGalleryService = new GoodsGalleryService(); // ID 为0,返回一个新建的 dataMapper $goodsGallery = $goodsGalleryService->_loadById('goods_gallery', 'img_id=?', 0); $goodsGallery->goods_id = $goods_id; $goodsGallery->img_url = $imageFileRelativeName; $goodsGallery->img_desc = '最土转化图片'; $goodsGallery->img_original = $imageFileRelativeName; $goodsGallery->thumb_url = $imageThumbFileRelativeName; $goodsGallery->save(); printLog('success fetch [' . $goods_id . '] [' . $imageUrl . ']', 'fetchGoodsImage'); out_release_res: unset($request); unset($webInstance); }
public function run(array $params) { global $f3; // 每次处理多少条记录 $batchProcessCount = 100; // 图片所在的根目录 $dataPathRoot = $f3->get('sysConfig[data_path_root]'); $image_thumb_width = $f3->get('sysConfig[image_thumb_width]'); $image_thumb_height = $f3->get('sysConfig[image_thumb_height]'); $goodsGalleryService = new GoodsGalleryService(); $baseService = new BaseService(); $totalGoodsGalleryCount = $baseService->_countArray('goods_gallery', null); // 记录处理开始 for ($offset = 0; $offset < $totalGoodsGalleryCount; $offset += $batchProcessCount) { $goodsGalleryArray = $baseService->_fetchArray('goods_gallery', '*', null, array('order' => 'img_id asc'), $offset, $batchProcessCount); foreach ($goodsGalleryArray as $goodsGalleryItem) { if (!is_file($dataPathRoot . '/' . $goodsGalleryItem['img_original'])) { continue; // 文件不存在,不处理 } $pathInfoArray = pathinfo($goodsGalleryItem['img_original']); //生成缩略图 $imageThumbFileRelativeName = $pathInfoArray['dirname'] . '/' . $pathInfoArray['filename'] . '_' . $image_thumb_width . 'x' . $image_thumb_height . '.jpg'; //重新生存缩略图 printLog('Re-generate File :' . $imageThumbFileRelativeName); StorageImageHelper::resizeImage($dataPathRoot, $goodsGalleryItem['img_original'], $imageThumbFileRelativeName, $image_thumb_width, $image_thumb_height); // 更新 goods_gallery 设置 printLog('update goods_gallery img_id [' . $goodsGalleryItem['img_id'] . ']'); $goodsGallery = $goodsGalleryService->loadGoodsGalleryById($goodsGalleryItem['img_id']); if (!$goodsGallery->isEmpty()) { $goodsGallery->thumb_url = $imageThumbFileRelativeName; $goodsGallery->save(); } // 主动释放资源 unset($goodsGallery); unset($pathInfoArray); unset($imageThumbFileRelativeName); } unset($goodsGalleryArray); printLog('re-generate thumb image offset : ' . $offset); } printLog('re-generate thumb image finished , offset : ' . $offset); }
/** * 取得 goods 和 goods_promote 的 join 结果,同时取得 goods 对应的所有商品 * 由于这个函数的代价非常大(几千个商品,上万个图片),所以我们一定要做缓存 * * @return array array('goods' => $goodsArray, 'goodsIdToGalleryArray' => $goodsIdToGalleryArray) * * @param array $queryCondArray 查询条件 * @param string $sort * @param int $offset * @param int $limit * @param int $ttl 缓存时间 * * @return array */ public function fetchGoodsGalleryPromote($queryCondArray, $sort, $offset, $limit, $ttl = 0) { // 首先做参数验证 $validator = new Validator(array('sort' => $sort, 'offset' => $offset, 'limit' => $limit, 'ttl' => $ttl)); $offset = $validator->digits()->min(0)->validate('offset'); $limit = $validator->digits()->min(0)->validate('limit'); $ttl = $validator->digits()->min(0)->validate('ttl'); $sort = $validator->validate('sort'); // 查询商品信息 $condArray = array(); $condArray[] = array('g.goods_id = gp.goods_id'); $formQuery = array(); $formQuery['is_delete'] = 0; $formQuery['is_on_sale'] = 1; $formQuery['is_alone_sale'] = 1; if (!empty($queryCondArray)) { $condArray = array_merge($condArray, $queryCondArray); } $condArray = array_merge($condArray, QueryBuilder::buildQueryCondArray($formQuery)); $goodsArray = $this->_fetchArray(array('goods' => 'g', 'goods_promote' => 'gp'), 'g.system_tag_list, g.goods_name, g.goods_name_short, g.brand_id, g.goods_number, g.market_price, g.shop_price, g.cat_id' . ', g.sort_order, g.goods_brief, g.seo_keyword, g.goods_notice , g.virtual_buy_number, g.user_pay_number' . ', gp.* ', $condArray, array('order' => $sort), $offset, $limit, $ttl); // options // 如果没有数据就退出 if (empty($goodsArray)) { return array(); } // 查询商品图片 $goodsIdArray = array(); foreach ($goodsArray as $goodsItem) { $goodsIdArray[] = $goodsItem['goods_id']; } $goodsGalleryService = new GoodsGalleryService(); $goodsGalleryArray = $goodsGalleryService->fetchGoodsGalleryArrayByGoodsIdArray($goodsIdArray, $ttl); // 建立 goods_id --> goods_gallery 的反查表 $goodsIdToGalleryArray = array(); foreach ($goodsGalleryArray as $goodsGalleryItem) { if (!isset($goodsIdToGalleryArray[$goodsGalleryItem['goods_id']])) { $goodsIdToGalleryArray[$goodsGalleryItem['goods_id']] = array(); } $goodsIdToGalleryArray[$goodsGalleryItem['goods_id']][] = $goodsGalleryItem; } return array('goods' => $goodsArray, 'goodsIdToGalleryArray' => $goodsIdToGalleryArray); }
/** * 从缓存中取得商品的图片集 * * @param int $goods_id */ public static function getGoodsGallery($goods_id) { // 参数不对,没有东西可以输出 if ($goods_id <= 0) { throw new \InvalidArgumentException('goods_id [' . $goods_id . '] invalid'); } $cacheId = static::makeGoodsGalleryCacheId($goods_id); if ($goodsGallery = ShareCache::get($cacheId)) { goto out; } // 优化,别老不断生成新的对象 static $goodGalleryService = null; if (!$goodGalleryService) { $goodGalleryService = new GoodsGalleryService(); } $goodsGallery = $goodGalleryService->fetchGoodsGalleryArrayByGoodsId($goods_id); // 非空,缓存 if (!empty($goodsGallery)) { // 缓存一天 ShareCache::set($cacheId, $goodsGallery, 86400); } out: return $goodsGallery; }
public function get($f3) { global $smarty; // 首先做参数合法性验证 $validator = new Validator($f3->get('GET')); $pageNo = $validator->digits('pageNo 参数非法')->min(0, true, 'pageNo 参数非法')->validate('pageNo'); // 搜索参数数组 $searchFormQuery = array(); $searchFormQuery['category_id'] = $validator->digits('分类id非法')->min(1, true, '分类id非法')->filter('ValidatorIntValue')->validate('category_id'); $searchFormQuery['suppliers_id'] = $validator->digits('供货商id非法')->min(1, true, '供货商id非法')->filter('ValidatorIntValue')->validate('suppliers_id'); $searchFormQuery['goods_name'] = $validator->validate('goods_name'); // 价格区间查询 $shopPriceMin = $validator->filter('ValidatorFloatValue')->validate('shop_price_min'); $shopPriceMax = $validator->filter('ValidatorFloatValue')->validate('shop_price_max'); $searchFormQuery['shop_price'] = array($shopPriceMin, $shopPriceMax); // 排序 $orderBy = $validator->oneOf(array('', 'total_buy_number', 'shop_price', 'add_time'))->validate('orderBy'); $orderDir = $validator->oneOf(array('', 'asc', 'desc'))->validate('orderDir'); $orderByParam = array(); if (!empty($orderBy)) { $orderByParam = array(array($orderBy, $orderDir)); } //增加一些我们的缺省排序 $orderByParam[] = array('sort_order', 'desc'); $orderByParam[] = array('goods_id', 'desc'); // 参数验证 if (!$this->validate($validator) || empty($searchFormQuery)) { goto out_fail; } $pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0; $pageSize = 10; // 每页固定显示 10 个商品 // 生成 smarty 的缓存 id $smartyCacheId = 'Goods|Search|' . md5(json_encode($searchFormQuery) . json_encode($orderByParam) . '_' . $pageNo . '_' . $pageSize); // 开启并设置 smarty 缓存时间 enableSmartyCache(true, MobileThemePlugin::getOptionValue('smarty_cache_time_goods_search')); if ($smarty->isCached('goods_search.tpl', $smartyCacheId)) { goto out_display; } // 合并查询参数 $searchParamArray = array_merge(QueryBuilder::buildSearchParamArray($searchFormQuery), $this->searchExtraCondArray); $totalCount = SearchHelper::count(SearchHelper::Module_Goods, $searchParamArray); if ($totalCount <= 0) { goto out_display; // 没有商品,直接显示 } // 页号可能是用户乱输入的,我们需要检查 if ($pageNo * $pageSize >= $totalCount) { goto out_fail; // 返回首页 } $goodsArray = SearchHelper::search(SearchHelper::Module_Goods, $this->searchFieldSelector, $searchParamArray, $orderByParam, $pageNo * $pageSize, $pageSize); if (empty($goodsArray)) { goto out_display; } // 取得 商品ID 列表 $goodsIdArray = array(); foreach ($goodsArray as $goodsItem) { $goodsIdArray[] = $goodsItem['goods_id']; } // 取得商品的图片 $goodsGalleryService = new GoodsGalleryService(); $goodsGalleryArray = $goodsGalleryService->fetchGoodsGalleryArrayByGoodsIdArray($goodsIdArray); $currentGoodsId = -1; $goodsThumbImageArray = array(); $goodsImageArray = array(); foreach ($goodsGalleryArray as $goodsGalleryItem) { if ($currentGoodsId == $goodsGalleryItem['goods_id']) { continue; //每个商品我们只需要一张图片,跳过其它的图片 } $currentGoodsId = $goodsGalleryItem['goods_id']; // 新的商品 id $goodsThumbImageArray[$currentGoodsId] = RouteHelper::makeImageUrl($goodsGalleryItem['thumb_url']); $goodsImageArray[$currentGoodsId] = RouteHelper::makeImageUrl($goodsGalleryItem['img_url']); } // 赋值给模板 $smarty->assign('totalCount', $totalCount); $smarty->assign('pageNo', $pageNo); $smarty->assign('pageSize', $pageSize); $smarty->assign('goodsArray', $goodsArray); $smarty->assign('goodsThumbImageArray', $goodsThumbImageArray); $smarty->assign('goodsImageArray', $goodsImageArray); out_display: $smarty->display('goods_search.tpl', $smartyCacheId); return; out_fail: // 失败从这里返回 RouteHelper::reRoute($this, '/'); // 返回首页 }
/** * 从网络抓取图片进入相册 * * @param $f3 */ public function Fetch($f3) { // 权限检查 $this->requirePrivilege('manage_goods_edit_edit_post'); // 参数验证 $validator = new Validator($f3->get('POST')); $goods_id = $validator->required('商品ID不能为空')->digits()->min(1)->validate('goods_id'); $imageUrl = $validator->required('图片地址不能为空')->validate('imageUrl'); if (!$this->validate($validator)) { goto out_fail; } // 抓取图片,伪装成浏览器防止被某些服务器阻止 $webInstance = \Web::instance(); $webInstance->engine('curl'); $request = $webInstance->request($imageUrl, array('user_agent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)')); if (!$request || isset($request['http_code']) && 200 != $request['http_code']) { $this->addFlashMessage('抓取失败,请检查你的抓取地址'); goto out; } // 把图片保存到 Storage 中 $cloudStorage = CloudHelper::getCloudModule(CloudHelper::CLOUD_MODULE_STORAGE); // 图片文件先保存到临时文件中 $tempSrcFilePath = $cloudStorage->getTempFilePath(); file_put_contents($tempSrcFilePath, $request['body']); // 上传目录 $dataPathRoot = $f3->get('sysConfig[data_path_root]'); $saveFilePathRelative = 'upload/image/' . date("Y/m/d") . '/' . date("YmdHis") . '_' . rand(1, 10000) . strtolower(strrchr($imageUrl, '.')); // 文件上传到 Storage if (!$cloudStorage->moveFileToStorage($dataPathRoot, $saveFilePathRelative, $tempSrcFilePath)) { $this->addFlashMessage('保存文件到存储失败,失败'); goto out; } @unlink($tempSrcFilePath); // 保存 goods_gallery 记录 $imageOriginalFileRelativeName = $saveFilePathRelative; $pathInfoArray = pathinfo($imageOriginalFileRelativeName); //生成头图 $imageFileRelativeName = $pathInfoArray['dirname'] . '/' . $pathInfoArray['filename'] . '_' . $f3->get('sysConfig[image_width]') . 'x' . $f3->get('sysConfig[image_height]') . '.jpg'; StorageImageHelper::resizeImage($dataPathRoot, $imageOriginalFileRelativeName, $imageFileRelativeName, $f3->get('sysConfig[image_width]'), $f3->get('sysConfig[image_height]')); //生成缩略图 $imageThumbFileRelativeName = $pathInfoArray['dirname'] . '/' . $pathInfoArray['filename'] . '_' . $f3->get('sysConfig[image_thumb_width]') . 'x' . $f3->get('sysConfig[image_thumb_height]') . '.jpg'; StorageImageHelper::resizeImage($dataPathRoot, $imageOriginalFileRelativeName, $imageThumbFileRelativeName, $f3->get('sysConfig[image_thumb_width]'), $f3->get('sysConfig[image_thumb_height]')); //保存 goods_gallery 记录 $goodsGalleryService = new GoodsGalleryService(); // ID 为0,返回一个新建的 dataMapper $goodsGallery = $goodsGalleryService->_loadById('goods_gallery', 'img_id=?', 0); $goodsGallery->goods_id = $goods_id; $goodsGallery->img_desc = '网络下载图片'; $goodsGallery->img_original = $imageOriginalFileRelativeName; $goodsGallery->img_url = $imageFileRelativeName; $goodsGallery->thumb_url = $imageThumbFileRelativeName; $goodsGallery->save(); $this->addFlashMessage('抓取图片成功'); //清除缓存,确保商品显示正确 ClearHelper::clearGoodsCacheById($goodsGallery->goods_id); out: // 释放资源 unset($request); unset($webInstance); RouteHelper::reRoute($this, RouteHelper::makeUrl('/Goods/Edit/Gallery', array('goods_id' => $goods_id), true)); return; // 成功从这里返回 out_fail: RouteHelper::reRoute($this, '/Goods/Search'); }
public function get($f3) { global $smarty; // 生成 smarty 的缓存 id $smartyCacheId = 'Mobile|Index'; // 开启并设置 smarty 缓存时间 enableSmartyCache(true, MobileThemePlugin::getOptionValue('smarty_cache_time_goods_index')); // 缓存页面 if ($smarty->isCached('mobile_index.tpl', $smartyCacheId)) { goto out_display; } $categoryLevel = 3; // 取得分类下面 3 层的商品 $categoryGoodsSzie = 4; // 每个分类下面取 4 个商品 // 取得分类列表 $categoryService = new CategoryService(); $categoryArray = $categoryService->fetchCategoryArray(0); //取得分类 // 取得商品列表 $categoryGoodsArray = array(); $goodsIdArray = array(); foreach ($categoryArray as $category) { // 分类下面 3 层子分类, 取 4 个商品 $goodsArray = $categoryService->fetchGoodsArray($category['meta_id'], $categoryLevel, PluginHelper::SYSTEM_MOBILE, 0, $categoryGoodsSzie); if (!empty($goodsArray)) { $categoryGoodsArray[$category['meta_id']] = $goodsArray; foreach ($goodsArray as $goodsItem) { $goodsIdArray[] = $goodsItem['goods_id']; } } } // 网站完全没有商品? 不缓存 if (empty($goodsIdArray)) { goto out_display; } // 取得商品的图片 $goodsGalleryService = new GoodsGalleryService(); $goodsGalleryArray = $goodsGalleryService->fetchGoodsGalleryArrayByGoodsIdArray($goodsIdArray); $currentGoodsId = -1; $goodsThumbImageArray = array(); $goodsImageArray = array(); foreach ($goodsGalleryArray as $goodsGalleryItem) { if ($currentGoodsId == $goodsGalleryItem['goods_id']) { continue; //每个商品我们只需要一张图片,跳过其它的图片 } $currentGoodsId = $goodsGalleryItem['goods_id']; // 新的商品 id $goodsThumbImageArray[$currentGoodsId] = RouteHelper::makeImageUrl($goodsGalleryItem['thumb_url']); $goodsImageArray[$currentGoodsId] = RouteHelper::makeImageUrl($goodsGalleryItem['img_url']); } // 赋值给模板 $smarty->assign('categoryArray', $categoryArray); $smarty->assign('categoryGoodsArray', $categoryGoodsArray); $smarty->assign('goodsThumbImageArray', $goodsThumbImageArray); $smarty->assign('goodsImageArray', $goodsImageArray); out_display: $smarty->assign('seo_title', $smarty->getTemplateVars('seo_title') . ',' . $f3->get('HOST')); $smarty->display('mobile_index.tpl', $smartyCacheId); }
private function getGoodsArrayXml($goodsArray) { $itemXmlList = ''; // 没有商品,退出 if (empty($goodsArray)) { goto out_output; } // 查询商品图片 $goodsIdArray = array(); foreach ($goodsArray as $goodsItem) { $goodsIdArray[] = $goodsItem['goods_id']; } $goodsGalleryService = new GoodsGalleryService(); $goodsGalleryArray = $goodsGalleryService->fetchGoodsGalleryArrayByGoodsIdArray($goodsIdArray); // 建立 goods_id --> goods_gallery 的反查表 $goodsIdToGalleryArray = array(); foreach ($goodsGalleryArray as $goodsGalleryItem) { if (!isset($goodsIdToGalleryArray[$goodsGalleryItem['goods_id']])) { $goodsIdToGalleryArray[$goodsGalleryItem['goods_id']] = array(); } $goodsIdToGalleryArray[$goodsGalleryItem['goods_id']][] = $goodsGalleryItem; } // 生成 商品列表 foreach ($goodsArray as $goodsItem) { $itemXmlList .= $this->getGoodsItemXml($goodsItem, $goodsIdToGalleryArray); } out_output: $apiXml = <<<XML <?xml version="1.0" encoding="utf-8" ?> <items> {$itemXmlList} </items> XML; return $apiXml; }
/** * 根据 img_id 取得缩略图 * * @param array $paramArray * @param $smarty * * @return string */ function smarty_helper_function_thumb_image(array $paramArray, $smarty) { $img_id = isset($paramArray['img_id']) ? intval($paramArray['img_id']) : 0; // 参数不对,没有东西可以输出 if ($img_id <= 0) { return ''; } static $goodsGalleryService = null; if (!$goodsGalleryService) { $goodsGalleryService = new GoodsGalleryService(); } $galleryItem = $goodsGalleryService->loadGoodsGalleryById($img_id, 1800); if ($galleryItem->isEmpty()) { return ''; } return RouteHelper::makeImageUrl($galleryItem['thumb_url']); }
public function get($f3) { // 权限检查 $this->requirePrivilege('manage_goods_create'); // 参数验证 $validator = new Validator($f3->get('GET')); $goods_id = $validator->required('商品ID不能为空')->digits()->min(1)->validate('goods_id'); if (!$this->validate($validator)) { goto out_fail; } // 取得商品信息 $goodsBasicService = new GoodsBasicService(); $goods = $goodsBasicService->loadGoodsById($goods_id); if ($goods->isEmpty()) { $this->addFlashMessage('非法商品ID'); goto out_fail; } $authAdminUser = AuthHelper::getAuthUser(); // 1. 复制 goods 信息 $goodsArray = $goods->toArray(); unset($goodsArray['goods_id']); // 清除主键 // 新商品缺省为下线状态 $goodsArray['is_on_sale'] = 0; // 清除购买数量统计 $goodsArray['user_buy_number'] = 0; $goodsArray['user_pay_number'] = 0; // 设置复制人 $goodsArray['admin_user_id'] = $authAdminUser['user_id']; $goodsArray['admin_user_name'] = $authAdminUser['user_name']; // 处理商品的规格 if (!empty($goodsArray['goods_spec'])) { $goodsSpecService = new GoodsSpecService(); $goodsSpecService->initWithJson($goodsArray['goods_spec']); $goodsSpecService->clearGoodsSpecImgIdArray(); // 清除图片 ID 的关联 $goodsArray['goods_spec'] = $goodsSpecService->getJsonStr(); unset($goodsSpecService); } $goodsArray['add_time'] = Time::gmTime(); $newGoods = $goodsBasicService->loadGoodsById(0); $newGoods->copyFrom($goodsArray); $newGoods->save(); // 更新 goods_sn $newGoods->goods_sn = $f3->get('sysConfig[goods_sn_prefix]') . $newGoods['goods_id']; $newGoods->save(); unset($goodsArray); // 2. 复制 goods_attr 信息 if ($goods->type_id > 0) { $goodsTypeService = new GoodsTypeService(); $goodsAttrValueArray = $goodsTypeService->fetchGoodsAttrItemValueArray($goods->goods_id, $goods->type_id); foreach ($goodsAttrValueArray as $goodsAttrValue) { $goodsAttr = $goodsTypeService->loadGoodsAttrById(0); $goodsAttr->goods_id = $newGoods->goods_id; $goodsAttr->attr_item_id = $goodsAttrValue['meta_id']; $goodsAttr->attr_item_value = $goodsAttrValue['attr_item_value']; $goodsAttr->save(); unset($goodsAttr); } unset($goodsAttrValueArray); unset($goodsTypeService); } // 3. 复制 goods_gallery 信息 $goodsGalleryService = new GoodsGalleryService(); $goodsGalleryArray = $goodsGalleryService->fetchGoodsGalleryArrayByGoodsId($goods_id); foreach ($goodsGalleryArray as $goodsGalleryItem) { // 新建一个 goods_gallery 记录 $goodsGallery = $goodsGalleryService->loadGoodsGalleryById(0); unset($goodsGalleryItem['img_id']); $goodsGallery->copyFrom($goodsGalleryItem); $goodsGallery->goods_id = $newGoods['goods_id']; $goodsGallery->save(); unset($goodsGallery); } unset($goodsGalleryArray); unset($goodsGalleryService); // 4. 复制 goods_team 信息 $goodsTeam = $goodsBasicService->loadGoodsTeamByGoodsId($goods_id); if (!$goodsTeam->isEmpty()) { $goodsTeamInfo = $goodsTeam->toArray(); unset($goodsTeamInfo['team_id']); $goodsTeamInfo['goods_id'] = $newGoods['goods_id']; $newGoodsTeam = new DataMapper('goods_team'); $newGoodsTeam->copyFrom($goodsTeamInfo); $newGoodsTeam->save(); unset($newGoodsTeam); unset($goodsTeamInfo); unset($goodsTeam); } // 5. 复制 link_goods 信息 $linkGoodsArray = $goodsBasicService->fetchSimpleLinkGoodsArray($goods_id); foreach ($linkGoodsArray as $linkGoodsItem) { unset($linkGoodsItem['link_id']); $linkGoodsItem['goods_id'] = $newGoods['goods_id']; $linkGoodsItem['admin_id'] = $authAdminUser['user_id']; $linkGoods = new DataMapper('link_goods'); $linkGoods->copyFrom($linkGoodsItem); $linkGoods->save(); unset($linkGoods); } unset($linkGoodsArray); // 6. 复制 goods_promote 信息 $goodsPromote = $goodsBasicService->loadGoodsPromoteByGoodsId($goods_id); if (!$goodsPromote->isEmpty()) { $goodsPromoteInfo = $goodsPromote->toArray(); unset($goodsPromoteInfo['promote_id']); $goodsPromoteInfo['goods_id'] = $newGoods['goods_id']; $newGoodspromote = new DataMapper('goods_promote'); $newGoodspromote->copyFrom($goodsPromoteInfo); $newGoodspromote->save(); unset($newGoodspromote); } unset($goodsPromote); // 记录编辑日志 $goodsLogContent = '从 [' . $goods_id . '] 复制过来'; $goodsLogService = new GoodsLogService(); $goodsLogService->addGoodsLog($newGoods['goods_id'], $authAdminUser['user_id'], $authAdminUser['user_name'], '复制商品', $goodsLogContent); $this->addFlashMessage('复制新建商品成功'); RouteHelper::reRoute($this, RouteHelper::makeUrl('/Goods/Edit/Edit', array('goods_id' => $newGoods['goods_id']), true)); return; //正常返回 out_fail: RouteHelper::reRoute($this, '/Goods/Search'); }