goods_cat_list();
$impotr=M('import_active')->where(['store_id'=>getAdmStoId(),'state'=>1,'type'=>101])->find();
$this->assign('import',$impotr);
$this->assign('cat_list', $cat_list);
// upload_ylp_log('商品分类');
return $this->fetch('', getAdmStoId());
}
/**
* 添加修改商品分类
* 手动拷贝分类正则 ([\u4e00-\u9fa5/\w]+) ('393','$1'),
* select * from tp_goods_category where id = 393
* select * from tp_goods_category where parent_id = 393
* update tp_goods_category set parent_id_path = concat_ws('_','0_76_393',id),`level` = 3 where parent_id = 393
* insert into `tp_goods_category` (`parent_id`,`name`) values
* ('393','时尚饰品'),
*/
public function addEditCategory()
{
$stoid=getAdmStoId();
$this->assign('erpid', getERPId());
$this->assign('curid', '0');
$GoodsLogic = new GoodsLogic();
if (IS_GET) {
$goods_category_info = D('goods_category')->where(' store_id=' . getAdmStoId() . ' and id=' . I('GET.id', 0))->find();
// var_dump($goods_category_info);
// die();
$level_cat = $GoodsLogic->find_parent_cat($goods_category_info['id']); // 获取分类默认选中的下拉框
$cat_list = M('goods_category')->where(" store_id=" . getAdmStoId() . " and parent_id = 0")->order('sort_order')->select(); // 已经改成联动菜单
$this->assign('level_cat', $level_cat);
$this->assign('cat_list', $cat_list);
if ($goods_category_info['id'] != "") {
$this->assign('curid', $goods_category_info['id']);
}
$m = Db::query("select max(sort_order)odr from __PREFIX__goods_category where store_id=" . getAdmStoId());
$md = (int)$m[0]["odr"];
$md++;
if (empty($goods_category_info)) {
$goods_category_info["sort_order"] = $md;
$goods_category_info['status']=1;
}
$this->assign('goods_category_info', $goods_category_info);
return $this->fetch('_category', getAdmStoId());
exit;
}
$GoodsCategory = D('GoodsCategory'); //
$type = I('id') > 0 ? 2 : 1; // 标识自动验证时的 场景 1 表示插入 2 表示更新
//ajax提交验证
if (I('is_ajax') == 1) {
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
// 数据验证
$validate = \think\Loader::validate('GoodsCategory');
if (!$validate->batch()->check(input('post.'))) {
$error = $validate->getError();
$error_msg = array_values($error);
$return_arr = array(
'status' => -1,
'msg' => $error_msg[0],
'data' => $error,
);
$this->ajaxReturn($return_arr);
} else {
// 收集数据
$GoodsCategory->data(input('post.'), true);
$GoodsCategory['store_id'] = $stoid;
$GoodsCategory['commission_rate'] = 0;
if ($GoodsCategory['imgtype'] == 0) {
$GoodsCategory['image'] = I('image');
} else {
$GoodsCategory['image'] = I('httimg');
}
$GoodsCategory->parent_id = I('parent_id_1');
input('parent_id_2') && ($GoodsCategory->parent_id = input('parent_id_2'));
//编辑判断
if ($type == 2) {
$children_where = array(
'parent_id_path' => array('like', '%_' . I('id') . "_%")
);
$children = M('goods_category')->where('store_id',getAdmStoId())->where($children_where)->max('level');
if (I('parent_id_1')) {
$parent_level = M('goods_category')->where(array('store_id'=>getAdmStoId(),'id' => I('parent_id_1')))->getField('level', false);
if (($parent_level + $children) > 3) {
$return_arr = array(
'status' => -1,
'msg' => '商品分类最多为三级',
'data' => '',
);
$this->ajaxReturn($return_arr);
}
}
if (I('parent_id_2')) {
$parent_level = M('goods_category')->where(array('store_id'=>getAdmStoId(),'id' => I('parent_id_2')))->getField('level', false);
if (($parent_level + $children) > 3) {
$return_arr = array(
'status' => -1,
'msg' => '商品分类最多为三级',
'data' => '',
);
$this->ajaxReturn($return_arr);
}
}
}
if ($GoodsCategory->id > 0 && $GoodsCategory->parent_id == $GoodsCategory->id) {
// 编辑
$return_arr = array(
'status' => -1,
'msg' => '上级分类不能为自己',
'data' => '',
);
$this->ajaxReturn($return_arr);
}
if ($GoodsCategory->commission_rate > 100) {
// 编辑
$return_arr = array(
'status' => -1,
'msg' => '分佣比例不得超过100%',
'data' => '',
);
$this->ajaxReturn($return_arr);
}
$rt=M("admin")->where('admin_id',getAdminId())->field('ERPUser')->find();
if ($type == 2) {
$GoodsCategory['edit_time'] = time();
$GoodsCategory['edit_ip'] = getIP();
$GoodsCategory['edit_man'] = $rt['ERPUser'];
$GoodsCategory->isUpdate(true)->save(); // 写入数据到数据库
$GoodsLogic->refresh_cat(I('id'),$stoid);
upload_ylp_log('B01商品分类编辑/确认提交');
} else {
//$GoodsCategory['add_time'] = time();
//$GoodsCategory['edit_ip'] = getIP();
//$GoodsCategory['edit_man'] = $rt['ERPUser'];
//$GoodsCategory->save(); // 写入数据到数据库
//$insert_id = $GoodsCategory->getLastInsID(); 读写分离的情况是获取不到ID的
$data=input('post.');
$data['add_time'] = time();
$data['edit_ip'] = getIP();
$data['edit_man'] = $rt['ERPUser'];
$data['store_id'] = $stoid;
$data['commission_rate'] = 0;
if ($data['imgtype'] == 0) { $data['image'] = I('image'); } else {$data['image'] = I('httimg');}
$data['parent_id'] = I('parent_id_1');
input('parent_id_2') && ($data['parent_id'] = input('parent_id_2'));
$insert_id = M("goods_category")->insertGetId($data);
$GoodsLogic->refresh_cat($insert_id,$stoid);
}
$return_arr = array(
'status' => 1,
'msg' => '操作成功',
'data' => array('url' => U('Admin/Goods/categoryList')),
);
$this->ajaxReturn($return_arr);
}
}
}
/**
* 获取商品分类 的帅选规格 复选框
*/
public function ajaxGetSpecList()
{
$GoodsLogic = new GoodsLogic();
$_REQUEST['category_id'] = $_REQUEST['category_id'] ? $_REQUEST['category_id'] : 0;
$filter_spec = M('GoodsCategory')->where("id = " . $_REQUEST['category_id'])->getField('filter_spec');
$filter_spec_arr = explode(',', $filter_spec);
$str = $GoodsLogic->GetSpecCheckboxList($_REQUEST['type_id'], $filter_spec_arr);
$str = $str ? $str : '没有可帅选的商品规格';
exit($str);
}
/**
* 获取商品分类 的帅选属性 复选框
*/
public function ajaxGetAttrList()
{
$GoodsLogic = new GoodsLogic();
$_REQUEST['category_id'] = $_REQUEST['category_id'] ? $_REQUEST['category_id'] : 0;
$filter_attr = M('GoodsCategory')->where("id = " . $_REQUEST['category_id'])->getField('filter_attr');
$filter_attr_arr = explode(',', $filter_attr);
$str = $GoodsLogic->GetAttrCheckboxList($_REQUEST['type_id'], $filter_attr_arr);
$str = $str ? $str : '没有可帅选的商品属性';
exit($str);
}
/**
* 删除分类
*/
public function delGoodsCategory()
{
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
$id = $this->request->param('id');
// 判断子分类
$GoodsCategory = M("goods_category");
$count = $GoodsCategory->where("parent_id = {$id}")
->where('store_id',getAdmStoId())->count("id");
$count > 0 && $this->error('该分类下还有分类不得删除!', U('Admin/Goods/categoryList'));
// 判断是否存在商品
$goods_count = M('Goods')->where(array('store_id'=>getAdmStoId(),'cat_id'=>$id))->count('1');
$goods_count > 0 && $this->error('该分类下有商品不得删除!', U('Admin/Goods/categoryList'));
//删除图片
$goods_search = D('goods_category')->where(array('store_id'=>getAdmStoId(),'id'=>$id))->find();
$delimg = $goods_search['image'];
if ($delimg) {
//删除腾讯云图片
vendor('qcloudcos.myqcloudcos');
//if(!empty($url)){
$rs = Myqcloudcos::delFile('wxd', $delimg);
mdelFile(ROOT_PATH . $delimg);
//}
//mdelFile(ROOT_PATH . $delimg);
}
// 删除分类
DB::name('goods_category')->where(array('store_id'=>getAdmStoId(),'id'=>$id))->delete();
$this->success("操作成功!!!", U('Admin/Goods/categoryList'));
}
/**
* 商品列表
*/
public function goodsList()
{
$key_word = I('key_word') ? trim(I('key_word')) : ''; // 关键词搜索
$intro = I('intro');//新品、推荐
$brand_id = I('brand_id');//品牌
$is_on_sale = I('is_on_sale');//上架、下架
$dis_type = I('dis_type');//分销类型
$iscontent = I('iscontent');//是否完善详情
if ($dis_type=="")
{
$dis_type=-1;
}
if ($is_on_sale=="")
{
$is_on_sale=1;
}
$cat_id = I('cat_id');//分类
$getp = I('p');
$pagenum = 10;//每页显示多少条
if ((int)I('pagenum/s') > 0) {
$pagenum = I('pagenum/s');
}
$order1 = I('order1/s');
$order2 = I('order2/s');
$GoodsLogic = new GoodsLogic();
$brandList = $GoodsLogic->getSortBrands();
$categoryList = $GoodsLogic->getSortCategory();
$oldurl = U('Admin/goods/goodsList', array(
"intro" => $intro,
"brand_id" => $brand_id,
"iscontent" => $iscontent,
"is_on_sale" => $is_on_sale,
"cat_id" => $cat_id,
"key_word" => $key_word,
"pagenum" => $pagenum,
"p" => $getp,
"order1" => $order1,
"order2" => $order2
));
$this->assign('oldurl', urlencode($oldurl));
$this->assign('key_word', $key_word);
$this->assign('iscontent', $iscontent);
$this->assign('intro', $intro);
$this->assign('dis_type', $dis_type);
$this->assign('brand_id', $brand_id);
$this->assign('is_on_sale', $is_on_sale == "" ? 2 : $is_on_sale);
$this->assign('cat_id',$cat_id);
$this->assign('brand_id',$brand_id);
$this->assign('pagenum', $pagenum);
$this->assign('cur_page', $getp);
$this->assign('order1', $order1);
$this->assign('order2', $order2);
$this->assign('categoryList', $categoryList);
$this->assign('brandList', $brandList);
return $this->fetch('', getAdmStoId());
}
/**
* 商品列表
*/
public function ajaxGoodsList()
{
$key_word = I('key_word') ? trim(I('key_word')) : ''; // 关键词搜索
$intro = I('intro');//新品、推荐
$brand_id = I('brand_id');//品牌
$is_on_sale = I('is_on_sale');//上架、下架
$dis_type = I('dis_type');//分销类型
$iscontent = I('iscontent');//是否完善详情
if ($is_on_sale == "") {
$is_on_sale = 1;
}
$exp_sum_type = I('exp_sum_type');//运费设置
$cat_id = I('cat_id');//分类
$cur_page = 1;//当前页数
if ((int)I('p/s') > 0) {
$cur_page = I('p/s');
}
$pagenum = 10;//每页显示多少条
if ((int)I('pagenum/s') > 0) {
$pagenum = I('pagenum/s');
}
$order1 = I('order1/s', "sort");
$order2 = I('order2/s', "asc");
$where = ' 1 = 1 '; // 搜索条件
$where .= " and store_id=" . getAdmStoId();
if ($intro) {
switch ($intro) {
case "is_new":
$where .= " and is_new=1";
break;
case "is_recommend":
$where .= " and is_recommend=1";
break;
case "is_hot":
$where .= " and is_hot=1";
break;
case "promtype1":
$where .= " and prom_type=1";
break;
case "promtype2":
$where .= " and prom_type=2";
break;
case "promtype3":
$where .= " and prom_type=3";
break;
case "promtype4":
$where .= " and prom_type=4";
break;
case "promtype6":
$where .= " and prom_type=6";
break;
default:
break;
}
}
$brand_id && $where = "$where and brand_id = " . $brand_id; //品牌
$time = time();
switch ($dis_type) {
case "0":
$where .= " and dis_type=0";
break;
case "1":
$where .= " and dis_type=1";
break;
default:
break;
}
switch ($iscontent) {
case 1:
$where .= " and goods_content=''";
break;
case 2:
$where .= " and goods_content<>'' ";
break;
default:
break;
}
//上架、下架
switch ($is_on_sale) {
case "0":
$where .= " and (on_time>" . $time . " or down_time<" . $time . " and down_time !='') ";
break;
case "1":
$where .= " and is_on_sale=1 and on_time<" . $time . " and (down_time>" . $time . " or down_time=0 or down_time='' or down_time is null) ";
break;
default:
break;
}
if ($exp_sum_type > 0) {
$where .= " and exp_sum_type=" . $exp_sum_type;
}
if ($key_word) {
$where = "$where and (goods_name like '%$key_word%' or goods_sn like '%$key_word%' or keywords like '%$key_word%' or sku like '%$key_word%')";
}
if ($cat_id > 0) {
$grandson_ids = getCatGrandson($cat_id);
$where .= " and cat_id in(" . implode(',', $grandson_ids) . ") "; // 初始化搜索条件
}
$model = M('Goods');
$count = $model->where($where)->count();
$countstr = $model->where($where)->fetchSql(true)->count();
$Page = new AjaxPage($count, $pagenum);
$show = $Page->show();
$order_str = "`$order1` $order2";
$goodsList = $model->where($where)->order($order_str)->limit($Page->firstRow . ',' . $Page->listRows)->select();
//判断是否上架
$t = time();
foreach ($goodsList as $kr => $vr) {
if ($vr['on_time'] < $t && ($vr['down_time'] > $t || $vr['down_time'] == "") && $vr['is_on_sale'] == 1) {
$goodsList[$kr]['is_on'] = 1;
}
}
$catList = D('goods_category')->where(" store_id=" . getAdmStoId())->select();
$catList = convert_arr_key($catList, 'id');
$this->assign('catList', $catList);
$this->assign('goodsList', $goodsList);
$this->assign('page', $show);// 赋值分页输出
$this->assign('pager', $Page);
$this->assign('pagenum', $pagenum);
// $GoodsLogic = new GoodsLogic();
// $brandList = $GoodsLogic->getSortBrands();
// $categoryList = $GoodsLogic->getSortCategory();
$oldurl = U('Admin/goods/goodsList', array(
"intro" => $intro,
"brand_id" => $brand_id,
"is_on_sale" => $is_on_sale,
"cat_id" => $cat_id,
"key_word" => $key_word,
"pagenum" => $pagenum,
"p" => $cur_page,
"order1" => $order1,
"order2" => $order2
));
$this->assign('oldurl', urlencode($oldurl));
$this->assign('key_word', $key_word);
$this->assign('intro', $intro);
$this->assign('brand_id', $brand_id);
$this->assign('is_on_sale', $is_on_sale);
$this->assign('cat_id', $cat_id);
$this->assign('pagenum', $pagenum);
$this->assign('cur_page', $cur_page);
$this->assign('order1', $order1);
$this->assign('order2', $order2);
// $this->assign('categoryList', $categoryList);
// $this->assign('brandList', $brandList);
$this->assign('store_warning_storage', tpCache('basic.warning_storage', getAdmStoId()));
// upload_ylp_log('商品管理');
return $this->fetch('', getAdmStoId());
}
public function exportreport()
{
$pattern = tpCache('distribut.pattern', getAdmStoId());
$key_word = I('key_word') ? trim(I('key_word')) : ''; // 关键词搜索
$intro = I('intro');//新品、推荐
$brand_id = I('brand_id');//品牌
$is_on_sale = I('is_on_sale');//上架、下架
$cat_id = I('cat_id');//分类
$dis_type = I('dis_type');//分销类型
$exp_sum_type = I('exp_sum_type');//运费设置
$cur_page = 1;//当前页数
if ((int)I('p/s') > 0) {
$cur_page = I('p/s');
}
$pagenum = 10;//每页显示多少条
if ((int)I('pagenum/s') > 0) {
$pagenum = I('pagenum/s');
}
$order1 = I('order1/s', "goods_id");
$order2 = I('order2/s', "desc");
$where = ' 1 = 1 '; // 搜索条件
$where .= " and a.store_id=" . getAdmStoId();
if ($intro) {
switch ($intro) {
case "is_new":
$where .= " and is_new=1";
break;
case "is_recommend":
$where .= " and is_recommend=1";
break;
case "is_hot":
$where .= " and is_hot=1";
break;
case "promtype1":
$where .= " and prom_type=1";
break;
case "promtype2":
$where .= " and prom_type=2";
break;
case "promtype3":
$where .= " and prom_type=3";
break;
case "promtype4":
$where .= " and prom_type=4";
break;
case "promtype6":
$where .= " and prom_type=6";
break;
default:
break;
}
}
$brand_id && $where = "$where and a.brand_id = " . $brand_id; //品牌
$time = time();
switch ($dis_type) {
case "0":
$where .= " and dis_type=0";
break;
case "1":
$where .= " and dis_type=1";
break;
default:
break;
}
//上架、下架
switch ($is_on_sale) {
case "0":
$where .= " and (on_time>" . $time . " or down_time<" . $time . " and down_time !='') ";
break;
case "1":
$where .= " and is_on_sale=1 and on_time<" . $time . " and (down_time>" . $time . " or down_time=0 or down_time='' or down_time is null) ";
break;
default:
break;
}
if ($exp_sum_type > 0) {
$where .= " and exp_sum_type=" . $exp_sum_type;
}
if ($key_word) {
$where = "$where and (a.goods_name like '%$key_word%' or a.goods_sn like '%$key_word%' or a.keywords like '%$key_word%')";
}
if ($cat_id > 0) {
$grandson_ids = getCatGrandson($cat_id);
$where .= " and a.cat_id in(" . implode(',', $grandson_ids) . ") "; // 初始化搜索条件
}
$order_str = "`$order1` $order2";
$goodsList = M('Goods')->alias('a')->join('goods_category b', 'a.cat_id=b.id and a.store_id=b.store_id', 'left')
->join('brand c', 'a.brand_id=c.id', 'left')
->join('nation d', 'a.nation_id=d.id', 'left')
->field('a.*,b.name as cat_name,c.name as brand_name,d.name as nation_name')
->where($where)->order($order_str)->select();
$catList = D('goods_category')->where(" store_id=" . getAdmStoId())->select();
//判断 是否开通等级卡
$is_cardstore = 0;
if (getERPId()) { //有ERP
$tk = M("store_config")->where("store_id", getAdmStoId())
->field("dj_num")->find();
if ($tk['dj_num'] > 0) {
$is_cardstore = 1;
}
}
$strTable = '
';
$strTable .= '';
$strTable .= '商品名称 | ';
$strTable .= '商品编号 | ';
$strTable .= '商品条码 | ';
$strTable .= '分类 | ';
$strTable .= '品牌 | ';
$strTable .= '国别 | ';
$strTable .= '分销类型 | ';
$strTable .= '商品重量 | ';
$strTable .= '配送方式 | ';
$strTable .= '佣金 | ';
$strTable .= '市场价 | ';
$strTable .= '手店价 | ';
if ($is_cardstore) {
$strTable .= '售价(一) | ';
$strTable .= '售价(二) | ';
$strTable .= '售价(三) | ';
}
$strTable .= '库存数量 | ';
$strTable .= '已售数量 | ';
$strTable .= '商品标签 | ';
$strTable .= '
';
if (is_array($goodsList)) {
foreach ($goodsList as $k => $val) {
$getpromname = "";
if ($val['prom_type']) {
switch ($val['prom_type']) {
case "1":
$getpromname = "秒杀商品";
break;
case "2":
$getpromname = "团购商品";
break;
case "3":
$getpromname = "促销优惠";
break;
case "4":
$getpromname = "积分购";
break;
case "6":
$getpromname = "天天拼单";
break;
default:
break;
}
}
$strTable .= '';
$strTable .= ' ' . $val['goods_name'] . ' | ';
$strTable .= ' ' . $val['goods_sn'] . ' | ';
$strTable .= ' ' . $val['sku'] . ' | ';
$strTable .= '' . $val['cat_name'] . ' | ';
$strTable .= '' . $val['brand_name'] . ' | ';
$strTable .= '' . $val['nation_name'] . ' | ';
$strTable .= '' . ($val['dis_type'] == '0' ? '主营' : '可选') . ' | ';
$strTable .= '' . $val['weight'] . '克 | ';
$strTable .= '' . ($val['distr_type'] == '0' ? '用户自选' : $val['distr_type'] == '1' ? '自提' : '物流') . ' | ';
$strTable .= '' . ($pattern == '1' ? $val['fir_rate'] + $val['sec_rate'] + $val['thi_rate'] : $val['commission']) . ' | ';
$strTable .= '' . $val['market_price'] . ' | ';
$strTable .= '' . $val['shop_price'] . ' | ';
if ($is_cardstore) {
$strTable .= '' . $val['cardprice1'] . ' | ';
$strTable .= '' . $val['cardprice2'] . ' | ';
$strTable .= '' . $val['cardprice3'] . ' | ';
}
$strTable .= '' . $val['store_count'] . ' | ';
$strTable .= '' . $val['sales_sum'] . ' | ';
$strTable .= '' . ($val['is_recommend'] == '1' ? '【推荐】' : '') . ($val['is_new'] == '1' ? '【新品】' : '') . ($val['is_hot'] == '1' ? '【热卖】' : '') . '';
$strTable .= ($getpromname != '' ? '【' . $getpromname . '】' : '');
$strTable .= ' | ';
$strTable .= '
';
}
}
$strTable .= '
';
echo $strTable;
unset($goodsList);
downloadExcel($strTable, 'goods');
exit();
}
//库存日志
public function stock_list()
{
$model = M('stock_log');
$map = array();
$mtype = I('mtype');
if ($mtype == 1) {
$map['stock'] = array('gt', 0);
}
if ($mtype == -1) {
$map['stock'] = array('lt', 0);
}
$goods_name = I('goods_name');
if ($goods_name) {
$map['goods_name'] = array('like', "%$goods_name%");
}
$stoid = getAdmStoId();
$map['store_id'] = $stoid;
$ctime = I('ctime');
if ($ctime) {
$gap = explode(' - ', $ctime);
$this->assign('start_time', $gap[0]);
$this->assign('end_time', $gap[1]);
$this->assign('ctime', $gap[0] . ' - ' . $gap[1]);
$map['ctime'] = array(array('gt', strtotime($gap[0])), array('lt', strtotime($gap[1])));
}
$count = $model->where($map)->count();
$Page = new Page($count, 20);
$show = $Page->show();
$this->assign('pager', $Page);
$this->assign('page', $show);// 赋值分页输出
$stock_list = $model->where($map)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
$this->assign('stock_list', $stock_list);
// if($stock_list){
// $uids = get_arr_column($stock_list,'store_id');
// $store = M('store')->where("store_id in (". implode(',', $uids).")")->getField('store_id,store_name');
// $this->assign('store',$store);
// }
return $this->fetch('stock_list', getAdmStoId());
}
/**
* 添加修改商品
*/
public function addEditGoods()
{
$oldurl = I('oldurl/s');
$sto_id = getAdmStoId();
if ($oldurl) {
$oldurl = urldecode(urldecode($oldurl));
}
$this->assign('oldurl', $oldurl);
$this->assign('erpid', getERPId());
$GoodsLogic = new GoodsLogic();
$water = tpCache('water', $sto_id);
$storeconfig=M('store_config')->where("store_id",getAdmStoId())->field('switch_list')->find();
$rank=json_decode($storeconfig['switch_list'],true);
$rank_switch = $rank['rank_switch'];//等级开关
$mz_switch = tpCache('shopping.is_beauty',$sto_id);//美妆开关
$this->assign('rank_switch',$rank_switch);
$this->assign('mz_switch',$mz_switch);
if ($water['is_mark'] == 1) {
if ($water['mark_type'] == 'img' ) {
if(!empty($water['mark_img'])) {
$imgpath = $water['mark_img'];
vendor('qcloudcos.myqcloudcos');
$resfolder = Myqcloudcos::stat('wxd', $imgpath);
if ($resfolder && $resfolder['code'] != 0)//不存在
{
$this->assign('isnowater',"1");
}
}else{
$this->assign('isnowater',"1");
}
}
}
$getsku=preg_replace('/\r|\n|\t/', '', I('sku',0));
$goods_list=null;
if($getsku)
$goods_list = M('goods')->where(['sku'=>$getsku,'store_id'=>$sto_id])->order('goods_id desc')->select();
$goodsInfo = array();
$goodsImages =array();
$goodsInfo['is_virtual']=0;
if(!empty($goods_list)){
$goodsInfo=$goods_list[0];
$goodsInfo['sku']=trim($goodsInfo['sku']);
$goodsInfo['goods_list']=$goods_list;
$goodsImages=M('goods_images')->where('goods_id',$goodsInfo['goods_id'])
->order('ismain desc,img_sort asc')->select();
$v_url = M('goods_videos')->where('goods_id', $goodsInfo['goods_id'])->order('video_sort desc')->select();
}
$level_cat = $GoodsLogic->find_parent_cat($goodsInfo['cat_id']); // 获取分类默认选中的下拉框
$level_cat2 = $GoodsLogic->find_parent_cat($goodsInfo['extend_cat_id']); // 获取分类默认选中的下拉框
$cat_list = M('goods_category')->where("parent_id = 0")->where('store_id',$sto_id)->select(); // 已经改成联动菜单
$brandList = $GoodsLogic->getSortBrands();
$nationList = $GoodsLogic->getSortNations();
$this->assign('level_cat', $level_cat);
$this->assign('level_cat2', $level_cat2);
$this->assign('cat_list', $cat_list);
$this->assign('brandList', $brandList);
$this->assign('nationList', $nationList);
$pattern=tpCache('distribut.pattern',getAdmStoId());
$this->assign('pattern', $pattern);
if (empty($goodsInfo)){
$goodsInfo['give_integral'] = 1;
}
if (empty($goodsInfo["exp_sum_type"])) {
$goodsInfo["exp_sum_type"] = 3;
}
if (empty($goodsInfo["on_time"])) {
$goodsInfo['on_time'] = time();
}
$mokun = tpCache('basic.default_storage', $sto_id);
$this->assign('mokun', $mokun); // 默认库存
$this->assign('goodsInfo', $goodsInfo); // 商品详情
$this->assign('goodsImages', $goodsImages); // 商品相册
$this->assign('v_url', $v_url); // 商品视频
$this->initEditor(); // 编辑器
return $this->fetch('_goods', getAdmStoId());
}
public function goods_handle()
{
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
$Goods = I('post.');
$Goods['sku'] = preg_replace('/\r|\n|\t/', '',trim($Goods['sku']));
$old_url = $Goods['oldurl'];
$goods_list = $Goods['data'];
$goods_img = $Goods['goods_images'];
$data_img=[];
array_pop($goods_img); // 弹出最后一个
unset($Goods['data']);
unset($Goods['oldurl']);
unset($Goods['goods_images']);
$sto_id = getAdmStoId();
$goods_video= $Goods['v_url'];
$video_img= $Goods['v_img'];
$up_video_id= $Goods['up_video_id'];
$data_img = []; //从表图片
$video_data=[]; //从表视频
//分类
$cat_id_3=I('cat_id_3');
$cat_id_2=I('cat_id_2');
$cat_id_1=I('cat_id');
$gcat_id=$cat_id_3;
if(empty($cat_id_3)){ $gcat_id=$cat_id_2; }
if(empty($cat_id_2)){$gcat_id=$cat_id_1;}
if(empty($cat_id_1)){
return json(['code' => -1, 'msg' => '请选择分类', 'url' => $old_url]);
}
/*---判断统一运费---*/
$Goods['is_free_shipping'] = 0;
$t = $Goods['exp_sum_type'];
/*--如果是统一运费--*/
if ($t == 1) {
if (empty($Goods['uniform_exp_sum'])) {
$Goods['uniform_exp_sum'] = 0;
}
/*--如果是统一运费等于0就是包邮--*/
if ($Goods['uniform_exp_sum'] <= 0) {
$Goods['is_free_shipping'] = 1;
}
}
$Goods['on_time'] = strtotime($Goods['on_time']);
$Goods['down_time'] = strtotime($Goods['down_time']);
$Goods['store_id'] = $sto_id;
$sort = M('goods')->where('store_id', $sto_id)->Max('sort');
$tk = tpCache('shop_info.api_token', getAdmStoId());
$c = 0;
foreach ($goods_list as $k => $v) {
$id = $v['goods_id'];
$Goods['erpwareid'] = $v['erpwareid'];
$Goods['goods_spec'] = $v['goods_spec'];
$Goods['goods_color'] = $v['goods_color'];
$Goods['goods_sn'] = $v['goods_sn'];
$Goods['market_price'] = $v['market_price'];
$Goods['shop_price'] = $v['shop_price'];
$Goods['mz_price'] = $v['mz_price'];
$Goods['store_count'] = $v['store_count'];
$Goods['sales_sum'] = $v['sales_sum'];
$Goods['viplimited'] = $v['viplimited'];
$Goods['weight'] = $v['weight'];
$Goods['cardprice1'] = $v['cardprice1'];
$Goods['cardprice2'] = $v['cardprice2'];
$Goods['cardprice3'] = $v['cardprice3'];
$Goods['is_mainshow'] = (int)$v['is_mainshow'];
$Goods['spec_img'] = $v['spec_img'];
$pattern=tpCache('distribut.pattern',getAdmStoId());
if($pattern==1) {
$Goods['fir_rate'] = $v['fir_rate'];
$Goods['sec_rate'] = $v['sec_rate'];
$Goods['thi_rate'] = $v['thi_rate'];
}else{
$Goods['commission'] = $v['commission'];
}
$Goods['cat_id'] = $gcat_id;//分类ID
if (empty($id)) {
if (!empty($Goods['goods_content']) && empty($c)) {
$this->mztk($Goods, 1);
$c++;
}
++$sort;
$Goods['sort'] = $sort;
$res = getApiData("wxd.mshop.wares.add", $tk, array(['WareId' => $v['erpwareid']]));
mlog($res, "good/" . getAdmStoId());
if ($res)
{
$res_adderp=json_decode($res,true);
if ($res_adderp['code']==1)
{
$Goods['is_erpwares'] = 1;
}
}
$id = M('goods')->add($Goods);
$img_sort=0;
if (!empty($Goods['original_img'])) {
$data_img[] = ['goods_id' => $id, 'image_url' => $Goods['original_img'], 'store_id' => $sto_id, 'ismain' => 1,"img_sort"=>$img_sort];
}
if($goods_img) {
foreach ($goods_img as $val) {
if ($Goods['original_img'] != $val && $val != "") {
$img_sort++;
$data_img[] = ['goods_id' => $id, 'image_url' => $val, 'store_id' => $sto_id, 'ismain' => 0, "img_sort" => $img_sort];
}
}
}
//---视频从表----
if($goods_video[0]) {
$video_sort = 0;
foreach ($goods_video as $k => $val) {
$video_data[] = ['goods_id' => $id, 'video_url' => $val, 'video_img' => $video_img[$k],
'store_id' => $sto_id, "$video_sort" => $video_sort,'up_video_id'=>$up_video_id[$k]];
$video_sort++;
}
}
} else {
$Goods['edit_time'] = time();
$Goods['edit_ip'] = getIP();
if (!empty($Goods['goods_content']) && empty($c)) {
$this->mztk($Goods, 2);
$c++;
}
M('goods')->where('goods_id', $id)->update($Goods);
//if ($Goods['iseditimg']==1 || !in_array($Goods['original_img'],$goods_img)) {
M('goods_images')->where('goods_id', $id)->where('store_id', getAdmStoId())->delete();
$img_sort=0;
if ($Goods['original_img']) {
$data_img[] = ['goods_id' => $id, 'image_url' => $Goods['original_img'], 'store_id' => $sto_id, 'ismain' => 1,"img_sort"=>$img_sort];
}
if($goods_img) {
foreach ($goods_img as $val) {
if ($Goods['original_img'] != $val && $val != "") {
$img_sort++;
$data_img[] = ['goods_id' => $id, 'image_url' => $val, 'store_id' => $sto_id, 'ismain' => 0,"img_sort"=>$img_sort];
}
}
}
//}
//---视频从表----
if($goods_video[0]) {
M('goods_videos')->where('goods_id', $id)->where('store_id', getAdmStoId())->delete();
$video_sort = 0;
foreach ($goods_video as $k => $val) {
$video_data[] = ['goods_id' => $id, 'video_url' => $val, 'video_img' => $video_img[$k],
'store_id' => $sto_id, "video_sort" => $video_sort,'up_video_id'=>$up_video_id[$k]];
$video_sort++;
}
}
upload_ylp_log('B04商品管理编辑/确认提交');
}
}
if ($data_img) {
M('goods_images')->insertAll($data_img);
}
//---保存视频--
if ($video_data) {
M('goods_videos')->insertAll($video_data);
}else{
M('goods_videos')->where('goods_id',$id)->delete();
}
return json(['code' => 1, 'msg' => '操作成功', 'url' => $old_url]);
}
/**
* 商品类型 用于设置商品的属性
*/
public function goodsTypeList()
{
$model = M("GoodsType");
$count = $model->count();
$Page = $pager = new Page($count, 14);
$show = $Page->show();
$goodsTypeList = $model->order("id desc")->limit($Page->firstRow . ',' . $Page->listRows)->select();
$this->assign('pager', $pager);
$this->assign('show', $show);
$this->assign('goodsTypeList', $goodsTypeList);
return $this->fetch('goodsTypeList', getAdmStoId());
}
/**
* 添加修改编辑 商品属性类型
*/
public function addEditGoodsType()
{
$id = $this->request->param('id', 0);
$model = M("GoodsType");
if (IS_POST) {
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
$data = $this->request->post();
if ($id)
DB::name('GoodsType')->update($data);
else
DB::name('GoodsType')->insert($data);
$this->success("操作成功!!!", U('Admin/Goods/goodsTypeList'));
exit;
}
$goodsType = $model->find($id);
$this->assign('goodsType', $goodsType);
return $this->fetch('_goodsType', getAdmStoId());
}
/**
* 商品属性列表
*/
public function goodsAttributeList()
{
$goodsTypeList = M("GoodsType")->select();
$this->assign('goodsTypeList', $goodsTypeList);
return $this->fetch('', getAdmStoId());
}
/**
* 商品属性列表
*/
public function ajaxGoodsAttributeList()
{
//ob_start('ob_gzhandler'); // 页面压缩输出
$where = ' 1 = 1 '; // 搜索条件
I('type_id') && $where = "$where and type_id = " . I('type_id');
// 关键词搜索
$model = M('GoodsAttribute');
$count = $model->where($where)->count();
$Page = new AjaxPage($count, 13);
$show = $Page->show();
$goodsAttributeList = $model->where($where)->order('`order` desc,attr_id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
$goodsTypeList = M("GoodsType")->getField('id,name');
$attr_input_type = array(0 => '手工录入', 1 => ' 从列表中选择', 2 => ' 多行文本框');
$this->assign('attr_input_type', $attr_input_type);
$this->assign('goodsTypeList', $goodsTypeList);
$this->assign('goodsAttributeList', $goodsAttributeList);
$this->assign('page', $show);// 赋值分页输出
return $this->fetch('', getAdmStoId());
}
/**
* 商品列表导入
*/
public function goodsImport(){
$data = I('data');
$page = I('page');
$sto_id = getAdmStoId();
$import_data = include APP_PATH . 'admin/conf/import_data.php';
$data = json_decode($data, true);
$arrs=0;
foreach ($data as $key => $value) {
$i=0;
$is_set_categ=0;
$is_set_brand=0;
$is_set_nation=0;
$arr = null;
//判断当前时间是不是为null 是的话填写当前时间
if(empty( $value['上架时间'])){
$value['上架时间']= strtotime(date('Y-m-d h:i:s', time()));
}else{
$value['上架时间']= strtotime( $value['上架时间']);
}
$value['下架时间']= strtotime( $value['下架时间']);
foreach ($value as $kl => $vl) {
$is_set= isset($import_data[$kl]);
if(!$is_set&&$i==0) {
$err_arr[] = $kl."查无此列,请核对后重新导入";
$i=1;
break 2;
}
$table_key = $import_data[$kl];
switch ($kl) {
case "品类":
$categ_arr = explode("/",$value['品类']);
foreach ( $categ_arr as $klss => $vlss ){
$rs = M('goods_category')->where('name', $vlss)->where('store_id', $sto_id)->find();
if( $is_set_categ==0){
if (!$rs||rs==null) {
$cat_arr[] = $arr['goods_sn']."商品编号" . "查无品类";
$is_set_categ=1;
break ;
}
}
}
$categ_arr = explode("/", $vl);
$len = count($categ_arr) - 1;
$name = $categ_arr[$len];
$rs = M('goods_category')->where('name', $name)->where('store_id', $sto_id)->find();
$arr[$table_key] = $rs["id"];
break;
case '品牌':
if($vl) {
$rs = M('brand')->where('name', $vl)->where('store_id', $sto_id)->find();
$arr[$table_key] = $rs["id"];
if($rs==null){
$is_set_brand=1;
}
}
break;
case '国别':
if($vl) {
$rs = M('nation')->where('name', $vl)->where('store_id', $sto_id)->find();
$arr[$table_key] = $rs["id"];
if($rs==null) {
$is_set_nation = 1;
}
}
break;
default:
$arr[$table_key] = $vl;
break;
}
}
if(empty($arr['goods_sn'])||$arr['goods_sn']=="\n") {
if(empty($arr['goods_name'])){
$err_arr[] = $arr['sku']."商品条码"."未填写商品编号";
continue;
}else{
$err_arr[] = $arr['goods_name']."商品"."未填写商品编号";
}
continue;
}
if(empty($arr['sku'])||$arr['sku']=="\n") {
$nation_arr[] = $arr['goods_sn']."商品编号"."未填写条码";
continue;
}
if(empty($arr['shop_price'])||$arr['shop_price']=="\n") {
$err_arr[] = $arr['goods_sn']."商品编号"."未填写手店价";
continue;
}
if(empty($arr['cat_id'])&& $is_set_categ==0) {
$err_arr[] = $arr['goods_sn']."商品编号"."未填写品类";
continue;
}
if(empty($arr['nation_id']) && $is_set_nation) {
$nation_arr[] = $arr['goods_sn']."商品编号"."查无国别";
continue;
}
if(empty($arr['brand_id']) && $is_set_brand) {
$brand_arr[] =$arr['goods_sn']."商品编号"."查无品牌";
continue;
}
if( empty($arr['give_integral'])){
$arr['give_integral']=0;
} else if($arr['give_integral']!="1" && $arr['give_integral']!="0"){
$err_arr[] = $arr['goods_sn']."该商品"."积分填写错误";
continue;
}
if( empty($arr['distr_type'])){
$arr['distr_type']=0;
} else if($arr['distr_type']!="0"&&$arr['distr_type']!="1"&&$arr['distr_type']!="2"){
$err_arr[] = $arr['goods_sn']."该商品"."配送方式填写错误";
continue;
}
if( empty($arr['dis_type'])){
$arr['dis_type']=0;
}else if($arr['dis_type']!="0"&&$arr['dis_type']!="1"){
$err_arr[] = $arr['goods_sn']."该商品"."分销类型填写错误";
continue;
}
if( empty($arr['exp_sum_type'])){
$arr['exp_sum_type']=3;
}if($arr['exp_sum_type']!="1"&&$arr['exp_sum_type']!="2"&&$arr['exp_sum_type']!="3"){
$err_arr[] = $arr['goods_sn']."该商品"."运费设置填写错误";
continue;
}
//调接口,有没有该商品
$where['WareNo'] = array('=', $arr['goods_sn']);
$getapi_token = tpCache('shop_info.api_token', getAdmStoId());
/* $rs = getApiData('wxd.mshop.wares.list.get', $getapi_token, null, $where, 1, 10, null);*/
$sn=$arr['goods_sn'];
$accdb=tpCache("shop_info.ERPId",getAdmStoId());
$wdata['State']=1;
$wdata['KeyWord']=urlencode($sn);
$wdata['page']=1;
$wdata['pageSize']=10;
$rs = getApiData_java_p('/api/erp/msg/mshop/wares/page', $accdb, $wdata);
$good_sn = M('goods')->where('goods_sn', $sn)->where('store_id',$sto_id)->find();
//把字符串转换成数组
$datas = json_decode($rs, true);
$goods_data=$datas['data']['pageData'];
if($good_sn) {
// if ($datas['data']) {
$err_arr[] = $arr['goods_sn'] . "该商品已存在";
continue;
// }
}
if ($goods_data!=null&&count($goods_data)!=0) {
//市场价
foreach ($goods_data as $k => $v) {
//市场价
if (empty($arr['market_price'])) {
$arr['market_price'] = $v['PosPrice'];
}
//商品名称
if (empty($arr['goods_name'])) {
$arr['goods_name'] = $v['WareName'];
}
//商品条码
if (empty($arr['sku'])) {
$arr['sku'] = $v['BarCode'];
}
}
} else{
//判断在线下有没有存在
$err_arr[] = $arr['goods_sn']."商品编号" . "线下查无此商品编号";
continue;
}
$arr['erpwareid'] = $datas['data'][0]['Id'];
if(empty($arr['cat_id'])){
$arr['cat_id']=0;
}
if(empty($arr['brand_id'])){
$arr['brand_id']=0;
}
if(empty($arr['nation_id'])){
$arr['nation_id']=0;
}
//插入数据
$arr['store_id'] = getAdmStoId();
//判断积分给是不是null,是给默认值
if(empty($arr['give_integral'])) $arr['give_integral']=0;
//判断库存数是不是null,获取数据库中的默认库存数量
if(empty($arr['store_count']))
$arr['store_count']=tpCache('basic.default_storage', getAdmStoId());
$arr['is_mainshow']=1;
$rs_save_goods=null;
if( $is_set_categ==0) {
$rs_save_goods = M('goods')->save($arr);
$arrs++;
}
if (!$rs_save_goods) {
/*插入失败的日志*/
$fail_err=M('goods')->fetchSql(ture)->save($arr);
mlog($fail_err,"goodsImport/". $arr['store_id']);
$err_arr[] = $arr['goods_sn']."商品编号". "此商品插入失败";
continue;
// Cache::rm("name");
}
/* 设置成功插入的日志*/
$res = getApiData("wxd.mshop.wares.add", $getapi_token, array(['WareId' => $arr['erpwareid']]));
mlog($res, "good/" .getAdmStoId());
}
return json(['code' => 0, 'msg' => '成功', 'err_data' => $err_arr,'nation_data' => $nation_arr,'cat_data' => $cat_arr,'brand_data' => $brand_arr,"arrs"=>$arrs]);
}
/**
* 添加修改编辑 商品属性
*/
public function addEditGoodsAttribute()
{
$model = D("GoodsAttribute");
$type = I('attr_id') > 0 ? 2 : 1; // 标识自动验证时的 场景 1 表示插入 2 表示更新
$attr_values = str_replace('_', '', I('attr_values')); // 替换特殊字符
$attr_values = str_replace('@', '', $attr_values); // 替换特殊字符
$attr_values = trim($attr_values);
$post_data = input('post.');
$post_data['attr_values'] = $attr_values;
if ((I('is_ajax') == 1) && IS_POST)//ajax提交验证
{
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
// 数据验证
$validate = \think\Loader::validate('GoodsAttribute');
if (!$validate->batch()->check($post_data)) {
$error = $validate->getError();
$error_msg = array_values($error);
$return_arr = array(
'status' => -1,
'msg' => $error_msg[0],
'data' => $error,
);
$this->ajaxReturn($return_arr);
} else {
$model->data($post_data, true); // 收集数据
if ($type == 2) {
$model->isUpdate(true)->save(); // 写入数据到数据库
} else {
$model->save(); // 写入数据到数据库
$insert_id = $model->getLastInsID();
}
$return_arr = array(
'status' => 1,
'msg' => '操作成功',
'data' => array('url' => U('Admin/Goods/goodsAttributeList')),
);
$this->ajaxReturn($return_arr);
}
}
// 点击过来编辑时
$attr_id = I('attr_id/d', 0);
$goodsTypeList = M("GoodsType")->select();
$goodsAttribute = $model->find($attr_id);
$this->assign('goodsTypeList', $goodsTypeList);
$this->assign('goodsAttribute', $goodsAttribute);
return $this->fetch('_goodsAttribute', getAdmStoId());
}
/**
* 更改指定表的指定字段
*/
public function updateField()
{
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
$primary = array(
'goods' => 'goods_id',
'goods_category' => 'id',
'brand' => 'id',
'goods_attribute' => 'attr_id',
'ad' => 'ad_id',
);
$model = D($_POST['table']);
$model->$primary[$_POST['table']] = $_POST['id'];
$model->$_POST['field'] = $_POST['value'];
$model->save();
$return_arr = array(
'status' => 1,
'msg' => '操作成功',
'data' => array('url' => U('Admin/Goods/goodsAttributeList')),
);
$this->ajaxReturn($return_arr);
}
/**
* 动态获取商品属性输入框 根据不同的数据返回不同的输入框类型
*/
public function ajaxGetAttrInput()
{
$GoodsLogic = new GoodsLogic();
$str = $GoodsLogic->getAttrInput($_REQUEST['goods_id'], $_REQUEST['type_id']);
exit($str);
}
/**
* 删除商品
*/
public function delGoods()
{
$goods_id = I('id/d');
$error = '';
// 商品团购
// $c1 = M('group_buy')->where("goods_id = $goods_id")->count('1');
// $c1 && $error .= '此商品有团购,不得删除!
';
//
// // 商品退货记录
// $c1 = M('return_goods')->where("goods_id = $goods_id")->count('1');
// $c1 && $error .= '此商品有退货记录,不得删除!
';
//编辑器图片删除
$gos = M("goods")->where('goods_id =' . $goods_id)->find();
if ($gos) {
if ($gos['prom_type'] != 0) {
$error = '此商品有活动,不得删除!
';
} else { // 判断此商品是否有订单
$c1 = M('OrderGoods')->where("goods_id = $goods_id")->count('1');
$c1 && $error .= '此商品有订单,不得删除!
';
}
}
if ($error) {
$return_arr = array('status' => -1, 'msg' => $error, 'data' => '',); //$return_arr = array('status' => -1,'msg' => '删除失败','data' =>'',);
$this->ajaxReturn($return_arr);
}
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
//如果美妆图库有记录就不删除图片
// $num = M('barcodegoods')->where(['store_id'=>getAdmStoId(),'sku'=>$gos['sku']])->count(1);//查找同条码商品个数
// if (empty($num)){
// vendor('qcloudcos.myqcloudcos');
// if ($gos['editorimg']) {
// $editorimg = explode(',', $gos['editorimg']);
// foreach ($editorimg as $kl => $vl) {
// if (!empty($vl)) {
// $rs = Myqcloudcos::delFile('wxd', $vl);
// mdelFile(ROOT_PATH . $vl);
//
// }
// }
// }
// //商品主图删除
// if ($gos['original_img']) {
// $orimg = $gos['original_img'];
// if (!empty($orimg)) {
// $rs = Myqcloudcos::delFile('wxd', $orimg);
// mdelFile(ROOT_PATH . $orimg);
//
// $slocalpath = ROOT_PATH . '/' . UPLOAD_PATH . dirname($orimg) . '/s_' . basename($orimg);
// $sypath = '/' . UPLOAD_PATH . dirname($orimg) . '/s_' . basename($orimg);
// $rs = Myqcloudcos::delFile('wxd', $sypath);
// mdelFile(ROOT_PATH . $slocalpath);
// }
// }
// //商品图片列表
// $goimglist = M("goods_images")->where('goods_id =' . $goods_id)->select();
// if ($goimglist) {
// foreach ($goimglist as $kimg => $vimg) {
// if (!empty($vimg['image_url'])) {
// $rs = Myqcloudcos::delFile('wxd', $vimg['image_url']);
// mdelFile(ROOT_PATH . $vimg['image_url']);
// }
// }
// }
// }
//
$getapi_token=tpCache('shop_info.api_token',getAdmStoId());
//下架商品
$res = getApiData("wxd.mshop.wares.del", $getapi_token,null, array(['WareId'=>$gos['erpwareid']]));
// 删除此商品
M("Goods")->where('store_id',getAdmStoId())->where('goods_id =' . $goods_id)->delete(); //商品表
M("cart")->where('store_id',getAdmStoId())->where('goods_id =' . $goods_id)->delete(); // 购物车
M("comment")->where('store_id',getAdmStoId())->where('goods_id =' . $goods_id)->delete(); //商品评论
M("goods_images")->where('store_id',getAdmStoId())->where('goods_id =' . $goods_id)->delete(); //商品相册
M("goods_collect")->where('store_id',getAdmStoId())->where('goods_id =' . $goods_id)->delete(); //商品收藏
$return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => '',); //$return_arr = array('status' => -1,'msg' => '删除失败','data' =>'',);
$this->ajaxReturn($return_arr);
}
/**
* 异常商品修复
*/
public function updateerpgoods()
{
$key = I('key');
$gos = M('goods')->where('goods_sn',$key)->where('store_id', getAdmStoId())->field('erpwareid')->find();
if ($gos) {
$return_arr = array('status' => -1, 'msg' => '商品编号已存在商品列表', 'data' => '',);
}
else {
$getapi_token = tpCache('shop_info.api_token', getAdmStoId());
$where['WareNo'] = $key;
$res = getApiData('wxd.base.wares.list.get', $getapi_token, null, $where, 1, 10, null);
if ($res) {
$res = json_decode($res, true);
if ($res['code'] == 1 && $res['data']) {
if ($res['count']==1) {
//下架商品
$res = getApiData("wxd.mshop.wares.del", $getapi_token, null, array(['WareId' => $res['data'][0]['Id']]));
$return_arr = array('status' => 1, 'msg' => '修复成功', 'data' => '',);
}else
{
$return_arr = array('status' => -1, 'msg' => '修复失败,您所输入的关键字查询出来数量大于1', 'data' => '',);
}
}
else
{
$return_arr = array('status' => -1, 'msg' => '修复失败', 'data' => '',);
}
}
else
{
$return_arr = array('status' => -1, 'msg' => '修复失败,接口获取失败', 'data' => '',);
}
}
$this->ajaxReturn($return_arr);
}
/**
* 更新商品
*/
public function updatesku()
{
$geterpwareid = I('erpwareid');
$getapi_token=tpCache('shop_info.api_token',getAdmStoId());
$where['Id'] = $geterpwareid;
$res = getApiData('wxd.base.wares.list.get',$getapi_token, null, $where, 1, 10, null);
if ($res)
{
$res=json_decode($res,true);
if ($res['code']==1 && $res['data']) {
$postdata['goods_sn'] = $res['data'][0]['WareNo'];
$postdata['sku'] = $res['data'][0]['BarCode'];
$postdata['market_price'] = $res['data'][0]['PosPrice'];
$row = M('goods')->where(array('store_id' => getAdmStoId(), 'erpwareid' => $geterpwareid))->save($postdata);
$return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => '',);
}
else
{
$return_arr = array('status' => -1,'msg' => '获取商品资料失败','data' =>'',);
}
}
else{
$return_arr = array('status' => -1,'msg' => '获取商品资料失败','data' =>'',);
}
$this->ajaxReturn($return_arr);
}
/**
* 删除商品类型
*/
public function delGoodsType()
{
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
// 判断 商品规格
$id = $this->request->param('id');
$count = M("Spec")->where("type_id = {$id}")->count("1");
$count > 0 && $this->error('该类型下有商品规格不得删除!', U('Admin/Goods/goodsTypeList'));
// 判断 商品属性
$count = M("GoodsAttribute")->where("type_id = {$id}")->count("1");
$count > 0 && $this->error('该类型下有商品属性不得删除!', U('Admin/Goods/goodsTypeList'));
// 删除分类
M('GoodsType')->where("id = {$id}")->delete();
$this->success("操作成功!!!", U('Admin/Goods/goodsTypeList'));
}
/**
* 删除商品属性
*/
public function delGoodsAttribute()
{
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
$id = input('id');
// 判断 有无商品使用该属性
$count = M("GoodsAttr")->where("attr_id = {$id}")->count("1");
$count > 0 && $this->error('有商品使用该属性,不得删除!', U('Admin/Goods/goodsAttributeList'));
// 删除 属性
M('GoodsAttribute')->where("attr_id = {$id}")->delete();
$this->success("操作成功!!!", U('Admin/Goods/goodsAttributeList'));
}
/**
* 删除商品规格
*/
public function delGoodsSpec()
{
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
$id = input('id');
// 判断 商品规格项
$count = M("SpecItem")->where("spec_id = {$id}")->count("1");
$count > 0 && $this->error('清空规格项后才可以删除!', U('Admin/Goods/specList'));
// 删除分类
M('Spec')->where("id = {$id}")->delete();
$this->success("操作成功!!!", U('Admin/Goods/specList'));
}
/**
* 品牌列表
*/
public function brandList()
{
$pagenum = 10;//每页显示多少条
if ((int)I('pagenum/s') > 0) {
$pagenum = I('pagenum/s');
}
$model = M("Brand");
$where = " 1=1 ";
$keyword = urldecode(urldecode(I('keyword')));
$getAdmStoId = getAdmStoId();
$where .= $keyword ? " and name like '%$keyword%' " : "";
$where .= " and store_id='" . $getAdmStoId . "'";
$count = $model->where($where)->count();
$Page = $pager = new Page($count, $pagenum);
$brandList = $model->where($where)->order("`sort` asc")->limit($Page->firstRow . ',' . $Page->listRows)->select();
$show = $Page->show();
$cat_list = M('goods_category')->where("parent_id = 0")->getField('id,name'); // 已经改成联动菜单
$impotr=M('import_active')->where(['store_id'=>$getAdmStoId,'state'=>1,'type'=>102])->find();
$this->assign('import',$impotr);
$this->assign('cat_list', $cat_list);
$this->assign('pager', $pager);
$this->assign('show', $show);
$this->assign('brandList', $brandList);
$this->assign('keyword',$keyword);
$this->assign('stoid', $getAdmStoId);
$this->assign('pagenum', $pagenum);
$this->assign('oldurl', urlencode(curPageURL()));
// upload_ylp_log('品牌列表');
return $this->fetch('brandList', getAdmStoId());
}
/**
* 添加修改编辑 商品品牌
*/
public function addEditBrand()
{
$id = I('id');
$this->assign('erpid', getERPId());
$this->assign('curid', '0');
$oldurl = I('oldurl/s');
if ($oldurl) {
$oldurl = urldecode(urldecode($oldurl));
}
if (!empty($id)) {
$this->assign('curid', $id);
}
if (IS_POST) {
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
$data = input('post.');
$getAdmStoId = getAdmStoId();
$data['store_id'] = $getAdmStoId;
$data['zm'] = substr($data['zm'],0,1);
if ($data['imgtype'] == 0) {
$data['logo'] = I('logo');
} else {
$data['logo'] = I('httimg');
}
$rt=M("admin")->where('store_id',$getAdmStoId)->where('admin_id',getAdminId())->field('ERPUser')->find();
if ($id){
$data['edit_time'] = time();
$data['edit_ip'] = getIP();
$data['edit_man'] = $rt['ERPUser'];
upload_ylp_log('B03品牌详情编辑/确认提交');
M("Brand")->update($data);
}
else{
$data['add_time'] = time();
$data['edit_ip'] = getIP();
$data['edit_man'] = $rt['ERPUser'];
M("Brand")->insert($data);
}
if ($oldurl) {
$this->success("操作成功!!!", U('Admin/Goods/brandList', $oldurl));
} else {
$this->success("操作成功!!!", U('Admin/Goods/brandList', array('p' => input('p'))));
}
exit;
}
$cat_list = M('goods_category')->where(" store_id=" . getAdmStoId() . " and parent_id = 0")->select(); // 已经改成联动菜单
$this->assign('cat_list', $cat_list);
$brand = M("Brand")->find($id);
if (empty($brand)) {
$m = Db::query("select max(sort)so from wxd_brand where store_id=" . getAdmStoId());
$md = (int)$m[0]["so"];
$md++;
$brand["sort"] = $md;
$brand["is_hot"]=1;
}
$this->assign('brand', $brand);
return $this->fetch('_brand', getAdmStoId());
}
/**
* 删除品牌
*/
public function delBrand()
{
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
// 判断此品牌是否有商品在使用
$goods_count = M('Goods')
->where("brand_id = {$_GET['id']}")->where('store_id',getAdmStoId())->count('1');
if ($goods_count) {
$return_arr = array('status' => -1, 'msg' => '此品牌有商品在用不得删除!', 'data' => '',); //$return_arr = array('status' => -1,'msg' => '删除失败','data' =>'',);
$this->ajaxReturn($return_arr);
}
$model = M("Brand");
$brandlist = $model->where('store_id',getAdmStoId())->where('id=' . $_GET['id'])->find();
$dellogo = ltrim($brandlist['logo'], '/');
vendor('qcloudcos.myqcloudcos');
if (!empty($dellogo)) {
$rs = Myqcloudcos::delFile('wxd', $dellogo);
mdelFile(ROOT_PATH . $dellogo);
}
//delFile($dellogo);
$model->where('store_id',getAdmStoId())->where('id =' . $_GET['id'])->delete();
$return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => '',); //$return_arr = array('status' => -1,'msg' => '删除失败','data' =>'',);
$this->ajaxReturn($return_arr);
}
/**
* 国别列表
*/
public function nationList()
{
$pagenum = 10;//每页显示多少条
if ((int)I('pagenum/s') > 0) {
$pagenum = I('pagenum/s');
}
$model = M("nation");
$where = " 1=1 ";
$keyword = urldecode(urldecode(I('keyword')));
$getAdmStoId = getAdmStoId();
$where .= $keyword ? " and name like '%$keyword%' " : "";
$where .= " and store_id=" . $getAdmStoId . "";
$count = $model->where($where)->count();
$Page = $pager = new Page($count, $pagenum);
$nationList = $model->where($where)->order("`sort` asc")->limit($Page->firstRow . ',' . $Page->listRows)->select();
$show = $Page->show();
$this->assign('pager', $pager);
$this->assign('show', $show);
$this->assign('nationList', $nationList);
$this->assign('keyword',$keyword);
$this->assign('pagenum', $pagenum);
$this->assign('oldurl', urlencode(curPageURL()));
// upload_ylp_log('国别列表');
return $this->fetch('nationList', getAdmStoId());
}
/**
* 添加修改编辑 商品国别
*/
public function addEditNation()
{
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
$oldurl = I('oldurl/s');
if ($oldurl) {
$oldurl = urldecode(urldecode($oldurl));
}
$id = I('id');
$this->assign('erpid', getERPId());
$this->assign('curid', '0');
if (!empty($id)) {
$this->assign('curid', $id);
}
$getAdmStoId = getAdmStoId();
$brand = M("nation")->where('store_id',$getAdmStoId)->find($id);
if (IS_POST) {
$data = input('post.');
$data['store_id'] = $getAdmStoId;
if ($data['imgtype'] == 0) {
$data['logo'] = I('logo');
} else {
$data['logo'] = I('httimg');
}
$rt=M("admin")->where(array('store_id'=>$getAdmStoId,'admin_id'=>getAdminId()))->field('ERPUser')->find();
if ($id){
$data['edit_time'] = time();
$data['edit_ip'] = getIP();
$data['edit_man'] = $rt['ERPUser'];
upload_ylp_log('B02国家分类编辑/确认提交');
M("nation")->update($data);
}
else{
$data['add_time'] = time();
$data['edit_ip'] = getIP();
$data['edit_man'] = $rt['ERPUser'];
M("nation")->insert($data);
}
if ($oldurl) {
$this->success("操作成功!!!", $oldurl);
} else {
$this->success("操作成功!!!", U('Admin/Goods/nationlist', array('p' => input('p'))));
}
exit;
}
if (empty($brand)) {
$m = Db::query("select max(sort)odr from wxd_nation where store_id=" . getAdmStoId());
$md = (int)$m[0]["odr"];
$md++;
$brand["sort"] = $md;
$brand["is_hot"]=1;
}
$this->assign('nation', $brand);
return $this->fetch('_nation', getAdmStoId());
}
/**
* 删除国别
*/
public function delNation()
{
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
// 判断此品牌是否有商品在使用
$goods_count = M('Goods')->where('store_id',getAdmStoId())->where("nation_id = {$_GET['id']}")->count('1');
if ($goods_count) {
$return_arr = array('status' => -1, 'msg' => '此国别有商品在用不得删除!', 'data' => '',); //$return_arr = array('status' => -1,'msg' => '删除失败','data' =>'',);
$this->ajaxReturn($return_arr);
}
$model = M("Nation");
$nationlist = $model->where('store_id',getAdmStoId())->where('id =' . $_GET['id'])->find();
$dellogo = ltrim($nationlist['logo'], '/');
if ($dellogo) {
vendor('qcloudcos.myqcloudcos');
$rs = Myqcloudcos::delFile('wxd', $dellogo);
mdelFile(ROOT_PATH . $dellogo);
}
//delFile($dellogo); // 删除缩略图
$model->where('store_id',getAdmStoId())->where('id =' . $_GET['id'])->delete();
$return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => '',); //$return_arr = array('status' => -1,'msg' => '删除失败','data' =>'',);
$this->ajaxReturn($return_arr);
}
/**
* 初始化编辑器链接
* 本编辑器参考 地址 http://fex.baidu.com/ueditor/
*/
private function initEditor()
{
$this->assign("URL_upload", U('admin/Ueditor/imageUp', array('savepath' => 'goods', 'savepath1' => getERPId()))); // 图片上传目录
$this->assign("URL_imageUp", U('admin/Ueditor/imageUp', array('savepath' => 'goods', 'savepath1' => getERPId()))); // 不知道啥图片
$this->assign("URL_fileUp", U('admin/Ueditor/fileUp', array('savepath' => 'goods', 'savepath1' => getERPId()))); // 文件上传s
$this->assign("URL_scrawlUp", U('admin/Ueditor/scrawlUp', array('savepath' => 'goods', 'savepath1' => getERPId()))); // 图片流
$this->assign("URL_getRemoteImage", U('admin/Ueditor/getRemoteImage', array('savepath' => 'goods', 'savepath1' => getERPId()))); // 远程图片管理
$this->assign("URL_imageManager", U('admin/Ueditor/imageManager', array('savepath' => 'goods', 'savepath1' => getERPId()))); // 图片管理
$this->assign("URL_getMovie", U('admin/Ueditor/getMovie', array('savepath' => 'goods', 'savepath1' => getERPId()))); // 视频上传
$this->assign("URL_Home", "");
}
/**
* 商品规格列表
*/
public function specList()
{
$goodsTypeList = M("GoodsType")->select();
$this->assign('goodsTypeList', $goodsTypeList);
return $this->fetch('', getAdmStoId());
}
/**
* 商品规格列表
*/
public function ajaxSpecList()
{
//ob_start('ob_gzhandler'); // 页面压缩输出
$where = ' 1 = 1 '; // 搜索条件
I('type_id') && $where = "$where and type_id = " . I('type_id');
// 关键词搜索
$model = D('spec');
$count = $model->where($where)->count();
$Page = new AjaxPage($count, 13);
$show = $Page->show();
$specList = $model->where($where)->order('`type_id` desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
$GoodsLogic = new GoodsLogic();
foreach ($specList as $k => $v) { // 获取规格项
$arr = $GoodsLogic->getSpecItem($v['id']);
$specList[$k]['spec_item'] = implode(' , ', $arr);
}
$this->assign('specList', $specList);
$this->assign('page', $show);// 赋值分页输出
$goodsTypeList = M("GoodsType")->select(); // 规格分类
$goodsTypeList = convert_arr_key($goodsTypeList, 'id');
$this->assign('goodsTypeList', $goodsTypeList);
return $this->fetch('', getAdmStoId());
}
/**
* 添加image修改编辑 商品规格
*/
public function addEditSpec()
{
$model = D("spec");
$type = I('id') > 0 ? 2 : 1; // 标识自动验证时的 场景 1 表示插入 2 表示更新
if ((I('is_ajax') == 1) && IS_POST)//ajax提交验证
{
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
// 数据验证
$validate = \think\Loader::validate('Spec');
$post_data = input('post.');
if ($type == 2) {
//更新数据
$check = $validate->scene('edit')->batch()->check($post_data);
} else {
//插入数据
$check = $validate->batch()->check($post_data);
}
if (!$check) {
$error = $validate->getError();
$error_msg = array_values($error);
$return_arr = array(
'status' => -1,
'msg' => $error_msg[0],
'data' => $error,
);
$this->ajaxReturn($return_arr);
}
$model->data($post_data, true); // 收集数据
if ($type == 2) {
$model->isUpdate(true)->save(); // 写入数据到数据库
$model->afterSave(I('id'));
} else {
$model->save(); // 写入数据到数据库
$insert_id = $model->getLastInsID();
$model->afterSave($insert_id);
}
$return_arr = array(
'status' => 1,
'msg' => '操作成功',
'data' => array('url' => U('Admin/Goods/specList')),
);
$this->ajaxReturn($return_arr);
}
// 点击过来编辑时
$id = I('id/d', 0);
$spec = $model->find($id);
$GoodsLogic = new GoodsLogic();
$items = $GoodsLogic->getSpecItem($id);
$spec[items] = implode(PHP_EOL, $items);
$this->assign('spec', $spec);
$goodsTypeList = M("GoodsType")->select();
$this->assign('goodsTypeList', $goodsTypeList);
return $this->fetch('_spec', getAdmStoId());
}
/**
* 动态获取商品规格选择框 根据不同的数据返回不同的选择框
*/
public function ajaxGetSpecSelect()
{
$goods_id = I('get.goods_id/d') ? I('get.goods_id/d') : 0;
$GoodsLogic = new GoodsLogic();
//$_GET['spec_type'] = 13;
$specList = M('Spec')->where("type_id = " . I('get.spec_type/d'))->order('`order` desc')->select();
foreach ($specList as $k => $v)
$specList[$k]['spec_item'] = M('SpecItem')->where("spec_id = " . $v['id'])->order('id')->getField('id,item'); // 获取规格项
$items_id = M('SpecGoodsPrice')->where('goods_id = ' . $goods_id)->getField("GROUP_CONCAT(`key` SEPARATOR '_') AS items_id");
$items_ids = explode('_', $items_id);
// 获取商品规格图片
if ($goods_id) {
$specImageList = M('SpecImage')->where("goods_id = $goods_id")->getField('spec_image_id,src');
}
$this->assign('specImageList', $specImageList);
$this->assign('items_ids', $items_ids);
$this->assign('specList', $specList);
return $this->fetch('ajax_spec_select', getAdmStoId());
}
/**
* 动态获取商品规格输入框 根据不同的数据返回不同的输入框
*/
public function ajaxGetSpecInput()
{
$GoodsLogic = new GoodsLogic();
$goods_id = I('goods_id/d') ? I('goods_id/d') : 0;
$str = $GoodsLogic->getSpecInput($goods_id, I('post.spec_arr/a', [[]]));
exit($str);
}
/**
* 删除商品相册图
*/
public function del_goods_images()
{
ClearALLCache();
delFile(TEMP_PATH . "/" . getAdmStoId());
$path = I('filename', '');
$sku = I('sku');
if ($sku) {
$rs = M('goods')->where(array('store_id'=>getAdmStoId(),'sku'=>$sku))->select();
if ($rs) {
foreach ($rs as $k=>$v) {
if ($v['ismain'] == 1) {
M('goods')->where('goods_id', $v['goods_id'])->save(['original_img' => ""]);
}
M('goods_images')->where("image_url = '$path'")->where('goods_id', $v['goods_id'])->delete();
}
}
}
}
/**
* 更新商品相册图
*/
public function updateimgmain()
{
$path = I('filename', '');
$img_id = I('img_id');
$sku = I('sku');
//判断是否有小图
$smallfilename=basename($path);
$spath=str_replace($smallfilename,'',$path)."s_".$smallfilename;
vendor('qcloudcos.myqcloudcos');
$resfolder=Myqcloudcos::stat('wxd',$spath);
if ($resfolder && $resfolder['code']!=0)//不存在创建
{
$resfolder_new=Myqcloudcos::copyFile('wxd',$path,$spath);
}
//
$imgres=M('goods')->where('store_id',getAdmStoId())->where('sku', $sku)->order('goods_id desc')->find();
if ($imgres) {
M('goods_images')->where('store_id',getAdmStoId())->where('goods_id', $imgres['goods_id'])->save(['ismain' => 0]);
M('goods_images')->where('store_id',getAdmStoId())->where('img_id', $img_id)->save(['ismain' => 1]);
M('goods')->where('store_id',getAdmStoId())->where('sku', $sku)->save(['original_img' =>$path]);
exit(1);
}
else{
exit(0);
}
}
/*--访问接口选择商品--*/
public function SelectGood()
{
$p = I('p/d', 1);
$key = trim(urldecode(I('key/s')));
$accdb=tpCache("shop_info.ERPId",getAdmStoId());
$wdata['State']=1;
$wdata['KeyWord']=urlencode($key);
$wdata['page']=$p;
$wdata['pageSize']=10;
$rs = getApiData_java_p('/api/erp/msg/mshop/wares/page', $accdb, $wdata);
if (empty($rs))
{
return json(["code" => -1, "msg" => "获取接口数据错误"]);
}
$wdata=null;
if ($rs) {
$data = json_decode($rs, true);
if ($data['data']) {
$wdata['data']=$data['data']['pageData'];
if ($wdata['data']) {
foreach ($wdata['data'] as $k => $v) {
$wdata['data'][$k]['PurPrice'] = number_format(empty($v['PurPrice']) ? 0 : $v['PurPrice'], 2, ".", "");
$wdata['data'][$k]['PosPrice'] = number_format(empty($v['PosPrice']) ? 0 : $v['PosPrice'], 2, ".", "");
$wdata['data'][$k]['VipPrice'] = number_format(empty($v['VipPrice']) ? 0 : $v['VipPrice'], 2, ".", "");
$wdata['data'][$k]['TradePrice'] = number_format(empty($v['TradePrice']) ? 0 : $v['TradePrice'], 2, ".", "");
}
$wdata['code'] = 1;
$wdata['count'] = $data['data']['total'];
return json($wdata);
}
else
{
$check = M('goods')->where('goods_sn= "'.$key.'" or goods_name="'.$key.'" or sku="'.$key.'" ')->where('store_id',getAdmStoId())->find();
if ($check){
return json(['code' => -1 ,'msg'=>'该商品已添加']);
}else{
return json(["code" => -1, "msg" => "未找到您要搜索的商品"]);
}
}
} else {
$check = M('goods')->where('goods_sn= "'.$key.'" or goods_name="'.$key.'" or sku="'.$key.'" ')->where('store_id',getAdmStoId())->find();
if ($check){
return json(['code' => -1 ,'msg'=>'该商品已添加']);
}else{
return json(["code" => -1, "msg" => "未找到您要搜索的商品"]);
}
}
}
}
public function select_all()
{
$sku = I('sku/s');
$where['BarCode'] = array('=', $sku);
$where['State'] = array('=', 1);
$tk = M("store")->where("store_id", getAdmStoId())->field("api_token")->find();
$rs = getApiData('wxd.mshop.wares.list.get', $tk['api_token'],
null, $where, 1, 100, null);
if ($rs) {
$data = json_decode($rs, true);
if (!empty($data['data'])) {
foreach ($data['data'] as $k => $v) {
$data['data'][$k]['PurPrice'] = number_format(empty($v['PurPrice']) ? 0 : $v['PurPrice'], 2, ".", "");
$data['data'][$k]['PosPrice'] = number_format(empty($v['PosPrice']) ? 0 : $v['PosPrice'], 2, ".", "");
$data['data'][$k]['VipPrice'] = number_format(empty($v['VipPrice']) ? 0 : $v['VipPrice'], 2, ".", "");
$data['data'][$k]['TradePrice'] = number_format(empty($v['TradePrice']) ? 0 : $v['TradePrice'], 2, ".", "");
}
return json($data);
} else {
return json(["code" => -1, "msg" => "未找到您要搜索的商品"]);
}
}else {
return json(["code" => -1, "msg" => "获取接口数据错误"]);
}
}
/*--渲染商品--*/
function getGoods()
{
return $this->fetch('search', getAdmStoId());
}
/**
* 判断选择的商品条形码是否重复
*/
public function checkisok()
{
$sku = I('sku');
$rk = M('goods')->where(['sku' => $sku, 'store_id' => getAdmStoId()])->field('goods_id,sku')->find();
if (!empty($rk)) {
return json(['code' => 0, 'msg' => '该商品的条码【'.$rk['sku'].'】已添加,是否继续']);
} else {
return json(['code' => 1, 'msg' => '操作成功']);
}
}
function getfile()
{
$f = ROOT_PATH . "/application/json.txt";
$myfile = fopen($f, "r") or die("Unable to open file!");
$str = fread($myfile, filesize($f));
fclose($myfile);
return $str;
}
function getbrandzm()
{
$getname = I('brandname');
$getzm = getFirstCharter($getname);
if ($getzm) {
return $getzm;
} else {
return "";
}
}
/**************************************************商品分组 小张 17-05-22******************************************************************/
//商品分组
function goodsGroup()
{
$list = array();
$where['store_id'] = getAdmStoId();
$res = D('goods_group')->where($where)->order("gp_ordid")->select();
if ($res) {
foreach ($res as $val) {
$val['add_time'] = date('Y-m-d H:i:s', $val['add_time']);
$list[] = $val;
}
}
$this->assign('grouplist', $list);
$this->assign('store_id', getAdmStoId());
// upload_ylp_log('商品分组');
return $this->fetch('', getAdmStoId());
}
//删除商品分组
public function delGoodsGroup()
{
$id = $this->request->param('gpid');
DB::name('goods_group')->where(array('store_id'=>getAdmStoId(),'gpid'=>$id))->delete();
$this->success("操作成功!!!", U('Admin/Goods/goodsGroup'));
}
//编辑新增分组
public function goodsGroupInfo()
{
$gpid = I('gpid');
if ($gpid > 0) {
$group = M('goods_group')->where('store_id',getAdmStoId())->where(" gpid =" . $gpid . " ")->find();
$this->assign('group', $group);
if ($group['gp_goodslist'] != "") {
$where = 'goods_id in (' . $group['gp_goodslist'] . ')';
$group_goods = M('goods')->where('store_id',getAdmStoId())->where($where)->order('sort asc')->select();
$this->assign('group_goods', $group_goods);
}
} else {
$m = Db::query("select max(gp_ordid)odr from __PREFIX__goods_group where store_id=" . getAdmStoId());
$md = (int)$m[0]["odr"];
$md++;
$group["gp_ordid"] = $md;
$group["status"] = 1;
}
$this->assign('group', $group);
return $this->fetch('', getAdmStoId());
}
//选择商品
public function search_goods()
{
$GoodsLogic = new GoodsLogic;
$brandList = $GoodsLogic->getSortBrands();
$this->assign('brandList', $brandList);
$categoryList = $GoodsLogic->getSortCategory();
$this->assign('categoryList', $categoryList);
$pagenum = I('pagenum/d',10);//每页显示多少条
if ((int)I('pagenum/s') > 0) {
$pagenum = I('pagenum/s');
}
$this->assign('pagenum', $pagenum);
$goods_id = I('goods_id');
if ($goods_id)
{
$goods_id=$goods_id.",";
$this->assign('idlist', $goods_id);
}
$tpl = I('get.tpl', 'search_goods');
return $this->fetch($tpl, getAdmStoId());
}
public function ajax_search_goods()
{
$goods_id = I('sel_list');
$formid = I('formid');
$pagenum = I('pagenum/d',10);//每页显示多少条
if ((int)I('pagenum/s') > 0) {
$pagenum = I('pagenum/s');
}
if ($formid == "Group") {
$where = ' is_on_sale = 1 ';//搜索条件
} else {
$where = ' is_on_sale = 1 and store_count>0 ';//搜索条件
}
if ($goods_id) {
$goods_id=$goods_id."0";
$where .= " and goods_id not in ($goods_id) ";
}
I('intro') && $where = "$where and " . I('intro') . " = 1";
//分类
if (I('cat_id')) {
$this->assign('cat_id', I('cat_id'));
$grandson_ids = getCatGrandson(I('cat_id'));
$where = " $where and cat_id in(" . implode(',', $grandson_ids) . ") "; // 初始化搜索条件
}
//品牌
if (I('brand_id')) {
$this->assign('brand_id', I('brand_id'));
$where = "$where and brand_id = " . I('brand_id');
}
if (!empty($_REQUEST['keywords'])) {
$kw = I('keywords');
$kw = urldecode(urldecode($kw));
$this->assign('keywords', $kw);
$where = "$where and (goods_sn like '%" . $kw . "%' or goods_name like '%" . $kw . "%' or keywords like '%" . $kw . "%' or sku = '" . $kw . "')";
}
$t = time();
$getAdmStoId = getAdmStoId();
$where .= " and store_id=" . $getAdmStoId;
$where .= " and on_time<" . $t . " and (down_time>" . $t . " or down_time=0 or down_time='' or down_time is null)";
$count = M('goods')->where($where)->count();
$Page = new AjaxPage($count, $pagenum);
$goodsList = M('goods')->where($where)->order('goods_id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
$show = $Page->show();//分页显示输出
$this->assign('page', $show);//赋值分页输出
$this->assign('formid', $formid);
$this->assign('goodsList', $goodsList);
$this->assign('pager', $Page);//赋值分页输出
return $this->fetch("", getAdmStoId());
}
//保存分组
public function goodsgroup_save()
{
$data = I('post.');
$gpid = $data["gpid"];
if ($gpid) {
$r = D('goods_group')->where('store_id',getAdmStoId())->where('gpid', $data['gpid'])->save($data);
} else {
unset($data['gpid']);
$data['add_time'] = time();
$data["store_id"] = getAdmStoId();
$r = D('goods_group')->add($data);
}
upload_ylp_log('B05商品分组编辑/确认提交');
$this->success('编辑分组成功', U('Goods/goodsGroup'));
}
/**
* 判断是否参与活动
*/
public function isinactive()
{
$gid = I('gid');
$good = M('goods')->where('store_id',getAdmStoId())->where('goods_id', $gid)->find();
if ($good['prom_type'] > 0) {
$title = "";
switch ($good['prom_type']) {
case 1:
$title = "参与秒杀活动";
break;
case 2:
$title = "参与团购活动";
break;
case 3:
$title = "参与优惠促销活动";
break;
case 4:
$title = "参与积分购活动";
break;
}
return json(['code' => 1, 'msg' => $title]);
} else {
return json(['code' => -1, 'msg' => "未参与活动"]);
}
}
/*--判断库存是否OK--*/
public function isstorage()
{
$goods_id = I('gid');
$storecount1 = I('count/d', 0);
$ggg = M('goods')->where('store_id',getAdmStoId())->where('goods_id', $goods_id)->find();
$now0 = time();
switch ($ggg['prom_type']) {
case 2:
$where0 = [
'end_time' => ['>=', $now0],
'start_time' => ['<=', $now0],
'goods_id' => $goods_id,
'is_end' => 0
];
/*--->where('goods_num>buy_num')--*/
$rs0 = M('group_buy')->where($where0)->find();
if (!empty($rs0)) {
$onlybuy = $rs0["goods_num"] - $rs0["buy_num"];
if ($storecount1 < $onlybuy) {
$this->ajaxReturn(['code' => -1,
'msg' => '该商品有参加团购活动,库存数量不能小于团购允许购买的数量',
'count' => $ggg['store_count']]);
}
}
break;
case 1:
$where1 = [
'end_time' => ['>=', $now0],
'show_time' => ['<=', $now0],
'goods_id' => $goods_id,
'is_end' => 0
];
$rs1 = M('flash_sale')->where($where1)->find();
if (!empty($rs1)) {
$onlybuy = $rs1["goods_num"] - $rs1["buy_num"];
if ($storecount1 < $onlybuy) {
$this->ajaxReturn(['code' => -1,
'msg' => '该商品有参加秒杀活动,库存数量不能小于活动允许购买的数量', 'count' => $ggg['store_count']]);
}
}
break;
case 4:
$where2 = [
'end_time' => ['>=', $now0],
'start_time' => ['<=', $now0],
'goods_id' => $goods_id,
'is_end' => 0,
'is_show' => 1,
];
$rs2 = M('integral_buy')->where($where2)->find();
if (!empty($rs2)) {
$onlybuy = $rs2["limitqty"] - $rs2["buy_num"];
if ($storecount1 < $onlybuy) {
$this->ajaxReturn(['code' => -1,
'msg' => '该商品有参加积分购,库存数量不能小于活动允许购买的数量', 'count' => $ggg['store_count']]);
}
}
break;
}
/*ok*/
$this->ajaxReturn(['code' => 1]);
}
// 商品搬家
public function goods_move()
{
set_time_limit(0);//剪切图片避免30秒超时,0表示不限时
$name = I('name');//网址关键字(1688、taobao、tmall)
$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, I('url'));
curl_setopt($curl, CURLOPT_HEADER, 0); //0表示不输出Header,1表示输出
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_ENCODING, '');
curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$text = curl_exec($curl);
$text = mb_convert_encoding($text, 'utf-8','GB2312');
// echo curl_errno($curl); //返回0时表示程序执行成功 如何从curl_errno返回值获取错误信息
// $text = file_get_contents(I('url'));
// 获取网页主图
if ($name == '1688') {
// 阿里巴巴
preg_match('/]*class=\"nav nav-tabs fd-clr\"[^>]*>(.*?)<\/ul>/is', $text, $ul);
preg_match_all('/
][^r]*rc=\"([^"]*)\"[^>]*>/', $ul[1], $m_img);
foreach ($m_img[0] as $k => $v) {
if (stripos($v, 'data-lazy-src')) {
preg_match('/data-lazy-src=\"(.*?)\"/', $v, $src);
$m_img[1][$k] = $src[1];
}
}
} else {
// 天猫、淘宝
preg_match('/]*id=\"J_UlThumb\"[^>]*>(.*?)<\/ul>/is', $text, $ul);
preg_match_all('/
][^r]*rc=\"([^"]*)\"[^>]*>/', $ul[1], $m_img);
}
// 本地存储路径
$path = 'goods/' . cookie('newshop_admin_erpid') . '/' . date('Y') . '/' . date('m-d') . '/';
// 保存在腾讯云
vendor('qcloudcos.myqcloudcos');
$resfolder = Myqcloudcos::statFolder('wxd', UPLOAD_PATH . $path);
// 判断腾讯云是否存在路径
if ($resfolder && $resfolder['code'] != 0) {
Myqcloudcos::createFolder('wxd', UPLOAD_PATH . $path);//创建目录
}
// 判断本地是否存在路径
if (!file_exists(UPLOAD_PATH . $path)) {
mkdir(UPLOAD_PATH . $path,0777,true);//创建目录
}
foreach ($m_img[1] as $k => $v) {
// $src = 'http:' . strstr($v, '.jpg', true) . 'jpg';//主图淘宝天猫网上地址
preg_match('/(.*?)(_50x50.jpg|_60x60q90.jpg|60x60.jpg)/', $v, $src);
if ($name == '1688') {
$I_src = trim(str_replace('.60x60', '', $src[0]));//主图阿里巴巴网上地址
} else {
$I_src = 'https:' . $src[1];//主图淘宝天猫网上地址
}
$img_f = file_get_contents($I_src);
$ImgName = 'WXD_' . md5(microtime(true)) . '.jpg';
$saveFileName = $path . $ImgName;//保存路径加文件名
file_put_contents(UPLOAD_PATH . $saveFileName, $img_f);
// 剪切图片
m_cut_img($ImgName, ROOT_PATH . '/' . UPLOAD_PATH . $path);
// 图片水印处理
// $water = tpCache('water', getAdmStoId());
// $original_img = "./public/upload/" . $saveFileName;
// $image = \think\Image::open($original_img);
// if ($water['is_mark'] == 1) {
// if ($water['mark_type'] == 'img' && !empty($water['mark_img'])) {
// try {
// if (file_exists(ROOT_PATH . $water['mark_img'])) {
// $image->open($original_img)->water("." . $water['mark_img'], $water['mark_position'], $water['mark_degree'])->save($original_img);
// }
// } catch (Exception $e) {
// return NOIMG;
// }
// } else {
// //检查字体文件是否存在
// if (file_exists('./zhjt.ttf')) {
//
// $image->open($original_img)->text($water['mark_text'], './zhjt.ttf', 20, '#000000', $water['mark_position'])->save($original_img);
// }
// }
//
// }
//商品表的主表生成小图
//生成小图
m_cut_img($ImgName, ROOT_PATH . '/public/upload/' . $path, 400, 1);
$slocalpath = ROOT_PATH . '/' . UPLOAD_PATH . $path . 's_' . $ImgName;
$sypath = '/' . UPLOAD_PATH . $path . 's_' . $ImgName;
Myqcloudcos::upload('wxd', $slocalpath, $sypath);
//上传到腾讯云
$localpath = ROOT_PATH . '/' . UPLOAD_PATH . $saveFileName;
$ypath = '/' . UPLOAD_PATH . $saveFileName;
$res = Myqcloudcos::upload('wxd', $localpath, $ypath);
mlog(json_encode($res), "imageUp");
$img_src[] = '/public/upload/' . $saveFileName;//图片路径名
}
delFile(ROOT_PATH . '/' . UPLOAD_PATH . "/" . $path);//删除本地图片
switch ($name) {
case '1688':
preg_match('/]*id=\"desc-lazyload-container\"[^>]*data-tfs-url=\"(.*?)\"/', $text, $url0);
$url = $url0[1];
$text0 = file_get_contents($url);
preg_match_all('/
![]()
][^r].*?rc=\\\"([^"]*)\\\"[^>]*>/', $text0, $img0);
break;
case 'taobao':
preg_match('/