代码优化

This commit is contained in:
Anyon 2020-09-02 16:13:19 +08:00
parent 5d5e38361f
commit ffe2cb932a
5 changed files with 39 additions and 36 deletions

View File

@ -43,30 +43,13 @@ class News extends Controller
$this->app->db->name('DataNewsHistory')->insert($history); $this->app->db->name('DataNewsHistory')->insert($history);
} }
} }
$query = $this->_query('DataNewsItem')->equal('id')->like('title,mark'); $query = $this->_query('DataNewsItem')->like('title,mark')->equal('id');
$query->where(['deleted' => 0, 'status' => 1])->withoutField('sort,status,deleted'); $query->where(['deleted' => 0, 'status' => 1])->withoutField('sort,status,deleted');
$result = $query->order('sort desc,id desc')->page(true, false, false, 15); $result = $query->order('sort desc,id desc')->page(true, false, false, 15);
if (count($result['list']) > 0 && input('mid') > 0) { NewsService::instance()->buildListState($result['list'], input('mid'));
NewsService::instance()->buildListState($result['list'], input('mid'));
}
$this->success('获取文章内容列表', $result); $this->success('获取文章内容列表', $result);
} }
/**
* 获取文章评论
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getComment()
{
$data = $this->_vali(['cid.require' => '文章ID不能为空']);
$query = $this->_query('DataNewsXComment')->where($data);
$result = $query->order('id desc')->page(false, false, false, 5);
NewsService::instance()->buildListByMid($result['list']);
$this->success('获取文章评论成功!', $result);
}
/** /**
* 获取已点赞的会员 * 获取已点赞的会员
*/ */
@ -86,4 +69,20 @@ class News extends Controller
$query = $this->app->db->name('DataNewsXCollect')->where($data); $query = $this->app->db->name('DataNewsXCollect')->where($data);
$this->success('获取已收藏的会员', ['list' => $query->order('mid asc')->column('mid')]); $this->success('获取已收藏的会员', ['list' => $query->order('mid asc')->column('mid')]);
} }
/**
* 获取文章评论
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getComment()
{
$data = $this->_vali(['cid.require' => '文章ID不能为空']);
$query = $this->_query('DataNewsXComment')->where($data);
$result = $query->order('id desc')->page(false, false, false, 5);
NewsService::instance()->buildListByMid($result['list']);
$this->success('获取文章评论成功!', $result);
}
} }

View File

@ -89,7 +89,7 @@ class Center extends Member
public function getFrom() public function getFrom()
{ {
$query = $this->_query($this->table); $query = $this->_query($this->table);
$query->where(['from' => $this->mid])->field('id,from,user,nickname,headimg,create_at'); $query->where(['from' => $this->mid])->field('id,from,username,nickname,headimg,create_at');
$this->success('获取我邀请的朋友', $query->order('id desc')->page(true, false, false, 15)); $this->success('获取我邀请的朋友', $query->order('id desc')->page(true, false, false, 15));
} }
} }

View File

@ -24,7 +24,7 @@ class News extends Member
'content.require' => '评论内容不能为空!', 'content.require' => '评论内容不能为空!',
]); ]);
if ($this->app->db->name('DataNewsXComment')->insert($data) !== false) { if ($this->app->db->name('DataNewsXComment')->insert($data) !== false) {
NewsService::instance()->syncTotal($data['cid']); NewsService::instance()->syncNewsTotal($data['cid']);
$this->success('添加评论成功!'); $this->success('添加评论成功!');
} else { } else {
$this->error('添加评论失败!'); $this->error('添加评论失败!');
@ -73,7 +73,7 @@ class News extends Member
$this->success('您已收藏!'); $this->success('您已收藏!');
} }
if ($this->app->db->name('DataNewsXCollect')->insert($data) !== false) { if ($this->app->db->name('DataNewsXCollect')->insert($data) !== false) {
NewsService::instance()->syncTotal($data['cid']); NewsService::instance()->syncNewsTotal($data['cid']);
$this->success('收藏成功!'); $this->success('收藏成功!');
} else { } else {
$this->error('收藏失败!'); $this->error('收藏失败!');
@ -88,7 +88,7 @@ class News extends Member
{ {
$data = $this->_vali(['mid.value' => $this->mid, 'cid.require' => '文章ID不能为空']); $data = $this->_vali(['mid.value' => $this->mid, 'cid.require' => '文章ID不能为空']);
if ($this->app->db->name('DataNewsXCollect')->where($data)->delete() !== false) { if ($this->app->db->name('DataNewsXCollect')->where($data)->delete() !== false) {
NewsService::instance()->syncTotal($data['cid']); NewsService::instance()->syncNewsTotal($data['cid']);
$this->success('取消收藏成功!'); $this->success('取消收藏成功!');
} else { } else {
$this->error('取消收藏失败!'); $this->error('取消收藏失败!');
@ -118,7 +118,7 @@ class News extends Member
$this->success('您已点赞!'); $this->success('您已点赞!');
} }
if ($this->app->db->name('DataNewsXLike')->insert($data) !== false) { if ($this->app->db->name('DataNewsXLike')->insert($data) !== false) {
NewsService::instance()->syncTotal($data['cid']); NewsService::instance()->syncNewsTotal($data['cid']);
$this->success('点赞成功!'); $this->success('点赞成功!');
} else { } else {
$this->error('点赞失败!'); $this->error('点赞失败!');
@ -133,7 +133,7 @@ class News extends Member
{ {
$data = $this->_vali(['mid.value' => $this->mid, 'cid.require' => '内容ID不能为空']); $data = $this->_vali(['mid.value' => $this->mid, 'cid.require' => '内容ID不能为空']);
if ($this->app->db->name('DataNewsXLike')->where($data)->delete() !== false) { if ($this->app->db->name('DataNewsXLike')->where($data)->delete() !== false) {
NewsService::instance()->syncTotal($data['cid']); NewsService::instance()->syncNewsTotal($data['cid']);
$this->success('取消点赞成功!'); $this->success('取消点赞成功!');
} else { } else {
$this->error('取消点赞失败!'); $this->error('取消点赞失败!');

View File

@ -29,10 +29,12 @@ class MemberService extends Service
*/ */
public function get(string $token, array $data = []): array public function get(string $token, array $data = []): array
{ {
$query = $this->app->db->name($this->table); $map = ['token' => $token, 'deleted' => 0];
$query->where(['token' => $token, 'deleted' => 0]); $query = $this->app->db->name($this->table)->where($map);
$member = $query->withoutField('status,deleted')->find(); $member = $query->withoutField('status,deleted')->find();
if (empty($member)) throw new \think\Exception('会员查询失败'); if (empty($member)) {
throw new \think\Exception('登录授权失败');
}
if ($member['tokenv'] !== $this->buildTokenVerify()) { if ($member['tokenv'] !== $this->buildTokenVerify()) {
throw new \think\Exception('请重新登录授权'); throw new \think\Exception('请重新登录授权');
} }
@ -51,13 +53,15 @@ class MemberService extends Service
*/ */
public function token(int $mid, array $data = []): array public function token(int $mid, array $data = []): array
{ {
do $up = ['token' => md5(uniqid("{$mid}#", true) . rand(100, 999))]; // 生成新的接口令牌
while ($this->app->db->name($this->table)->where($up)->count() > 0); do $update = ['token' => md5(uniqid("{$mid}#", true) . rand(100, 999))];
$count = $this->app->db->name($this->table)->where(['id' => $mid, 'deleted' => 0])->update([ while ($this->app->db->name($this->table)->where($update)->count() > 0);
'token' => $up['token'], 'tokenv' => $this->buildTokenVerify(), // 更新账号授权令牌
$this->app->db->name($this->table)->where(['id' => $mid, 'deleted' => 0])->update([
'token' => $update['token'], 'tokenv' => $this->buildTokenVerify(),
]); ]);
if ($count < 1) throw new \think\Exception('生成授权TOKEN失败'); // 获取新的会员数据
return $this->get($up['token'], $data); return $this->get($update['token'], $data);
} }
/** /**
@ -68,7 +72,7 @@ class MemberService extends Service
public function total(int $mid): array public function total(int $mid): array
{ {
$query = $this->app->db->name($this->table); $query = $this->app->db->name($this->table);
return ['myinvited' => $query->where(['from' => $mid])->count()]; return ['my_invite' => $query->where(['from' => $mid])->count()];
} }
/** /**

View File

@ -16,7 +16,7 @@ class NewsService extends Service
* @param integer $cid 文章ID * @param integer $cid 文章ID
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
*/ */
public function syncTotal($cid) public function syncNewsTotal($cid)
{ {
$this->app->db->name('DataNewsItem')->where(['id' => $cid])->update([ $this->app->db->name('DataNewsItem')->where(['id' => $cid])->update([
'num_like' => $this->app->db->name('DataNewsXLike')->where(['cid' => $cid])->count(), 'num_like' => $this->app->db->name('DataNewsXLike')->where(['cid' => $cid])->count(),