增加友链功能

This commit is contained in:
2026-03-11 23:44:55 +08:00
parent e46a4100c7
commit 1f8d876cef
9 changed files with 503 additions and 83 deletions
+27
View File
@@ -109,6 +109,33 @@ abstract class BaseController
return $this->tenantId;
}
/**
* 根据域名获取租户ID(静态方法,方便全局调用)
* @return int
*/
public static function getTenantIdByDomain(): int
{
$request = request();
// 优先从 Referer 获取来源域名(跨域调用场景)
$referer = $request->header('Referer');
if ($referer) {
$host = parse_url($referer, PHP_URL_HOST);
} else {
// 否则使用当前访问域名
$host = $request->host(true);
}
// 查询域名对应的租户ID
$tenantDomain = Db::name('mete_tenant_domain')
->where('full_domain', $host)
->where('status', 1)
->whereNull('delete_time')
->find();
return $tenantDomain ? (int)$tenantDomain['tid'] : 0;
}
/**
* 验证数据
* @access protected
+23 -75
View File
@@ -14,8 +14,7 @@ use think\db\exception\DbException;
use think\facade\Env;
use think\facade\Request;
use app\model\Cms\TemplateSiteConfig;
use app\model\Tenant\Tenant;
use app\model\Cms\Demand;
use app\model\Cms\Friendlink;
class Index extends BaseController
{
@@ -351,24 +350,35 @@ class Index extends BaseController
}
/**
* 获取企业信息
* 获取友情链接列表
* @return \think\response\Json
*/
public function getCompanyInfos()
public function getFriendlinkList()
{
// 从基类获取租户ID
$tid = $this->tenantId;
try {
$companyInfo = Tenant::where('delete_time', null)
->where('id', $tid)
->field('contact_phone, contact_email, address, worktime')
->find();
// 通过域名获取租户ID
$tid = BaseController::getTenantIdByDomain();
if (empty($tid)) {
return json([
'code' => 400,
'msg' => '无法识别租户信息',
'data' => []
]);
}
$friendlinkList = Friendlink::where('delete_time', null)
->where('tid', $tid)
->where('status', 1)
->order('sort', 'asc')
->field('id,link_name,link_url,link_logo,description')
->select()
->toArray();
return json([
'code' => 200,
'msg' => 'success',
'data' => $companyInfo ?: null
'data' => $friendlinkList
]);
} catch (DbException $e) {
return json([
@@ -378,66 +388,4 @@ class Index extends BaseController
]);
}
}
/**
* 客户需求接口
* post /index/requirement
* @param string $title 标题
* @param string $desc 描述
* @param string $applicant 申请人
* @param string $phone 手机号
* @return \think\response\Json
*/
public function requirement()
{
// 获取参数
$title = Request::param('title', '');
$desc = Request::param('desc', '');
$applicant = Request::param('applicant', '');
$phone = Request::param('phone', '');
// 验证必填字段
if (empty($title)) {
return json([
'code' => 400,
'msg' => '标题不能为空',
'data' => null
]);
}
if (empty($desc)) {
return json([
'code' => 400,
'msg' => '描述不能为空',
'data' => null
]);
}
// 从基类获取租户ID
$tid = $this->tenantId;
try {
// 保存到需求表
$id = Demand::insertGetId([
'tid' => $tid,
'title' => $title,
'desc' => $desc,
'applicant' => $applicant,
'phone' => $phone,
'status' => 1,
'create_time' => date('Y-m-d H:i:s')
]);
return json([
'code' => 200,
'msg' => '提交成功',
'data' => ['id' => $id]
]);
} catch (DbException $e) {
return json([
'code' => 500,
'msg' => '提交失败:' . $e->getMessage(),
'data' => null
]);
}
}
}
}
+1
View File
@@ -12,6 +12,7 @@ Route::get('init', 'app\index\controller\Index@init');
Route::get('footerdata', 'app\index\controller\Index@getFooterData');
Route::get('companyInfos', 'app\index\controller\Index@getCompanyInfos');
Route::post('requirement', 'app\index\controller\Index@requirement');
Route::get('friendlinks', 'app\index\controller\Index@getFriendlinkList');
// --- 客户需求路由 ---