lihai-oa/app/api/controller/Common.php

641 lines
22 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @copyright Copyright (c) 2021 勾股工作室
* @license https://opensource.org/licenses/GPL-3.0
* @link https://www.gougucms.com
*/
declare (strict_types = 1);
namespace app\api\controller;
use app\api\ApiController;
use app\api\middleware\Auth;
use think\facade\Db;
class Common extends ApiController
{
protected $middleware = [
Auth::class => ['except' => []]
];
//获取部门
public function get_department()
{
$department = get_department();
return to_assign(0, '', $department);
}
//获取部门树形节点列表
public function get_department_tree()
{
$department = get_department();
$list = get_tree($department, 0, 2);
$data['trees'] = $list;
$this->apiSuccess('获取成功', $data);
}
//获取子部门所有员工
public function get_employee($did = 0)
{
$did = get_params('did');
if($did == 1){
$department = $did;
}
else{
$department = get_department_son($did);
}
$employee = Db::name('admin')
->field('a.id,a.did,a.position_id,a.mobile,a.name,a.nickname,a.sex,a.status,a.thumb,a.username,d.title as department')
->alias('a')
->join('Department d', 'a.did = d.id')
->where(['a.status' => 1])
->where('a.id', ">", 1)
->where('a.did', "in", $department)
->select();
$this->apiSuccess('获取成功', $employee);
}
//获取报销类型
public function get_expense_cate()
{
$expense_cate = Db::name('ExpenseCate')->where(['status' => 1])->field(['id', 'title'])->select()->toArray();
$this->apiSuccess('获取成功', $expense_cate);
}
//获取开票主体
public function get_invoice_subject()
{
$subject = Db::name('InvoiceSubject')->where(['status' => 1])->order('id desc')->select()->toArray();
$this->apiSuccess('获取成功', $subject);
}
//获取待办事项
public function get_todo_subject()
{
$this->uid = JWT_UID;
$subject = [
'approve'=>Db::name('Approve')->where([['', 'exp', Db::raw("FIND_IN_SET('{$this->uid}',check_admin_ids)")]])->count(),
'expenses'=>Db::name('Expense')->where([['', 'exp', Db::raw("FIND_IN_SET('{$this->uid}',check_admin_ids)")],['delete_time', '=', 0]])->count(),
'invoice'=>Db::name('Invoice')->where([['', 'exp', Db::raw("FIND_IN_SET('{$this->uid}',check_admin_ids)")],['delete_time', '=', 0]])->count(),
'task'=>$handle['task'] = Db::name('ProjectTask')->where([['director_uid', '=', $this->uid],['flow_status', '<', 3],['delete_time', '=', 0]])->count()
];
$this->apiSuccess('获取成功', $subject);
}
//获取待办任务
public function get_task_list()
{
$user_id = JWT_UID;
$map1[] = ['', 'exp', Db::raw("FIND_IN_SET('{$user_id}',f.check_admin_ids)")];
$param['status'] = 1;
$list = Db::name('Approve')
->field('f.*,a.name,d.title as department_name,t.title as flow_type')
->alias('f')
->join('Admin a', 'a.id = f.admin_id', 'left')
->join('Department d', 'd.id = f.department_id', 'left')
->join('FlowType t', 't.id = f.type', 'left')
->where($map1)
->order('f.id desc')
->group('f.id')
->paginate(['list_rows' => 2, 'query' => $param])
->each(function($item, $key){
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
$item['start_time'] = date('Y-m-d H:i:s', $item['start_time']);
$item['end_time'] = date('Y-m-d H:i:s', $item['end_time']);
$item['check_user'] = '-';
if($item['check_status']<2 && !empty($item['check_admin_ids'])){
$check_user = Db::name('Admin')->where('id','in',$item['check_admin_ids'])->column('name');
$item['check_user'] = implode(',',$check_user);
}
return $item;
});
$list = $list->toArray();
$this->apiSuccess('获取成功', $list['data']);
}
private function isAuthProject($uid)
{
if($uid == 1){
return 1;
}
$map = [];
$map[] = ['name', '=', 'project_admin'];
$map[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',uids)")];
$count = Db::name('DataAuth')->where($map)->count();
return $count;
}
//上传文件
public function upload()
{
$uid = JWT_UID;
$sourse = 'file';
if(isset($param['sourse'])){
$sourse = $param['sourse'];
}
if($sourse == 'file' || $sourse == 'tinymce'){
if(request()->file('file')){
$file = request()->file('file');
}
else{
$this->apiError('没有选择上传文件');
}
}
else{
if (request()->file('editormd-image-file')) {
$file = request()->file('editormd-image-file');
} else {
$this->apiError('没有选择上传文件');
}
}
// 获取上传文件的hash散列值
$sha1 = $file->hash('sha1');
$md5 = $file->hash('md5');
$rule = [
'image' => 'jpg,png,jpeg,gif',
'doc' => 'txt,doc,docx,ppt,pptx,xls,xlsx,pdf',
'file' => 'zip,gz,7z,rar,tar',
'video' => 'mpg,mp4,mpeg,avi,wmv,mov,flv,m4v',
];
$fileExt = $rule['image'] . ',' . $rule['doc'] . ',' . $rule['file'] . ',' . $rule['video'];
//1M=1024*1024=1048576字节
$fileSize = 100 * 1024 * 1024;
if (isset($param['type']) && $param['type']) {
$fileExt = $rule[$param['type']];
}
if (isset($param['size']) && $param['size']) {
$fileSize = $param['size'];
}
$validate = \think\facade\Validate::rule([
'image' => 'require|fileSize:' . $fileSize . '|fileExt:' . $fileExt,
]);
$file_check['image'] = $file;
if (!$validate->check($file_check)) {
$this->apiError($validate->getError());
}
// 日期前綴
$dataPath = date('Ym');
$use = 'thumb';
$filename = \think\facade\Filesystem::disk('public')->putFile($dataPath, $file, function () use ($md5) {
return $md5;
});
if ($filename) {
//写入到附件表
$data = [];
$path = get_config('filesystem.disks.public.url');
$data['filepath'] = $path . '/' . $filename;
$data['name'] = $file->getOriginalName();
$data['mimetype'] = $file->getOriginalMime();
$data['fileext'] = $file->extension();
$data['filesize'] = $file->getSize();
$data['filename'] = $filename;
$data['sha1'] = $sha1;
$data['md5'] = $md5;
$data['module'] = \think\facade\App::initialize()->http->getName();
$data['action'] = app('request')->action();
$data['uploadip'] = app('request')->ip();
$data['create_time'] = time();
$data['user_id'] = $uid;
if ($data['module'] = 'admin') {
//通过后台上传的文件直接审核通过
$data['status'] = 1;
$data['admin_id'] = $data['user_id'];
$data['audit_time'] = time();
}
$data['use'] = request()->has('use') ? request()->param('use') : $use; //附件用处
$res['id'] = Db::name('file')->insertGetId($data);
$res['filepath'] = $data['filepath'];
$res['name'] = $data['name'];
$res['filename'] = $data['filename'];
$res['filesize'] = $data['filesize'];
$res['fileext'] = $data['fileext'];
add_log('upload', $data['user_id'], $data,'文件');
$this->apiSuccess('上传成功', $res);
} else {
$this->apiError('上传失败,请重试');
}
}
//流程审核
public function flow_check()
{
$param = get_params();
$this->uid = JWT_UID;
$loginAdmin = Db::name('Admin')->where(['id' => $this->uid])->find();
$this->did = $loginAdmin['did'];
if (empty($param['id'])) {
$this->apiError("审批流程id不能为空");
}
if (empty($param['type'])) {
$this->apiError("审批流程类型不能为空");
}
// 1同意 2拒绝 3撤销
if (empty($param['check'])) {
$this->apiError("审批状态不能为空");
}
if ($param['check'] != 3) {
if (empty($param['content'])) {
$this->apiError("审批内容不能为空");
}
//1审核结束 2下一审批人
if (empty($param['check_node'])) {
$this->apiError("审批节点不能为空");
}
}
$id = $param['id'];
$type = $param['type'];
$detail = [];
$subject = '一个审批';
if($type==1){
//日常审核
$detail = Db::name('Approve')->where(['id' => $id])->find();
$subject = '一个日常审批';
$msg_title_type = $detail['type'] ?? 0;
}
else if($type==2){
//报销审核
$detail = Db::name('Expense')->where(['id' => $id])->find();
$subject = '一个报销审批';
$msg_title_type = 22;
}
else if($type==3){
//发票审核
$detail = Db::name('Invoice')->where(['id' => $id])->find();
$subject = '一个发票审批';
$msg_title_type = 23;
}
else if($type==4){
//合同审核
$detail = Db::name('Contract')->where(['id' => $id])->find();
$subject = '一个合同审批';
$msg_title_type = 24;
}
if (empty($detail)){
$this->apiError("审批数据错误");
}
//当前审核节点详情
$step = Db::name('FlowStep')->where(['action_id'=>$id,'type'=>$type,'sort'=>$detail['check_step_sort'],'delete_time'=>0])->find();
//审核通过
if($param['check'] == 1){
$check_admin_ids = explode(",", strval($detail['check_admin_ids']));
if (!in_array($this->uid, $check_admin_ids)){
$this->apiError("您没权限审核该审批", [], 2);
}
//多人会签审批
if($step['flow_type'] == 4){
//查询当前会签记录数
$check_count = Db::name('FlowRecord')->where(['action_id'=>$id,'type'=>$type,'step_id'=>$step['id']])->count();
//当前会签记应有记录数
$flow_count = explode(',', $step['flow_uids']);
if(($check_count+1) >=count($flow_count)){
$next_step = Db::name('FlowStep')->where(['action_id'=>$id,'type'=>$type,'sort'=>($detail['check_step_sort']+1),'delete_time'=>0])->find();
if($next_step){
//存在下一步审核
if($next_step['flow_type'] == 1){
$param['check_admin_ids'] = get_department_manager($detail['admin_id']);
}
else if($next_step['flow_type'] == 2){
$param['check_admin_ids'] = get_department_manager($detail['admin_id'],1);
}
else if($next_step['flow_type'] == 5){
$param['check_admin_ids'] = get_department_leader($detail['admin_id']);
}
else if($next_step['flow_type'] == 6){
$param['check_admin_ids'] = get_department_leader($detail['admin_id'],1);
}
else{
$param['check_admin_ids'] = $next_step['flow_uids'];
}
$param['check_step_sort'] = $detail['check_step_sort']+1;
$param['check_status'] = 1;
}
else{
//不存在下一步审核,审核结束
$param['check_status'] = 2;
$param['check_admin_ids'] ='';
}
}
else{
$param['check_status'] = 1;
$param['check_admin_ids'] = $step['flow_uids'];
}
}
else if($step['flow_type'] == 0){
//自由人审批
if($param['check_node'] == 2){
$next_step = $detail['check_step_sort']+1;
$flow_step = array(
'action_id' => $id,
'sort' => $next_step,
'type' => $type,
'flow_uids' => $param['check_admin_ids'],
'create_time' => time()
);
$fid = Db::name('FlowStep')->strict(false)->field(true)->insertGetId($flow_step);
//下一步审核步骤
$param['check_admin_ids'] = $param['check_admin_ids'];
$param['check_step_sort'] = $next_step;
$param['check_status'] = 1;
}
else{
//不存在下一步审核,审核结束
$param['check_status'] = 2;
$param['check_admin_ids'] ='';
}
}
else{
$next_step = Db::name('FlowStep')->where(['action_id'=>$id,'type'=>$type,'sort'=>($detail['check_step_sort']+1),'delete_time'=>0])->find();
if($next_step){
//存在下一步审核
if($next_step['flow_type'] == 1){
$param['check_admin_ids'] = get_department_manager($detail['admin_id']);
}
else if($next_step['flow_type'] == 2){
$param['check_admin_ids'] = get_department_manager($detail['admin_id'],1);
}
else if($next_step['flow_type'] == 5){
$param['check_admin_ids'] = get_department_leader($detail['admin_id']);
}
else if($next_step['flow_type'] == 6){
$param['check_admin_ids'] = get_department_leader($detail['admin_id'],1);
}
else{
$param['check_admin_ids'] = $next_step['flow_uids'];
}
$param['check_step_sort'] = $detail['check_step_sort']+1;
$param['check_status'] = 1;
}
else{
//不存在下一步审核,审核结束
$param['check_status'] = 2;
$param['check_admin_ids'] ='';
}
}
if($param['check_status'] == 1 && empty($param['check_admin_ids'])){
$this->apiError("找不到下一步的审批人该审批流程设置有问题请联系HR或者管理员");
}
//审核通过数据操作
$param['last_admin_id'] = $this->uid;
$param['flow_admin_ids'] = $detail['flow_admin_ids'].$this->uid.',';
if($type==1){
//日常审核
$res = Db::name('Approve')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
}
else if($type==2){
//报销审核
$res = Db::name('Expense')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
}
else if($type==3){
//发票审核
$res = Db::name('Invoice')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
}
else if($type==4){
//合同审核
$res = Db::name('Contract')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
}
if($res!==false){
$checkData=array(
'action_id' => $id,
'step_id' => $step['id'],
'check_user_id' => $this->uid,
'type' => $type,
'check_time' => time(),
'status' => $param['check'],
'content' => $param['content'],
'create_time' => time()
);
$aid = Db::name('FlowRecord')->strict(false)->field(true)->insertGetId($checkData);
add_log('check', $param['id'], $param,$subject);
//发送消息通知
$msg=[
'create_time'=>date('Y-m-d H:i:s',$detail['create_time']),
'action_id'=>$id,
'title' => Db::name('FlowType')->where('id',$msg_title_type)->value('title'),
'from_uid'=>$detail['admin_id']
];
if($param['check_status'] == 1){
$users = $param['check_admin_ids'];
sendMessage($users,($type*10+11),$msg);
}
if($param['check_status'] == 2){
$users = $detail['admin_id'];
sendMessage($users,($type*10+12),$msg);
}
$this->apiSuccess('操作成功');
}
else{
$this->apiError("操作失败");
}
}
else if($param['check'] == 2){
$check_admin_ids = explode(",", strval($detail['check_admin_ids']));
if (!in_array($this->uid, $check_admin_ids)){
$this->apiError("您没权限审核该审批", [], 2);
}
//拒绝审核,数据操作
$param['check_status'] = 3;
$param['last_admin_id'] = $this->uid;
$param['flow_admin_ids'] = $detail['flow_admin_ids'].$this->uid.',';
$param['check_admin_ids'] ='';
if($step['flow_type'] == 5){
//获取上一步的审核信息
$prev_step = Db::name('FlowStep')->where(['action_id'=>$id,'type'=>$type,'sort'=>($detail['check_step_sort']-1),'delete_time'=>0])->find();
if($prev_step){
//存在上一步审核
$param['check_step_sort'] = $prev_step['sort'];
$param['check_admin_ids'] = $prev_step['flow_uids'];
$param['check_status'] = 1;
}
else{
//不存在上一步审核,审核初始化步骤
$param['check_step_sort'] = 0;
$param['check_admin_ids'] = '';
$param['check_status'] = 0;
}
}
if($type==1){
//日常审核
$res = Db::name('Approve')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
}
else if($type==2){
//报销审核
$res = Db::name('Expense')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
}
else if($type==3){
//发票审核
$res = Db::name('Invoice')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
}
else if($type==4){
//合同审核
$res = Db::name('Contract')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
}
if($res!==false){
$checkData=array(
'action_id' => $id,
'step_id' => $step['id'],
'check_user_id' => $this->uid,
'type' => $type,
'check_time' => time(),
'status' => $param['check'],
'content' => $param['content'],
'create_time' => time()
);
$aid = Db::name('FlowRecord')->strict(false)->field(true)->insertGetId($checkData);
add_log('refue', $param['id'], $param,$subject);
//发送消息通知
$msg=[
'create_time'=>date('Y-m-d H:i:s',$detail['create_time']),
'action_id'=>$detail['id'],
'title' => Db::name('FlowType')->where('id',$msg_title_type)->value('title'),
'from_uid'=>$detail['admin_id']
];
$users = $detail['admin_id'];
sendMessage($users,($type*10+13),$msg);
$this->apiSuccess('操作成功');
}
else{
$this->apiError("操作失败");
}
}
else if($param['check'] == 3){
if($detail['admin_id'] != $this->uid){
$this->apiError("你没权限操作", [], 2);
}
//撤销审核,数据操作
$param['check_status'] = 4;
$param['check_admin_ids'] ='';
$param['check_step_sort'] =0;
if($type==1){
//日常审核
$res = Db::name('Approve')->strict(false)->field('check_step_sort,check_status,check_admin_ids')->update($param);
}
else if($type==2){
//报销审核
$res = Db::name('Expense')->strict(false)->field('check_step_sort,check_status,check_admin_ids')->update($param);
}
else if($type==3){
//发票审核
$res = Db::name('Invoice')->strict(false)->field('check_step_sort,check_status,check_admin_ids')->update($param);
}
else if($type==4){
//合同审核
$res = Db::name('Contract')->strict(false)->field('check_step_sort,check_status,check_admin_ids')->update($param);
}
if($res!==false){
$checkData=array(
'action_id' => $id,
'step_id' => 0,
'check_user_id' => $this->uid,
'type' => $type,
'check_time' => time(),
'status' => $param['check'],
'content' => $param['content'],
'create_time' => time()
);
$aid = Db::name('FlowRecord')->strict(false)->field(true)->insertGetId($checkData);
add_log('back', $param['id'], $param,$subject);
$this->apiSuccess('操作成功');
}else{
$this->apiError("操作失败");
}
}
}
//未读消息统计
public function msg_count()
{
$admin_id = JWT_UID;
$msg_map[] = ['to_uid', '=', $admin_id];
$msg_map[] = ['read_time', '=', 0];
$msg_map[] = ['status', '=', 1];
$msg_count = Db::name('Message')->where($msg_map)->count();
$statistics['msg_num'] = $msg_count;
$this->apiSuccess('获取成功', $statistics);
}
//获取审核步骤人员
public function get_flow_users()
{
$param = get_params();
if (empty($param['id'])) {
$this->apiError("审批流程id不能为空");
}
$flow = Db::name('Flow')->where(['id' => $param['id']])->find();
if (empty($flow)) {
$this->apiError("审批流程不存在");
}
$flowData = unserialize($flow['flow_list']);
if(!empty($flowData)){
foreach ($flowData as $key => &$val) {
$val['user_id_info'] = Db::name('Admin')->field('id,name,thumb')->where('id','in',$val['flow_uids'])->select()->toArray();
$val['flow_type_txt'] = '';
if($val['flow_type'] == 1){
$val['flow_type_txt'] = '当前部门负责人';
}
if($val['flow_type'] == 2){
$val['flow_type_txt'] = '上级部门负责人';
}
if($val['flow_type'] == 3){
$val['flow_type_txt'] = '指定人员或签';
}
if($val['flow_type'] == 4){
$val['flow_type_txt'] = '指定人员会签';
}
if($val['flow_type'] == 5){
$val['flow_type_txt'] = '当前部门分管领导';
}
if($val['flow_type'] == 6){
$val['flow_type_txt'] = '上级部门分管领导';
}
}
}
$data['copy_uids'] = $flow['copy_uids'];
$data['copy_unames'] ='';
if($flow['copy_uids']!=''){
$copy_unames = Db::name('Admin')->where('id', 'in', $flow['copy_uids'])->column('name');
$data['copy_unames'] = implode(',', $copy_unames);
}
$data['flow_data'] = $flowData;
$this->apiSuccess('获取成功', $data);
}
//获取审核流程节点
public function get_flow_nodes($id=0,$type=1)
{
$flows = Db::name('FlowStep')->where(['action_id'=>$id,'type'=>$type,'delete_time'=>0])->order('sort asc')->select()->toArray();
foreach ($flows as $key => &$val) {
$user_id_info = Db::name('Admin')->field('id,name,thumb')->where('id','in',$val['flow_uids'])->select()->toArray();
foreach ($user_id_info as $k => &$v) {
$v['check_time'] = 0;
$v['content'] = '';
$v['status'] = 0;
$check_array = Db::name('FlowRecord')->where(['check_user_id' => $v['id'],'step_id' => $val['id']])->order('check_time desc')->select()->toArray();
if(!empty($check_array)){
$checked = $check_array[0];
$v['check_time'] = date('Y-m-d H:i', $checked['check_time']);
$v['content'] = $checked['content'];
$v['status'] = $checked['status'];
}
}
$check_list = Db::name('FlowRecord')
->field('f.*,a.name,a.thumb')
->alias('f')
->join('Admin a', 'a.id = f.check_user_id', 'left')
->where(['f.step_id' => $val['id']])->select()->toArray();
foreach ($check_list as $kk => &$vv) {
$vv['check_time_str'] = date('Y-m-d H:i', $vv['check_time']);
}
$val['user_id_info'] = $user_id_info;
$val['check_list'] = $check_list;
}
$this->apiSuccess('获取成功', $flows);
}
}