Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
|
3c83650908 | 1 year ago |
|
4961769634 | 1 year ago |
|
6c275750ed | 1 year ago |
@ -0,0 +1,487 @@ |
||||
<?php |
||||
|
||||
namespace app\api\controller; |
||||
|
||||
use app\common\controller\Api; |
||||
use think\Db; |
||||
use think\Cache; |
||||
/** |
||||
* 首页接口 |
||||
*/ |
||||
|
||||
class Dynamic extends Api |
||||
{ |
||||
protected $noNeedLogin = ['list']; |
||||
protected $noNeedRight = ['*']; |
||||
/** |
||||
* 首页 |
||||
* |
||||
*/ |
||||
|
||||
public function index() |
||||
|
||||
{ |
||||
$users = Db::name("dynamic_form")->select(); |
||||
$this->success('请求成功!', $users); |
||||
} |
||||
|
||||
|
||||
public function test1(){ |
||||
$x=5; // 全局变量 |
||||
$y=10; // 局部变量 |
||||
echo "<p>测试函数内变量:<p>"; |
||||
echo "变量 x 为: $x"; |
||||
echo "<br>"; |
||||
echo "变量 y 为: $y"; |
||||
|
||||
} |
||||
/** |
||||
* 动态发表 |
||||
*/ |
||||
public function article(){ |
||||
$post = $this->request->post(); |
||||
$user_id = $this->auth->id; |
||||
$inData = [ |
||||
"user_id"=>$user_id, |
||||
"headline"=>$post['headline'] , |
||||
"content_type"=>$post["content_type"], |
||||
"content"=>$post['content'], |
||||
"topic_id"=>$post['topic_id'], |
||||
"create_time"=>date('Y-m-d H:i:s') |
||||
]; |
||||
$dynamic = Db::name("dynamic_form")->insert($inData); |
||||
$this->success("发布成功",$dynamic); |
||||
} |
||||
|
||||
/** |
||||
*分页查询 |
||||
*/ |
||||
|
||||
public function list(){ |
||||
//接收get请求的参数赋值给 $params |
||||
$params = $this->request->get(); |
||||
// 判断缓存是否存在 |
||||
$cacheKey = 'dynamic_list_' . md5(serialize($params)); |
||||
|
||||
if (Cache::store('redis')->has($cacheKey)) { |
||||
$result = Cache::store('redis')->get($cacheKey); |
||||
$this->success("OK", $result); |
||||
} |
||||
// echo "22222222"; |
||||
// exit; |
||||
//分页查询动态表某些字段的集合,并且进行遍历,数据处理 |
||||
$page = Db::name("dynamic_form")->field("id,headline,total_collect,image,user_id,topic_id,create_time")->paginate($params['limit'] ?? 10)->each(function($item, $key){//$item是forech 里面的$value,$key则是Key |
||||
//查询发表当前动态的用户信息 |
||||
$user = Db::name("user")->where("id",$item["user_id"] )->field("id,username,avatar")->find(); |
||||
//查询当前动态关联的话题 |
||||
$topic = Db::name("topic")->where("id", $item["topic_id"])->find(); |
||||
|
||||
//查询当前用户是否点赞了当前动态 |
||||
$like = Db::name("like") |
||||
->where("item_id",$item["id"]) |
||||
->where("item_type",1) |
||||
->where("user_id",$item["user_id"]) |
||||
->where('status',1) |
||||
->find(); |
||||
|
||||
$item["is_like"] = $like ? 1 : 0;//把是否点赞赋值给is_like对象 |
||||
$item["user"] = $user;//把查询到的当前动态用户信息赋值给user对象 |
||||
$item["topic"] = $topic;//把查询到动态关联的话题赋值给topic对象 |
||||
$item['create_time'] = strtotime($item['create_time']);//把发表时间进行时间戳转换 |
||||
|
||||
return $item; |
||||
}); |
||||
|
||||
// 设置缓存 |
||||
$expireTime = 3600; // 过期时间(单位:秒) |
||||
Cache::store('redis')->set($cacheKey, $page); |
||||
Cache::store('redis')->expire($cacheKey, $expireTime); |
||||
|
||||
$this->success("发布成功",$page); |
||||
} |
||||
|
||||
/** |
||||
* 查看详情 |
||||
* @return [type] [description] |
||||
*/ |
||||
public function detail(){ |
||||
|
||||
$id = $this->request->get("id"); |
||||
$user_id = 47; |
||||
$detail = Db::name('dynamic_form')->where('id', $id)->find(); |
||||
if(!$detail){ |
||||
$this->error("动态已删除"); |
||||
} |
||||
$user = Db::name("user")->where("id",$detail["user_id"] )->field("id,username,avatar")->find(); |
||||
//查询当前动态是否是否收藏 |
||||
$like = Db::name("like") |
||||
->where("item_id",$detail["id"]) |
||||
->where("item_type",1) |
||||
->where("user_id",$detail["user_id"]) |
||||
->where('status',1) |
||||
->find(); |
||||
//查询当前动态是否收藏 |
||||
$collect = Db::name("collect") |
||||
->where("item_id",$detail["id"]) |
||||
->where("item_type",1) |
||||
->where("user_id",$detail["user_id"]) |
||||
->where('status',1) |
||||
->find(); |
||||
|
||||
$detail["user"]=$user; |
||||
//空 0 false都为false |
||||
$detail["is_like"]= $like ? 1 : 0; |
||||
$detail["is_collect"]= $collect ? 1 : 0; |
||||
|
||||
$this->success("查看成功",$detail); |
||||
} |
||||
|
||||
public function user(){ |
||||
$userId = $this->request->get("uesr_id"); |
||||
$user = Db::name("user")->where("id",$userId)->field("id,username,avatar")->find(); |
||||
$details["user"]=$user; |
||||
$this->success("查看成功",$user); |
||||
} |
||||
|
||||
/** |
||||
* 点赞 |
||||
* @return [type] [description] |
||||
*/ |
||||
public function like(){ |
||||
$params=$this->request->post(); |
||||
$user_id = 47; |
||||
$currentTime = date("Y-m-d H:i:s"); |
||||
|
||||
|
||||
$data = Db::name("dynamic_form")->where("id",$params["item_id"])->find(); |
||||
if(!$data){ |
||||
$this->error("点赞对象不存在"); |
||||
} |
||||
$like = Db::name("like")->where("item_id",$params["item_id"])->where("user_id",$user_id)->find(); |
||||
$totalLike = $data["total_likes"]; |
||||
|
||||
if(!$like){ |
||||
$data = [ |
||||
"user_id"=>$user_id, |
||||
"item_type"=>$params["item_type"], |
||||
"item_id"=>$params["item_id"], |
||||
"status"=>"1", |
||||
"create_time"=>$currentTime, |
||||
"update_time"=>$currentTime, |
||||
]; |
||||
|
||||
$res = Db::name("like")->insert( $data); |
||||
} else{ |
||||
|
||||
if($like["status"]==1){ |
||||
$res = Db::name("like")->where("id",$like["id"])->update(["status"=>"0","update_time"=>$currentTime]); |
||||
//添加点赞总数 |
||||
Db::name("dynamic_form")->where ("id",$like["item_id"])->update(["total_likes"=>$totalLike-1]); |
||||
|
||||
} else { |
||||
$res = Db::name("like")->where("id",$like["id"])->update(["status"=>"1","update_time"=>$currentTime]); |
||||
Db::name("dynamic_form")->where ("id",$like["item_id"])->update(["total_likes"=>$totalLike+1]); |
||||
|
||||
} |
||||
|
||||
|
||||
} |
||||
$this->success("succ", $res); |
||||
} |
||||
|
||||
public function collect(){ |
||||
$params=$this->request->post(); |
||||
$user_id = 47; |
||||
$currentTime = date("Y-m-d H:i:s"); |
||||
|
||||
|
||||
$data = Db::name("dynamic_form")->where("id",$params["item_id"])->find(); |
||||
if(!$data){ |
||||
$this->error("收藏动态不存在"); |
||||
} |
||||
$collect = Db::name("collect")->where("item_id",$params["item_id"])->where("user_id",$user_id)->find(); |
||||
|
||||
$totalCollect = $data["total_collect"]; |
||||
|
||||
if(!$collect){ |
||||
$data = [ |
||||
"user_id"=>$user_id, |
||||
"item_type"=>$params["item_type"], |
||||
"item_id"=>$params["item_id"], |
||||
"status"=>"1", |
||||
"create_time"=>$currentTime, |
||||
"update_time"=>$currentTime, |
||||
]; |
||||
|
||||
$res = Db::name("collect")->insert( $data); |
||||
} else{ |
||||
|
||||
if($collect["status"]==1){ |
||||
$res = Db::name("collect")->where("id",$collect["id"])->update(["status"=>"0","update_time"=>$currentTime]); |
||||
//添加收藏总数 |
||||
Db::name("dynamic_form")->where ("id",$collect["item_id"])->update(["total_collect"=>$totalCollect-1]); |
||||
|
||||
} else { |
||||
$res = Db::name("collect")->where("id",$collect["id"])->update(["status"=>"1","update_time"=>$currentTime]); |
||||
Db::name("dynamic_form")->where ("id",$collect["item_id"])->update(["total_collect"=>$totalCollect+1]); |
||||
|
||||
} |
||||
|
||||
|
||||
} |
||||
$this->success("succ", $res); |
||||
} |
||||
|
||||
/** |
||||
* 评论 |
||||
* @return [type] [description] |
||||
*/ |
||||
public function comment (){ |
||||
$params=$this->request->post(); |
||||
$currentTime = date("Y-m-d H:i:s"); |
||||
|
||||
$dynamic =Db::name("dynamic_form")->where("id",$params["item_id"])->find(); |
||||
|
||||
|
||||
if(!$dynamic){ |
||||
$this->error("动态不存在", $params); |
||||
} |
||||
|
||||
$inData = [ |
||||
"user_id"=>$dynamic["user_id"], |
||||
"item_id"=>$dynamic["id"], |
||||
"item_type"=>$params["item_type"], |
||||
"content"=>$params["content"], |
||||
"create_time"=>$currentTime, |
||||
"update_time"=>$currentTime, |
||||
]; |
||||
|
||||
$res = Db::name("comment")->insert($inData); |
||||
if($res === false){ |
||||
$this->error("服务器异常,请稍后重试"); |
||||
} |
||||
$upData = [ |
||||
"total_comments"=>$dynamic["total_comments"] + 1 |
||||
]; |
||||
$res= Db::name("dynamic_form")->where("id",$dynamic["id"])->update($upData); |
||||
if($res===false){ |
||||
$this->error("服务器异常,请稍后重试"); |
||||
} |
||||
$this->success("succ"); |
||||
} |
||||
|
||||
/** |
||||
* 查看评论数 |
||||
* @return [type] [description] |
||||
*/ |
||||
public function commentList(){ |
||||
$params=$this->request->get(); |
||||
$page =$params["page"]; |
||||
$pageSize =$params["pageSize"]; |
||||
// 查询评论集合 |
||||
$comments = Db::name("comment") |
||||
->alias("c") |
||||
->join("user u", "c.user_id = u.id") |
||||
->field("c.*, u.avatar, u.username") |
||||
->limit(($page - 1) * $pageSize, $pageSize) |
||||
->select(); |
||||
/* foreach ($comments as $comment) { |
||||
echo "评论ID:" . $comment['id'] . "<br>"; |
||||
echo "评论内容:" . $comment['content'] . "<br>"; |
||||
echo "评论时间:" . $comment['create_time'] . "<br>"; |
||||
echo "评论人名字:" . $comment['username'] . "<br>"; |
||||
echo "评论人头像:" . $comment['avatar'] . "<br>"; |
||||
}*/ |
||||
|
||||
$this->success($comments); |
||||
} |
||||
|
||||
/** |
||||
* 回复 |
||||
* @return [type] [description] |
||||
*/ |
||||
public function reply(){ |
||||
$params=$this->request->post(); |
||||
$user_id = 47; |
||||
$currentTime = date("Y-m-d H:i:s"); |
||||
|
||||
//1是回复评论,2是回复回复 |
||||
switch ($params["item_type"]) { |
||||
case '1': |
||||
$info = Db::name("comment")->where("id",$params["item_id"])->find(); |
||||
|
||||
// $dynamic =Db::name("dynamic_form")->where("id", $info["item_id"])->find(); |
||||
break; |
||||
case '2': |
||||
$info = Db::name('reply')->where("id",$params["item_id"])->find(); |
||||
break; |
||||
|
||||
} |
||||
|
||||
|
||||
if(!$info){ |
||||
$this->error("该评论或回复不存在"); |
||||
} |
||||
$totalReply = $info["total_reply"]; |
||||
|
||||
$inData = [ |
||||
"item_type"=>$params["item_type"], |
||||
"item_id"=>$params["item_id"], |
||||
"user_id"=>$user_id, |
||||
"content"=>$params["content"], |
||||
"repllied_user_id"=> $info["user_id"], |
||||
"create_time"=>$currentTime, |
||||
"update_time"=>$currentTime, |
||||
"comment_id"=>$params["item_id"] |
||||
]; |
||||
|
||||
$res = Db::name("reply")->insert($inData); |
||||
|
||||
|
||||
|
||||
//更新回复数 |
||||
switch($params["item_type"]){ |
||||
case '1': |
||||
$res = Db::name("comment")->where('id', $info['id'])->update(["total_reply"=>$totalReply + 1]); |
||||
|
||||
break; |
||||
|
||||
case '2': |
||||
$res = Db::name("reply")->where('id', $info['id'])->update(["total_reply"=>$totalReply + 1]); |
||||
break; |
||||
} |
||||
// Db::name("dynamic_form")->where('id',$dynamic["item_id"])->update(["total_reply"=>$totalReply+1]); |
||||
$this->success($res); |
||||
} |
||||
|
||||
/** |
||||
* 关注 |
||||
* @return [type] [description] |
||||
*/ |
||||
public function follow(){ |
||||
$params = $this->request->post(); |
||||
$user_id = $this->auth->id; |
||||
$currentTime = date("Y-m-d H:i:s"); |
||||
$follow = Db::name("follow")->where("user_id",$user_id) |
||||
->where("item_type",$params["item_type"]) |
||||
->where("item_id",$params["item_id"])->find(); |
||||
|
||||
//数据不存在进行新增,存在进行判断 |
||||
if(!$follow){ |
||||
$inData = [ |
||||
"item_type"=>$params["item_type"], |
||||
"item_id"=>$params["item_id"], |
||||
"user_id"=>$user_id, |
||||
"status"=>1, |
||||
"create_time"=>$currentTime, |
||||
"update_time"=>$currentTime, |
||||
]; |
||||
|
||||
Db::name("follow")->insert($inData); |
||||
}else if($follow["status"]==0){ |
||||
Db::name("follow")->where("id",$follow["id"])->update(["status"=>1]); |
||||
}else{ |
||||
Db::name("follow")->where("id",$follow["id"])->update(["status"=>0]); |
||||
} |
||||
$this->success("succ"); |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 我的关注 |
||||
* @return [type] [description] |
||||
*/ |
||||
public function myFocus(){ |
||||
$params = $this->request->get(); |
||||
$user_id = $this->auth->id; |
||||
$followList = Db::name("follow")->where("user_id",$user_id)->where("status",1)->select(); |
||||
foreach($followList as $followUser){ |
||||
$userInfo = Db::name("user")->where("id",$followUser["item_id"])->field("id,username,avatar")->find(); |
||||
$followUser["user"]=$userInfo; |
||||
} |
||||
|
||||
$this->success($followList); |
||||
} |
||||
|
||||
/** |
||||
* 我的粉丝 |
||||
*/ |
||||
public function MyFans(){ |
||||
$params = $this->request->get(); |
||||
$user_id = $this->auth->id; |
||||
$followList = Db::name("follow")->where("item_id",$user_id)->where("status",1)->select(); |
||||
foreach($followList as $followUser){ |
||||
$userInfo = Db::name("user")->where("id",$followUser["item_id"])->field("id,username,avatar")->find(); |
||||
$followUser["user"]=$userInfo; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 轮播图 |
||||
* @return [type] [description] |
||||
*/ |
||||
public function slideshow(){ |
||||
$params = $this->request->get(); |
||||
$show = Db::name("slideshow")->where("status",$params["status"])->order('sort DESC, create_time DESC')->select(); |
||||
$this->success($show); |
||||
|
||||
} |
||||
|
||||
public function new(){ |
||||
$page = isset($_GET['page']); |
||||
$limit = 10; |
||||
$offset = ($page - 1) * $limit; |
||||
|
||||
$newList = Db::name("new")->where("status",1)->limit($offset, $limit)->select(); |
||||
if(empty($newList)){ |
||||
$this->success("新闻数据为空"); |
||||
} |
||||
$this->success($newList); |
||||
|
||||
} |
||||
|
||||
public function newDetail(){ |
||||
$id = isset($_GET['id']); |
||||
|
||||
$newDetail = Db::name("new")->where("status",1)->where("id",$id)->select(); |
||||
if(!$newDetail){ |
||||
$this->success("新闻数据为空"); |
||||
} |
||||
$this->success($newDetail); |
||||
|
||||
} |
||||
|
||||
public function category(){ |
||||
$params = $this->request->get(); |
||||
$category = Db::name("category_type")->where("status",1)->where("type",2)->select(); |
||||
$this->success($category); |
||||
} |
||||
|
||||
public function course(){ |
||||
$params = $this->request->get(); |
||||
$page = isset($_GET['page']); |
||||
$limit = 10; |
||||
$offset = ($page - 1) * $limit; |
||||
|
||||
$courseList = Db::name("course")->where("status",1)->where("item_type",$params["item_type"]) |
||||
->where("item_id",$params["item_id"])->limit($offset, $limit)->select(); |
||||
if(!$courseList){ |
||||
$this->success("课程数据为空"); |
||||
} |
||||
$this->success($courseList); |
||||
|
||||
} |
||||
|
||||
public function courseDetail(){ |
||||
$id = isset($_GET['id']); |
||||
|
||||
$newDetail = Db::name("course")->where("status",1)->where("item_type",$params["item_type"]) |
||||
->where("item_id",$params["item_id"])->where("id",$id)->select(); |
||||
if(!$newDetail){ |
||||
$this->success("新闻数据为空"); |
||||
} |
||||
$this->success($newDetail); |
||||
|
||||
} |
||||
|
||||
} |
@ -1,56 +1,56 @@ |
||||
<?php |
||||
|
||||
// +---------------------------------------------------------------------- |
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
||||
// +---------------------------------------------------------------------- |
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. |
||||
// +---------------------------------------------------------------------- |
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
||||
// +---------------------------------------------------------------------- |
||||
// | Author: liu21st <liu21st@gmail.com> |
||||
// +---------------------------------------------------------------------- |
||||
|
||||
use think\Env; |
||||
|
||||
return [ |
||||
// 数据库类型 |
||||
'type' => Env::get('database.type', 'mysql'), |
||||
// 服务器地址 |
||||
'hostname' => Env::get('database.hostname', '127.0.0.1'), |
||||
// 数据库名 |
||||
'database' => Env::get('database.database', 'hzy'), |
||||
// 用户名 |
||||
'username' => Env::get('database.username', 'hzy'), |
||||
// 密码 |
||||
'password' => Env::get('database.password', 'hzy'), |
||||
// 端口 |
||||
'hostport' => Env::get('database.hostport', ''), |
||||
// 连接dsn |
||||
'dsn' => '', |
||||
// 数据库连接参数 |
||||
'params' => [], |
||||
// 数据库编码默认采用 utf8mb4 |
||||
'charset' => Env::get('database.charset', 'utf8mb4'), |
||||
// 数据库表前缀 |
||||
'prefix' => Env::get('database.prefix', 'fa_'), |
||||
// 数据库调试模式 |
||||
'debug' => Env::get('database.debug', false), |
||||
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) |
||||
'deploy' => 0, |
||||
// 数据库读写是否分离 主从式有效 |
||||
'rw_separate' => false, |
||||
// 读写分离后 主服务器数量 |
||||
'master_num' => 1, |
||||
// 指定从服务器序号 |
||||
'slave_no' => '', |
||||
// 是否严格检查字段是否存在 |
||||
'fields_strict' => true, |
||||
// 数据集返回类型 |
||||
'resultset_type' => 'array', |
||||
// 自动写入时间戳字段 |
||||
'auto_timestamp' => false, |
||||
// 时间字段取出后的默认时间格式,默认为Y-m-d H:i:s |
||||
'datetime_format' => false, |
||||
// 是否需要进行SQL性能分析 |
||||
'sql_explain' => false, |
||||
]; |
||||
<?php |
||||
|
||||
// +---------------------------------------------------------------------- |
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
||||
// +---------------------------------------------------------------------- |
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved. |
||||
// +---------------------------------------------------------------------- |
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
||||
// +---------------------------------------------------------------------- |
||||
// | Author: liu21st <liu21st@gmail.com> |
||||
// +---------------------------------------------------------------------- |
||||
|
||||
use think\Env; |
||||
|
||||
return [ |
||||
// 数据库类型 |
||||
'type' => Env::get('database.type', 'mysql'), |
||||
// 服务器地址 |
||||
'hostname' => Env::get('database.hostname', '127.0.0.1'), |
||||
// 数据库名 |
||||
'database' => Env::get('database.database', 'hzy'), |
||||
// 用户名 |
||||
'username' => Env::get('database.username', 'hzy'), |
||||
// 密码 |
||||
'password' => Env::get('database.password', 'hzy'), |
||||
// 端口 |
||||
'hostport' => Env::get('database.hostport', ''), |
||||
// 连接dsn |
||||
'dsn' => '', |
||||
// 数据库连接参数 |
||||
'params' => [], |
||||
// 数据库编码默认采用 utf8mb4 |
||||
'charset' => Env::get('database.charset', 'utf8mb4'), |
||||
// 数据库表前缀 |
||||
'prefix' => Env::get('database.prefix', 'fa_'), |
||||
// 数据库调试模式 |
||||
'debug' => Env::get('database.debug', false), |
||||
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) |
||||
'deploy' => 0, |
||||
// 数据库读写是否分离 主从式有效 |
||||
'rw_separate' => false, |
||||
// 读写分离后 主服务器数量 |
||||
'master_num' => 1, |
||||
// 指定从服务器序号 |
||||
'slave_no' => '', |
||||
// 是否严格检查字段是否存在 |
||||
'fields_strict' => true, |
||||
// 数据集返回类型 |
||||
'resultset_type' => 'array', |
||||
// 自动写入时间戳字段 |
||||
'auto_timestamp' => false, |
||||
// 时间字段取出后的默认时间格式,默认为Y-m-d H:i:s |
||||
'datetime_format' => false, |
||||
// 是否需要进行SQL性能分析 |
||||
'sql_explain' => false, |
||||
]; |
||||
|
@ -1 +0,0 @@ |
||||
|
@ -0,0 +1 @@ |
||||
/.idea |
@ -0,0 +1,21 @@ |
||||
The MIT License (MIT) |
||||
|
||||
Copyright (c) 2020 nelsons |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
of this software and associated documentation files (the "Software"), to deal |
||||
in the Software without restriction, including without limitation the rights |
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
copies of the Software, and to permit persons to whom the Software is |
||||
furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in all |
||||
copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
SOFTWARE. |
@ -0,0 +1,36 @@ |
||||
{ |
||||
"name": "nelsonkti/sensitive-word", |
||||
"description": "敏感词", |
||||
"keywords": ["laravel", "敏感词", "sensitive", "word", "DFA"], |
||||
"type": "library", |
||||
"license": "MIT", |
||||
"support": { |
||||
"email": "nelsons.goole@gmail.com" |
||||
}, |
||||
"authors": [ |
||||
{ |
||||
"name": "nelsonkti", |
||||
"email": "nelsons.goole@gmail.com" |
||||
} |
||||
], |
||||
"require": { |
||||
"php":">=5.6" |
||||
}, |
||||
"require-dev": { |
||||
"phpunit/phpunit": "~4.1", |
||||
"mockery/mockery": "dev-master" |
||||
}, |
||||
"autoload": { |
||||
"psr-4": { |
||||
"Nelsonkti\\SensitiveWord\\": "src/" |
||||
} |
||||
}, |
||||
"extra": { |
||||
"branch-alias": { |
||||
"dev-master": "1.0-dev" |
||||
} |
||||
}, |
||||
"minimum-stability": "dev", |
||||
"prefer-stable": true |
||||
|
||||
} |
@ -0,0 +1,14 @@ |
||||
<?php |
||||
|
||||
|
||||
namespace Nelsonkti\SensitiveWord\Facades; |
||||
|
||||
use Illuminate\Support\Facades\Facade; |
||||
|
||||
class SensitiveWord extends Facade |
||||
{ |
||||
protected static function getFacadeAccessor() |
||||
{ |
||||
return 'Nelsonkti\SensitiveWord\SensitiveWord'; |
||||
} |
||||
} |
@ -0,0 +1,250 @@ |
||||
<?php |
||||
|
||||
|
||||
namespace Nelsonkti\SensitiveWord; |
||||
|
||||
|
||||
class SensitiveWord |
||||
{ |
||||
/** |
||||
* 替换码 |
||||
* |
||||
* @var string |
||||
*/ |
||||
private $replaceCode = '*'; |
||||
|
||||
/** |
||||
* 敏感词库集合 |
||||
* |
||||
* @var array |
||||
*/ |
||||
protected $trieTreeMap = array(); |
||||
|
||||
/** |
||||
* 干扰因子集合 |
||||
* |
||||
* @var array |
||||
*/ |
||||
private $disturbList = array('*'); |
||||
|
||||
/** |
||||
* 文件路径 |
||||
* |
||||
* @var string |
||||
*/ |
||||
private $filename = null; |
||||
|
||||
/** |
||||
* 敏感词树 |
||||
* |
||||
* @var array |
||||
*/ |
||||
private static $sensitiveWordTree = []; |
||||
|
||||
/** |
||||
* 干扰因子集合 |
||||
* |
||||
* @param array $disturbList |
||||
*/ |
||||
public function interference($disturbList = array()) |
||||
{ |
||||
$this->disturbList = $disturbList ?? $this->disturbList; |
||||
} |
||||
|
||||
/** |
||||
* 设置文件路径 |
||||
* |
||||
* @param $filename |
||||
*/ |
||||
protected function setFileName($filename) |
||||
{ |
||||
$this->filename = $filename; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* 获取文件内容 |
||||
* |
||||
* @param $filename "文件路径" |
||||
* @return \Generator |
||||
* @throws \Exception |
||||
*/ |
||||
protected function getFileContent() |
||||
{ |
||||
$handle = fopen($this->filename, 'r'); |
||||
|
||||
if (!$handle) { |
||||
throw new \Exception('open the file failed'); |
||||
} |
||||
|
||||
while (!feof($handle)) { |
||||
yield str_replace(['\'', ' ', PHP_EOL, ','], '', fgets($handle)); |
||||
} |
||||
|
||||
fclose($handle); |
||||
} |
||||
|
||||
/** |
||||
* 生成敏感词库集合 |
||||
* |
||||
* @param $filename "文件路径" |
||||
* @throws \Exception |
||||
*/ |
||||
protected function generateWords() |
||||
{ |
||||
// 获取文件内容 |
||||
$text = $this->getFileContent(); |
||||
|
||||
foreach ($text as $key => $words) { |
||||
$len = mb_strlen($words); |
||||
$treeArr = &$this->trieTreeMap; |
||||
for ($i = 0; $i < $len; $i++) { |
||||
$word = mb_substr($words, $i, 1); |
||||
//敏感词树结尾记录状态为false; |
||||
if ($i + 1 == $len) { |
||||
$treeArr[$word]['end'] = false; |
||||
} |
||||
$treeArr = &$treeArr[$word] ?? false; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 获取敏感词库集合 |
||||
* |
||||
* @param $filename "文件路径" |
||||
*/ |
||||
private function getTrieTreeMap() |
||||
{ |
||||
$trieTreeMap = &$this->trieTreeMap; |
||||
|
||||
if (!$trieTreeMap) { |
||||
$this->generateWords($this->filename); |
||||
} |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* 匹配对应敏感词 |
||||
* |
||||
* @param $txt "内容" |
||||
* @param bool $hasReplace "是否替换原内容" |
||||
* @param array $replaceCodeList "替换符合" |
||||
* @return array |
||||
*/ |
||||
private function getWord($txt, $hasReplace = false, &$replaceCodeList = array()) |
||||
{ |
||||
$wordsList = $wordsListArr = array(); |
||||
$txtLength = mb_strlen($txt); |
||||
for ($i = 0; $i < $txtLength; $i++) { |
||||
$wordLength = $this->checkWord($txt, $i, $txtLength); |
||||
if ($wordLength > 0) { |
||||
$words = mb_substr($txt, $i, $wordLength); |
||||
|
||||
if ($hasReplace) { |
||||
$wordsListArr[] = array( |
||||
'length' => strlen($words), |
||||
'world' => $words, |
||||
'replace_code' => str_repeat($this->replaceCode, mb_strlen($words)) |
||||
); |
||||
} else { |
||||
$wordsList[] = $words; |
||||
} |
||||
|
||||
$i += $wordLength - 1; |
||||
} |
||||
} |
||||
|
||||
$hasReplace && $wordsList = $this->sortWord($wordsListArr, $replaceCodeList); |
||||
|
||||
return $wordsList; |
||||
} |
||||
|
||||
/** |
||||
* 对敏感词按长度进行倒叙排序 |
||||
* |
||||
* @param $wordsList |
||||
* @param $replaceCodeList |
||||
* @param $txt |
||||
*/ |
||||
private function sortWord($wordsListArr, &$replaceCodeList) |
||||
{ |
||||
$array_column = array_column($wordsListArr, 'length'); |
||||
array_multisort($array_column, SORT_DESC, $wordsListArr); |
||||
|
||||
$replaceCodeList = array_column($wordsListArr, 'replace_code'); |
||||
|
||||
return array_column($wordsListArr, 'world'); |
||||
} |
||||
|
||||
/** |
||||
* 查找对应敏感词 |
||||
* |
||||
* @param $txt "内容" |
||||
* @param bool $hasReplace "是否替换原内容" |
||||
* @param array $replaceCodeList "替换符合" |
||||
* @return array |
||||
*/ |
||||
public function searchWord($txt, $filename) |
||||
{ |
||||
return $this->setFileName($filename)->getTrieTreeMap()->getWord($txt); |
||||
} |
||||
|
||||
/** |
||||
* 过滤敏感词 |
||||
* |
||||
* @param $txt "内容" |
||||
* @param $filename "文件路径" |
||||
* @return string|string[] |
||||
*/ |
||||
public function filterWord($txt, $filename) |
||||
{ |
||||
$filename && $this->setFileName($filename)->interference(); |
||||
|
||||
$replaceCodeList = array(); |
||||
|
||||
$wordsList = $this->getTrieTreeMap()->getWord($txt, true, $replaceCodeList); |
||||
|
||||
|
||||
return $wordsList ? str_replace($wordsList, $replaceCodeList, $txt) : $txt; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 敏感词检测 |
||||
* |
||||
* @param $txt "内容" |
||||
* @param $begin "开始位置" |
||||
* @param $length "长度" |
||||
* @return int |
||||
*/ |
||||
private function checkWord($txt, $begin, $length) |
||||
{ |
||||
$treeArr = &$this->trieTreeMap; |
||||
$wordLength = 0; //敏感字符个数 |
||||
$wordLengthArray = []; |
||||
$flag = false; |
||||
for ($i = $begin; $i < $length; $i++) { |
||||
$txtWord = mb_substr($txt, $i, 1); |
||||
|
||||
//如果搜索字不存在词库中直接停止循环。 |
||||
if (!isset($treeArr[$txtWord])) { |
||||
break; |
||||
} |
||||
|
||||
$wordLength++; |
||||
if (isset($treeArr[$txtWord]['end'])) { |
||||
$flag = true; |
||||
$wordLengthArray[] = $wordLength; |
||||
} |
||||
$treeArr = &$treeArr[$txtWord]; |
||||
} |
||||
|
||||
$flag ?: $wordLength = 0; |
||||
|
||||
return $wordLength; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,40 @@ |
||||
<?php |
||||
|
||||
namespace Nelsonkti\SensitiveWord; |
||||
|
||||
use Illuminate\Support\ServiceProvider; |
||||
|
||||
class SensitiveWordServiceProvider extends ServiceProvider |
||||
{ |
||||
/** |
||||
* Register services. |
||||
* |
||||
* @return void |
||||
*/ |
||||
public function register() |
||||
{ |
||||
$this->app->singleton('Nelsonkti\SensitiveWord\Facades\SensitiveWordFacade', function ($app) { |
||||
return 'Nelsonkti\SensitiveWord\SensitiveWord'; |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* Bootstrap services. |
||||
* |
||||
* @return void |
||||
*/ |
||||
public function boot() |
||||
{ |
||||
// |
||||
} |
||||
|
||||
/** |
||||
* Get the services provided by the provider. |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function provides() |
||||
{ |
||||
return ['Nelsonkti\SensitiveWord\SensitiveWord', 'SensitiveWord']; |
||||
} |
||||
} |
Loading…
Reference in new issue