File: /web/data/www.tbbprovision.com/application/admin/controllers/ProductController.class.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/5/4
* Time: 9:14
*/
class ProductController extends Controller
{
public function __construct($controller='', $action='', $module='')
{
parent::__construct($controller, $action, $module);
$this->check_manage_login();
$this->check_permission();
$this->manage_init();
}
/**
* 分类列表页面加载
*/
public function category_action()
{
$this->render();
}
/**
* 分类删除
*/
public function cate_del_action()
{
try {
$cate_id = (int)f::post('cate_id');
//检测是否有子分类
$childs = $this->model('product')->get_cate_by_parent_id($cate_id);
if(count($childs)>0){
throw new Exception('清空子分类后才能删除');
}
$row = $this->model('product')->table('pro_cate')->where(array("cate_id='{$cate_id}'"))->fetch_one();
$this->model('product')->add_log('删除产品分类',$row);
$this->model('product')->table('pro_cate')->where(array("cate_id='{$cate_id}'"))->delete();
f::e_json('删除产品分类成功');
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 分类名称编辑页面请求
* @param string $parames
*/
public function cate_add_action()
{
try {
$this->render();
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 分类名称编辑数据提交
*/
public function cate_add_post_action()
{
try {
$data['cate_name'] = trim(f::post('cate_name'));
$parent_id = (int)f::post('parent_id');
$data['is_close'] = (int)f::post('is_close');
$data['brief'] = f::post('brief');
$data['seo_title'] = f::post('seo_title');
$data['seo_keyword'] = f::post('seo_keyword');
$data['seo_description'] = f::post('seo_description');
$data['list_picture'] = f::post('list_picture');
$data['my_order'] = (int)f::post('my_order');
$data['contents'] = f::post('contents');
$data['uid'] = '0,';
$data['dept'] = 1;
if(!$data['cate_name']){
throw new Exception('数据不完整。');
}
if($parent_id){
$parent_cate = $this->model('product')->get_cate_one("cate_id='{$parent_id}'");
$data['uid']=$parent_cate['uid'].$parent_id.',';
$data['dept']=(int)$parent_cate['dept']+1;
}
$this->model('product')->table('pro_cate')->add($data);
$cate_id = $this->model('product')->get_insert_id();
$this->model('product')->table('pro_cate')->where("cate_id='{$cate_id}'")->update(array(
'page_url' => '/'.f::str_to_url($data['cate_name']).'-'.$cate_id.'/'
));
$this->model('manage')->add_log('添加产品分类名称', $_POST);
f::e_json('添加产品分类名称成功');
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 分类名称编辑页面请求
* @param string $parames
*/
public function cate_edit_action($parames='')
{
try {
$parames = str::strtoparames($parames);
$cate_id = (int)$parames['cate_id'];
$cate_row = $this->model('product')->table('pro_cate')->where(array("cate_id='{$cate_id}'"))->fetch_one();
if(!$cate_row)
throw new Exception('分类名称不存在');
$cate_row['parent_id'] = category::get_parent_cate_id_by_uid($cate_row['uid']);
$this->assign('cate_row',$cate_row);
$this->render();
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 分类名称编辑数据提交
*/
public function cate_edit_post_action()
{
try {
$data['cate_name'] = trim(f::post('cate_name'));
$parent_id = (int)f::post('parent_id');
$data['is_close'] = (int)f::post('is_close');
$data['brief'] = f::post('brief');
$data['seo_title'] = f::post('seo_title');
$data['seo_keyword'] = f::post('seo_keyword');
$data['seo_description'] = f::post('seo_description');
$data['list_picture'] = f::post('list_picture');
$data['my_order'] = (int)f::post('my_order');
$cate_id = f::post('cate_id');
$data['uid'] = '0,';
$data['dept'] = 1;
$data['contents'] = f::post('contents');
$data['page_url'] = '/'.f::str_to_url($data['cate_name']).'-'.$cate_id.'/';
if(!$data['cate_name'] || !$cate_id){
throw new Exception('数据不完整。');
}
if($parent_id){
$parent_cate = $this->model('product')->get_cate_one("cate_id='{$parent_id}'");
$data['uid']=$parent_cate['uid'].$parent_id.',';
$data['dept']=(int)$parent_cate['dept']+1;
}
//更新子分类数据
$old_cate = $this->model('product')->get_cate_one("cate_id='{$cate_id}'");
$this->model('product')->update_child_cate($old_cate['uid'].$cate_id.',',$data['uid'].$cate_id.',',$data['dept']);
$this->model('product')->table('pro_cate')->where(array("cate_id='{$cate_id}'"))->update($data);
$this->model('manage')->add_log('编辑产品分类名称', $_POST);
f::e_json('修改分类名称成功');
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 产品属性列表页面加载
*/
public function attribute_list_action()
{
$this->render();
}
/**
* 列表页面数据加载
*/
public function attribute_list_json_action()
{
$parames = json_decode(htmlspecialchars_decode($GLOBALS['HTTP_RAW_POST_DATA']),true);
$where = array("1");
$order_data = array('`my_order` desc,`attr_id` asc');
$page_size = (int)$parames['limit'];
!$page_size && $page_size=50;
$start = isset($parames['offset']) ? (int)$parames['offset'] : 0;
$total = $this->model('product')->table('pro_attr')->where($where)->get_row_count();
$this->model('product')->order($order_data);
$list = $this->model('product')->limit($start,$page_size)->selectAll();
foreach((array)$list as $k=>$v){
$list[$k]['name'] = htmlspecialchars_decode($v['name']);
}
exit(str::json_data(array('total'=>$total,'rows'=>$list)));
}
/**
* 产品属性添加页面加载
*/
public function attribute_add_action()
{
$this->render();
}
/**
* 产品属性添加页面数据提交
*/
public function attribute_add_post_action()
{
try {
$data['name'] = htmlspecialchars(trim(f::post('name')));
$data['unit'] = trim(f::post('unit'));
$data['type'] = (int)trim(f::post('type'));
$data['my_order'] = (int)trim(f::post('my_order'));
if($this->model('product')->get_attr_one("name='{$data['name']}'")){
throw new Exception('属性已经存在,不能重复添加');
}
if(!$data['name']){
throw new Exception('数据不完整。');
}
if($data['type']==32){
$data['attr_value'] = f::post('attr_value');
}
$this->model('product')->table('pro_attr')->add($data);
$this->model('manage')->add_log('添加产品属性', $_POST);
f::e_json('添加产品属性成功');
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 产品属性编辑页面加载
*/
public function attribute_edit_action($parame)
{
try {
$parame = str::strtoparames($parame);
$attr_id = (int)$parame['attr_id'];
$attr_one = $this->model('product')->get_attr_one("attr_id='{$parame['attr_id']}'");
if(!$attr_one){
throw new Exception('参数错误。');
}
$this->assign('row',$attr_one);
$this->render();
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 产品属性修改页面数据提交
*/
public function attribute_edit_post_action()
{
try {
$attr_id = (int)f::post('attr_id');
$data['name'] = htmlspecialchars(trim(f::post('name')));
$data['unit'] = trim(f::post('unit'));
$data['type'] = (int)trim(f::post('type'));
$data['my_order'] = (int)trim(f::post('my_order'));
$data['attr_value'] = '';
if(!$attr_id){
throw new Exception('参数错误');
}
if(!$data['name']){
throw new Exception('数据不完整。');
}
if($data['type']==2){
$data['attr_value'] = f::post('attr_value');
}
$this->model('product')->table('pro_attr')->where("attr_id='{$attr_id}'")->update($data);
$this->model('manage')->add_log('修改产品属性', $_POST);
f::e_json('修改产品属性成功');
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 产品删除
*/
public function attribute_del_action()
{
try {
$id = $_POST['id'];
if(!$id){
throw new Exception('你没有选中项');
}
$where = array(" attr_id in(0," .$id. "-1)");
$delete_row = $this->model('product')->table('pro_attr')->where($where)->field('name')->selectAll();
$this->model('product')->table('pro_attr')->where($where)->delete();
$this->model('manage')->add_log('批量删除产品属性',$delete_row);
f::e_json('删除产品属性成功');
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 产品列表页面加载
*/
public function list_action()
{
$this->render();
}
/**
* 列表页面数据加载
*/
public function list_json_action()
{
$parames = json_decode(htmlspecialchars_decode($GLOBALS['HTTP_RAW_POST_DATA']),true);
$where = array("1");
$search = isset($parames['search']) ? $parames['search'] : '';
$start_add_date = strtotime(isset($parames['start_add_date']) ? $parames['start_add_date'] : '');
$last_add_date = strtotime(isset($parames['last_add_date']) ? $parames['last_add_date'] : '');
if(($start_add_date && !$last_add_date) || (!$start_add_date && $last_add_date)){
$time_start = $start_add_date ? $start_add_date : $last_add_date;
$time_end = $time_start + 86400 -1 ;
array_push($where," and (acc_time>='$time_start' and acc_time <='$time_end' )");
}elseif($start_add_date && $last_add_date && ($last_add_date > $start_add_date || $last_add_date == $start_add_date)){
$time_start = $start_add_date;
$time_end = $last_add_date + 86400 -1 ;
array_push($where," and (acc_time>='$time_start' and acc_time <='$time_end' )");
}
if($search!==''){
array_push($where," and (name like '%{$search}%' or model like '%{$search}%')");
}
$order_data = array('proid desc');
$sort = isset($parames['sort']) ? $parames['sort'] : 'acc_time';
$order = isset($parames['order']) ? $parames['order'] : 'desc';
array_unshift($order_data," {$sort} {$order}");
$cate_id = isset($parames['cate_id']) ? $parames['cate_id'] : '';
if($cate_id){
$cate_row = $this->model('product')->get_cate_one("cate_id='{$cate_id}'");
$cate_id_where = category::get_wehre_cateid_by_uid($cate_row['uid'].$cate_id.',','pro_cate');
$cate_id && array_push($where , $cate_id_where);
}
$ext_id = isset($parames['ext_id']) ? $parames['ext_id'] : '';
$ext_ary = array(
1 => ' and is_index = 1',
2 => ' and is_recommend = 1',
3 => ' and is_show = 1',
4 => ' and is_show = 0',
);
if($ext_id){
array_push($where , $ext_ary[$ext_id]);
}
$page_size = (int)$parames['limit'];
!$page_size && $page_size=20;
$start = isset($parames['offset']) ? (int)$parames['offset'] : 0;
$total = $this->model('product')->table('pro')->where($where)->get_row_count();
$this->model('product')->order($order_data);
$list = $this->model('product')->limit($start,$page_size)->list_json();
//分类
$cate_list = $this->model('product')->table('pro_cate')->where(array("1"))->selectAll();
$cate_list = f::arrayChangeKey($cate_list,'cate_id');
foreach((array)$list as $k=>$v){
$list[$k]['cate_name'] = $cate_list[$v['cate_id']]['cate_name'];
$list[$k]['picture'] = _e(@explode('|',$v['picture'])[0]);
$list[$k]['acc_time'] = f::date($v['acc_time'],'y/m/d H:i');
$list[$k]['n_d'] = output_float($v['n_d']);
$list[$k]['n_d_unit'] = $v['n_d_unit'].'';
$list[$k]['u_D'] = output_float($v['u_D']);
$list[$k]['u_D_unit'] = $v['u_D_unit'].'';
$list[$k]['u_H'] = output_float($v['u_H']);
$list[$k]['u_H_unit'] = $v['u_H_unit'].'';
$list[$k]['u_B'] = output_float($v['u_B']);
$list[$k]['u_B_unit'] = $v['u_B_unit'].'';
$list[$k]['n_m'] = output_float($v['n_m']);
$list[$k]['n_m_unit'] = $v['n_m_unit'].'';
}
exit(str::json_data(array('total'=>$total,'rows'=>$list)));
}
/**
* 编辑产品选择产品属性页面打开
*/
public function choice_attribute_action()
{
$attr_list = $this->model('product')->get_attr_list();
$this->assign('attr_list',$attr_list);
$this->render();
}
/**
* 产品编辑页面加载
* @param string $parames
*/
public function pro_edit_action($parames='')
{
try {
$parames = str::strtoparames($parames);
$attr_list = $this->model('product')->get_attr_list();
$attr_list = f::arrayChangeKey($attr_list,'attr_id');
$this->assign('attr_list',$attr_list);
$row = str::str_code($this->model('product')->table('pro')->where(array("proid='{$parames['proid']}'"))->fetch_one());
$contents_row = $this->model('product')->table('pro_contents')->where(array("proid='{$parames['proid']}'"))->fetch_one();
$row['attributes'] = @json_decode(htmlspecialchars_decode($row['attributes']),true);
$row['documents'] = @json_decode(htmlspecialchars_decode($row['documents']),true);
$row['picture'] = @explode('|',$row['picture']);
$this->assign('row',$row);
$this->assign('contents_row',$contents_row);
$this->render();
}catch (Exception $e){
exit($e->getMessage());
}
}
/**
* 编辑数据提交
*/
public function pro_edit_post_action()
{
try{
$proid = (int)f::post('proid');
$data['cate_id'] = (int)trim(f::post('cate_id'));
$data['name'] = trim(f::post('name'));
$data['model'] = trim(f::post('model'));
$data['my_order'] = (int)f::post('my_order');
$data['views'] = (int)f::post('views');
$data['picture'] = trim(@implode('|',f::post('pic-item')));
$data['brief_description'] = f::post('brief_description',1);
$data['is_index'] = (int)f::post('is_index');
$data['is_recommend'] = (int)f::post('is_recommend');
$data['is_show'] = (int)f::post('is_show');
$data['n_d'] = (float)f::post('n_d');
$data['n_d_unit'] = f::post('n_d_unit',1);
$data['n_d_desc'] = f::post('n_d_desc',1);
$data['u_D'] = (float)f::post('u_D');
$data['u_D_unit'] = f::post('u_D_unit',1);
$data['u_D_desc'] = f::post('u_D_desc',1);
$data['u_B'] = (float)f::post('u_B');
$data['u_B_unit'] = f::post('u_B_unit',1);
$data['u_B_desc'] = f::post('u_B_desc',1);
$data['u_H'] = (float)f::post('u_H');
$data['u_H_unit'] = f::post('u_H_unit',1);
$data['u_H_desc'] = f::post('u_H_desc',1);
$data['n_m'] = (float)f::post('n_m');
$data['n_m_unit'] = f::post('n_m_unit',1);
$data['n_m_desc'] = f::post('n_m_desc',1);
//产品属性
$post_attr_id = (array)f::post('attr_id');
$post_attr_val = (array)f::post('attr_val');
$attr_ary = array();
foreach((array)$post_attr_id as $k=>$v){
if(!$post_attr_val[$k]){continue;}
$attr_ary[$v] = $post_attr_val[$k];
}
$attr_ary = json_encode($attr_ary,JSON_UNESCAPED_UNICODE);
$data['attributes'] = $attr_ary;
//产品文档
$document_item_name = (array)f::post('document_item_name');
$document_item_url = (array)f::post('document_item_url');
$document_ary = array();
foreach((array)$document_item_name as $k=>$v){
$document_ary[$k]['name'] = $v;
$document_ary[$k]['url'] = $document_item_url[$k];
}
$document_ary = json_encode($document_ary,JSON_UNESCAPED_UNICODE);
$data['documents'] = $document_ary;
$where = " proid = '{$proid}'";
$more_data['contents'] = f::post('contents');
$this->model('product')->table('pro')->where($where)->update($data);
if($this->model('product')->table('pro_contents')->where($where)->fetch_one()) {
$this->model('product')->table('pro_contents')->where($where)->update($more_data);
}else{
$more_data['proid'] = $proid;
$this->model('product')->table('pro_contents')->where($where)->add($more_data);
}
$this->model('manage')->add_log('修改产品详情',$data);
f::e_json('修改产品详情成功');
}catch (Exception $e){
exit($e->getMessage());
}
}
/**
* 添加页面加载
*/
public function pro_add_action()
{
try {
$this->render();
}catch (Exception $e){
exit($e->getMessage());
}
}
/**
* 添加数据提交
*/
public function pro_add_post_action()
{
try{
$data['cate_id'] = (int)trim(f::post('cate_id'));
$data['name'] = trim(f::post('name'));
$data['model'] = trim(f::post('model'));
$data['my_order'] = (int)f::post('my_order');
$data['views'] = (int)f::post('views');
$data['picture'] = trim(@implode('|',f::post('pic-item')));
$data['acc_time'] = WEB_TIME;
$data['brief_description'] = f::post('brief_description',1);
$data['is_index'] = (int)f::post('is_index');
$data['is_recommend'] = (int)f::post('is_recommend');
$data['is_show'] = (int)f::post('is_show');
$data['n_d'] = (float)f::post('n_d');
$data['n_d_unit'] = f::post('n_d_unit',1);
$data['n_d_desc'] = f::post('n_d_desc',1);
$data['u_D'] = (float)f::post('u_D');
$data['u_D_unit'] = f::post('u_D_unit',1);
$data['u_D_desc'] = f::post('u_D_desc',1);
$data['u_B'] = (float)f::post('u_B');
$data['u_B_unit'] = f::post('u_B_unit',1);
$data['u_B_desc'] = f::post('u_B_desc',1);
$data['u_H'] = (float)f::post('u_H');
$data['u_H_unit'] = f::post('u_H_unit',1);
$data['u_H_desc'] = f::post('u_H_desc',1);
$data['n_m'] = (float)f::post('n_m');
$data['n_m_unit'] = f::post('n_m_unit',1);
$data['n_m_desc'] = f::post('n_m_desc',1);
//产品属性
$post_attr_id = (array)f::post('attr_id');
$post_attr_val = (array)f::post('attr_val');
$attr_ary = array();
foreach((array)$post_attr_id as $k=>$v){
if(!$post_attr_val[$k]){continue;}
$attr_ary[$v] = $post_attr_val[$k];
}
$attr_ary = json_encode($attr_ary,JSON_UNESCAPED_UNICODE);
$data['attributes'] = $attr_ary;
//产品文档
$document_item_name = (array)f::post('document_item_name');
$document_item_url = (array)f::post('document_item_url');
$document_ary = array();
foreach((array)$document_item_name as $k=>$v){
$document_ary[$k]['name'] = $v;
$document_ary[$k]['url'] = $document_item_url[$k];
}
$document_ary = json_encode($document_ary,JSON_UNESCAPED_UNICODE);
$data['documents'] = $document_ary;
$more_data['contents'] = f::post('contents');
$this->model('product')->table('pro')->add($data);
$proid = $this->model('product')->get_insert_id();
$more_data['proid'] = $proid;
$this->model('product')->table('pro_contents')->add($more_data);
$this->model('manage')->add_log('添加产品详情',$data);
f::e_json('添加产品详情成功');
}catch (Exception $e){
exit($e->getMessage());
}
}
/**
* 复制页面加载
* @param string $parames
*/
public function pro_copy_action($parames='')
{
try {
$attr_list = $this->model('product')->get_attr_list();
$attr_list = f::arrayChangeKey($attr_list,'attr_id');
$this->assign('attr_list',$attr_list);
$parames = str::strtoparames($parames);
$row = $this->model('product')->table('pro')->where(array("proid='{$parames['id']}'"))->fetch_one();
$row['name'] .= " - 副本";
$row['acc_time'] = WEB_TIME;
unset($row['proid']);
$this->model('product')->table('pro')->add($row);
$id = $this->model('product')->get_insert_id();
$row['proid'] = $id;
//产品详情
$row_content = $this->model('product')->table('pro_contents')->where("proid='{$parames['id']}'")->fetch_one();
$this->model('product')->table('pro_contents')->add(array(
'proid' => $id,
'contents' => $row_content['contents']
));
$contents_row = $row_content;
$this->assign('contents_row',$contents_row);
$row['picture'] = @explode('|',$row['picture']);
$row['attributes'] = @json_decode(htmlspecialchars_decode($row['attributes']),true);
$row['documents'] = @json_decode(htmlspecialchars_decode($row['documents']),true);
$this->assign('row',$row);
$this->model('product')->add_log('复制产品',$row);
$this->render();
}catch (Exception $e){
exit($e->getMessage());
}
}
/**
* 产品删除
*/
public function pro_del_action()
{
try {
$id = $_POST['id'];
if(!$id){
throw new Exception('你没有选中项');
}
$where = array(" proid in(0," .$id. "-1)");
$delete_row = $this->model('product')->table('pro')->where($where)->field('name')->selectAll();
$this->model('product')->table('pro')->where($where)->delete();
$this->model('product')->table('pro_contents')->where($where)->delete();
$this->model('manage')->add_log('批量删除产品',$delete_row);
f::e_json('删除成功');
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 批量设置首页显示
*/
public function index_show_bat_action($parame)
{
try {
$id = $_POST['id'];
$value = (int)$parame;
if(!$id){
throw new Exception('你没有选中项');
}
$where = array(" proid in(" .$id. "-1)");
$this->model('product')->table('pro')->where($where)->update(array(
'is_index' => $value
));
$this->model('manage')->add_log('批量设置首页显示产品','-');
f::e_json('批量设置成功');
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 批量设置首页推荐
*/
public function index_recommend_bat_action($parame)
{
try {
$id = $_POST['id'];
$value = (int)$parame;
if(!$id){
throw new Exception('你没有选中项');
}
$where = array(" proid in(" .$id. "-1)");
$this->model('product')->table('pro')->where($where)->update(array(
'is_recommend' => $value
));
$this->model('manage')->add_log('批量设置首页推荐产品','-');
f::e_json('批量设置成功');
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 批量设置产品上下架
*/
public function show_bat_action($parame)
{
try {
$id = $_POST['id'];
$value = (int)$parame;
if(!$id){
throw new Exception('你没有选中项');
}
$where = array(" proid in(" .$id. "-1)");
$this->model('product')->table('pro')->where($where)->update(array(
'is_show' => $value
));
$this->model('manage')->add_log('批量设置产品前台显示','-');
f::e_json('批量设置成功');
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 产品列表排序
*/
public function pro_order_action()
{
try {
$new_my_order = trim($_POST['new_my_order']);
$proid = (int)$_POST['post_id'];
$where = array("1 and proid='{$proid}'");
$data = array('my_order'=>$new_my_order);
$this->model('product')->table('pro')->where($where)->update($data);
$data=array();
$data['产品名称'] = f::post('name');
$data['旧排序'] = f::post('my_order');
$data['新排序'] = $new_my_order;
$this->model('manage')->add_log('修改产品排序',$data);
f::e_json('修改成功');
}catch (Exception $e){
echo $e->getMessage();
}
}
/**
* 产品批量上传
*/
public function upload_action()
{
try{
if($_POST){
if(!$_FILES['file']['name']){ throw new Exception('EXCEL文件不能不空'); }
if(!in_array(file::get_ext_name($_FILES['file']['name']),array('xls','xlsx'))){throw new Exception('文件格式错误!');}
$file = file::write_file("/upload/excel/",str::rand_code().'.'.file::get_ext_name($_FILES['file']['name']),file_get_contents($_FILES['file']['tmp_name']));
//将上传文件记录添加到数据库
$this->model('product')->table('pictures')->add(array(
'file_name' => $_FILES['file']['name'],
'url' => $file,
'from' => '产品批量上传',
'file_type' => file::get_ext_name($_FILES['file']['name']),
'add_time' => WEB_TIME
));
set_time_limit(0);
ob_end_clean();
echo "<style>body{padding:0;margin:2px;}p{background:#ddd;margin:0 0 2px 0;padding:5px;transition:all .3s ease-out;}#body p:first-child{background:green;font-weight: bold;font-size: 1em;color:#fff;}p.error{background:red !important;;color:#fff !important;}p.info{background:green !important;;color:#fff !important;}</style>";
echo "<div id='body'></div>";
flush();
$sheet_num = (int)f::post('sheet');
$upload_model = (int)f::post('upload_model'); //上传模式,0忽略重复的,1,更新重复的
require_once APP_PATH.'/lib/excel.class/PHPExcel/IOFactory.php';
$file = APP_PATH.$file;
$file_ext = file::get_ext_name($file);
//重新赋值给国家数组
if($file_ext=='xls'){ //旧版本
$reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
}elseif($file_ext=='xlsx'){ //新版本..
$reader = PHPExcel_IOFactory::createReader('Excel2007'); //设置以Excel5格式(Excel97-2003工作簿)
}
$PHPExcel = $reader->load($file); // 载入excel文件
$sheet = $PHPExcel->getSheet($sheet_num); // 读取第二個工作表
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumm = $sheet->getHighestColumn(); // 取得总列数
$highestColumm= PHPExcel_Cell::columnIndexFromString($highestColumm); //字母列转换为数字列 如:AA变为27
if($highestColumm!=18){
throw new Exception('上传失败。表格列数不正确。');
}
//每条记录图片标签ID数组
$cate_ary=array();
//数量统计
$upload_count = $error_count = $update_count = 0;
/** 循环读取每个单元格的数据 **/
for ($row = 2; $row <= $highestRow; $row++){//行数是以第1行开始
$name = @addslashes((trim($sheet->getCellByColumnAndRow(0, $row)->getValue())));
if(!$name)
continue;
$n_d = @(trim($sheet->getCellByColumnAndRow(2, $row)->getValue()));
$n_d_unit = @(trim($sheet->getCellByColumnAndRow(3, $row)->getValue()));
$n_d_desc = @(trim($sheet->getCellByColumnAndRow(4, $row)->getValue()));
$u_D = @(trim($sheet->getCellByColumnAndRow(5, $row)->getValue()));
$u_D_unit = @(trim($sheet->getCellByColumnAndRow(6, $row)->getValue()));
$u_D_desc = @(trim($sheet->getCellByColumnAndRow(7, $row)->getValue()));
$u_B = @(trim($sheet->getCellByColumnAndRow(8, $row)->getValue()));
$u_B_unit = @(trim($sheet->getCellByColumnAndRow(9, $row)->getValue()));
$u_B_desc = @(trim($sheet->getCellByColumnAndRow(10, $row)->getValue()));
$u_H = @(trim($sheet->getCellByColumnAndRow(11, $row)->getValue()));
$u_H_unit = @(trim($sheet->getCellByColumnAndRow(12, $row)->getValue()));
$u_H_desc = @(trim($sheet->getCellByColumnAndRow(13, $row)->getValue()));
$n_m = @(trim($sheet->getCellByColumnAndRow(14, $row)->getValue()));
$n_m_unit = @(trim($sheet->getCellByColumnAndRow(15, $row)->getValue()));
$n_m_desc = @(trim($sheet->getCellByColumnAndRow(16, $row)->getValue()));
$cate_id = (int)($sheet->getCellByColumnAndRow(17, $row)->getValue());
$picture = '';
$xunhuan_item = 0;
$item_ary = array();
if($cate_id){
if($cate_ary[$cate_id]){
}else{
$cate_ary[$cate_id] = $this->model('product')->table('pro_cate')->where("cate_id = '{$cate_id}'")->fetch_one();
$cate_ary[$cate_id]['pictures'] = @explode('|',$cate_ary[$cate_id]['pictures']);
}
while(true){
if($xunhuan_item >= count($cate_ary[$cate_id]['pictures'])){
break;
}
if($xunhuan_item >= 4){
break;
}
$item = mt_rand(0,count($cate_ary[$cate_id]['pictures']));
if(in_array($item,$item_ary)){
continue;
}
$xunhuan_item += 1;
$item_ary[] = $item;
$picture .= "{$cate_ary[$cate_id]['pictures'][$item]}|";
}
}
@$picture = @trim($picture,'|');
$data = array(
'name' => $cate_ary[$cate_id]['pro_name_fix'].' '.$name,
'model' => $name,
'cate_id' => $cate_id,
'picture' => $picture,
'n_d' => (float)$n_d,
'n_d_unit' => $n_d_unit,
'n_d_desc' => $n_d_desc,
'n_m' => (float)$n_m,
'n_m_unit' => $n_m_unit,
'n_m_desc' => $n_m_desc,
'u_D' => (float)$u_D,
'u_D_unit' => $u_D_unit,
'u_D_desc' => $u_D_desc,
'u_H' => (float)$u_H,
'u_H_unit' => $u_H_unit,
'u_H_desc' => $u_H_desc,
'u_B' => (float)$u_B,
'u_B_unit' => $u_B_unit,
'u_B_desc' => $u_B_desc,
'acc_time' => WEB_TIME
);
if($old_row = $this->model('product')->table('pro')->where("model='{$name}'")->fetch_one()){
if($upload_model){ //更新模式
$this->model('product')->table('pro')->where("proid='{$old_row['proid']}'")->update($data);
echo "<script>";
echo "var toObj = document.getElementById('body');";
echo "var newElement = document.createElement('p');";
echo "newElement.className = 'info';";
echo "newElement.innerHTML = '第【 ".($row-1)." 】个产品【{$name}】更新成功';";
echo "document.getElementById('body').insertBefore(newElement,document.getElementById('body').childNodes[0]);";
echo "</script>";
flush();
$update_count ++;
}elseif(!$upload_model){ //去重复模式
echo "<script>";
echo "var toObj = document.getElementById('body');";
echo "var newElement = document.createElement('p');";
echo "newElement.className = 'error';";
echo "newElement.innerHTML = '第【 ".($row-1)." 】个产品【{$name}】已经存在,上传失败';";
echo "document.getElementById('body').insertBefore(newElement,document.getElementById('body').childNodes[0]);";
echo "</script>";
flush();
$error_count ++ ;
}
}else{
$this->model('product')->table('pro')->add($data);
$proid = $this->model('product')->get_insert_id();
$this->model('product')->table('pro_contents')->add(array(
'proid' => $proid
));
echo "<script>";
echo "var toObj = document.getElementById('body');";
echo "var newElement = document.createElement('p');";
echo "newElement.innerHTML = '第【 ".($row-1)." 】个产品【{$name}】导入成功';";
echo "document.getElementById('body').insertBefore(newElement,document.getElementById('body').childNodes[0]);";
echo "</script>";
flush();
$upload_count ++ ;
}
}
echo "<script>";
echo "var toObj = document.getElementById('body');";
echo "var newElement = document.createElement('p');";
echo "newElement.innerHTML = '本次共 上传【 ".($upload_count)." 】个产品; 更新【".$update_count."】个产品;忽略【".$error_count."】个产品';";
echo "document.getElementById('body').insertBefore(newElement,document.getElementById('body').childNodes[0]);";
echo "</script>";
flush();
}else {
$this->render();
}
}catch (Exception $e){
exit($e->getMessage());
}
}
/**
* 产品静态文件生成
*/
public function html_action()
{
$this->render();
}
public function html_post_action()
{
$lang = f::post('language');
$file_ext = $lang;
$cate_id = (int)f::post('cate_id');
$cate_where = array('1');
$cate_id && array_push($cate_where," and cate_id='{$cate_id}'");
$lang =='cn' && $lang = '';
$save_dir ='/'.$lang.'/';
$cate_list = $this->model('product')->table('pro_cate')->where($cate_where)->selectAll();
$attr_list = $this->model('product')->table('pro_attr')->where('1')->order('my_order desc,attr_id asc')->selectAll();
$attr_list = f::arrayChangeKey($attr_list,'attr_id');
$all_cate = $this->model('product')->table('pro_cate')->where('1')->order('my_order desc,cate_id asc')->selectAll();
$all_cate = f::arrayChangeKey($all_cate,'cate_id');
ob_end_clean();
echo "<style>body{padding:0;margin:2px;}p{background:#ddd;margin:0 0 2px 0;padding:5px;transition:all .3s ease-out;}#body p:first-child{background:green;font-weight: bold;font-size: 1em;color:#fff;}p.error{background:red !important;;color:#fff !important;}p.info{background:green !important;;color:#fff !important;}</style>";
echo "<div id='body'></div>";
flush();
foreach((array)$cate_list as $k=>$v){
$page_url = '/'.$lang.'/';
$html_save_dir = str_replace('//','/',$save_dir.f::str_to_url($v['brief']).'/');
$page_url = str_replace('//','/',$page_url.f::str_to_url($v['brief']).'/');
$where = array('1',' and is_show=1'," and cate_id='{$v['cate_id']}'");
$page_count = 12;
$row_count = $this->model('product')->table('pro')->where($where)->get_row_count();
$total_page = ceil($row_count/$page_count);
!$total_page && $total_page = 1;
for($i=1;$i<=$total_page;$i++){
//获取所有标签
$query_string = $page_url.f::query_string('page').'page-';
//分页参数
$page = $i;
$start = ($page - 1) * $page_count;
//获取文章列表
$pro_list = $this->model('product')->table('pro')->where($where)->limit($start,$page_count)->selectAll();
ob_start();
include(APP_PATH.'/application/admin/views/product/list_model_'.$file_ext.'.php');
$html = ob_get_contents();
ob_clean();
file::write_file($html_save_dir,'page-'.$i.'.html',$html);
if($i==1){
file::write_file($html_save_dir,'index.html',$html);
}
ob_end_clean();
echo "<script>";
echo "var toObj = document.getElementById('body');";
echo "var newElement = document.createElement('p');";
echo "newElement.innerHTML = '生成文件:".($html_save_dir.'page-'.$i.'.html')."';";
echo "document.getElementById('body').insertBefore(newElement,document.getElementById('body').childNodes[0]);";
echo "</script>";
flush();
foreach((array)$pro_list as $k3=>$v3){
$v3['picture'] = @explode("|",$v3['picture']);
$v3['attributes'] = @json_decode($v3['attributes'],true);
$detail = $this->model('product')->table('pro_contents')->where("proid='{$v3['proid']}'")->fetch_one();
ob_start();
include(APP_PATH.'/application/admin/views/product/pro_detail_'.$file_ext.'.php');
$html = ob_get_contents();
ob_clean();
file::write_file($html_save_dir,f::str_to_url($v3['name']).'-'.$v3['proid'].'.html',$html);
ob_end_clean();
echo "<script>";
echo "var toObj = document.getElementById('body');";
echo "var newElement = document.createElement('p');";
echo "newElement.innerHTML = '生成文件:".(f::str_to_url($v3['name']).'-'.$v3['proid'].'.html')."';";
echo "document.getElementById('body').insertBefore(newElement,document.getElementById('body').childNodes[0]);";
echo "</script>";
flush();
}
}
}
}
}