Files
YwxAppThink/addon/haonav/controller/trait/SearchTrait.php
T
yangwx 18101cf11a feat: 上线前安全加固与发布工程化;appmall 由插件迁移为独立应用
安全(P0)
- 发布包签名:新增 scripts/package_keygen.php、scripts/package_verify.php、
  ywxapp\service\PackageVerifier;FrameworkService::apply() 解压前强制验签
  (ywxapp.framework_package_verify,默认开启)
- JWT 收紧:config/jwt.php 移除公开默认密钥,JwtService::assertStrongSecret()
  校验非空 / 长度≥32 / 已知弱值;.env.production.example 键名由 JWT_SECRET_KEY 修正为 JWT_SECRET
- /api/v1/Article:免登录白名单收窄为只读接口,写操作字段白名单 + assertOwner() 越权校验
- 开发者令牌:查找接口不再回显明文、统一失败文案 + 限流
- TemplateInstaller:文件名白名单 + realpath 归一化 + 目录内断言,修复 Zip Slip;补 use ZipArchive
- 后台登录:接入验证码、失败锁定与登录限流(config/ratelimit.php)
- 缓存 / Session 驱动可由 .env 覆盖(CACHE_DRIVER / SESSION_TYPE),集群部署改 redis
- paycenter 支付日志开关改为 PAY_LOG,不再跟随 APP_DEBUG

工程化 / 交付
- 新增 CHANGELOG.md 与 .env.production.example;composer.lock 入库锁定依赖版本
- 插件离线安装 CLI 增加 --uid,后台上传安装透传 uid,付费插件授权校验可通过
- 授权拒绝文案区分「未携带 uid」与「未找到有效授权」,便于排障
- .gitignore 放行 .env.production.example / .env.addon.example / .gitattributes / public/.htaccess

结构
- appmall 由 addon/appmall 迁移为独立应用 app/appmall(市场 / 开发者中心 / 审核 / 分润)
2026-10-22 00:53:39 +08:00

127 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace addon\haonav\controller\trait;
use think\facade\Db;
use addon\haonav\model\Links as LinksModel;
/**
* 搜索相关:站内搜索页 + 自动补全 + 用户评分
*/
trait SearchTrait
{
/**
* 站内搜索
* 普通请求渲染搜索结果页;ajax=1 返回 JSON(供前端无刷新搜索)
*/
public function search()
{
$q = $this->request->param('q', '');
$page = $this->request->param('page/d', 1);
$limit = 20;
if ($this->request->isAjax() || $this->request->param('ajax/d', 0)) {
$data = LinksModel::search($q, $page, $limit);
$this->result->setCount($data->total());
$this->result->success($data->items());
}
$results = [];
if ($q !== '') {
$results = LinksModel::search($q, 1, 50)->items();
}
$this->view->assign('q', $q);
$this->view->assign('results', $results);
$this->view->assign('config', $this->siteConfig());
$this->assignSeo();
return $this->view->fetch('search');
}
/**
* 搜索建议(自动补全)
* 标题/关键词/网址/拼音(首字母+全拼)均可命中
*/
public function suggest()
{
$q = trim((string)$this->request->param('q', ''));
$list = [];
if ($q !== '') {
$isAlpha = (bool)preg_match('/^[a-zA-Z]+$/', $q);
$list = LinksModel::where('status', 1)
->where(function ($query) use ($q, $isAlpha) {
$query->where('title', 'like', "%{$q}%")
->whereOr('keywords', 'like', "%{$q}%")
->whereOr('url', 'like', "%{$q}%");
if ($isAlpha) {
$query->whereOr('pinyin', 'like', '%' . strtolower($q) . '%');
}
})
->field('id,title,url')
->order('click_count', 'desc')
->limit(10)
->select();
}
return $this->result->success($list);
}
/**
* 网站评分(登录后 1-5 星,按用户去重可改分;聚合更新综合评分)
* POST /haonav/rate {id, score}
*/
public function rate()
{
if (!$this->request->isPost()) {
return $this->result->error('请求方式错误');
}
$uid = $this->uid();
if (!$uid) {
return $this->result->error('请先登录后再评分', 401);
}
$id = (int)$this->request->post('id/d', 0);
$score = (int)$this->request->post('score/d', 0);
if ($id <= 0 || $score < 1 || $score > 5) {
return $this->result->error('参数错误');
}
$link = LinksModel::find($id);
if (!$link || $link->status != 1) {
return $this->result->error('网站不存在或已下架');
}
// 评分记录表由安装/升级流程(install.sql + Addon::upgrade 钩子)保证存在
$now = time();
$exists = Db::name('haonav_ratings')->where('user_id', $uid)->where('link_id', $id)->find();
if ($exists) {
Db::name('haonav_ratings')->where('id', $exists['id'])->update([
'score' => $score,
'update_at' => $now,
]);
} else {
Db::name('haonav_ratings')->insert([
'user_id' => $uid,
'link_id' => $id,
'score' => $score,
'create_at' => $now,
'update_at' => $now,
]);
}
// 重新聚合该站评分,保证 rating / rating_count 精确
$agg = Db::name('haonav_ratings')
->where('link_id', $id)
->field('AVG(score) as avg_score, COUNT(*) as cnt')
->find();
$rating = ($agg && $agg['cnt']) ? round((float)$agg['avg_score'], 1) : 0.0;
$count = $agg ? (int)$agg['cnt'] : 0;
LinksModel::where('id', $id)->update([
'rating' => $rating,
'rating_count' => $count,
]);
return $this->result->success([
'rating' => $rating,
'rating_count' => $count,
'my_score' => $score,
], '评分成功');
}
}