调整标签处理机制

This commit is contained in:
Anyon 2020-09-22 16:49:32 +08:00
parent 9513874a25
commit 1008d4bff0
7 changed files with 50 additions and 83 deletions

View File

@ -2,6 +2,7 @@
namespace app\data\controller; namespace app\data\controller;
use app\data\service\NewsService;
use think\admin\Controller; use think\admin\Controller;
/** /**
@ -53,9 +54,7 @@ class NewsItem extends Controller
*/ */
protected function _page_filter(&$data) protected function _page_filter(&$data)
{ {
foreach ($data as &$vo) { NewsService::instance()->buildListState($data);
$vo['mark'] = think_string_to_array($vo['mark'] ?: '');
}
} }
/** /**

View File

@ -83,7 +83,7 @@ class ShopGoods extends Controller
$clist = $this->app->db->name('ShopGoodsCate')->whereIn('id', array_column($data, 'cate'))->column('pid,name,status', 'id'); $clist = $this->app->db->name('ShopGoodsCate')->whereIn('id', array_column($data, 'cate'))->column('pid,name,status', 'id');
foreach ($data as &$vo) { foreach ($data as &$vo) {
$vo['cate'] = $clist[$vo['cate']] ?? $vo['cate']; $vo['cate'] = $clist[$vo['cate']] ?? $vo['cate'];
$vo['mark'] = think_string_to_array($vo['mark'] ?: ''); $vo['mark'] = think_string_to_array($vo['mark'] ?: '', ',', $this->marks);
} }
} }

View File

@ -37,7 +37,7 @@ class News extends Controller
$this->app->db->name('DataNewsItem')->where(['id' => $id])->update([ $this->app->db->name('DataNewsItem')->where(['id' => $id])->update([
'num_read' => $this->app->db->raw('`num_read`+1'), 'num_read' => $this->app->db->raw('`num_read`+1'),
]); ]);
if (input('mid') > 0) { if (input('mid', 0) > 0) {
$history = ['mid' => input('mid'), 'cid' => $id]; $history = ['mid' => input('mid'), 'cid' => $id];
$this->app->db->name('DataNewsXHistory')->where($history)->delete(); $this->app->db->name('DataNewsXHistory')->where($history)->delete();
$this->app->db->name('DataNewsXHistory')->insert($history); $this->app->db->name('DataNewsXHistory')->insert($history);
@ -46,32 +46,10 @@ class News extends Controller
$query = $this->_query('DataNewsItem')->like('name,mark')->equal('id'); $query = $this->_query('DataNewsItem')->like('name,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);
NewsService::instance()->buildListState($result['list'], input('mid')); NewsService::instance()->buildListState($result['list'], input('mid', 0));
$this->success('获取文章内容列表', $result); $this->success('获取文章内容列表', $result);
} }
/**
* 获取已点赞的会员
*/
public function getLike()
{
$query = $this->app->db->name('DataNewsXCollect')->where($this->_vali([
'cid.require' => '文章ID不能为空', 'type.value' => 2,
]));
$this->success('获取已点赞的会员', ['list' => $query->order('mid asc')->column('mid')]);
}
/**
* 获取已收藏的会员
*/
public function getCollect()
{
$query = $this->app->db->name('DataNewsXCollect')->where($this->_vali([
'cid.require' => '文章ID不能为空', 'type.value' => 1,
]));
$this->success('获取已收藏的会员', ['list' => $query->order('mid asc')->column('mid')]);
}
/** /**
* 获取文章评论 * 获取文章评论
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
@ -80,10 +58,10 @@ class News extends Controller
*/ */
public function getComment() public function getComment()
{ {
$data = $this->_vali(['cid.require' => '文章ID不能为空!']); $map = $this->_vali(['cid.require' => '文章不能为空!']);
$query = $this->_query('DataNewsXComment')->where($data); $query = $this->_query('DataNewsXComment')->where($map);
$result = $query->order('id desc')->page(false, false, false, 5); $result = $query->order('id desc')->page(false, false, false, 5);
NewsService::instance()->buildListByMid($result['list']); NewsService::instance()->buildListByCidAndMid($result['list']);
$this->success('获取文章评论成功!', $result); $this->success('获取文章评论成功!', $result);
} }

View File

@ -20,8 +20,8 @@ class News extends Auth
{ {
$data = $this->_vali([ $data = $this->_vali([
'mid.value' => $this->mid, 'mid.value' => $this->mid,
'cid.require' => '文章ID不能为空!', 'cid.require' => '文章不能为空!',
'content.require' => '评论内容不能为空!', 'content.require' => '内容不能为空!',
]); ]);
if ($this->app->db->name('DataNewsXComment')->insert($data) !== false) { if ($this->app->db->name('DataNewsXComment')->insert($data) !== false) {
NewsService::instance()->syncNewsTotal($data['cid']); NewsService::instance()->syncNewsTotal($data['cid']);
@ -39,9 +39,10 @@ class News extends Auth
*/ */
public function getComment() public function getComment()
{ {
$data = $this->_vali(['mid.value' => $this->mid, 'cid.require' => '内容ID不能为空']); $map = $this->_vali(['mid.value' => $this->mid, 'cid.require' => '文章不能为空!']);
$query = $this->app->db->name('DataNewsXComment')->where($data)->order('id desc'); $result = $this->_query('DataNewsXComment')->where($map)->order('id desc')->page(true, false);
$this->success('获取评论列表成功', ['list' => $query->select()->toArray()]); NewsService::instance()->buildListByCidAndMid($result);
$this->success('获取评论列表成功', $result);
} }
/** /**
@ -50,12 +51,12 @@ class News extends Auth
*/ */
public function delComment() public function delComment()
{ {
$data = $this->_vali([ $map = $this->_vali([
'mid.value' => $this->mid, 'mid.value' => $this->mid,
'id.require' => '评论ID不能为空',
'cid.require' => '文章ID不能为空', 'cid.require' => '文章ID不能为空',
'id.require' => '评论ID不能为空',
]); ]);
if ($this->app->db->name('DataNewsXComment')->where($data)->delete() !== false) { if ($this->app->db->name('DataNewsXComment')->where($map)->delete() !== false) {
$this->success('评论删除成功!'); $this->success('评论删除成功!');
} else { } else {
$this->error('认证删除失败!'); $this->error('认证删除失败!');
@ -104,7 +105,7 @@ class News extends Auth
$map = ['mid' => $this->mid, 'type' => 1]; $map = ['mid' => $this->mid, 'type' => 1];
$query = $this->_query('DataNewsXCollect')->where($map); $query = $this->_query('DataNewsXCollect')->where($map);
$result = $query->order('id desc')->page(true, false, false, 15); $result = $query->order('id desc')->page(true, false, false, 15);
NewsService::instance()->buildListByCid($result['list']); NewsService::instance()->buildListByCidAndMid($result['list']);
$this->success('获取收藏记录成功!', $result); $this->success('获取收藏记录成功!', $result);
} }
@ -150,7 +151,7 @@ class News extends Auth
$map = ['mid' => $this->mid, 'type' => 2]; $map = ['mid' => $this->mid, 'type' => 2];
$query = $this->_query('DataNewsXCollect')->where($map); $query = $this->_query('DataNewsXCollect')->where($map);
$result = $query->order('id desc')->page(true, false, false, 15); $result = $query->order('id desc')->page(true, false, false, 15);
NewsService::instance()->buildListByCid($result['list']); NewsService::instance()->buildListByCidAndMid($result['list']);
$this->success('获取点赞记录成功!', $result); $this->success('获取点赞记录成功!', $result);
} }
@ -165,7 +166,7 @@ class News extends Auth
$map = ['mid' => $this->mid]; $map = ['mid' => $this->mid];
$query = $this->_query('DataNewsXHistory')->where($map); $query = $this->_query('DataNewsXHistory')->where($map);
$result = $query->order('id desc')->page(true, false, false, 15); $result = $query->order('id desc')->page(true, false, false, 15);
NewsService::instance()->buildListByCid($result['list']); NewsService::instance()->buildListByCidAndMid($result['list']);
$this->success('获取浏览历史成功!', $result); $this->success('获取浏览历史成功!', $result);
} }
@ -179,7 +180,7 @@ class News extends Auth
return $this->_vali([ return $this->_vali([
'mid.value' => $this->mid, 'mid.value' => $this->mid,
'type.value' => $type, 'type.value' => $type,
'cid.require' => '文章ID不能为空!', 'cid.require' => '文章不能为空!',
]); ]);
} }

View File

@ -85,22 +85,6 @@ class GoodsService extends Service
return $query->where($map)->order('sort desc,id desc')->column('name'); return $query->where($map)->order('sort desc,id desc')->column('name');
} }
/**
* 一维数组生成数据树
* @param array $list 待处理数据
* @param string $cid 自己的主键
* @param string $pid 上级的主键
* @param string $sub 子数组名称
* @return array
*/
public function arr2tree(array $list, string $cid = 'id', string $pid = 'pid', string $sub = 'sub'): array
{
[$tree, $tmp] = [[], array_combine(array_column($list, $cid), array_values($list))];
foreach ($list as $vo) isset($vo[$pid]) && isset($tmp[$vo[$pid]]) ? $tmp[$vo[$pid]][$sub][] = &$tmp[$vo[$cid]] : $tree[] = &$tmp[$vo[$cid]];
unset($tmp, $list);
return $tree;
}
/** /**
* 商品数据绑定 * 商品数据绑定
* @param array $data 商品主数据 * @param array $data 商品主数据
@ -117,8 +101,9 @@ class GoodsService extends Service
} }
$map = [['goods_code', 'in', array_unique(array_column($data, 'code'))], ['status', '=', 1]]; $map = [['goods_code', 'in', array_unique(array_column($data, 'code'))], ['status', '=', 1]];
$items = $this->app->db->name('ShopGoodsItem')->withoutField('id,status,create_at')->where($map)->select()->toArray(); $items = $this->app->db->name('ShopGoodsItem')->withoutField('id,status,create_at')->where($map)->select()->toArray();
$marks = $this->app->db->name('ShopGoodsMark')->where(['status' => 1])->column('name');
foreach ($data as &$vo) { foreach ($data as &$vo) {
$vo['marks'] = think_string_to_array($vo['mark']); $vo['marks'] = think_string_to_array($vo['mark'], ',', $marks);
$vo['cates'] = $cates[$vo['cate']] ?? []; $vo['cates'] = $cates[$vo['cate']] ?? [];
$vo['slider'] = explode('|', $vo['slider']); $vo['slider'] = explode('|', $vo['slider']);
$vo['specs'] = json_decode($vo['data_specs'], true); $vo['specs'] = json_decode($vo['data_specs'], true);

View File

@ -34,29 +34,23 @@ class NewsService extends Service
* @param array $list 数据列表 * @param array $list 数据列表
* @return array * @return array
*/ */
public function buildListByCid(array &$list = []): array public function buildListByCidAndMid(array &$list = []): array
{ {
if (count($list) > 0) { if (count($list) > 0) {
/*! 读取文章内容 */
$cids = array_unique(array_column($list, 'cid')); $cids = array_unique(array_column($list, 'cid'));
$cols = 'id,name,cover,status,deleted,create_at,num_like,num_read,num_comment,num_collect'; $cols = 'id,name,cover,status,deleted,create_at,num_like,num_read,num_comment,num_collect';
$news = $this->app->db->name('DataNewsItem')->whereIn('id', $cids)->column($cols, 'id'); $items = $this->app->db->name('DataNewsItem')->whereIn('id', $cids)->column($cols, 'id');
foreach ($list as &$vo) $vo['record'] = $news[$vo['cid']] ?? []; $marks = $this->app->db->name('DataNewsMark')->where(['status' => 1])->column('name');
} foreach ($items as &$vo) $vo['mark'] = think_string_to_array($vo['mark'] ?: '', ',', $marks);
return $list; /*! 绑定会员数据 */
}
/**
* 根据MID绑定列表数据
* @param array $list 数据列表
* @return array
*/
public function buildListByMid(array &$list = []): array
{
if (count($list) > 0) {
$mids = array_unique(array_column($list, 'mid')); $mids = array_unique(array_column($list, 'mid'));
$cols = 'id,phone,nickname,username,headimg,status'; $cols = 'id,phone,nickname,username,headimg,status';
$mems = $this->app->db->name('DataMember')->whereIn('id', $mids)->column($cols, 'id'); $users = $this->app->db->name('DataMember')->whereIn('id', $mids)->column($cols, 'id');
foreach ($list as &$vo) $vo['member'] = $mems[$vo['mid']] ?? []; foreach ($list as &$vo) {
$vo['record'] = $items[$vo['cid']] ?? [];
$vo['member'] = $users[$vo['mid']] ?? [];
}
} }
return $list; return $list;
} }
@ -69,13 +63,18 @@ class NewsService extends Service
*/ */
public function buildListState(array &$list, int $mid = 0): array public function buildListState(array &$list, int $mid = 0): array
{ {
if (count($list) > 0 && $mid > 0) { if (count($list) > 0) {
$map = [['mid', '=', $mid], ['cid', 'in', array_column($list, 'id')]]; [$cid1s, $cid2s] = [[], []];
$cid1s = $this->app->db->name('DataNewsXCollect')->where($map)->where(['type' => 2])->column('cid'); if ($mid > 0) {
$cid2s = $this->app->db->name('DataNewsXCollect')->where($map)->where(['type' => 1])->column('cid'); $map = [['mid', '=', $mid], ['cid', 'in', array_column($list, 'id')]];
$cid1s = $this->app->db->name('DataNewsXCollect')->where($map)->where(['type' => 2])->column('cid');
$cid2s = $this->app->db->name('DataNewsXCollect')->where($map)->where(['type' => 1])->column('cid');
}
$marks = $this->app->db->name('DataNewsMark')->where(['status' => 1])->column('name');
foreach ($list as &$vo) { foreach ($list as &$vo) {
$vo['my_like_state'] = in_array($vo['id'], $cid1s) ? 1 : 0; $vo['my_like_state'] = in_array($vo['id'], $cid1s) ? 1 : 0;
$vo['my_coll_state'] = in_array($vo['id'], $cid2s) ? 1 : 0; $vo['my_coll_state'] = in_array($vo['id'], $cid2s) ? 1 : 0;
$vo['mark'] = think_string_to_array($vo['mark'] ?: '', ',', $marks);
} }
} }
return $list; return $list;

View File

@ -5,12 +5,17 @@ if (!function_exists('think_string_to_array')) {
* 字符串转数组 * 字符串转数组
* @param string $text 待转内容 * @param string $text 待转内容
* @param string $separ 分隔字符 * @param string $separ 分隔字符
* @param null|array $allow 限定规则
* @return array * @return array
*/ */
function think_string_to_array(string $text, string $separ = ','): array function think_string_to_array(string $text, string $separ = ',', $allow = null): array
{ {
$text = trim($text, $separ); $text = trim($text, $separ);
return $text ? explode($separ, $text) : []; $data = $text ? explode($separ, $text) : [];
if (is_array($allow)) foreach ($data as $key => $mark) {
if (!in_array($mark, $allow)) unset($data[$key]);
}
return $data;
} }
} }