feat: 企业版合作模式(后端、管理端、小程序)

- API:合作模式配置、用户选择、企业版入口与迁移 SQL

- 管理端:合作方式管理、企业/用户侧设置与 CSV 导出等

- 小程序:合作模式弹窗与结果/简历等页对接;开发脚本小调整

Made-with: Cursor
This commit is contained in:
Ghost
2026-04-23 16:08:13 +08:00
parent d7137f9349
commit 830e3611d7
40 changed files with 2518 additions and 33 deletions

View File

@@ -170,6 +170,7 @@ class AppUser extends BaseController
$ent = Db::name('enterprises')->where('id', $enterpriseId)->find();
$enterpriseName = $ent['name'] ?? ('企业' . $enterpriseId);
}
$coopMap = [];
if (!empty($ids)) {
// 测试统计严格按 test_results.enterpriseId 归属企业过滤
$trBase = Db::name('test_results')->where('userId', 'in', $ids);
@@ -284,6 +285,33 @@ class AppUser extends BaseController
} catch (\Throwable $e) {
$coldFaceMap = [];
}
// 本企业下用户合作意向user_cooperation_choices 唯一 keyuserId + enterpriseId
if ($enterpriseId) {
try {
$eid = (int) $enterpriseId;
$coopRows = Db::name('user_cooperation_choices')->alias('uc')
->leftJoin('enterprise_cooperation_modes ecm', 'ecm.enterpriseId = uc.enterpriseId AND ecm.modeCode = uc.modeCode')
->whereIn('uc.userId', $ids)
->where('uc.enterpriseId', $eid)
->field('uc.userId, uc.modeCode, uc.chosenAt, uc.updatedAt, ecm.title as modeTitle')
->select()
->toArray();
foreach ($coopRows as $cr) {
$uCo = (int) ($cr['userId'] ?? 0);
if ($uCo > 0) {
$coopMap[$uCo] = [
'modeCode' => (string) ($cr['modeCode'] ?? ''),
'modeTitle' => (string) ($cr['modeTitle'] ?? ''),
'chosenAt' => (int) ($cr['chosenAt'] ?? 0),
'updatedAt' => (int) ($cr['updatedAt'] ?? 0),
];
}
}
} catch (\Throwable $e) {
$coopMap = [];
}
}
}
foreach ($list as &$row) {
@@ -317,6 +345,18 @@ class AppUser extends BaseController
$row['coldFaceScore'] = $cf && $cf['score'] !== null ? (int) $cf['score'] : null;
$row['coldFaceLevel'] = $cf && !empty($cf['level']) ? (string) $cf['level'] : null;
$row['coldFaceUpdatedAt'] = $cf ? ($cf['updatedAt'] ?? null) : null;
$co = $coopMap[$id] ?? null;
if ($co && ($co['modeCode'] !== '' || $co['modeTitle'] !== '' || (int) ($co['chosenAt'] ?? 0) > 0)) {
$row['cooperationModeCode'] = $co['modeCode'] !== '' ? $co['modeCode'] : null;
$row['cooperationModeTitle'] = $co['modeTitle'] !== '' ? $co['modeTitle'] : null;
$chAt = (int) ($co['chosenAt'] ?? 0);
$row['cooperationChosenAt'] = $chAt > 0 ? $chAt : null;
} else {
$row['cooperationModeCode'] = null;
$row['cooperationModeTitle'] = null;
$row['cooperationChosenAt'] = null;
}
}
return paginate_response($list, $total, $page, $pageSize);
@@ -432,6 +472,30 @@ class AppUser extends BaseController
$data['coldFaceLevel'] = $coldFace['level'] ?? null;
$data['coldFaceUpdatedAt'] = $coldFace['updatedAt'] ?? null;
$data['cooperationModeCode'] = null;
$data['cooperationModeTitle'] = null;
$data['cooperationChosenAt'] = null;
if ($enterpriseId) {
try {
$cr = Db::name('user_cooperation_choices')->alias('uc')
->leftJoin('enterprise_cooperation_modes ecm', 'ecm.enterpriseId = uc.enterpriseId AND ecm.modeCode = uc.modeCode')
->where('uc.userId', $id)
->where('uc.enterpriseId', (int) $enterpriseId)
->field('uc.modeCode, uc.chosenAt, ecm.title as modeTitle')
->find();
if ($cr) {
$mc = trim((string) ($cr['modeCode'] ?? ''));
$mt = trim((string) ($cr['modeTitle'] ?? ''));
$data['cooperationModeCode'] = $mc !== '' ? $mc : null;
$data['cooperationModeTitle'] = $mt !== '' ? $mt : null;
$chAt = (int) ($cr['chosenAt'] ?? 0);
$data['cooperationChosenAt'] = $chAt > 0 ? $chAt : null;
}
} catch (\Throwable $e) {
// 表未迁移时忽略
}
}
return success($data);
}

View File

@@ -0,0 +1,146 @@
<?php
namespace app\controller\admin;
use app\BaseController;
use app\common\service\EnterpriseCooperationService;
use app\model\Enterprise as EnterpriseModel;
use think\facade\Db;
use think\facade\Request;
use think\Response;
/**
* 企业管理员:本企业合作模式配置
*/
class EnterpriseCooperation extends BaseController
{
public function get()
{
$user = $this->request->user ?? null;
if (!$this->canAccessEnterpriseCooperation($user)) {
return error('仅已绑定企业的管理员可配置', 403);
}
$eid = $this->resolveEnterpriseId($user);
if (!$eid || $eid <= 0) {
return error('未绑定企业', 403);
}
if (!EnterpriseModel::find($eid)) {
return error('企业不存在', 404);
}
$list = EnterpriseCooperationService::listModesForEnterprise($eid, false);
return success(['list' => $list]);
}
public function update()
{
$user = $this->request->user ?? null;
if (!$this->canAccessEnterpriseCooperation($user)) {
return error('仅已绑定企业的管理员可配置', 403);
}
$eid = $this->resolveEnterpriseId($user);
if (!$eid || $eid <= 0) {
return error('未绑定企业', 403);
}
if (!EnterpriseModel::find($eid)) {
return error('企业不存在', 404);
}
$body = Request::put();
if (!is_array($body)) {
$body = [];
}
$modes = $body['modes'] ?? null;
if (!is_array($modes)) {
return error('请提交 modes 数组', 400);
}
try {
EnterpriseCooperationService::saveConfigs($eid, $modes);
} catch (\InvalidArgumentException $e) {
return error($e->getMessage(), 400);
}
$list = EnterpriseCooperationService::listModesForEnterprise($eid, false);
return success(['list' => $list], '已保存');
}
/**
* GET enterprise/cooperation-choices
* 本企业下用户已选合作意向(分页)
*/
public function listChoices()
{
$user = $this->request->user ?? null;
if (!$this->canAccessEnterpriseCooperation($user)) {
return error('无权限访问', 403);
}
$eid = $this->resolveEnterpriseId($user);
if (!$eid || $eid <= 0) {
return error('未绑定企业', 403);
}
if (!EnterpriseModel::find($eid)) {
return error('企业不存在', 404);
}
$page = (int) Request::param('page', 1);
$pageSize = (int) Request::param('pageSize', 20);
$keyword = trim((string) Request::param('keyword', ''));
$data = EnterpriseCooperationService::listUserCooperationChoices($eid, $page, $pageSize, $keyword);
return paginate_response($data['list'], $data['total'], $page, $pageSize);
}
/**
* GET enterprise/cooperation-choices/export
* 导出本企业 CSV
*/
public function exportChoices()
{
$user = $this->request->user ?? null;
if (!$this->canAccessEnterpriseCooperation($user)) {
return error('无权限访问', 403);
}
$eid = $this->resolveEnterpriseId($user);
if (!$eid || $eid <= 0) {
return error('未绑定企业', 403);
}
if (!EnterpriseModel::find($eid)) {
return error('企业不存在', 404);
}
$keyword = trim((string) Request::param('keyword', ''));
$rows = EnterpriseCooperationService::listUserCooperationChoicesForExport($eid, $keyword, 10000);
$csv = "\xEF\xBB\xBF" . EnterpriseCooperationService::buildUserCooperationChoicesCsvContent($rows, false);
$name = 'cooperation-choices-e' . $eid . '.csv';
return Response::create($csv, 'html', 200)
->contentType('text/csv; charset=UTF-8')
->header(['Content-Disposition' => 'attachment; filename="' . $name . '"']);
}
private function canAccessEnterpriseCooperation($user): bool
{
if (!is_array($user)) {
return false;
}
$r = (string) ($user['role'] ?? '');
return in_array($r, ['admin', 'enterprise_admin'], true);
}
private function resolveEnterpriseId(array $user): ?int
{
$adminRow = Db::name('users')->where('id', $user['userId'] ?? 0)->find();
$eid = $adminRow['enterpriseId'] ?? null;
if ($eid === null || $eid === '') {
return null;
}
return (int) $eid;
}
}