Customer_list_adminAction.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <?php
  2. /**
  3. * 导入客户信息,导出客户信息
  4. */
  5. require_once( ONU_ROOT . 'application/module/ctrl/Action.class.php');
  6. require_once ( ONU_ROOT . 'application/lib/data/adminAction.php');
  7. class Customer_list_adminAction extends Action{
  8. public function __construct(){
  9. parent::__construct();
  10. if(empty($_SESSION['mds_user'])){
  11. header("Location:/?a=index&m=admin_login");
  12. exit();
  13. }
  14. // $this->checkAdministratorRight('1');
  15. }
  16. //客户列表
  17. public function index(){
  18. require_once (ONU_ROOT . "frame/Page.class.php");
  19. $admin = new AdvertAdminAction();
  20. $uid = $_SESSION['user_infos']['id'];
  21. $info = $_REQUEST['info'];
  22. $page = $_REQUEST['page']?$_REQUEST['page']:1;
  23. $pageSize = isset($_REQUEST['pagesize'])?$_REQUEST['pagesize']:20;
  24. $n = ($page-1)*$pageSize;
  25. $pa = "";//查询参数
  26. $pa='&pagesize='.$pageSize;
  27. $limit = " $n,$pageSize";
  28. $where = " where uid='$uid' ";
  29. $group_list = $admin->select_user_group($where);
  30. $username = trim($_REQUEST['username']);
  31. $group_name = $_REQUEST['group_name'];
  32. $isEmail = (!empty($username) && strpos($username, '@') !== false);
  33. if(!empty($username) && strpos($username, '@') !== false){
  34. $where.=" and email='$username'";
  35. $pa .= "&username=$username";
  36. }elseif(!empty($username)) {
  37. $where.=" and uname='$username'";
  38. $pa .= "&username=$username";
  39. }
  40. if(!empty($group_name)){
  41. $where.=" and group_name='$group_name'";
  42. $pa .= "&group_name=$group_name";
  43. }
  44. // if(!empty($date_show)){
  45. // $where.=" and create_time>=$begin_time and create_time<=$end_time";
  46. // $t = "$bd-$ed";
  47. // $pa .= "&date_show=$t";
  48. // }
  49. $customer_list = $admin->selesctCustomer($where,$limit);
  50. $all_total = $admin->selectCustomerCount($where);
  51. $p = new Page($all_total,$pageSize,$pa);
  52. $pp = $p->fpage();
  53. $tmp_data = $admin->select_tmp_data();
  54. $tmp_num = count($tmp_data);
  55. $this->assign('tmp_num',$tmp_num);
  56. $this->assign('username',$username);
  57. $this->assign('list',$customer_list);
  58. $this->assign('group_list',$group_list);
  59. $this->assign('group_name',$group_name);
  60. $this->assign('info',$info);
  61. $this->assign('page',$page);
  62. $this->assign('pagesize',$pageSize);
  63. $this->assign("show",$pp);
  64. $this->display("index.html");
  65. }
  66. public function uploadInvalidEmail(){
  67. $this->display("address_invalid_email.html");
  68. }
  69. public function addressInvalidEmail(){
  70. require_once ONU_ROOT . 'PhpOffice/PhpSpreadsheet/Spreadsheet.php';
  71. require_once ONU_ROOT . 'PhpOffice/PhpSpreadsheet/Reader/Csv.php';
  72. require_once ONU_ROOT . 'PhpOffice/PhpSpreadsheet/Reader/Xlsx.php';
  73. $file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  74. if(isset($_FILES['file']['name']) && in_array($_FILES['file']['type'], $file_mimes)) {
  75. $arr_file = explode('.', $_FILES['file']['name']);
  76. $extension = end($arr_file);
  77. if('csv' == $extension) {
  78. $reader = new Csv();
  79. } else {
  80. $reader = new Xlsx();
  81. }
  82. $spreadsheet = $reader->load($_FILES['file']['tmp_name']);
  83. $sheetData = $spreadsheet->getActiveSheet()->toArray();
  84. print_r($sheetData);
  85. }
  86. }
  87. //添加客户
  88. public function add(){
  89. $admin = new AdvertAdminAction();
  90. $uid = $_SESSION['user_infos']['id'];
  91. $where = "where uid='$uid' ";
  92. $group_list = $admin->select_user_group($where);
  93. if($_POST['email']){
  94. $data['uname'] = $_POST['username'];
  95. $data['email'] = $_POST['email'];
  96. $data['group_name'] = $_POST['group_name'];
  97. $data['country'] = $_POST['country'];
  98. $data['city'] = $_POST['city'];
  99. $data['tel'] = $_POST['tel'];
  100. $data['uid'] = $uid;
  101. $data['add_time'] = time();
  102. $is_exist = $admin->selectCustomerByEmail($data['email'],$data['uid']);
  103. if(empty($is_exist)){
  104. $add = $admin->add_customer($data);
  105. if($add){
  106. $info = "添加客户资料成功";
  107. header("Location:/?a=customer_list_admin&m=index&info=$info");
  108. exit();
  109. }else{
  110. $info = "添加客户资料失败";
  111. $this->assign('info',$info);
  112. }
  113. }else{
  114. $info = "您之前已经添加过此用户";
  115. header("Location:/?a=customer_list_admin&m=index&info=$info");
  116. exit();
  117. }
  118. }
  119. $this->assign('group_list',$group_list);
  120. $this->display('add.html');
  121. }
  122. //编辑客户
  123. public function edit(){
  124. $admin = new AdvertAdminAction();
  125. $uid = $_SESSION['user_infos']['id'];
  126. $where = "where uid='$uid' ";
  127. $group_list = $admin->select_user_group($where);
  128. $id = $_REQUEST['id'];
  129. $one_info = $admin->selectOneCustomer($id);
  130. if($_POST['email']){
  131. $id = $_POST['id'];
  132. $data['uname'] = $_POST['username'];
  133. $data['email'] = $_POST['email'];
  134. $data['group_name'] = $_POST['group_name'];
  135. $data['country'] = $_POST['country'];
  136. $data['city'] = $_POST['city'];
  137. $data['tel'] = $_POST['tel'];
  138. $update = $admin->updateCustomer($data,$id);
  139. if($update){
  140. $info = "编辑客户资料成功";
  141. header("Location:/?a=customer_list_admin&m=index&info=$info");
  142. exit();
  143. }else{
  144. $info = "编辑客户资料失败";
  145. $this->assign('info',$info);
  146. }
  147. }
  148. $this->assign('id',$id);
  149. $this->assign('one_info',$one_info);
  150. $this->assign('group_list',$group_list);
  151. $this->display('edit.html');
  152. }
  153. //删除客户
  154. public function delete(){
  155. $admin = new AdvertAdminAction();
  156. $uid = $_SESSION['user_infos']['id'];
  157. $id = $_REQUEST['id'];
  158. if($_REQUEST['id']){
  159. $delete = $admin->deleteCustomer($id,$uid);
  160. $info = "删除成功";
  161. header("Location:/?a=customer_list_admin&m=index&info=$info");
  162. exit();
  163. }
  164. }
  165. //批量删除客户
  166. public function batch_delete(){
  167. $admin = new AdvertAdminAction();
  168. $uid = $_SESSION['user_infos']['id'];
  169. $id_array = $_POST['id_array'];
  170. $count = count($id_array);
  171. if($count==1){
  172. $id = $id_array[0];
  173. $delete = $admin->deleteCustomer($id,$uid);
  174. $info = "删除成功";
  175. header("Location:/?a=customer_list_admin&m=index&info=$info");
  176. exit();
  177. }else if($count>1){
  178. $in_array = implode(',',$id_array);
  179. $delete = $admin->deleteBatchCustomer($in_array,$uid);
  180. $info = "删除成功";
  181. header("Location:/?a=customer_list_admin&m=index&info=$info");
  182. exit();
  183. }
  184. }
  185. //导出excel
  186. public function export(){
  187. $admin = new AdvertAdminAction();
  188. $uid = $_SESSION['user_infos']['id'];
  189. $group_name = $_REQUEST['group_name'];
  190. $where = " where uid='$uid' ";
  191. if(!empty($group_name)){
  192. $where.=" and group_name='$group_name'";
  193. }
  194. $list = $admin->selesctCustomer($where);
  195. foreach($list as &$ls){
  196. $ls['add_time'] = date('Y-m-d H:i:s',strtotime($ls['add_time']));
  197. }
  198. $d = date('Y-m-d',time());
  199. $this->generateEXCEL($d."导出客户数据",$list);
  200. }
  201. public function test(){
  202. $admin = new AdvertAdminAction();
  203. $uid = $_SESSION['user_infos']['id'];
  204. $blackListArr = $admin->selectAllBlack($uid);
  205. $ndrListArr = $admin->selectAllNdrUser($uid);
  206. echo $uid.PHP_EOL;
  207. print_r(array_column($blackListArr, 'email'));
  208. echo "------------------------".PHP_EOL;
  209. print_r(array_column($ndrListArr, 'email'));
  210. }
  211. //导入excel
  212. public function import(){
  213. $admin = new AdvertAdminAction();
  214. $uid = $_SESSION['user_infos']['id'];
  215. if($_POST['to_import_user']=='yes'){
  216. $filename = $_FILES['inputExcel']['name'];
  217. $tmp_name = $_FILES['inputExcel']['tmp_name'];
  218. $excel_array = $this->uploadFile($filename,$tmp_name);
  219. if(!empty($excel_array['error'])){
  220. //导入失败
  221. $info = $excel_array['error'];
  222. }else{
  223. unset($excel_array[0]);//若第一排的数据是字段名的话,删除
  224. header("Content-type: text/html; charset=utf-8");
  225. $blackList = $admin->selectAllBlack($uid);
  226. $blackListArr = array_column($blackList, 'email');
  227. $ndrList = $admin->selectAllNdrUser($uid);
  228. $ndrListArr = array_column($ndrList, 'email');
  229. $delEmailArr = array_flip(array_flip(array_merge($blackListArr,$ndrListArr)));
  230. $oldUserEmailList = $admin->selesctCustomerByUid($uid);
  231. $oldUserEmailListArr = array_column($oldUserEmailList, 'email');
  232. $oldUserEmailListArr = array_map('strtolower', $oldUserEmailListArr);
  233. foreach($excel_array as $array){
  234. if(!empty($array[1])){
  235. $data['uname'] = preg_replace("/[^a-zA-Z\s]+/", "", $array[0]);
  236. $data['email'] = $email_tmp = trim($array[1]);
  237. $data['tel'] = trim($array[2]);
  238. $data['country'] = trim($array[3]);
  239. $data['city'] = trim($array[4]);
  240. $data['group_name'] = trim($array[5]);
  241. $data['add_time'] = time();
  242. $data['uid'] = $uid;
  243. //先查询一下用户邮箱是否在退信列表,如果在退信列表就不导入
  244. //$where = "uid = '$uid'";
  245. //$is_ndr_user = $admin->select_ndr_user($where,$data['email']);
  246. //过滤字符串
  247. if(strpos($email_tmp, "'") !== false || strpos($email_tmp, '"') !== false){
  248. $info = "导入失败".$email_tmp."含有引号!";
  249. header("Location:/?a=customer_list_admin&m=index&info=$info");
  250. exit();
  251. }
  252. //查询在不在黑名单里面,如果在就不导入
  253. //$is_black_user = $admin->selectBlackByEmail($data['email'],$data['uid']);
  254. if(!in_array($data['email'], $delEmailArr)){
  255. $insertData[] = $data;
  256. }
  257. }
  258. }
  259. $insert = $up = true;
  260. if(count($insertData) > 0){
  261. $insert = $admin->addpl_customer($insertData);
  262. }
  263. $info = "导入成功";
  264. }
  265. header("Location:/?a=customer_list_admin&m=index&info=$info");
  266. exit();
  267. }
  268. }
  269. //导出excel
  270. function generateEXCEL($filename = 'report',$data = ''){
  271. //引入PHPExcel相关文件
  272. require_once( ONU_ROOT . 'phpexcel/PHPExcel.php');
  273. require_once( ONU_ROOT . 'phpexcel/PHPExcel/IOFactory.php');
  274. require_once( ONU_ROOT . 'phpexcel/PHPExcel/Writer/Excel5.php');
  275. //新建
  276. $resultPHPExcel = new PHPExcel();
  277. //设置参数
  278. //设值
  279. $resultPHPExcel->getActiveSheet()->setCellValue('A1', '客户名');
  280. $resultPHPExcel->getActiveSheet()->setCellValue('B1', '客户邮箱');
  281. $resultPHPExcel->getActiveSheet()->setCellValue('C1', '手机号');
  282. $resultPHPExcel->getActiveSheet()->setCellValue('D1', '国家');
  283. $resultPHPExcel->getActiveSheet()->setCellValue('E1', '城市 ');
  284. $resultPHPExcel->getActiveSheet()->setCellValue('F1', '所属分组');
  285. //设置水平居中
  286. $resultPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  287. $resultPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  288. $resultPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  289. $resultPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  290. $resultPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  291. $resultPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  292. //设置水平居中
  293. $resultPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  294. $resultPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  295. $resultPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  296. $resultPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  297. $resultPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  298. $resultPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  299. $i = 2;
  300. foreach($data as $item){
  301. $resultPHPExcel->getActiveSheet()->setCellValue('A' . $i, $item['uname']);
  302. $resultPHPExcel->getActiveSheet()->setCellValue('B' . $i, $item['email']);
  303. $resultPHPExcel->getActiveSheet()->setCellValue('C' . $i, $item['tel']);
  304. $resultPHPExcel->getActiveSheet()->setCellValue('D' . $i, $item['country']);
  305. $resultPHPExcel->getActiveSheet()->setCellValue('E' . $i, $item['city']);
  306. $resultPHPExcel->getActiveSheet()->setCellValue('F' . $i, $item['group_name']);
  307. //设置单元格宽度
  308. $resultPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(40);
  309. $resultPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
  310. $resultPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
  311. $resultPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
  312. $resultPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
  313. $resultPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(30);
  314. //设置水平居中
  315. $resultPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  316. $resultPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  317. $resultPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  318. $resultPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  319. $resultPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  320. $resultPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  321. //设置行高度
  322. $resultPHPExcel->getActiveSheet()->getRowDimension("1")->setRowHeight(20);
  323. $resultPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(20);
  324. $i ++;
  325. }
  326. //设置导出文件名
  327. $xlsWriter = new PHPExcel_Writer_Excel5($resultPHPExcel);
  328. ob_end_clean();//清除缓冲区,避免乱码
  329. //ob_start(); ob_flush();
  330. header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
  331. header("Content-Type: application/force-download");
  332. header("Content-Type: application/octet-stream");
  333. header("Content-Type: application/download");
  334. //header('Content-Disposition:inline;filename="'.$outputFileName.'"');
  335. header('Content-Disposition:attachment;filename='.'"'.$filename.'.xls"'); //可以对文件名进行处理
  336. header("Content-Transfer-Encoding: binary");
  337. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  338. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  339. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  340. header("Pragma: no-cache");
  341. $xlsWriter->save('php://output');
  342. exit;
  343. }
  344. //导入Excel文件
  345. function uploadFile($file,$filetempname)
  346. {
  347. //自己设置的上传文件存放路径
  348. $filePath = 'static/upload/';
  349. $str = "";
  350. //下面的路径按照你PHPExcel的路径来修改
  351. require_once ONU_ROOT . 'phpexcel/PHPExcel.php';
  352. require_once ONU_ROOT . 'phpexcel/PHPExcel/IOFactory.php';
  353. require_once ONU_ROOT . 'phpexcel/PHPExcel/Reader/Excel5.php';
  354. //require_once ONU_ROOT . 'phpexcel/PHPExcel/Reader/CSV.php';
  355. //注意设置时区
  356. $time=date("y-m-d-H-i-s");//去当前上传的时间
  357. //获取上传文件的扩展名
  358. $extend=strrchr ($file,'.');
  359. //上传后的文件名
  360. $name=$time.$extend;
  361. $uploadfile=$filePath.$name;//上传后的文件名地址
  362. //move_uploaded_file() 函数将上传的文件移动到新位置。若成功,则返回 true,否则返回 false。
  363. $result=move_uploaded_file($filetempname,$uploadfile);//假如上传到当前目录下
  364. //echo $result;
  365. if($result) //如果上传文件成功,就执行导入excel操作
  366. {
  367. $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
  368. $objPHPExcel = $objReader->load($uploadfile);
  369. $sheet = $objPHPExcel->getSheet(0);
  370. $highestRow = $sheet->getHighestRow(); //取得总行数
  371. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  372. /* 第二种方法*/
  373. $objWorksheet = $objPHPExcel->getActiveSheet();
  374. $highestRow = $objWorksheet->getHighestRow();
  375. echo 'highestRow='.$highestRow;
  376. echo "<br>";
  377. $highestColumn = $objWorksheet->getHighestColumn();
  378. $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
  379. echo 'highestColumnIndex='.$highestColumnIndex;
  380. echo "<br>";
  381. $headtitle=array();
  382. //获取到的excel数据
  383. $excel_array = array();
  384. for ($row = 1;$row <= $highestRow;$row++)
  385. {
  386. $strs=array();
  387. //注意highestColumnIndex的列数索引从0开始
  388. for ($col = 0;$col < $highestColumnIndex;$col++)
  389. {
  390. $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
  391. if($strs[$col] instanceof PHPExcel_RichText){ //富文本转换字符串
  392. $strs[$col] = $strs[$col]->__toString();
  393. }
  394. //第七个数字是日期型的
  395. // if($col==7){
  396. // $strs[$col]=gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($strs[$col]));
  397. // }
  398. }
  399. array_push($excel_array, $strs);
  400. }
  401. }
  402. else
  403. {
  404. $excel_array['error'] = "导入失败!";
  405. }
  406. return $excel_array;
  407. }
  408. // class end
  409. }
  410. ?>