Skip to content

Commit

Permalink
聊天室优化
Browse files Browse the repository at this point in the history
添加定时器 每隔5s检测客户端是否已断开 不刷新实现断线重连

归档链接优化
归档文章不存在时跳转到404页面
修复404页面bug
  • Loading branch information
nangge committed Apr 8, 2018
1 parent 8c8eece commit 4dfb9ed
Show file tree
Hide file tree
Showing 17 changed files with 739 additions and 219 deletions.
12 changes: 9 additions & 3 deletions application/admin/controller/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use app\common\model\Category;
use app\common\model\Article as articleModel;
use think\Exception;

class Article extends Common
{
Expand Down Expand Up @@ -181,19 +182,24 @@ public function copy()

try {
\phpQuery::newDocumentFile($url);
$title = pq('.link_title a')->text();
$title = pq('.link_title')->text();
if (!$title) {
$title = pq('.list_c_t a')->text();
}
if (!$title) {
$title = pq('h1.csdn_top')->text();
}
$title = trim($title);

if(mb_strlen($title,'utf-8')>60){
return ['status' => 0, 'msg' => '转载失败,文章标题超过60字', 'url' => ''];
}

$content = pq('#article_content')->text();

//如果抓取不到主内容
// //如果抓取不到主内容
if (!$content) {
throw new Exception("文章不存在或禁止爬虫");

}
$params['cid'] = $cid;
$params['content'] = $content;
Expand Down
8 changes: 4 additions & 4 deletions application/admin/validate/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Admin extends Validate {
protected $rule = [
'username' => 'require|max:25|token',
'email' => 'email',
'password' => 'require|min:6',
'repassword'=>'require|confirm:password'
'repassword'=>'require|min:6',
'password' => 'require|min:6|confirm:repassword',
];

protected $message = [
Expand All @@ -23,7 +23,7 @@ class Admin extends Validate {
'password.min' => '密码长度至少六位',
'email' => '邮箱格式错误',
'repassword.require' => '确认密码必须',
'repassword.confirm' => '两次密码必须一致'
'password.confirm' => '两次密码必须一致'
];

// /**
Expand All @@ -39,6 +39,6 @@ class Admin extends Validate {
// edit 验证场景定义
public function sceneEdit()
{
return $this->remove('password', 'require')->remove('repassword','require');
return $this->remove('repassword','require')->remove('password', 'require');
}
}
39 changes: 39 additions & 0 deletions application/common/lib/log/SentryLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2018/3/28
* Time: 14:44
*/

namespace app\common\lib\log;
use PHPMailer\PHPMailer\Exception;
use Raven_Client;
use Raven_ErrorHandler;

class SentryLog
{
protected $dsn = 'https://cd8fc57db1ef4d86b41d85070f933ffa:[email protected]/711251';
protected $client;
public function __construct($config)
{
if(!$config){
$this->dsn = $config;
}
try{
$sentryClient = new Raven_Client($this->dsn);
$error_handler = new Raven_ErrorHandler($sentryClient);
$error_handler->registerExceptionHandler();
$error_handler->registerErrorHandler();
$error_handler->registerShutdownFunction();
$this->client = $sentryClient;
}catch (Exception $e){
throw new Exception('Client Keys 配置不正确');
}

}

public function addLog($e,$data = []){
$this->client->captureException($e, $data);
}
}
17 changes: 17 additions & 0 deletions application/common/model/Chatrecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2018/3/29
* Time: 15:53
*/

namespace app\common\model;


use think\Model;

class Chatrecord extends Model
{
protected $autoWriteTimestamp = true;
}
17 changes: 17 additions & 0 deletions application/common/model/Chatroom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2018/4/3
* Time: 13:47
*/

namespace app\common\model;


use think\Model;

class Chatroom extends Model
{
protected $autoWriteTimestamp = true;
}
18 changes: 18 additions & 0 deletions application/common/model/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2018/3/29
* Time: 14:47
*/

namespace app\common\model;


use think\Model;

class User extends Model
{
protected $autoWriteTimestamp = true;

}
14 changes: 5 additions & 9 deletions application/index/controller/Index.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
namespace app\index\controller;

use think\facade\Config;
use think\Db;
use think\facade\Config;

use think\View;

class Index extends Common
Expand Down Expand Up @@ -56,14 +57,9 @@ public function push(){
** 异步获取聊天记录
**/
public function getMessageHis(){
$page = input('param.page');
$list = Db::name('chat')
->field("name,content,FROM_UNIXTIME(send_time,'%Y-%m-%d %H:%i') as send_date")
->order('send_time DESC')
->limit(($page-1)*10,10)
->select();

exit(json_encode(array_reverse($list)));
$page = input('param.page',0,'intval');
$list = Db::name('chatrecord')->join('user','none_user.id = none_chatrecord.user_id')->where('type',0)->page($page)->limit(10)->order('none_chatrecord.create_time desc')->column('none_chatrecord.id,content,nick,img,FROM_UNIXTIME(none_chatrecord.create_time) as create_time');
exit(json_encode($list));
}


Expand Down
19 changes: 19 additions & 0 deletions application/index/controller/Room.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2018/4/3
* Time: 13:51
*/

namespace app\index\controller;

use app\common\model\Chatroom;

class Room
{
public function getList(){
$room = Chatroom::all(['status'=>1])->hidden(['status','type']);
return json($room);
}
}
27 changes: 19 additions & 8 deletions application/index/controller/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,25 @@ public function index()
$cid = input('param.cid');//栏目id
$id = input('param.id');//资源id

//根据cid来获取表模型
$cat_info = Category::get(['id' => $cid]);
$modeln = $cat_info->modeln;

//获取资源内容
$data = Db::name($modeln['tablename'])->find($id);
Db::name($modeln['tablename'])->where('id',$id)->setInc('click');//点击+1

if($cid){
//根据cid来获取表模型
$cat_info = Category::get(['id' => $cid]);
$modeln = $cat_info->modeln;
//获取资源内容
$data = Db::name($modeln['tablename'])->find($id);
if(!$data){
$this->redirect('/error_page/404.html','文章不存在');
}
Db::name($modeln['tablename'])->where('id',$id)->setInc('click');//点击+1
}else{
$data = Db::name('article')->find($id);
if(!$data){
$this->redirect('error_page/404.html','文章不存在');
}
$cat_info = Category::get(['id' => $data['cid']]);
$modeln = $cat_info->modeln;
Db::name('article')->where('id',$id)->setInc('click');//点击+1
}
//图片处理
if(isset($data['pictureurls'])){
$data['pictureurls'] = explode('|',$data['pictureurls']);
Expand Down
22 changes: 17 additions & 5 deletions application/push/controller/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,17 @@ public function sendMessage4Type($data, $connection = '')
{
switch ($data['type']) {
case 'login':
$imgfile = file_get_contents($data['img']);
$img = "data:image/jpeg;base64,".base64_encode($imgfile);
self::$hasConnections[$connection->id] = [
'name' => $data['name'],
'id' => $connection->id
'id' => $connection->id,
'img' => $img
];
$data['img'] = $img;
$content = '欢迎 <i>' . $data['name'] . '</i> 加入聊天室! ';
//生成用户图片并base64编码保存

//拼装返回的数据结构
$back_data = [
'content' => $content,
Expand All @@ -136,10 +142,12 @@ public function sendMessage4Type($data, $connection = '')
'client_name' => $data['name'],
'type' => 'login',
'clients' => self::$hasConnections,
'time' => date('Y-m-d H:i')
'img'=>self::$hasConnections[$connection->id]['img'],
'time' => date('Y-m-d H:i'),
];
break;
case 'prisay':
$data['img'] = self::$hasConnections[$connection->id]['img'];
$recive_user = self::$hasConnections[$data['to_client_id']]['name'];//接收者
$send_user = $data['name'];//发送者
$recive_content = '<i style="color:green">' . $send_user . '</i>对你 说:' . $data['content'];
Expand All @@ -149,24 +157,28 @@ public function sendMessage4Type($data, $connection = '')
'content' => $recive_content,
'type' => 'say',
'nick' => $recive_user,
'time' => date('Y-m-d H:i')
'time' => date('Y-m-d H:i'),
'img' => $data['img']
];
$mycontent = [
'content' => $send_content,
'type' => 'say',
'nick' => $send_user,
'time' => date('Y-m-d H:i')
'time' => date('Y-m-d H:i'),
'img' => $data['img']
];
//写入
self::insert_chat($data);
break;
default:
//拼装返回的数据结构
$data['img'] = self::$hasConnections[$connection->id]['img'];
$back_data = [
'content' => $data['content'],
'nick' => $data['name'],
'type' => 'say',
'time' => date('Y-m-d H:i')
'time' => date('Y-m-d H:i'),
'img' => $data['img']
];
//写入
self::insert_chat($data);
Expand Down
Loading

0 comments on commit 4dfb9ed

Please sign in to comment.