public function post($f3) { // 权限检查 $this->requirePrivilege('manage_goods_edit_edit_post'); global $smarty; // 参数验证 $validator = new Validator($f3->get('GET')); $goods_id = $validator->required('商品ID不能为空')->digits()->min(1)->validate('goods_id'); if (!$this->validate($validator)) { goto out_fail_list_goods; } unset($validator); // 用户提交的商品信息做验证 $goodsPromoteInfo = $f3->get('POST.goods_promote'); if (empty($goodsPromoteInfo)) { goto out_fail_validate; } //安全性处理 unset($goodsPromoteInfo['promote_id']); $goodsPromoteInfo['goods_id'] = $goods_id; // 写入到数据库 $goodsBasicService = new GoodsBasicService(); $goodsPromote = $goodsBasicService->loadGoodsPromoteByGoodsId($goods_id); $goodsPromote->copyFrom($goodsPromoteInfo); $goodsPromote->save(); // 记录商品编辑日志 $goodsLogContent = '360分类:' . $goodsPromote['360tuan_category'] . ',' . $goodsPromote['360tuan_category_end'] . "\n" . "360排序:" . $goodsPromote['360tuan_sort_order']; $authAdminUser = AuthHelper::getAuthUser(); $goodsLogService = new GoodsLogService(); $goodsLogService->addGoodsLog($goods_id, $authAdminUser['user_id'], $authAdminUser['user_name'], static::$goodsLogDesc, $goodsLogContent); // 成功,显示商品详情 $this->addFlashMessage('商品推广渠道保存成功'); //清除缓存,确保商品显示正确 ClearHelper::clearGoodsCacheById($goods_id); RouteHelper::reRoute($this, RouteHelper::makeUrl('/Goods/Edit/Promote', array('goods_id' => $goods_id), true)); return; // 参数验证失败 out_fail_validate: $smarty->display('goods_edit_promote.tpl'); return; out_fail_list_goods: RouteHelper::reRoute($this, '/Goods/Search'); }
public function get($f3) { // 权限检查 $this->requirePrivilege('manage_goods_edit_edit_get'); global $smarty; // 参数验证 $validator = new Validator($f3->get('GET')); $goods_id = $validator->required('商品ID不能为空')->digits()->min(1)->validate('goods_id'); $pageNo = $validator->digits()->min(0)->validate('pageNo'); $pageSize = $validator->digits()->min(0)->validate('pageSize'); // 设置缺省值 $pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0; $pageSize = isset($pageSize) && $pageSize > 0 ? $pageSize : 20; if (!$this->validate($validator)) { goto out_fail; } $goodsLogService = new GoodsLogService(); $totalCount = $goodsLogService->countGoodsLogArray($goods_id); $goodsLogArray = $goodsLogService->fetchGoodsLogArray($goods_id, $pageNo * $pageSize, $pageSize); // 格式化内容的输出 foreach ($goodsLogArray as &$goodsLog) { if (!empty($goodsLog['content'])) { $goodsLog['content'] = nl2br($goodsLog['content']); } } unset($goodsLog); // 给模板赋值 $smarty->assign('goodsLogArray', $goodsLogArray); $smarty->assign('totalCount', $totalCount); $smarty->assign('pageNo', $pageNo); $smarty->assign('pageSize', $pageSize); $smarty->display('goods_edit_log.tpl'); return; out_fail: RouteHelper::reRoute($this, '/Goods/Search'); }
public function post($f3) { // 权限检查 $this->requirePrivilege('manage_goods_edit_edit_post'); global $smarty; $isCreateGoods = false; // 是否是创建新商品 // 参数验证 $validator = new Validator($f3->get('GET')); $goods_id = $validator->digits()->filter('ValidatorIntValue')->validate('goods_id'); if (!$this->validate($validator)) { goto out_fail_list_goods; } unset($validator); // 用户提交的商品信息做验证 $goods = $f3->get('POST[goods]'); if (empty($goods)) { goto out_fail_validate; } $validator = new Validator($goods); $goodsInfo = array(); //表单数据验证、过滤 $goodsInfo['goods_name'] = $validator->required('商品名不能为空')->validate('goods_name'); $goodsInfo['goods_name_short'] = $validator->required('商品短标题不能为空')->validate('goods_name_short'); $goodsInfo['keywords'] = $validator->validate('keywords'); $goodsInfo['seo_title'] = $validator->validate('seo_title'); $goodsInfo['seo_keyword'] = $validator->validate('seo_keyword'); $goodsInfo['seo_description'] = $validator->validate('seo_description'); $goodsInfo['goods_sn'] = $validator->validate('goods_sn'); $goodsInfo['warehouse'] = $validator->validate('warehouse'); $goodsInfo['shelf'] = $validator->validate('shelf'); $goodsInfo['cat_id'] = $validator->required('商品分类不能为空')->filter('ValidatorIntValue')->validate('cat_id'); // 记录管理员 $authAdminUser = AuthHelper::getAuthUser(); $goodsInfo['admin_user_id'] = $validator->filter('ValidatorIntValue')->validate('admin_user_id'); // 如果没有选择管理员,就用当前管理员 if (empty($goodsInfo['admin_user_id'])) { $goodsInfo['admin_user_id'] = $authAdminUser['user_id']; $goodsInfo['admin_user_name'] = $authAdminUser['user_name']; } else { $adminUserService = new AdminUserService(); $adminUser = $adminUserService->loadAdminById($goodsInfo['admin_user_id']); if ($adminUser->isEmpty()) { $this->addFlashMessage('管理员[' . $goodsInfo['admin_user_id'] . ']不存在'); goto out_fail_validate; } $goodsInfo['admin_user_name'] = $adminUser['user_name']; unset($adminUser); unset($adminUserService); } $goodsInfo['brand_id'] = $validator->filter('ValidatorIntValue')->validate('brand_id'); $goodsInfo['suppliers_id'] = $validator->required('供货商不能为空')->filter('ValidatorIntValue')->validate('suppliers_id'); $goodsInfo['is_alone_sale'] = $validator->filter('ValidatorIntValue')->validate('is_alone_sale'); $goodsInfo['is_best'] = $validator->filter('ValidatorIntValue')->validate('is_best'); $goodsInfo['is_new'] = $validator->filter('ValidatorIntValue')->validate('is_new'); $goodsInfo['is_hot'] = $validator->filter('ValidatorIntValue')->validate('is_hot'); $goodsInfo['is_on_sale'] = $validator->filter('ValidatorIntValue')->validate('is_on_sale'); $goodsInfo['market_price'] = Money::toStorage($validator->validate('market_price')); $goodsInfo['shop_price'] = Money::toStorage($validator->validate('shop_price')); $goodsInfo['shipping_fee'] = Money::toStorage($validator->validate('shipping_fee')); $goodsInfo['shipping_free_number'] = $validator->validate('shipping_free_number'); $goodsInfo['goods_number'] = abs($validator->filter('ValidatorIntValue')->validate('goods_number')); $goodsInfo['virtual_buy_number'] = $validator->filter('ValidatorIntValue')->validate('virtual_buy_number'); $goodsInfo['suppliers_price'] = Money::toStorage($validator->validate('suppliers_price')); $goodsInfo['suppliers_shipping_fee'] = Money::toStorage($validator->validate('suppliers_shipping_fee')); $goodsInfo['sort_order'] = $validator->validate('sort_order'); $goodsInfo['warn_number'] = $validator->filter('ValidatorIntValue')->validate('warn_number'); $goodsInfo['goods_brief'] = @$goods['goods_brief']; //不需要过滤 html $goodsInfo['goods_notice'] = @$goods['goods_notice']; //不需要过滤 html $goodsInfo['goods_after_service'] = @$goods['goods_after_service']; //不需要过滤 html $goodsInfo['seller_note'] = $validator->validate('seller_note'); $goodsInfo['system_tag_list'] = Utils::makeTagString(@$goods['system_tag_list']); // 生成系统的 tag string $goodsInfo['update_time'] = Time::gmTime(); // 商品的更新时间 $goodsInfo['goods_desc'] = @$goods['goods_desc']; //不需要过滤 html if (!$this->validate($validator)) { goto out_fail_validate; } // 某些时候,我们不允许编辑直接粘贴别人网站的图片上来,所以我们需要过滤图片的域名 $goodsDescAllowImageDomainArray = $f3->get('sysConfig[goods_desc_allow_image_domain_array]'); if ($goodsDescAllowImageDomainArray && is_array($goodsDescAllowImageDomainArray) && !empty($goodsDescAllowImageDomainArray)) { $patternMatch = array(); preg_match_all('/<img(.*?)src="(.*?)"(.*?)\\/?>/', $goodsInfo['goods_desc'], $patternMatch, PREG_SET_ORDER); // 检查每一个图片 foreach ($patternMatch as $matchItem) { $imageUrl = $matchItem[2]; $urlInfo = parse_url($imageUrl); if (!in_array(@$urlInfo['host'], $goodsDescAllowImageDomainArray)) { $this->addFlashMessage('商品详情非法图片 ' . $imageUrl); goto out_fail_validate; } } } // 写入到数据库 unset($goods); $goodsBasicService = new GoodsBasicService(); $goods = $goodsBasicService->loadGoodsById($goods_id); // 判断是否是新建商品 $isCreateGoods = $goods->isEmpty(); if ($isCreateGoods) { // 权限检查 $this->requirePrivilege('manage_goods_create'); $goodsInfo['add_time'] = Time::gmTime(); } $post_goods_sn = $validator->validate('goods_sn'); if ($isCreateGoods && !Utils::isBlank($post_goods_sn)) { $goodsInfo['goods_sn'] = $post_goods_sn; } $goods->copyFrom($goodsInfo); $goods->save(); // 新商品需要自动生成 goods_sn if ($isCreateGoods && Utils::isBlank($post_goods_sn)) { $goods->goods_sn = $f3->get('sysConfig[goods_sn_prefix]') . $goods['goods_id']; $goods->save(); } // 取得供货商信息 $supplierName = ''; if (!empty($goods['suppliers_id'])) { $supplierUserService = new SupplierUserService(); $supplierInfo = $supplierUserService->loadSupplierById($goods['suppliers_id']); if (!$supplierInfo->isEmpty()) { $supplierName = $supplierInfo['suppliers_name']; } } // 记录商品编辑日志 $goodsLogContent = '商品编辑:[' . $goods['admin_user_id'] . ']' . $goods['admin_user_name'] . "\n" . '上架状态:' . ($goods['is_on_sale'] > 0 ? '已上架' : '未上架') . "\n" . '销售价:' . Money::toSmartyDisplay($goods['shop_price']) . ' 供货价:' . Money::toSmartyDisplay($goods['suppliers_price']) . "\n" . '快递费:' . Money::toSmartyDisplay($goods['shipping_fee']) . ' 供货快递费:' . Money::toSmartyDisplay($goods['suppliers_shipping_fee']) . "\n" . ($goods['shipping_free_number'] > 0 ? '' . $goods['shipping_free_number'] . "件免邮\n" : '') . '商品排序:' . $goods['sort_order'] . "\n" . '系统Tag:' . $goods['system_tag_list'] . "\n" . '供货商:[' . $goods['suppliers_id'] . ']' . $supplierName; $goodsLogService = new GoodsLogService(); $goodsLogService->addGoodsLog($goods['goods_id'], $authAdminUser['user_id'], $authAdminUser['user_name'], $isCreateGoods ? '新建商品' : static::$goodsLogDesc, $goodsLogContent); // 成功,显示商品详情 $this->addFlashMessage('商品信息保存成功'); //清除缓存,确保商品显示正确 ClearHelper::clearGoodsCacheById($goods->goods_id); RouteHelper::reRoute($this, RouteHelper::makeUrl('/Goods/Edit/Edit', array('goods_id' => $goods->goods_id), true)); return; // 参数验证失败 out_fail_validate: if (!$goods_id) { // 新建商品验证失败 RouteHelper::reRoute($this, '/Goods/Create'); return; } $smarty->assign('goods', $goodsInfo); $smarty->display('goods_edit_edit.tpl'); return; out_fail_list_goods: RouteHelper::reRoute($this, '/Goods/Search'); }
/** * 增加商品关联 * * @param $f3 */ public function ajaxAddLink($f3) { // 权限检查 $this->requirePrivilege('manage_goods_edit_edit_post', true); // 首先做参数验证 $validator = new Validator($f3->get('GET')); $errorMessage = ''; $goods_id = $validator->required()->digits()->min(1)->validate('goods_id'); $link_goods_id = $validator->required()->digits()->min(1)->validate('link_goods_id'); if (!$this->validate($validator)) { $errorMessage = implode('|', $this->flashMessageArray); goto out_fail; } $dataMapper = new DataMapper('link_goods'); $dataMapper->loadOne(array('goods_id = ? and link_goods_id = ?', $goods_id, $link_goods_id)); // 已经关联了,不要重复关联 if (!$dataMapper->isEmpty()) { goto out; } $authAdminUser = AuthHelper::getAuthUser(); // 添加记录 $dataMapper->goods_id = $goods_id; $dataMapper->link_goods_id = $link_goods_id; $dataMapper->admin_id = $authAdminUser['user_id']; $dataMapper->save(); //清除缓存,确保商品显示正确 ClearHelper::clearGoodsCacheById($goods_id); // 记录商品编辑日志 $goodsLogService = new GoodsLogService(); $goodsLogService->addGoodsLog($goods_id, $authAdminUser['user_id'], $authAdminUser['user_name'], '添加商品关联', $link_goods_id); out: Ajax::header(); echo Ajax::buildResult(null, null, null); return; out_fail: // 失败,返回出错信息 Ajax::header(); echo Ajax::buildResult(-1, $errorMessage, null); }
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'); }
public function post($f3) { // 权限检查 $this->requirePrivilege('manage_goods_edit_edit_post'); $goodsLogContent = ''; // 参数验证 $validator = new Validator($f3->get('GET')); $goods_id = $validator->required()->digits()->min(1)->validate('goods_id'); if (!$this->validate($validator)) { goto out_fail_list_goods; } unset($validator); $goodsBasicService = new GoodsBasicService(); $goods = $goodsBasicService->loadGoodsById($goods_id, 1); if ($goods->isEmpty()) { $this->addFlashMessage('商品 id[' . $goods_id . '] 非法'); goto out_fail_list_goods; } // POST 参数验证 $validator = new Validator($f3->get('POST')); $goodsSpecNameArray = $validator->validate('goodsSpecNameArray'); $goodsSpecValue1Array = $validator->validate('goodsSpecValue1Array'); $goodsSpecValue2Array = $validator->validate('goodsSpecValue2Array'); $goodsSpecValue3Array = $validator->validate('goodsSpecValue3Array'); $goodsNumberArray = $validator->validate('goodsNumberArray'); $goodsSpecAddPriceArray = $validator->validate('goodsSpecAddPriceArray'); $goodsSnArray = $validator->validate('goodsSnArray'); $imgIdArray = $validator->validate('imgIdArray'); if (empty($goodsSpecValue1Array)) { goto save_spec; } // 检查,商品属性名不能为空 foreach ($goodsSpecNameArray as $goodsSpecName) { if (!empty($goodsSpecName)) { break; } $this->addFlashMessage('商品属性名不能为空'); goto out_fail_edit_spec; } // 商品选项中不能有特殊符号 $valueArray = array_merge($goodsSpecValue1Array, $goodsSpecValue2Array, $goodsSpecValue3Array); foreach ($valueArray as $valueItem) { if (empty($valueItem)) { continue; } // 商品规格不允许有特殊符号 if (preg_match('#[,\\\\t\\s\\n\\+\\?\\^~!%/$]+#', $valueItem)) { $this->addFlashMessage('商品选项不能有特殊符号: 逗号、空格、回车、\\、? 等 ...'); goto out_validate_fail; } } // 做数据格式转换,商品库存 foreach ($goodsNumberArray as &$number) { $number = abs(intval($number)); } unset($number); // 做数据格式转换,商品规格对应的加价 foreach ($goodsSpecAddPriceArray as &$add_price) { $add_price = Money::toStorage(abs(floatval($add_price))); } unset($add_price); if (!$this->validate($validator)) { goto out_reroute; } save_spec: $goodsSpecService = new GoodsSpecService(); $goodsSpecService->initWithData($goodsSpecNameArray, $goodsSpecValue1Array, $goodsSpecValue2Array, $goodsSpecValue3Array, $goodsNumberArray, $goodsSpecAddPriceArray, $goodsSnArray, $imgIdArray); // 保存数据 $goodsSpecService->saveGoodsSpec($goods_id); $this->addFlashMessage('更新商品规格成功'); out: // 正常退出 $goodsSpecNameArray = is_array($goodsSpecNameArray) ? $goodsSpecNameArray : array(); $goodsLogContent .= '属性名:' . implode(',', $goodsSpecNameArray) . "\n"; $valueCount = count($goodsSpecValue1Array); for ($valueIndex = 0; $valueIndex < $valueCount; $valueIndex++) { $goodsLogContent .= '选择:' . @$goodsSpecValue1Array[$valueIndex] . ',' . @$goodsSpecValue2Array[$valueIndex] . ',' . @$goodsSpecValue3Array[$valueIndex] . ',' . '库存:' . @$goodsNumberArray[$valueIndex] . ',' . '加价:' . Money::toSmartyDisplay(@$goodsSpecAddPriceArray[$valueIndex]) . ',' . 'SN:' . @$goodsSnArray[$valueIndex] . ',' . 'image:' . @$imgIdArray[$valueIndex] . ',' . "\n"; } $authAdminUser = AuthHelper::getAuthUser(); $goodsLogService = new GoodsLogService(); $goodsLogService->addGoodsLog($goods_id, $authAdminUser['user_id'], $authAdminUser['user_name'], '商品规格', $goodsLogContent); //清除缓存,确保商品显示正确 ClearHelper::clearGoodsCacheById($goods_id); out_reroute: RouteHelper::reRoute($this, RouteHelper::makeUrl('/Goods/Edit/Spec', array('goods_id' => $goods_id), true)); return; out_fail_list_goods: RouteHelper::reRoute($this, '/Goods/Search'); return; out_validate_fail: global $smarty; $smarty->display('goods_edit_spec.tpl'); return; out_fail_edit_spec: RouteHelper::reRoute($this, RouteHelper::makeUrl('/Goods/Edit/Spec', array('goods_id' => $goods->goods_id), true)); }