Files
oms/back/application/api/controller/User.php
2026-03-18 14:02:51 +08:00

510 lines
17 KiB
PHP

<?php
namespace app\api\controller;
use DateTime;
use think\Controller;
use think\Db;
use think\Request;
use think\Cache;
use think\Log;
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");
class User extends Common
{
public function login()
{
$param = request()->param();
$res = db('system_user')->where('user_idcard', $param['idcard'])->where('del_time',null)->find();
if ($res == null)
{
return $this->sendError(40001,"用户未注册,请联系管理员",null);
}
if ($res != null && md5($param['password']) != $res['user_pw'])
{
return $this->sendError(40001,"密码错误,请重新输入",null);
}
//分类查找用户详细信息
$ret = [];
//客户
if (1 == $res['user_type'])
{
$ret = db('hr_staff')->where('staff_idcard', $param['idcard'])
->field('staff_id,staff_name as name, staff_idcard as idcard')
->find();
if ($ret != null)
{
$ret['company'] = "上海诺力智能科技有限公司";
}
}
//客户
if (2 == $res['user_type'])
{
$ret = db('partner_contacts')->alias('c')
->join('partner_company i','i.company_id=c.company_id')
->where('c.contacts_idcard', $param['idcard'])
->field('c.contacts_name as name, c.contacts_idcard as idcard, i.company_name as company')
->find();
}
if(md5($param['password']) == $res['user_pw'])
{
$roleInfo = db('system_role')->where('id', $res['user_role'])->find();
if ($ret != null)
{
if($res['user_state'] === 1){
$ret['id'] = $res['id'];
$ret["token"] = $roleInfo['role_name'];
$ret["roleName"] = $roleInfo['role_des'];
return $this->sendSuccess($ret);
}else{
return $this->sendError(10001, '用户账号已禁用,请联系管理员!');
}
}
else
{
return $this->sendError(40001,"登录用户信息错误,请重新输入",null);
}
}
}
public function info()
{
$data_par = request()->param();
$res = db('system_user')->where("id", $data_par["id"])->find();
if (NULL !=$res)
{
return $this->sendSuccess($res);
}
else
{
return $this->sendError(40003,"获取用户信息失败");
}
}
/**
** 新写用户信息功能
*/
//获取角色的信息
public function getRole()
{
$param = request()->param();
$where=[];
if (isset($param['token'])) {
$where['role_name'] = $param['token'];
}
$res = db('system_role')->where($where)->select();
if (NULL !=$res)
{
return $this->sendSuccess($res);
}
else
{
return $this->sendError(40004,"获取角色列表失败");
}
}
//获取用户信息列表
public function getUserList()
{
$param = request()->param();
$currPage = isset($param['currPage']) ? $param['currPage'] : 1;
$pageSize = isset($param['pageSize']) ? $param['pageSize'] : 20;
$dbUser ='system_user';
$dbRole ='system_role';
// 查询用户列表
$userList = db($dbUser)->alias('u')
->join($dbRole.' r', 'u.user_role=r.id')
->where('u.del_time', null)
->field(['u.*,r.role_name,r.role_des'])
->page($currPage, $pageSize)
->select();
// 遍历用户列表,为每个用户查询对应的项目名称
foreach ($userList as &$user) {
$user_project = explode(',', $user['user_project']);
if (!empty($user_project)) {
$project_info = db('project_info')
->whereIn('project_id', $user_project)
->where('project_state', '<', 10) // 未关闭的项目
->field('project_name')
->select();
$project_names = [];
foreach ($project_info as $project) {
$project_names[] = $project['project_name'];
}
$user['project_names'] = implode(',', $project_names);
} else {
$user['project_names'] = '';
}
// 根据用户类型补充用户信息
if (1 == $user['user_type']) {
$ret = db('hr_staff')->where('staff_id', $user['user_id'])->find();
$user['user_name'] = $ret['staff_name'] ?? '';
$user['user_phone'] = $ret['staff_phone'] ?? '';
$user['user_dept'] = '内部用户';
} elseif (2 == $user['user_type']) {
$ret = db('partner_contacts')->alias('c')
->join('partner_company i', 'i.company_id = c.company_id')
->where('c.contacts_id', $user['user_id'])
->field('c.*, i.company_name')
->find();
$user['user_name'] = $ret['contacts_name'] ?? '';
$user['user_phone'] = $ret['contacts_phone'] ?? '';
$user['user_dept'] = $ret['company_name'] ?? '';
}
}
unset($user); // 解除引用
// 查询总记录数
$total = db($dbUser)->alias('u')
->join($dbRole.' r', 'u.user_role=r.id')
->where('u.del_time', null)
->count();
$res['list'] = $userList;
$res['total'] = $total;
$this->sendSuccess($res);
}
public function getCompanyList()
{
$param = request()->param();
$ret = [];
//客户
if (2 == $param['user_type'])
{
$ret = db('customer_info')->field('customer_id as company_id, customer_name as company_name ')->select();
}
//供应商
if (3 == $param['user_type'])
{
$ret = db('supplier_info')->field('supplier_id as company_id, supplier_name as company_name ')->select();
}
return $this->sendSuccess($ret);
}
public function GetContactsList()
{
$param = request()->param();
$ret = db('hr_staff')->field('staff_id as contacts_id, staff_name as contacts_name, staff_idcard as contacts_idcard')->select();
return $this->sendSuccess($ret);
}
//添加用户
public function addUser()
{
$param = request()->param();
$sameIdcard = db('system_user')->where('user_idcard', $param['user_idcard'])->where('del_time', null)->count();
if ($sameIdcard > 0)
{
return $this->sendError(40003, '存在相同工号的用户,请确认后再次添加');
}
else
{
$msg = '添加用户时,信息写入数据库失败';
$insert['user_type'] = $param['user_type'];
$insert['user_idcard'] = $param['user_idcard'];
$insert['user_id'] = $param['user_id'];
$insert['user_pw'] = md5($param['password']);
$insert['user_role'] = $param['user_role'];
$insert['user_state'] = 1;
if (isset($param['user_project']) && ''!=$param['user_project'])
{
$insert['user_project'] = implode(',', $param['user_project']);
}
$insert['create_time'] = date("Y-m-d H:i:s");
$res = db('system_user')->insert($insert);
return $this->sendSuccess("添加用户成功");
}
}
//更新用户
public function updateUser()
{
$param = request()->param();
$update = [];
if ($param['password'] != '')
{
$update['user_pw'] = md5($param['password']);
}
$update['user_role'] = $param['user_role'];
$update['user_state'] = $param['user_state'];
if (isset($param['user_project']) && ''!=$param['user_project'])
{
$update['user_project'] = implode(',', $param['user_project']);
}
$res = db('system_user')->where('id', $param['user_id'])->update($update);
return $this->sendSuccess("更新成功");
}
//删除用户
public function deleteUser()
{
$param = request()->param();
$update['del_time'] = date("Y-m-d H:i:s");
$res = db('system_user')->where('id', $param['user_id'])->update($update);
return $this->sendSuccess("更新成功");
}
//更新用户密码
public function updateUserPassword()
{
$param = request()->param();
$ret = db('system_user')->where('id', $param['user_id'])->find();
if ($ret['user_pw'] != md5($param['password']))
{
return $this->sendError(40004, "用户密码错误,请重新输入");
}
else
{
$update['user_pw'] = md5($param['new_password']);
$res = db('system_user')->where('id', $param['user_id'])->update($update);
return $this->sendSuccess("更新用户密码成功");
}
}
public function GetUserInfoByLoginId($login_id)
{
$ret = null;
$res = db('system_user')->where('id', $login_id)->find();
if ($res != null)
{
//分类查找用户详细信息
//客户
if (1 == $res['user_type'])
{
$ret = db('hr_staff')->where('staff_id', $res['user_id'])
->field('staff_name as name, staff_idcard as idcard')
->find();
if ($ret != null)
{
$ret['company'] = "上海诺力智能科技有限公司";
}
$ret['id'] = $login_id;
$ret['name'] = $res['user_name'];
}
//客户
if (2 == $res['user_type'])
{
$ret = db('partner_company')->alias('c')
->join('partner_contacts i','i.company_id=c.company_id')
->join('system_user u','u.user_id=i.contacts_id')
->where('u.id', $login_id)
->field('i.contacts_name as name, i.contacts_idcard as idcard, c.company_name as company')
->find();
$ret['id'] = $login_id;
$ret['name'] = $res['user_name'];
}
}
return $ret;
}
public function GetProjectStaffList()
{
$param = request()->post();
if ($param) {
// 初始化 $array 为空数组
$array = [];
$story_info_member = [];
$story_info = db('project_story')
->where('story_id', $param['story_id'])
->where('project_id', $param['project_id'])
->find();
if ($story_info) {
$story_info_member = explode(',', $story_info['story_member']);
foreach ($story_info_member as $memberId) {
$ret = db('system_user')->alias('su')
->join('hr_staff hs', 'su.user_id = hs.staff_id')
->where('su.id', $memberId) // 使用 where 方法进行单值查询
->field('su.id, hs.staff_name')
->find();
if ($ret) {
$array[] = $ret;
}
}
}
return $this->sendSuccess($array);
}
}
//获取项目部门所有的员工信息
public function GetProjectDeptStaffList()
{
$param = request()->post();
$StaffList = db('system_user')->alias('u')
->join('hr_staff s','s.staff_id=u.user_id', 'left')
->where('u.user_type', 1)
->field('u.id as staff_id, s.staff_name')
->select();
//dump($StaffList);
/*$result = db('hr_staff')->alias('i')
->join('hr_staff_type t', 'i.staff_type=t.id', 'left')
->field('i.*, t.desp as staff_type_desp')
->select();
//查找员工所属部门信息
for ($i=0; $i<count($result); $i++)
{
$dept_arr = explode(",", $result[$i]['staff_dept']);
$dept_desp = db('hr_dept')->where('dept_id', 'in', $dept_arr)->column('dept_name');
$result[$i]['staff_dept_desp'] = implode(',', $dept_desp);
}
$StaffList = [];
//工程项目部员工
$project_detp_id = 3;
for ($i=0; $i<count($result); $i++)
{
$dept_arr = explode(",", $result[$i]['staff_dept']);
if (in_array($project_detp_id, $dept_arr))
{
$StaffList[] = $result[$i];
}
}
//技术研发部员工
$project_detp_id = 4;
for ($i=0; $i<count($result); $i++)
{
$dept_arr = explode(",", $result[$i]['staff_dept']);
if (in_array($project_detp_id, $dept_arr))
{
$StaffList[] = $result[$i];
}
}
*/
return $this->sendSuccess($StaffList);
}
//根据项目ID获取用户在项目中的角色
public function GetProjectUsrRole()
{
$param = request()->post();
$user = db('system_user')->where('id', $param['user_id'])->find();
$ret = [];
$ret['user_type'] = $user['user_type'];
$ret['project_role'] = [];
$mergedProjectRole = [];
// 外部用户
if (2 == $user['user_type'])
{
$company = db('partner_contacts')->where('contacts_id', $user['user_id'])->find();
$project = explode(',', $user['user_project']);
for ($i = 0; $i < count($project); $i++)
{
$customer = db('project_info')->where('project_id', $project[$i])->find();
if (!$customer) {
continue;
}
$par_company = db('partner_company')->where('company_id',$company['company_id'])->column('company_type');
if(!$par_company){
continue;
}
$find = false;
if ($company['company_id'] == $customer['customer_id'] || in_array(2, $par_company))
{
// 用户是客户
$role = 1;
if (!isset($mergedProjectRole[$role])) {
$mergedProjectRole[$role] = [
'project' => $project[$i],
'role' => $role
];
} else {
$mergedProjectRole[$role]['project'] .= ',' . $project[$i];
}
$find = true;
}
// 查找项目的供应商
$supplier = db('project_partner')->where('project_id', $project[$i])->column('company_id');
if (in_array($company['company_id'], $supplier))
{
// 用户是供应商
$role = 4;
if (!isset($mergedProjectRole[$role])) {
$mergedProjectRole[$role] = [
'project' => $project[$i],
'role' => $role
];
} else {
$mergedProjectRole[$role]['project'] .= ',' . $project[$i];
}
$find = true;
}
if (false == $find)
{
// 用户是合作方
$role = 8;
if (!isset($mergedProjectRole[$role])) {
$mergedProjectRole[$role] = [
'project' => $project[$i],
'role' => $role
];
} else {
$mergedProjectRole[$role]['project'] .= ',' . $project[$i];
}
}
}
$ret['project_role'] = array_values($mergedProjectRole);
}
return $this->sendSuccess($ret);
}
}