function ajax_upload_vgoods_sn($filename = "", $rt = array()) { @set_time_limit(600); //最大运行时间 $gid = $rt['gid']; $fop = Import::fileop(); unset($rt); $vgoods_type = $this->App->findvar("SELECT vgoods_type FROM `{$this->App->prefix()}userconfig` WHERE type = 'basic' LIMIT 1"); if (!empty($_FILES[$filename]['tmp_name'])) { $yuming = str_replace(array('www', '.'), '', $_SERVER["HTTP_HOST"]); if (!empty($yuming)) { $yuming = $yuming . DS; } $fn = SYS_PATH . 'cache' . DS . $yuming . 'vgoods' . DS . 'vgoodssn.xls'; if (file_exists($fn)) { unlink($fn); } //删除原来文件 $fop->copyfile($filename, $fn); //复制文件到服务器 if (!file_exists($fn)) { $fop->copyfile($filename, $fn); if (!file_exists($fn)) { echo '<script> alert("上传时发生意外错误!"); </script>'; return false; } } $data = Import::excel(); $data->read($fn); //读取文件 $importkey = $data->sheets[0]['cells'][1]; for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) { $rD = array(); for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { $rD[] = $data->sheets[0]['cells'][$i][$j]; } if (empty($rD[0])) { continue; } $rtd = array(); $rtd['goods_id'] = $gid; if ($vgoods_type == '2') { $rtd['goods_pass'] = $rD[0]; $sn = $rD[0]; } else { $rtd['goods_pass'] = $rD[1]; $rtd['goods_sn'] = $rD[0]; $sn = $rD[1]; } $rtd['addtime'] = mktime(); $sql = "SELECT id FROM `{$this->App->prefix()}goods_sn` WHERE goods_id='{$gid}' AND goods_pass='******' LIMIT 1"; $id = $this->App->findvar($sql); if ($id > 0) { continue; } $this->App->insert('goods_sn', $rtd); } echo '<script> alert("上传成功!");</script>'; return false; } echo '<script> alert("上传失败!");</script>'; return true; }
function ajax_upload($filename = "") { $uid = $this->Session->read('User.uid'); $rank = $this->Session->read('User.rank'); $active = $this->App->findvar("SELECT active FROM `{$this->App->prefix()}user` WHERE user_id='{$uid}'"); $this->Session->write('User.active', $active); if ($active != '1') { echo '<script> alert("抱歉,你账户处于失活状态,请联系管理员激活!"); </script>'; return false; } if (!($uid > 0) || $rank != '10') { echo '<script> alert("抱歉,你没有权限上传!"); </script>'; return false; } $fop = Import::fileop(); $fn = SYS_PATH . 'cache' . DS . 'admin-' . $uid . '.php'; $fop->writefile($fn, ""); $tw_s = intval($GLOBALS['LANG']['th_width_s']) > 0 ? intval($GLOBALS['LANG']['th_width_s']) : 200; $th_s = intval($GLOBALS['LANG']['th_height_s']) > 0 ? intval($GLOBALS['LANG']['th_height_s']) : 200; $tw_b = intval($GLOBALS['LANG']['th_width_b']) > 0 ? intval($GLOBALS['LANG']['th_width_b']) : 450; $th_b = intval($GLOBALS['LANG']['th_height_b']) > 0 ? intval($GLOBALS['LANG']['th_height_b']) : 450; if (!empty($_FILES[$filename]['tmp_name'])) { $fn = SYS_PATH . 'cache' . DS . 'admin-upload-' . $uid . '.xls'; if (file_exists($fn)) { unlink($fn); } //删除原来文件 $fop->copyfile($filename, $fn); //复制文件到服务器 if (!file_exists($fn)) { $fop->copyfile($filename, $fn); if (!file_exists($fn)) { echo '<script> alert("上传时发生意外错误!"); </script>'; return false; } } $data = Import::excel(); $data->read($fn); //读取文件 $importkey = $data->sheets[0]['cells'][1]; for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) { //以下注释的for循环打印excel表数据 $this->rtData = array(); //商品数据 $this->rtData_gallery = array(); //商品相册 for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { $this->goods_key($importkey[$j], $data->sheets[0]['cells'][$i][$j]); //传送 键=>值 处理 } $this->rtData['is_on_sale'] = '0'; $this->rtData['add_time'] = mktime(); $this->rtData['uid'] = $uid; $inData = $this->rtData; //print_r($inData); exit; $goods_id = 0; //检查该商品已经存在数据库中 $sn = $inData['goods_sn']; //优先级是商品条形码检查 if (!empty($sn)) { $sql = "SELECT goods_id FROM `{$this->App->prefix()}goods` WHERE goods_sn='{$sn}'"; $goods_id = $this->App->findvar($sql); //if(!empty($snvar)) continue; } /*else{ $sa = $inData['goods_name']; //商品名称检查是否该商品已经存在 if(!empty($sa)){ //最后更新:2012-12-11 10:26 $sql = "SELECT goods_id FROM `{$this->App->prefix()}goods` WHERE goods_name='$sa'"; $goods_id = $this->App->findvar($sql); //if(!empty($savar)) continue; } }*/ if (!empty($inData['goods_name'])) { //商品图片 $val = $inData['original_img']; if (!empty($val)) { $pa = dirname($val); $thumb = basename($val); if (is_file(SYS_PATH . $pa . DS . 'thumb_s' . DS . $thumb)) { $pp = SYS_PATH . $pa . DS . 'thumb_s' . DS . mktime() . $thumb; $pps = $pa . '/thumb_s/' . mktime() . $thumb; } else { $pp = SYS_PATH . $pa . DS . 'thumb_s' . DS . $thumb; $pps = $pa . '/thumb_s/' . $thumb; } Import::img()->thumb(SYS_PATH . $val, $pp, $tw_s, $th_s); //小缩略图 $inData['goods_thumb'] = $pps; if (is_file(SYS_PATH . $pa . DS . 'thumb_b' . DS . $thumb)) { $pp = SYS_PATH . $pa . DS . 'thumb_b' . DS . mktime() . $thumb; $pps = $pa . '/thumb_b/' . mktime() . $thumb; } else { $pp = SYS_PATH . $pa . DS . 'thumb_b' . DS . $thumb; $pps = $pa . '/thumb_b/' . $thumb; } Import::img()->thumb(SYS_PATH . $val, $pp, $tw_b, $th_b); //大缩略图 $inData['goods_img'] = $pps; } if ($goods_id > 0) { //更新 //$inData['is_check'] = 0; if ($this->App->update('goods', $inData, 'goods_id', $goods_id)) { //更新供应商商品表 $sql = "SELECT sgid FROM `{$this->App->prefix()}suppliers_goods` WHERE goods_id='{$goods_id}' AND suppliers_id='{$uid}'"; $sgid = $this->App->findvar($sql); $dd = array(); $dd['goods_number'] = $inData['goods_number'] > 0 ? $inData['goods_number'] : 1000; $dd['warn_number'] = $inData['warn_number'] > 0 ? $inData['warn_number'] : 10; if ($inData['pifa_price'] > 0) { $dd['market_price'] = $inData['pifa_price']; } if ($inData['pifa_price'] > 0) { $dd['pifa_price'] = $inData['pifa_price']; } if ($inData['shop_price'] > 0) { $dd['shop_price'] = $inData['shop_price']; } $dd['is_check'] = 0; if (empty($sgid) || !($sgid > 0)) { $dd['is_on_sale'] = 1; $dd['goods_id'] = $goods_id; $dd['suppliers_id'] = $uid; $dd['addtime'] = mktime(); $this->App->insert('suppliers_goods', $dd); } else { $this->App->update('suppliers_goods', $dd, array("suppliers_id='{$uid}'", "goods_id='{$goods_id}'")); } unset($dd); //将原来的相册图片删除 $sql = "SELECT img_url FROM `{$this->App->prefix()}goods_gallery` WHERE goods_id ='{$goods_id}'"; $gallery_img = $this->App->findcol($sql); if (!empty($gallery_img)) { foreach ($gallery_img as $img) { $q = dirname($img); $h = basename($img); Import::fileop()->delete_file(SYS_PATH . $q . DS . 'thumb_s' . DS . $h); Import::fileop()->delete_file(SYS_PATH . $q . DS . 'thumb_b' . DS . $h); Import::fileop()->delete_file(SYS_PATH . $img); // } unset($gallery_img); } $this->App->delete('goods_gallery', 'goods_id', $goods_id); //重新处理商品相册 $rt_gallery = $this->rtData_gallery; if (!empty($rt_gallery)) { foreach ($rt_gallery as $vv) { $vv = trim($vv); $pa = dirname($vv); $thumb = basename($vv); if (empty($vv) || !is_file(SYS_PATH . $vv)) { continue; } //生成缩略图 if (is_file(SYS_PATH . $pa . DS . 'thumb_s' . DS . $thumb)) { $p = SYS_PATH . $pa . DS . 'thumb_s' . DS . mktime() . $thumb; } else { $p = SYS_PATH . $pa . DS . 'thumb_s' . DS . $thumb; } Import::img()->thumb(SYS_PATH . $vv, $p, $tw_s, $th_s); //小缩略图 if (is_file(SYS_PATH . $pa . DS . 'thumb_b' . DS . $thumb)) { $p = SYS_PATH . $pa . DS . 'thumb_b' . DS . mktime() . $thumb; } else { $p = SYS_PATH . $pa . DS . 'thumb_b' . DS . $thumb; } Import::img()->thumb(SYS_PATH . $vv, $p, $tw_b, $th_b); //大缩略图 //插入商品相册属性表 $dd = array(); $dd['goods_id'] = $goods_id; $dd['img_url'] = $vv; $this->App->insert('goods_gallery', $dd); } // end foreach } //end if 商品相册 } else { //插入失败,写入日记 $aid = $this->Session->read('adminid'); $fn = SYS_PATH . 'cache' . DS . 'admin-' . $aid . '.php'; $fop->writefile($fn, "批量导入错误:\n" . '商品编号--' . implode('--', array_keys($inData)) . "\n" . $inData['goods_bianhao'] . '--' . implode('--', $inData) . "\n"); } } else { //插入数据库 if ($this->App->insert('goods', $inData)) { $lastid = $this->App->iid(); //添加到供应商商品表 $sql = "SELECT sgid FROM `{$this->App->prefix()}suppliers_goods` WHERE goods_id='{$lastid}' AND suppliers_id='{$uid}'"; $sgid = $this->App->findvar($sql); $dd = array(); $dd['goods_number'] = $inData['goods_number'] > 0 ? $inData['goods_number'] : 1000; $dd['warn_number'] = $inData['warn_number'] > 0 ? $inData['warn_number'] : 10; if ($inData['pifa_price'] > 0) { $dd['market_price'] = $inData['pifa_price']; } if ($inData['pifa_price'] > 0) { $dd['pifa_price'] = $inData['pifa_price']; } if ($inData['shop_price'] > 0) { $dd['shop_price'] = $inData['shop_price']; } $dd['is_check'] = 0; $dd['is_on_sale'] = 1; $dd['goods_id'] = $lastid; $dd['suppliers_id'] = $uid; $dd['addtime'] = mktime(); $this->App->insert('suppliers_goods', $dd); unset($dd); $rt_gallery = $this->rtData_gallery; //商品相册 //商品相册 if (!empty($rt_gallery)) { foreach ($rt_gallery as $vv) { $vv = trim($vv); $pa = dirname($vv); $thumb = basename($vv); if (empty($vv) || !is_file(SYS_PATH . $vv)) { continue; } //生成缩略图 if (is_file(SYS_PATH . $pa . DS . 'thumb_s' . DS . $thumb)) { $p = SYS_PATH . $pa . DS . 'thumb_s' . DS . mktime() . $thumb; } else { $p = SYS_PATH . $pa . DS . 'thumb_s' . DS . $thumb; } Import::img()->thumb(SYS_PATH . $vv, $p, $tw_s, $th_s); //小缩略图 if (is_file(SYS_PATH . $pa . DS . 'thumb_b' . DS . $thumb)) { $p = SYS_PATH . $pa . DS . 'thumb_b' . DS . mktime() . $thumb; } else { $p = SYS_PATH . $pa . DS . 'thumb_b' . DS . $thumb; } Import::img()->thumb(SYS_PATH . $vv, $p, $tw_b, $th_b); //大缩略图 //插入商品属性表 $dd = array(); $dd['goods_id'] = $lastid; $dd['img_url'] = $vv; $this->App->insert('goods_gallery', $dd); } // end foreach } //end if } else { //插入失败,写入日记 $aid = $this->Session->read('adminid'); $fn = SYS_PATH . 'cache' . DS . 'admin-' . $uid . '.php'; $fop->writefile($fn, "批量导入错误:\n" . '商品编号--' . implode('--', array_keys($inData)) . "\n" . $inData['goods_bianhao'] . '--' . implode('--', $inData) . "\n"); } } //end if } else { //写入错误日记 $aid = $this->Session->read('adminid'); $fn = SYS_PATH . 'cache' . DS . 'admin-' . $uid . '.php'; $fop->writefile($fn, "批量导入错误:\n" . implode('--', array_keys($inData)) . "\n" . implode('--', $inData) . "\n"); } //end if } //end for echo '<script> alert("上传成功!");</script>'; return false; } return $_FILES[$filename]; }