Browse Source

导入用户信息优化

master
wanghongjun 8 months ago
parent
commit
292571bb9c
  1. 31
      app/controller/admin/WechatUser.php
  2. 2
      app/service/user/UserService.php

31
app/controller/admin/WechatUser.php

@ -5,6 +5,7 @@ namespace app\controller\admin;
use app\model\WechatUser as WechatUserModel; use app\model\WechatUser as WechatUserModel;
use app\service\ExportService; use app\service\ExportService;
use app\service\user\UserService; use app\service\user\UserService;
use app\service\webService\FeeService;
use think\Response; use think\Response;
class WechatUser extends Base class WechatUser extends Base
@ -71,6 +72,7 @@ class WechatUser extends Base
} }
$max_size = '10'; $max_size = '10';
$size = ((int)$max_size * 1024 * 1024);
$mimetype = 'xls,xlsx'; $mimetype = 'xls,xlsx';
$fileInfo = []; $fileInfo = [];
$fileInfo['name'] = $file->getOriginalName(); //上传文件名 $fileInfo['name'] = $file->getOriginalName(); //上传文件名
@ -78,8 +80,8 @@ class WechatUser extends Base
$fileInfo['tmp_name'] = $file->getPathname(); $fileInfo['tmp_name'] = $file->getPathname();
$fileInfo['size'] = $file->getSize(); $fileInfo['size'] = $file->getSize();
if ($fileInfo['size'] > $max_size) { if ($fileInfo['size'] > $size) {
throw new \Exception('文件超过配置的最大值' . $max_size . 'M'); throw new \Exception('文件超过配置的最大值' . $size . 'M');
} }
@ -124,7 +126,9 @@ class WechatUser extends Base
$excel_array = $obj_PHPExcel->getSheet(0)->toArray(); $excel_array = $obj_PHPExcel->getSheet(0)->toArray();
array_shift($excel_array); array_shift($excel_array);
foreach ($excel_array as $data) { $error_array = [];
foreach ($excel_array as $key => $data) {
$key = $key + 1;
// 验证不能为空 // 验证不能为空
$is_empty = false; $is_empty = false;
foreach ($data as $value) { foreach ($data as $value) {
@ -142,24 +146,45 @@ class WechatUser extends Base
$taxId = $data[3]; $taxId = $data[3];
$type = $data[4]; $type = $data[4];
if (!preg_match('/^1[3-9]\d{9}$/', $phone)) { if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
$error_array[] = "第{$key}行:手机号格式不符合";
continue; continue;
} }
$FeeService = new FeeService($pucode);
$feeUserInfo = $FeeService->getUsers();
if ($feeUserInfo['UserCode'] != $pucode) {
$error_array[] = "第{$key}行:用户编号不存在";
continue;
}
if ($feeUserInfo['CellPhone'] != $phone) {
$error_array[] = "第{$key}行:手机号或用户编号错误";
continue;
}
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$error_array[] = "第{$key}行:邮箱格式不符合";
continue; continue;
} }
$str = '0-9A-HJ-NPQRTUWXY'; $str = '0-9A-HJ-NPQRTUWXY';
$pattern = '/^[' . $str . ']{2}\d{6}[' . $str . ']{10}$/'; $pattern = '/^[' . $str . ']{2}\d{6}[' . $str . ']{10}$/';
if (!preg_match($pattern, $taxId)) { if (!preg_match($pattern, $taxId)) {
$error_array[] = "第{$key}行:纳税人编号格式不符合";
continue; continue;
} }
$typeArr = ['单位', '个人']; $typeArr = ['单位', '个人'];
if (!in_array($type, $typeArr)) { if (!in_array($type, $typeArr)) {
$error_array[] = "第{$key}行:抬头类型不符合";
continue; continue;
} }
$type = array_search($type, $typeArr); $type = array_search($type, $typeArr);
UserService::addUser($phone, $email, $pucode, $taxId, $type); UserService::addUser($phone, $email, $pucode, $taxId, $type);
} }
if (!empty($error_array)) {
throw new \Exception(implode('\n', $error_array));
}
return $this->buildSuccess(); return $this->buildSuccess();
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->buildFailed(400, $e->getMessage()); return $this->buildFailed(400, $e->getMessage());

2
app/service/user/UserService.php

@ -65,7 +65,7 @@ class UserService extends BaseService
if (!$query) { if (!$query) {
$save = [ $save = [
'nick_name' => '导入用户', 'nickname' => '导入用户',
'phone' => $phone, 'phone' => $phone,
'email' => $email 'email' => $email
]; ];

Loading…
Cancel
Save