ImportMemberService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\shop\service;
  3. use app\common\model\user\User;
  4. use app\common\model\user\Grade;
  5. use app\common\model\user\GradeLog;
  6. use PhpOffice\PhpSpreadsheet\IOFactory;
  7. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  8. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  9. use think\facade\Validate;
  10. /**
  11. * 订单导出服务类
  12. */
  13. class ImportMemberService
  14. {
  15. function importExcel1($file = '', $sheet = 0)
  16. {
  17. try {
  18. /* 转码 */
  19. $file = iconv("utf-8", "gb2312", $file);
  20. if (empty($file) OR !file_exists($file)) {
  21. return ['code' => 1 ,'message' => '文件不存在!'];
  22. }
  23. /** @var Xlsx $objRead */
  24. $objRead = IOFactory::createReader('Xlsx');
  25. if (!$objRead->canRead($file)) {
  26. /** @var Xls $objRead */
  27. $objRead = IOFactory::createReader('Xls');
  28. if (!$objRead->canRead($file)) {
  29. return ['code' => 1 ,'message' => '只支持导入Excel文件!'];
  30. }
  31. }
  32. $file_size = $_FILES['iFile']['size'];
  33. if ($file_size > 5 * 1024 * 1024) {
  34. return ['code' => 1 ,'message' => '文件大小不能超过5M'];
  35. }
  36. /* 建立excel对象 */
  37. $objPHPExcel = $objRead->load($file);
  38. $sheet = $objPHPExcel->getSheet(0); //excel中的第一张sheet
  39. $highestRow = $sheet->getHighestRow(); // 取得总行数
  40. $highestColumn = $sheet->getHighestColumn(); // 取得总列数
  41. \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
  42. $lines = $highestRow -1;
  43. if($lines <= 0){
  44. return ['code' => 1 ,'message' => 'excel表格,没有数据'];
  45. }
  46. if($lines > 1000){
  47. return ['code' => 1 ,'message' => '单次最多添加1000个会员'];
  48. }
  49. $uservall = [];
  50. //循环读取excel表格,整合成数组。如果是不指定key的二维,就用$data[i][j]表示。
  51. for ($j = 2; $j <= $highestRow; $j++) {
  52. $mobile = trim($objPHPExcel->getActiveSheet()->getCell('D' . $j)->getValue());
  53. $mobile = preg_replace('/^[(\xc2\xa0)|\s]+/','', $mobile);
  54. // if (empty($mobile)) {
  55. // return ['code' => 1 ,'message' => '第'.$j.'行,请填写手机号'];
  56. // }
  57. // if (!Validate::regex($mobile,"/^1[3456789]{1}\d{9}$/")) {
  58. // return ['code' => 1 ,'message' => '第'.$j.'行,手机号格式错误!'];
  59. // }
  60. $nickname = trim($objPHPExcel->getActiveSheet()->getCell("A" . $j)->getValue());
  61. $nickname = preg_replace('/^[(\xc2\xa0)|\s]+/','', $nickname);
  62. if (empty($nickname)) {
  63. return ['code' => 1 ,'message' => '第'.$j.'行,请填写会员昵称/姓名'];
  64. }
  65. // if (User::detail(['mobile'=> $mobile])) {
  66. // return ['code' => 1 ,'message' => '第'.$j.'行,'.$mobile.'手机号已存在!'];
  67. // }
  68. // $password = trim($objPHPExcel->getActiveSheet()->getCell("B" . $j)->getValue());
  69. // $password = preg_replace('/^[(\xc2\xa0)|\s]+/','', $password);
  70. // if (empty($password)) {
  71. // return ['code' => 1 ,'message' => '第'.$j.'行,请填写会员密码'];
  72. // }
  73. $user_no = trim($objPHPExcel->getActiveSheet()->getCell("C" . $j)->getValue());
  74. $user_no = preg_replace('/^[(\xc2\xa0)|\s]+/','', $user_no);
  75. if (empty($user_no)) {
  76. return ['code' => 1 ,'message' => '第'.$j.'行,请填写会员号'];
  77. }
  78. if (User::detail(['user_no'=> $user_no])) {
  79. return ['code' => 1 ,'message' => '第'.$j.'行,'.$user_no.'会员号已存在!'];
  80. }
  81. $uservall[] = [
  82. 'nickName' => $nickname??'',
  83. 'mobile' => $mobile??'',
  84. 'gender' => 0,
  85. 'grade_id' => 2,
  86. 'user_no' => $user_no,
  87. 'avatarUrl' => 'http://cni-ekshop.cni.com.cn/image/agent/user_head.png',
  88. 'password' => md5(123456),
  89. 'app_id' => 10001,
  90. 'reg_source' => 'mp',
  91. 'create_time' => time(),
  92. ];
  93. }
  94. if (!empty($uservall)) {
  95. (new user)->saveAll($uservall);
  96. }
  97. return ['code' => 0 ,'message' => '操作成功'];
  98. } catch (\Exception $e) {
  99. throw $e;
  100. }
  101. }
  102. }