$params['name'], 'account' => $params['mobile'], 'avatar' => $params['thumb'] ?? config('project.default_image.admin_avatar'), 'password' => create_password($params['password'], Config::get('project.unique_identification')), 'org_id' => $params['org_id'], 'dept_id' => $params['dept_id'], 'job_id' => $params['job_id'], 'disable' => $params['status'] == 2 ? 1 : 0, ]); self::insertRole($admin['id'], $params['role_id'] ?? []); OaAdmin::create([ 'name' => $params['name'], 'email' => $params['email'] ?? '', 'mobile' => $params['mobile'] ?? '', 'sex' => $params['sex'] ?? 0, 'thumb' => $params['thumb'] ?? config('project.default_image.admin_avatar'), 'org_id' => $params['org_id'], 'dept_id' => $params['dept_id'], 'job_id' => $params['job_id'], 'type' => $params['type'], 'age' => $params['age'] ?? 0, 'native_place' => $params['native_place'] ?? '', 'idcard' => $params['idcard'] ?? '', 'education' => $params['education'] ?? '', 'bank_account' => $params['bank_account'] ?? '', 'bank_info' => $params['bank_info'] ?? '', 'desc' => $params['desc'] ?? null, 'entry_time' => !empty($params['entry_time']) ? strtotime($params['entry_time']) : 0, 'status' => $params['status'], 'admin_id' => $admin['id'] ]); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 编辑企业员工 * @param array $params * @return bool * @author likeadmin * @date 2024/05/22 15:49 */ public static function edit(array $params): bool { $data = OaAdmin::where('id', $params['id'])->findOrEmpty(); Db::startTrans(); try { OaAdmin::where('id', $params['id'])->update([ 'name' => $params['name'], 'email' => $params['email'] ?? '', 'mobile' => $params['mobile'] ?? '', 'sex' => $params['sex'] ?? 0, 'thumb' => $params['thumb'] ?? config('project.default_image.admin_avatar'), 'org_id' => $params['org_id'], 'dept_id' => $params['dept_id'], 'job_id' => $params['job_id'], 'type' => $params['type'], 'age' => $params['age'] ?? 0, 'native_place' => $params['native_place'] ?? '', 'idcard' => $params['idcard'] ?? '', 'education' => $params['education'] ?? '', 'bank_account' => $params['bank_account'] ?? '', 'bank_info' => $params['bank_info'] ?? '', 'desc' => $params['desc'] ?? null, 'entry_time' => !empty($params['entry_time']) ? strtotime($params['entry_time']) : 0, 'status' => $params['status'], ]); Admin::where('id',$data['admin_id'])->update([ 'name' => $params['name'], 'account' => $params['name'], 'avatar' => $params['thumb'] ?? config('project.default_image.admin_avatar'), 'password' => create_password($params['password'], Config::get('project.unique_identification')), 'org_id' => $params['org_id'], 'dept_id' => $params['dept_id'], 'job_id' => $params['job_id'], 'disable' => $params['status'] == 2 ? 1 : 0, ]); // 更换角色后.设置token过期 $roleId = AdminRole::where('admin_id', $data['admin_id'])->column('role_id'); if (!empty(array_diff_assoc($roleId, $params['role_id'])) || $params['status'] == 2) { $tokenArr = AdminSession::where('admin_id', $data['admin_id'])->select()->toArray(); foreach ($tokenArr as $token) { self::expireToken($token['token']); } } (new AdminAuthCache($data['admin_id']))->clearAuthCache(); // 删除旧的关联信息 AdminRole::delByUserId($data['admin_id']); // 角色 self::insertRole($data['admin_id'], $params['role_id']); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 删除企业员工 * @param array $params * @return bool * @author likeadmin * @date 2024/05/22 15:49 */ public static function delete(array $params): bool { $data = OaAdmin::where('id', $params['id'])->findOrEmpty(); Db::startTrans(); try { OaAdmin::destroy($params['id']); Admin::destroy($data['admin_id']); //设置token过期 $tokenArr = AdminSession::where('admin_id', $data['admin_id'])->select()->toArray(); foreach ($tokenArr as $token) { self::expireToken($token['token']); } (new AdminAuthCache($data['admin_id']))->clearAuthCache(); // 删除旧的关联信息 AdminRole::delByUserId($data['admin_id']); Db::commit(); return true; }catch (\Exception $e){ Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 获取企业员工详情 * @param $params * @return array * @author likeadmin * @date 2024/05/22 15:49 */ public static function detail($params): array { $data = OaAdmin::findOrEmpty($params['id']); $data['sex_text'] = $data->sex_text; $data['type_text'] = $data->type_text; $data['status_text'] = $data->status_text; $org = Orgs::field('name')->where('id',$data['org_id'])->findOrEmpty(); $dept = Dept::field('name')->where('id',$data['dept_id'])->findOrEmpty(); $job = Jobs::field('name')->where('id',$data['job_id'])->findOrEmpty(); $data['org_name'] = !$org->isEmpty() ? $org['name'] : ''; $data['dept_name'] = !$dept->isEmpty() ? $dept['name'] : ''; $data['job_name'] = !$job->isEmpty() ? $job['name'] : ''; $data['role_id'] = AdminRole::where('admin_id', $data['admin_id'])->column('role_id'); return $data->toArray(); } public static function insertRole($adminId, $roleIds) { if (!empty($roleIds)) { // 角色 $roleData = []; foreach ($roleIds as $roleId) { $roleData[] = [ 'admin_id' => $adminId, 'role_id' => $roleId, ]; } (new AdminRole())->saveAll($roleData); } } public static function expireToken($token): bool { $adminSession = AdminSession::where('token', '=', $token) ->with('admin') ->find(); if (empty($adminSession)) { return false; } $time = time(); $adminSession->expire_time = $time; $adminSession->update_time = $time; $adminSession->save(); return (new AdminTokenCache())->deleteAdminInfo($token); } }