374 lines
17 KiB
PHP
374 lines
17 KiB
PHP
<?php
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
require_once APPPATH . 'controllers/api/BaseController.php';
|
|
require_once COMMPATH . '/third_party/PHPExcel/IOFactory.php';
|
|
|
|
|
|
class User extends BaseController
|
|
{
|
|
const DEFAULT_ORG_TEAM_ID = 3; //默认机构团队id
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->load->model('agent/pingan/pingan_users_model', 'pinganUsers');
|
|
$this->load->model('agent/organization/Organization_model', 'mdOrganization');
|
|
$this->load->model('area_model');
|
|
$this->load->model('agent/pingan/Pingan_sys_role_model', 'mdSysRole');
|
|
}
|
|
|
|
public function page_get()
|
|
{
|
|
$page = $this->input_param('page');
|
|
$limit = $this->input_param('limit');
|
|
$username = $this->input_param('username');
|
|
$mobile = $this->input_param('mobile');
|
|
$orgId = $this->input_param('orgId');
|
|
$teamId = $this->input_param('teamId');
|
|
$groupType = $this->input_param('groupType');
|
|
$centerNumber = $this->input_param('centerNumber');
|
|
$orgName = $this->input_param('orgName');
|
|
$citys = $this->input_param('citys');
|
|
$sort = $this->input_param('sort');
|
|
$order = $this->input_param('order');
|
|
!$page && $page = 1;
|
|
!$limit && $limit = 10;
|
|
$sort_order = 'id desc';
|
|
if ($sort && $order) {
|
|
$sort_order = $sort . ' ' . $order;
|
|
}
|
|
$where = $list = [];
|
|
$where['status>='] = 0;
|
|
$username && $where["username like '%{$username}%'"] = null;
|
|
$mobile && $where["mobile like '%{$mobile}%'"] = null;
|
|
$orgId && $where['orgId'] = $orgId;
|
|
$teamId && $where['orgTeamId'] = $teamId;
|
|
$centerNumber && $where['centerNumber'] = $centerNumber;
|
|
strlen($groupType) && $where['groupType'] = $groupType;
|
|
$citys && $where['cityId'] = $citys[1];
|
|
$orgName && $where['orgName'] = $orgName;
|
|
$count = $this->pinganUsers->count($where);
|
|
if ($count) {
|
|
$res = $this->pinganUsers->select($where, $sort_order, $page, $limit);
|
|
$orgId = array_column($res, 'orgId');
|
|
$orgTeamId = array_column($res, 'orgTeamId');
|
|
$orgIdArray = array_merge($orgId, $orgTeamId);
|
|
$orgMap = $this->mdOrganization->get_map_by_ids($orgIdArray, '', 'id', 'title');
|
|
$areaIdArray = array_column($res, 'areaId');
|
|
$depIdArray = array_column($res, 'depId');
|
|
$teamIdArray = array_column($res, 'teamId');
|
|
$userIdArray = array_merge($areaIdArray, $depIdArray, $teamIdArray);
|
|
$userIds = implode(',', $userIdArray);
|
|
$mapUsers = [];
|
|
if ($userIds) {
|
|
$where = ["id in ({$userIds})" => null];
|
|
$mapUsers = $this->pinganUsers->map('id', '', $where, '', 0, 0, 'id,username,userCode');
|
|
}
|
|
foreach ($res as $v) {
|
|
$status = intval($v['status']);
|
|
// $orgName = $orgMap[$v['orgId']] ?: '';
|
|
$orgTeamName = $orgMap[$v['orgTeamId']] ?: '';
|
|
$city = $this->area_model->get(['city_id' => $v['cityId']]);
|
|
$areaUser = $mapUsers[$v['areaId']] ? $mapUsers[$v['areaId']][0] : [];
|
|
$depUser = $mapUsers[$v['depId']] ? $mapUsers[$v['depId']][0] : [];
|
|
$teamUser = $mapUsers[$v['teamId']] ? $mapUsers[$v['teamId']][0] : [];
|
|
$list[] = [
|
|
'id' => $v['id'], 'mobile' => $v['mobile'],
|
|
'username' => $v['username'], 'userCode' => $v['userCode'],
|
|
'status' => $status, 'updateTime' => $v['updateTime'],
|
|
'orgName' => $v['orgName'] ?: '', 'orgTeamName' => $orgTeamName,
|
|
'typeCn' => Pingan_users_model::TYPE_CN[$v['groupType']],
|
|
'groupType' => $v['groupType'], 'cityName' => $city['city_name'],
|
|
'centerNumber' => Pingan_users_model::TYPE_CENTER[$v['centerNumber']],
|
|
'areaUser' => $areaUser, 'depUser' => $depUser, 'teamUser' => $teamUser
|
|
];
|
|
}
|
|
}
|
|
$date = ['list' => $list, 'count' => $count];
|
|
$this->return_response_list($date);
|
|
}
|
|
|
|
public function groupType_get()
|
|
{
|
|
$res = Pingan_users_model::TYPE_CN;
|
|
$this->return_response($res);
|
|
}
|
|
|
|
public function center_get()
|
|
{
|
|
$res = Pingan_users_model::TYPE_CENTER;
|
|
$this->return_response($res);
|
|
}
|
|
|
|
public function orgNameList_get()
|
|
{
|
|
$this->return_response(Pingan_users_model::orgNameList);
|
|
}
|
|
|
|
public function index_put()
|
|
{
|
|
$id = $this->input_param('id');
|
|
$username = $this->input_param('username');
|
|
$mobile = $this->input_param('mobile');
|
|
$groupType = intval($this->input_param('groupType'));
|
|
$row = $this->pinganUsers->get(['id' => $id]);
|
|
if (!$row) {
|
|
$this->return_json('参数错误');
|
|
}
|
|
if ($mobile != $row['mobile']) {
|
|
$mobileRow = $this->pinganUsers->get(['mobile' => $mobile, 'status>=' => 0]);
|
|
if ($mobileRow) {
|
|
$this->return_json('手机号已存在');
|
|
}
|
|
}
|
|
$upDate = ['username' => $username, 'mobile' => $mobile, 'groupType' => $groupType];
|
|
$this->pinganUsers->update($upDate, ['id' => $id]);
|
|
$this->return_response();
|
|
}
|
|
|
|
public function index_post()
|
|
{
|
|
$username = $this->input_param('username');
|
|
$nickname = $this->input_param('nickname');
|
|
$phone = $this->input_param('phone');
|
|
$sex = $this->input_param('sex');
|
|
$birthday = $this->input_param('birthday');
|
|
$introduction = $this->input_param('introduction');
|
|
$email = $this->input_param('email');
|
|
$roleIds = $this->input_param('roleIds');
|
|
$password = $this->input_param('password');
|
|
if (!$username) {
|
|
$this->return_json('请输入用户帐户');
|
|
}
|
|
if (!$nickname) {
|
|
$this->return_json('请输入用户名');
|
|
}
|
|
if (!$sex) {
|
|
$this->return_json('请选择性别');
|
|
}
|
|
if (!$password) {
|
|
$this->return_json('请输入登录密码');
|
|
}
|
|
if (!$roleIds) {
|
|
$this->return_json('请选择角色');
|
|
}
|
|
$re = $this->mdSysAdmin->get(['username' => $username]);
|
|
if ($re) {
|
|
$this->return_json('用户帐号已存在');
|
|
}
|
|
$roleId = intval($roleIds[0]);
|
|
$password = password_hash($password, PASSWORD_BCRYPT);
|
|
$addDate = ['username' => $username, 'nickname' => $nickname, 'phone' => $phone, 'sex' => $sex, 'birthday' => $birthday
|
|
, 'password' => $password, 'introduction' => $introduction, 'email' => $email, 'roleId' => $roleId, 'createTime' => date('Y-m-d H:i:s')];
|
|
$id = $this->mdSysAdmin->add($addDate);
|
|
if (!$id) {
|
|
$this->return_json('添加用户失败');
|
|
}
|
|
$this->return_response();
|
|
}
|
|
|
|
public function status_put()
|
|
{
|
|
$id = $this->input_param('id');
|
|
$status = $this->input_param('status');
|
|
if (!$id) {
|
|
$this->return_json('参数错误');
|
|
}
|
|
$this->pinganUsers->update(['status' => $status], ['id' => $id]);
|
|
$this->return_response();
|
|
}
|
|
|
|
|
|
public function index_delete($id = null)
|
|
{
|
|
if (!$id) {
|
|
$this->return_json('参数错误');
|
|
}
|
|
$this->pinganUsers->update(['status' => -1], ['id' => $id]);
|
|
$this->return_response();
|
|
}
|
|
|
|
|
|
public function batch_delete()
|
|
{
|
|
$ids = $this->input_param('ids');
|
|
if (!$ids) {
|
|
$this->return_json('参数错误');
|
|
}
|
|
$str_ids = implode(',', $ids);
|
|
if ($str_ids) {
|
|
$this->pinganUsers->update(['status' => -1], ["id in($str_ids)" => null]);
|
|
}
|
|
$this->return_response();
|
|
}
|
|
|
|
public function import_post()
|
|
{
|
|
$log_file = 'import_user.log';
|
|
try {
|
|
$allowedExtensions = ['xlsx', 'xls'];
|
|
$file = $_FILES['file'];
|
|
if (!$file) {
|
|
throw new Exception('请选择文件');
|
|
}
|
|
if (!$file['tmp_name']) {//太大的图片上传,这个参数会变成空的
|
|
throw new Exception('参数错误');
|
|
}
|
|
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
|
|
if (!in_array($fileExtension, $allowedExtensions)) {
|
|
throw new Exception('无效的文件类型!请上传.xlsx或.xls格式的Excel文件');
|
|
}
|
|
$uploadDir = FCPATH . 'temp' . DIRECTORY_SEPARATOR . date('Ymd');
|
|
if (!file_exists($uploadDir)) {
|
|
mkdir($uploadDir, 0777, true);
|
|
}
|
|
$filePath = $uploadDir . DIRECTORY_SEPARATOR . uniqid() . '_' . $file['name'];
|
|
if (!move_uploaded_file($file['tmp_name'], $filePath)) {
|
|
throw new Exception('保存上传文件失败');
|
|
}
|
|
// 根据文件扩展名选择合适的读取器
|
|
if ($fileExtension === 'xlsx') {
|
|
$reader = new PHPExcel_Reader_Excel2007();
|
|
} else {
|
|
$reader = new PHPExcel_Reader_Excel5();
|
|
}
|
|
if (!file_exists($filePath)) {
|
|
throw new Exception('文件不存在');
|
|
}
|
|
$PHPExcel = $reader->load($filePath); // 文档名称
|
|
$objWorksheet = $PHPExcel->getActiveSheet();
|
|
$rowCnt = $objWorksheet->getHighestRow(); //获取总行数
|
|
$col_max = $objWorksheet->getHighestColumn();//最大列
|
|
$data = [];
|
|
$successNum = 0; //新增用户数量
|
|
$updateNum = 0; //更新用户数量
|
|
$orgTeamId = $_SESSION['orgTeamId'] ?: self::DEFAULT_ORG_TEAM_ID;
|
|
$org = $this->mdOrganization->get(['id' => $orgTeamId]);
|
|
for ($i = 2; $i <= $rowCnt; $i++) { //读取内容
|
|
$name = $objWorksheet->getCell('A' . $i)->getValue();
|
|
$userCode = $objWorksheet->getCell('B' . $i)->getValue();
|
|
$teamName = $objWorksheet->getCell('C' . $i)->getValue();
|
|
$teamCode = $objWorksheet->getCell('D' . $i)->getValue();
|
|
$depName = $objWorksheet->getCell('E' . $i)->getValue();
|
|
$depCode = $objWorksheet->getCell('F' . $i)->getValue();
|
|
$areaName = $objWorksheet->getCell('G' . $i)->getValue();
|
|
$areaCode = $objWorksheet->getCell('H' . $i)->getValue();
|
|
$number = $objWorksheet->getCell('I' . $i)->getValue();
|
|
$cityId = $objWorksheet->getCell('J' . $i)->getValue();
|
|
$cityName = $objWorksheet->getCell('K' . $i)->getValue();
|
|
$groupType = $objWorksheet->getCell('L' . $i)->getValue();
|
|
$importData = [
|
|
$name, $userCode, $teamName, $teamCode, $depName, $depCode,
|
|
$areaName, $areaCode, $number, $cityId, $cityName, $groupType
|
|
];
|
|
debug_log("导入数据:" . json_encode($importData, JSON_UNESCAPED_UNICODE), $log_file);
|
|
$centerNumber = Pingan_users_model::TYPE_CENTER_CODE[$number] ?: '';
|
|
$cityRow = $this->area_model->get(["city_name like '%{$cityId}%'" => null]);
|
|
$teamUserId = $depUserId = $areaUserId = '';
|
|
$role = $this->mdSysRole->map('roleCode', 'roleId', ['status' => 0], '', '', '', 'roleId,roleCode');
|
|
if ($areaCode) { //获取区域用户id
|
|
$areaRow = $this->pinganUsers->get(['userCode' => $areaCode]);
|
|
if ($areaRow) {
|
|
$areaUserId = $areaRow['id'];
|
|
} else {
|
|
$areaAddData = [
|
|
'username' => $areaName,
|
|
'userCode' => $areaCode,
|
|
'groupType' => Pingan_users_model::GROUP_TYPE_AREA,
|
|
'orgTeamId' => $orgTeamId,
|
|
'centerNumber' => $centerNumber,
|
|
'orgName' => $cityName
|
|
];
|
|
$org && $areaAddData['orgId'] = $org['parentId'];
|
|
$cityRow && $areaAddData['cityId'] = $cityRow['city_id'];
|
|
$cityRow && $areaAddData['provinceId'] = $cityRow['province_id'];
|
|
$roleCode = "role-{$areaAddData['groupType']}";
|
|
$role[$roleCode] && $areaAddData['roleId'] = $role[$roleCode];
|
|
$areaUserId = $this->pinganUsers->add($areaAddData);
|
|
}
|
|
}
|
|
if ($depCode) { //获取部门用户uid
|
|
$depRow = $this->pinganUsers->get(['userCode' => $depCode]);
|
|
if ($depRow) {
|
|
$depUserId = $areaRow['id'];
|
|
} else {
|
|
$depAddData = [
|
|
'username' => $depName,
|
|
'userCode' => $depCode,
|
|
'groupType' => Pingan_users_model::GROUP_TYPE_DEP,
|
|
'orgTeamId' => $orgTeamId,
|
|
'centerNumber' => $centerNumber,
|
|
'orgName' => $cityName
|
|
];
|
|
$org && $depAddData['orgId'] = $org['parentId'];
|
|
$cityRow && $depAddData['cityId'] = $cityRow['city_id'];
|
|
$cityRow && $depAddData['provinceId'] = $cityRow['province_id'];
|
|
$areaUserId && $depAddData['areaId'] = $areaUserId;
|
|
$roleCode = "role-{$depAddData['groupType']}";
|
|
$role[$roleCode] && $depAddData['roleId'] = $role[$roleCode];
|
|
$depUserId = $this->pinganUsers->add($depAddData);
|
|
}
|
|
}
|
|
if ($teamCode) { //获取团队用户id
|
|
$teamRow = $this->pinganUsers->get(['userCode' => $teamCode]);
|
|
if ($teamRow) {
|
|
$teamUserId = $areaRow['id'];
|
|
} else {
|
|
$teamAddData = [
|
|
'username' => $teamName,
|
|
'userCode' => $teamCode,
|
|
'groupType' => Pingan_users_model::GROUP_TYPE_TEAM,
|
|
'orgTeamId' => $orgTeamId,
|
|
'centerNumber' => $centerNumber,
|
|
'orgName' => $cityName
|
|
];
|
|
$org && $teamAddData['orgId'] = $org['parentId'];
|
|
$cityRow && $teamAddData['cityId'] = $cityRow['city_id'];
|
|
$cityRow && $teamAddData['provinceId'] = $cityRow['province_id'];
|
|
$areaUserId && $teamAddData['areaId'] = $areaUserId;
|
|
$depUserId && $teamAddData['depId'] = $depUserId;
|
|
$roleCode = "role-{$teamAddData['groupType']}";
|
|
$role[$roleCode] && $teamAddData['roleId'] = $role[$roleCode];
|
|
$teamUserId = $this->pinganUsers->add($teamAddData);
|
|
}
|
|
}
|
|
$userData = [
|
|
'username' => $name,
|
|
'userCode' => $userCode,
|
|
'groupType' => $groupType,
|
|
'orgTeamId' => $orgTeamId,
|
|
'centerNumber' => $centerNumber,
|
|
'orgName' => $cityName
|
|
];
|
|
$org && $userData['orgId'] = $org['parentId'];
|
|
$cityRow && $userData['cityId'] = $cityRow['city_id'];
|
|
$cityRow && $userData['provinceId'] = $cityRow['province_id'];
|
|
$areaUserId && $userData['areaId'] = $areaUserId;
|
|
$depUserId && $userData['depId'] = $depUserId;
|
|
$teamUserId && $userData['teamId'] = $teamUserId;
|
|
$roleCode = "role-{$userData['groupType']}";
|
|
$role[$roleCode] && $userData['roleId'] = $role[$roleCode];
|
|
$user = $this->pinganUsers->get(['userCode' => $userCode]);
|
|
if ($user) {
|
|
debug_log("更新数据:" . json_encode($userData, JSON_UNESCAPED_UNICODE), $log_file);
|
|
$result = $this->pinganUsers->update($userData, ['id' => $user['id']]);
|
|
debug_log("更新结果:" . $result, $log_file);
|
|
if (is_numeric($result) && $result) {
|
|
$updateNum += 1;
|
|
}
|
|
} else {
|
|
debug_log("新增数据:" . json_encode($userData, JSON_UNESCAPED_UNICODE), $log_file);
|
|
$result = $this->pinganUsers->add($userData);
|
|
if ($result) {
|
|
$successNum += 1;
|
|
}
|
|
}
|
|
}
|
|
$message = "新增{$successNum}条数据,更新{$updateNum}";
|
|
$this->return_response([], $message);
|
|
} catch (Exception $e) {
|
|
$this->return_json($e->getMessage());
|
|
}
|
|
|
|
}
|
|
} |