1 ,'message' => '文件不存在!']; } /** @var Xlsx $objRead */ $objRead = IOFactory::createReader('Xlsx'); if (!$objRead->canRead($file)) { /** @var Xls $objRead */ $objRead = IOFactory::createReader('Xls'); if (!$objRead->canRead($file)) { return ['code' => 1 ,'message' => '只支持导入Excel文件!']; } } $file_size = $_FILES['iFile']['size']; if ($file_size > 5 * 1024 * 1024) { return ['code' => 1 ,'message' => '文件大小不能超过5M']; } /* 建立excel对象 */ $objPHPExcel = $objRead->load($file); $sheet = $objPHPExcel->getSheet(0); //excel中的第一张sheet $highestRow = $sheet->getHighestRow(); // 取得总行数 $highestColumn = $sheet->getHighestColumn(); // 取得总列数 \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); $lines = $highestRow -1; if($lines <= 0){ return ['code' => 1 ,'message' => 'excel表格,没有数据']; } if($lines > 1000){ return ['code' => 1 ,'message' => '单次最多添加1000个会员']; } $uservall = []; //循环读取excel表格,整合成数组。如果是不指定key的二维,就用$data[i][j]表示。 for ($j = 2; $j <= $highestRow; $j++) { $mobile = trim($objPHPExcel->getActiveSheet()->getCell('D' . $j)->getValue()); $mobile = preg_replace('/^[(\xc2\xa0)|\s]+/','', $mobile); // if (empty($mobile)) { // return ['code' => 1 ,'message' => '第'.$j.'行,请填写手机号']; // } // if (!Validate::regex($mobile,"/^1[3456789]{1}\d{9}$/")) { // return ['code' => 1 ,'message' => '第'.$j.'行,手机号格式错误!']; // } $nickname = trim($objPHPExcel->getActiveSheet()->getCell("A" . $j)->getValue()); $nickname = preg_replace('/^[(\xc2\xa0)|\s]+/','', $nickname); if (empty($nickname)) { return ['code' => 1 ,'message' => '第'.$j.'行,请填写会员昵称/姓名']; } // if (User::detail(['mobile'=> $mobile])) { // return ['code' => 1 ,'message' => '第'.$j.'行,'.$mobile.'手机号已存在!']; // } // $password = trim($objPHPExcel->getActiveSheet()->getCell("B" . $j)->getValue()); // $password = preg_replace('/^[(\xc2\xa0)|\s]+/','', $password); // if (empty($password)) { // return ['code' => 1 ,'message' => '第'.$j.'行,请填写会员密码']; // } $user_no = trim($objPHPExcel->getActiveSheet()->getCell("C" . $j)->getValue()); $user_no = preg_replace('/^[(\xc2\xa0)|\s]+/','', $user_no); if (empty($user_no)) { return ['code' => 1 ,'message' => '第'.$j.'行,请填写会员号']; } if (User::detail(['user_no'=> $user_no])) { return ['code' => 1 ,'message' => '第'.$j.'行,'.$user_no.'会员号已存在!']; } $uservall[] = [ 'nickName' => $nickname??'', 'mobile' => $mobile??'', 'gender' => 0, 'grade_id' => 2, 'user_no' => $user_no, 'avatarUrl' => 'http://cni-ekshop.cni.com.cn/image/agent/user_head.png', 'password' => md5(123456), 'app_id' => 10001, 'reg_source' => 'mp', 'create_time' => time(), ]; } if (!empty($uservall)) { (new user)->saveAll($uservall); } return ['code' => 0 ,'message' => '操作成功']; } catch (\Exception $e) { throw $e; } } }