195 lines
7.6 KiB
PHP
195 lines
7.6 KiB
PHP
<?php
|
|
namespace app\controllers\index;
|
|
use Yii;
|
|
use yii\web\Controller;
|
|
use PHPExcel;
|
|
use PHPExcel_IOFactory;
|
|
class ExcelController extends CommonController{
|
|
//execl模板下载
|
|
public function actionTemplate_download()
|
|
{
|
|
$objExcel = new PHPExcel();
|
|
$objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
|
|
$objActSheet = $objExcel->getActiveSheet(0);
|
|
$objActSheet->setTitle('会员批量导入模板'); //设置excel的标题
|
|
$objActSheet->setCellValue('A1', '用户id');
|
|
$objActSheet->setCellValue('B1', '昵称');
|
|
$objActSheet->setCellValue('C1', '手机号');
|
|
|
|
$baseRow = 2; //数据从N-1行开始往下输出 这里是避免头信息被覆盖
|
|
//默认数据
|
|
$explame_data = array(
|
|
array(
|
|
'user_id' => '1',
|
|
'nickname' => '小明',
|
|
'phone' => '15012345678',
|
|
),
|
|
);
|
|
|
|
foreach ($explame_data as $key => $value) {
|
|
$i = $baseRow + $key;
|
|
$objExcel->getActiveSheet()->setCellValue('A' . $i, $value['user_id']);
|
|
$objExcel->getActiveSheet()->setCellValue('B' . $i, $value['nickname']);
|
|
$objExcel->getActiveSheet()->setCellValue('C' . $i, $value['phone']);
|
|
}
|
|
|
|
|
|
$objExcel->setActiveSheetIndex(0);
|
|
//4、输出
|
|
$objExcel->setActiveSheetIndex();
|
|
header('Content-Type: applicationnd.ms-excel');
|
|
$time = date('Y-m-d');
|
|
header("Content-Disposition: attachment;filename=会员批量导入模板" . $time . ".xls");
|
|
header('Cache-Control: max-age=0');
|
|
$objWriter->save('php://output');
|
|
}
|
|
public function import_batch_send()
|
|
{
|
|
header("content-type:text/html;charset=utf-8");
|
|
|
|
//上传excel文件
|
|
$file = request()->file('file');
|
|
//将文件保存到public/uploads目录下面
|
|
$info = $file->validate(['size' => 1048576, 'ext' => 'xls,xlsx'])->move('./uploads');
|
|
if ($info) {
|
|
//获取上传到后台的文件名
|
|
$fileName = $info->getSaveName();
|
|
//获取文件路径
|
|
$filePath = Env::get('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $fileName;
|
|
//获取文件后缀
|
|
$suffix = $info->getExtension();
|
|
//判断哪种类型
|
|
if ($suffix == "xlsx") {
|
|
$reader = \PHPExcel_IOFactory::createReader('Excel2007');
|
|
} else {
|
|
$reader = PHPExcel_IOFactory::createReader('Excel5');
|
|
}
|
|
} else {
|
|
return json(['status' => '1', 'message' => '文件过大或格式不正确导致上传失败-_-!']);
|
|
}
|
|
//载入excel文件
|
|
$excel = $reader->load($filePath, $encode = 'utf-8');
|
|
//读取第一张表
|
|
$sheet = $excel->getSheet(0);
|
|
//获取总行数
|
|
$row_num = $sheet->getHighestRow();
|
|
//获取总列数
|
|
$col_num = $sheet->getHighestColumn();
|
|
|
|
$import_data = []; //数组形式获取表格数据
|
|
for ($i = 2; $i <= $row_num; $i++) { $import_data[$i]['nickname'] = $sheet->getCell("B" . $i)->getValue();
|
|
$import_data[$i]['phone'] = $sheet->getCell("C" . $i)->getValue();
|
|
}
|
|
|
|
if (empty($import_data)) {
|
|
return json(['status' => '1', 'message' => '数据解析失败']);
|
|
}
|
|
|
|
//校验手机号是否重复
|
|
$phone_array = array_column($import_data, 'phone');
|
|
$phone_ids = implode(',', $phone_array);
|
|
$result_phone = db('user')
|
|
->field('phone')
|
|
->where('phone', 'in', $phone_ids)
|
|
->select();
|
|
if (!empty($result_phone)) {
|
|
$result_phone_array = array_column($result_phone, 'phone');
|
|
$result_phone_ids = implode(',', $result_phone_array);
|
|
return json(['status' => '3', 'message' => '数据重复', 'result' => $result_phone_ids]);
|
|
}
|
|
|
|
//将数据保存到数据库
|
|
$res = db('user')->insertAll($import_data);
|
|
if ($res) {
|
|
return json(['status' => '2', 'message' => '导入成功']);
|
|
} else {
|
|
return json(['status' => '1', 'message' => '提交失败,请刷新重试']);
|
|
}
|
|
}
|
|
public function addPost_import()
|
|
{
|
|
header("content-type:text/html;charset=utf-8");
|
|
|
|
//上传excel文件
|
|
$file = request()->file('file');
|
|
$pro_id = input('pro_id');
|
|
//将文件保存到public/uploads目录下面
|
|
$info = $file->validate(['size' => 1048576, 'ext' => 'xls,xlsx'])->move('./uploads');
|
|
if ($info) {
|
|
//获取上传到后台的文件名
|
|
$fileName = $info->getSaveName();
|
|
//获取文件路径
|
|
//$filePath = Env::get('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $fileName;
|
|
$filePath_ = 'uploads' . DIRECTORY_SEPARATOR . $fileName;
|
|
//获取文件后缀
|
|
$suffix = $info->getExtension();
|
|
//判断哪种类型
|
|
if ($suffix == "xlsx") {
|
|
$reader = \PHPExcel_IOFactory::createReader('Excel2007');
|
|
} else {
|
|
$reader = PHPExcel_IOFactory::createReader('Excel5');
|
|
}
|
|
} else {
|
|
//return json(['status' => '1', 'message' => '文件过大或格式不正确导致上传失败-_-!']);
|
|
$this->error(lang("文件过大或格式不正确导致上传失败-_-!"), url("coupon/import"));
|
|
}
|
|
//载入excel文件
|
|
$excel = $reader->load($filePath_, $encode = 'utf-8');
|
|
//读取第一张表
|
|
$sheet = $excel->getSheet(0);
|
|
//获取总行数
|
|
$row_num = $sheet->getHighestRow();
|
|
//获取总列数
|
|
$col_num = $sheet->getHighestColumn();
|
|
|
|
|
|
|
|
|
|
$import_data = []; //数组形式获取表格数据
|
|
for ($i = 1; $i <= $row_num; $i++) {
|
|
|
|
$import_data[$i]['coupon_name'] = $sheet->getCell("A" . $i)->getValue();
|
|
$import_data[$i]['create_time'] = date('Y-m-d H:i:s');
|
|
$import_data[$i]['pro_id'] = $pro_id;
|
|
|
|
}
|
|
//dump($import_data);die;
|
|
if (empty($import_data)) {
|
|
//return json(['status' => '1', 'message' => '数据解析失败']);
|
|
$this->error(lang("数据解析失败"), url("coupon/import"));
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//校验优惠券是否重复
|
|
$coupon_array = array_column($import_data, 'coupon_name');
|
|
|
|
// dump($coupon_array);die;
|
|
// $phone_ids = implode(',', $phone_array);
|
|
$result_phone = db('coupon')
|
|
->field('coupon_name')
|
|
->whereIn('coupon_name', $coupon_array)
|
|
->select()
|
|
->toArray();
|
|
|
|
//$result_phone_array = array_column($result_phone, 'coupon_name');
|
|
// dump($result_phone_array);die;
|
|
if (!empty($result_phone)) {
|
|
$result_phone_array = array_column($result_phone, 'coupon_name');
|
|
//$result_phone_ids = implode(',', $result_phone_array);
|
|
//return json(['status' => '3', 'message' => '数据重复', 'result' => $result_phone_ids]);
|
|
$this->error(lang("数据重复"), url("coupon/import"));
|
|
}
|
|
$result_phone_array =' error';
|
|
//将数据保存到数据库
|
|
$res = db('coupon')->insertAll($import_data);
|
|
if ($res) {
|
|
$this->success(lang("导入成功"), url("coupon/index"));
|
|
// return json(['status' => '2', 'message' => '导入成功']);
|
|
} else {
|
|
$this->error(lang("导入失败,重新导入"), url("coupon/import"));
|
|
}
|
|
}
|
|
} |