'/index.php/Super/GoodsCode/export_goods', ); $authArr = checkFunction($arr); $this->assign('authArr', $authArr); $this->display(); } public function export_goods() { $this->display(); } /** * 获取商品列表 */ public function goodsCodelist() { $GoodsCloud = new GoodsCloud(); $this->ajaxReturn($GoodsCloud->getGoodsList([ 'page' => I('post.page'), 'limit' => I('post.limit'), 'keyword' => I('post.keyword'), ])); } /** * 待导入数据列表 */ public function getGoodsList() { if (IS_POST) { $status = I('post.type'); $map['status'] = $status != -1 ? $status : ['neq', 2]; $data = M('goodscode_import')->where($map)->order('status asc')->select(); if ($data) { foreach ($data as &$v) { if ($v['status'] == 0) { $v['statusname'] = '未导入'; } else if ($v['status'] == 1) { $v['statusname'] = '已导入'; } else if ($v['status'] == 2) { $v['statusname'] = '已删除'; } else if ($v['status'] == 3) { $v['statusname'] = '商品名称重复'; } else if ($v['status'] == 4) { $v['statusname'] = '商品条码重复'; } } $rt['status'] = 1; $rt['msg'] = '查询成功'; $rt['data'] = $data; } else { $rt['status'] = 0; $rt['msg'] = '查询失败'; } $this->ajaxReturn($rt); } } /** * excel导入到import表 */ public function uploadFile() { if (IS_POST) { $upload = new \Think\Upload(); // 实例化上传类 $upload->maxSize = 1048576 * 10; // 设置附件上传大小10M $upload->exts = array( 'xls' ); // 设置附件上传类型 $upload->rootPath = 'Uploads'; // 设置附件上传根目录 $upload->savePath = '/Temp/'; // 设置附件上传(子)目录 $upload->saveName = array( 'uniqid', '' ); // 上传文件 $info = $upload->upload(); if (empty($info)) { $rt['status'] = 3; $rt['msg'] = '表格类型不正确,应为.xls后缀的数据表格,或者数据表格大于10M'; $this->ajaxReturn($rt); exit; } $path['path'] = '/' . 'Uploads' . $info['file']['savepath'] . $info['file']['savename']; $arr = $this->importExecl("." . $path['path']); $myrt['arr'] = $arr; if ($arr['error'] == 0) { $myrt['status'] = 2; $myrt['msg'] = '数据表格格式不正确,请下载导入模板,按模板填入数据。'; $this->ajaxReturn($myrt); exit; } else { $headArr = array( 'code' => '商品条码', 'name' => '商品名称', 'specification' => '商品规格', 'unit' => '商品单位', 'origin' => '商品产地', 'trademark' => '品牌', ); array_flip($headArr); header('Content-Type: text/html; charset=utf-8'); //网页编码 $GoodsCodeArr = $arr['data'][0]['Content']; $GoodsCodeArr = json_decode(json_encode($GoodsCodeArr), true); $titlearr = $GoodsCodeArr[1]; $keyarr = array(); foreach ($titlearr as $k => $v) { foreach ($headArr as $k2 => $v2) { if ($v == $v2) { $keyarr[$k2] = $k; } } } $rt['goodsarr'] = $GoodsCodeArr; $faultarr = array(); $logData = []; //为日志功能新建数组:用于存放合格上传的会员信息,并记录日志 foreach ($GoodsCodeArr as $key => &$val) { if ($key != 1) { if ($val[$keyarr['code']] != '0' && !empty($val[$keyarr['code']])) { $map2['code'] = $val[$keyarr['code']]; $map2['status'] = array('neq', 2); $exist = M('goodscode_import')->where($map2)->find(); if (!$exist) { if ($val[$keyarr['name']]) { $newInfo['specification'] = $val[$keyarr['specification']] ? $val[$keyarr['specification']] : ''; $newInfo['unit'] = $val[$keyarr['unit']] ? $val[$keyarr['unit']] : ''; $newInfo['origin'] = $val[$keyarr['origin']] ? $val[$keyarr['origin']] : ''; $newInfo['trademark'] = $val[$keyarr['trademark']] ? $val[$keyarr['trademark']] : ''; $newInfo['code'] = $val[$keyarr['code']]; $newInfo['name'] = $val[$keyarr['name']]; $newInfo['status'] = 0; M('goodscode_import')->add($newInfo); $logData[] = $newInfo; } else { $val['reson'] = "商品名称" . $val[$keyarr['name']] . "未填写"; $faultarr[] = $val; } } else { $val['reson'] = "商品条码" . $val[$keyarr['code']] . "已存在"; $faultarr[] = $val; } } else { $val['reson'] = "商品条码不能为空"; $faultarr[] = $val; } } } } $rt['headArr'] = $headArr; $rt['titlearr'] = $titlearr; $rt['faultarr'] = $faultarr; if (count($faultarr) > 0) { // 生成错误表格,并返回路径。 $path2 = './Uploads/Temp/goodsCodeImport' . date('Y-m-d') . '.txt'; file_put_contents($path2, json_encode($faultarr)); $rt['status'] = 1; $rt['path'] = $path2; if (count($faultarr) + 1 == count($GoodsCodeArr)) { $rt['msg'] = count($faultarr) . '项未导入成功'; } else { $rt['msg'] = '导入成功,但有' . count($faultarr) . '项未导入成功'; } } else { $rt['status'] = 1; $rt['msg'] = '导入成功'; } //操作日志记录 $source = 0; //请求来源: 0pc系统 1小助手 $type = 1; //平台:1合伙人端 2代理商端 3商户端 4门店端 $request_data = json_encode($logData); //请求参数 json数据 $desc = '上传商品云库表格'; //操作行为描述 action_log($source, $type, $request_data, $desc); //end $this->ajaxReturn($rt); } } /** * 把商品同步至云库 */ public function submitToMGoods() { if ($_POST) { $checkCodeIds = I('post.checkCodeIds'); if ($checkCodeIds) { $successNum = 0; $resArr = array(); $GoodsCodeModel = new GoodsCodeModel(); $GoodsCodeList = M("goodscode_import")->where(['id' => ['in', $checkCodeIds]])->select(); foreach ($GoodsCodeList as $k => &$v) { $v['specification'] = !$v['specification'] ? '' : $v['specification']; $v['unit'] = !$v['unit'] ? '' : $v['unit']; $v['origin'] = !$v['origin'] ? '' : $v['origin']; $v['trademark'] = !$v['trademark'] ? '' : $v['trademark']; if ($v['code'] != 0) { $map = array( 'code' => $v['code'], ); $find_code = $GoodsCodeModel->getFind($map); if (!empty($find_code)) { //修改导入表状态(商品条码重复) M("goodscode_import")->where(['id' => $v['id']])->save(['status' => 4]); $resArr[] = '第' . ($k + 1) . '条数据商品条码' . $v['code'] . '在商品云库里已存在'; } } if (empty($find_code)) { $data = $v; unset($data['id'], $data['status']); $res = $GoodsCodeModel->addOrder($data); if ($res) { $successNum++; M("goodscode_import")->where(["id" => $v['id']])->save(["status" => 1]); $resArr[] = '第' . ($k + 1) . '条数据导入成功'; } else { $resArr[] = '第' . ($k + 1) . '条数据导入失败'; } } } $result['data'] = $resArr; if ($successNum == count($GoodsCodeList)) { $result['status'] = 1; $result['msg'] = '全部导入成功'; } else { $result['status'] = 2; $result['msg'] = '部分导入失败'; } if ($successNum > 0) { //清空商品云库之前读取的缓存信息 S('goodscodecount', null); } //操作日志记录 $source = 0; //请求来源: 0pc系统 1小助手 $type = 1; //平台:1合伙人端 2代理商端 3商户端 4门店端 $request_data = json_encode($_POST); //请求参数 json数据 $desc = '对商品云库进行导入'; //操作行为描述 action_log($source, $type, $request_data, $desc); } else { $result['status'] = 0; $result['msg'] = '登录超时'; } $this->ajaxReturn($result); } } /** * 删除导入数据 */ public function delGoodsItem() { if (IS_POST) { $id = I('post.id'); if ($id) { $ret = M('goodscode_import')->where(['id' => ['in', $id]])->save(['status' => 2]); } else { $this->ajaxReturn(['status' => 0, 'msg' => '请选择数据']); } if ($ret) { $rt['status'] = 1; $rt['msg'] = '删除成功'; //操作日志记录 $source = 0; //请求来源: 0pc系统 1小助手 $type = 1; //平台:1合伙人端 2代理商端 3商户端 4门店端 $request_data = json_encode($_POST); //请求参数 json数据 $desc = '对上传商品云库信息进行删除'; //操作行为描述 action_log($source, $type, $request_data, $desc); //end } else { $rt['status'] = 0; $rt['msg'] = '删除失败'; } $this->ajaxReturn($rt); } } /** * 下载导入模板 */ public function downModelFile() { $url = './Public/flow/zip/goodsCodeImport.zip'; if (!file_exists($url)) { //判断文件是否存在 echo "文件不存在"; exit(); } ob_clean(); $file = fopen($url, "r"); Header("Content-type:application/octet-stream"); header("Accept-Ranges: bytes"); header("Accept-Length: " . filesize($url)); header("Content-Disposition: attachment; filename=导入模板.zip"); echo fread($file, filesize($url)); fclose($file); } /** * 下载失败文件信息 */ public function downFailFile() { $path = './Uploads/Temp/goodsCodeImport' . date('Y-m-d') . '.txt'; //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入 import("Vendor.PHPExcel.PHPExcel"); import("Vendor.PHPExcel.Writer.Excel5"); import("Vendor.PHPExcel.IOFactory.php"); $arr = file_get_contents($path); $arr = json_decode($arr, true); $filename = "导入失败商品云库信息"; $headArr = array( '0' => '商品条码', '1' => '商品名称', '2' => '商品规格', '3' => '商品单位', '4' => '商品产地', '5' => '使用次数', '6' => '品牌', 'reson' => '失败原因', ); $headArr2 = array(); foreach ($headArr as $k => $v) { $headArr2[] = array($k, $v); } unlink($path); getExcel($filename, $headArr2, $arr); } /** * 上传导入文件 */ private function importExecl($file) { if (!file_exists($file)) { return array("error" => 0, 'message' => 'file not found!'); } Vendor("PHPExcel.PHPExcel.IOFactory"); $objReader = \PHPExcel_IOFactory::createReader('Excel5'); try { $PHPReader = $objReader->load($file); } catch (\Exception $e) { } if (!isset($PHPReader)) return array("error" => 0, 'message' => 'read error!'); $allWorksheets = $PHPReader->getAllSheets(); $i = 0; foreach ($allWorksheets as $objWorksheet) { $sheetname = $objWorksheet->getTitle(); $allRow = $objWorksheet->getHighestRow(); //how many rows $highestColumn = $objWorksheet->getHighestColumn(); //how many columns $allColumn = \PHPExcel_Cell::columnIndexFromString($highestColumn); $array[$i]["Title"] = $sheetname; $array[$i]["Cols"] = $allColumn; $array[$i]["Rows"] = $allRow; $arr = array(); $isMergeCell = array(); foreach ($objWorksheet->getMergeCells() as $cells) { //merge cells foreach (\PHPExcel_Cell::extractAllCellReferencesInRange($cells) as $cellReference) { $isMergeCell[$cellReference] = true; } } for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) { $row = array(); for ($currentColumn = 0; $currentColumn < $allColumn; $currentColumn++) { ; $cell = $objWorksheet->getCellByColumnAndRow($currentColumn, $currentRow); $afCol = \PHPExcel_Cell::stringFromColumnIndex($currentColumn + 1); $bfCol = \PHPExcel_Cell::stringFromColumnIndex($currentColumn - 1); $col = \PHPExcel_Cell::stringFromColumnIndex($currentColumn); $address = $col . $currentRow; $value = $objWorksheet->getCell($address)->getValue(); if (substr($value, 0, 1) == '=') { return array("error" => 0, 'message' => 'can not use the formula!'); exit; } if ($isMergeCell[$col . $currentRow] && $isMergeCell[$afCol . $currentRow] && !empty($value)) { $temp = $value; } elseif ($isMergeCell[$col . $currentRow] && $isMergeCell[$col . ($currentRow - 1)] && empty($value)) { $value = $arr[$currentRow - 1][$currentColumn]; } elseif ($isMergeCell[$col . $currentRow] && $isMergeCell[$bfCol . $currentRow] && empty($value)) { $value = $temp; // $temp = $value; } $row[$currentColumn] = $value; } $arr[$currentRow] = $row; } $array[$i]["Content"] = $arr; $i++; } unset($objWorksheet); unset($PHPReader); unset($PHPExcel); unlink($file); return array("error" => 1, "data" => $array); } }