发票管理apiadmin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

192 lines
6.7 KiB

<?php
namespace app\controller\admin;
use app\model\WechatUser as WechatUserModel;
use app\service\ExportService;
use app\service\user\UserService;
use app\service\webService\FeeService;
use think\Response;
class WechatUser extends Base
{
/**
* 获取微信用户列表
* @return Response
* @throws \think\db\exception\DbException
*/
public function index(): Response
{
$param = $this->request->get();
$limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
$start = $this->request->get('page', 1);
$where = [];
if (isset($param['key']) && !empty($param['key'])) {
$where[] = ['phone', 'like', "%{$param['key']}%"];
}
$obj = new WechatUserModel();
$listObj = $obj->order('create_time', 'DESC')
->where($where)
->field('id,openid,headimgurl,phone,create_time,status,last_login_time')
->paginate(['page' => $start, 'list_rows' => $limit])
->each(function ($item, $key) {
$item->last_login_time = $item->last_login_time ? date("Y-m-d H:i:s", $item->last_login_time) : '';
})->toArray();
$listInfo = $listObj['data'];
return $this->buildSuccess([
'list' => $listInfo,
'count' => $listObj['total']
]);
}
/**
* 下载导入模板
* @return void
* @throws \PHPExcel_Exception
* @throws \PHPExcel_Reader_Exception
* @throws \PHPExcel_Writer_Exception
*/
public function exportTemplate()
{
$name = '导入用户信息模板';
$header = ['手机号', '邮箱编号', '用户编号', '纳税人编号', '抬头类型(单位、个人)'];
ExportService::excelExport($name, $header);
}
/**
* 导入用户
* @return Response
*/
public function importUser():Response
{
try {
$file = $this->request->file('file');
if (empty($file)) {
throw new \Exception('没有上传的文件');
}
$max_size = '10';
$size = ((int)$max_size * 1024 * 1024);
$mimetype = 'xls,xlsx';
$fileInfo = [];
$fileInfo['name'] = $file->getOriginalName(); //上传文件名
$fileInfo['type'] = $file->getOriginalMime(); //上传文件类型信息
$fileInfo['tmp_name'] = $file->getPathname();
$fileInfo['size'] = $file->getSize();
if ($fileInfo['size'] > $size) {
throw new \Exception('文件超过配置的最大值' . $size . 'M');
}
$suffix = strtolower(
pathinfo($fileInfo['name'], PATHINFO_EXTENSION)
);
$suffix = $suffix && preg_match('/^[a-zA-Z0-9]+$/', $suffix)
? $suffix : 'file';
$mimetypeArr = explode(',', strtolower($mimetype));
$typeArr = explode('/', $fileInfo['type']);
//禁止上传PHP和HTML文件
if (
in_array($fileInfo['type'], ['text/x-php', 'text/html'])
|| in_array($suffix, ['php', 'html', 'htm'])
) {
throw new \Exception('该文件类型不可上传');
}
//验证文件后缀
if ((!in_array($suffix, $mimetypeArr)
|| (stripos($typeArr[0] . '/', $mimetype) !== false
&& !in_array($fileInfo['type'], $mimetypeArr)
&& !in_array($typeArr[0] . '/*', $mimetypeArr)
))
) {
throw new \Exception('该文件类型不可上传');
}
switch($suffix){
case 'xls':
$obj = \PHPExcel_IOFactory::createReader('Excel5');
break;
case 'xlsx':
$obj = \PHPExcel_IOFactory::createReader('Excel2007');
break;
default:
throw new \Exception('该文件类型不可上传');
}
$obj_PHPExcel = $obj->load($fileInfo['tmp_name']);
$excel_array = $obj_PHPExcel->getSheet(0)->toArray();
array_shift($excel_array);
set_time_limit(0); // 无时间限制
ini_set('memory_limit', '512M'); // 增加内存限制
$error_array = [];
foreach ($excel_array as $key => $data) {
$key = $key + 1;
$phone = $data[0];
$email = $data[1] ?? '';
$pucode = $data[2];
$taxId = $data[3] ?? '';
$type = $data[4];
// 验证不能为空
if (empty($phone) || empty($pucode) || empty($type)) {
continue;
}
if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
$error_array[] = "{$key}行:手机号格式不符合";
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 ($email) {
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$error_array[] = "{$key}行:邮箱格式不符合";
continue;
}
}
if ($taxId) {
$str = '0-9A-HJ-NPQRTUWXY';
$pattern = '/^[' . $str . ']{2}\d{6}[' . $str . ']{10}$/';
if (!preg_match($pattern, $taxId)) {
$error_array[] = "{$key}行:纳税人编号格式不符合";
continue;
}
}
$typeArr = ['单位', '个人'];
if (!in_array($type, $typeArr)) {
$error_array[] = "{$key}行:抬头类型不符合";
continue;
}
$type = array_search($type, $typeArr);
UserService::addUser($phone, $email, $pucode, $taxId, $type);
}
if (!empty($error_array)) {
throw new \Exception(implode('\n', $error_array));
}
return $this->buildSuccess();
} catch (\Exception $e) {
return $this->buildFailed(400, $e->getMessage());
}
}
}