158 lines
4.8 KiB
PHP
158 lines
4.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Cms插件公共函数库
|
|
*/
|
|
|
|
use app\admin\model\cms\Comment;
|
|
|
|
if (!function_exists('get_page_render')) {
|
|
|
|
/**
|
|
* 获取分页地址v2
|
|
* @param mixed $currentPage 当前页
|
|
* @param mixed $totalPages 总页码
|
|
* @param mixed $pageUrl 分页地址
|
|
* @param int $halfPer 分页侧边长度
|
|
* @param mixed|null $linkPage 返回分页地址
|
|
* @return string
|
|
*/
|
|
function get_page_render($currentPage, $totalPages, $pageUrl, int $halfPer = 3, $linkPage = null)
|
|
{
|
|
$planUrl = 'page=page';
|
|
$pageUrl = strtolower($pageUrl);
|
|
if (strstr($pageUrl, $planUrl)) {
|
|
$pageUrl = str_replace($planUrl, 'planUrl=page', $pageUrl);
|
|
}
|
|
|
|
if ($currentPage <= 1) {
|
|
$linkPage .= '<em>首页</em><em>上一页</em>';
|
|
} else {
|
|
$linkPage .= '<a href="' . str_replace('page', 1, $pageUrl) . '" class="first">首页</a>';
|
|
$linkPage .= '<a href="' . str_replace('page', ($currentPage - 1), $pageUrl) . '" class="prev">上一页</a>';
|
|
}
|
|
|
|
/**
|
|
* 中间页码
|
|
*/
|
|
for ($i = $currentPage - $halfPer, $i > 1 || $i = 1,
|
|
$j = $currentPage + $halfPer,
|
|
$j < $totalPages || $j = $totalPages; $i < (int)$j + 1; $i++) {
|
|
$linkPage .= ($i == $currentPage) ? '<span>' . $i . '</span>' : '<a href="' . str_replace('page', $i, $pageUrl) . '">' . $i . '</a>';
|
|
}
|
|
|
|
// 当前页码小于总数
|
|
if ($currentPage < $totalPages) {
|
|
$linkPage .= '<a href="' . str_replace('page', ($currentPage + 1), $pageUrl) . '" class="next">下一页</a>';
|
|
$linkPage .= '<a href="' . str_replace('page', $totalPages, $pageUrl) . '" class="end">尾页</a>';
|
|
} else {
|
|
$linkPage .= '<em>下一页</em><em>尾页</em>';
|
|
}
|
|
|
|
// 祛除首页地址
|
|
$linkPage = str_replace('planUrl', 'page', $linkPage);
|
|
return str_replace('list_1.html', '', $linkPage);
|
|
}
|
|
}
|
|
|
|
if (!function_exists('get_adwords')) {
|
|
|
|
/**
|
|
* 获取广告词
|
|
* @param string $adwords
|
|
* @return string
|
|
*/
|
|
function get_adwords(string $adwords): string
|
|
{
|
|
return '<div class="advertising-show">广告位招租</div>';
|
|
$adwords = explode(',', $adwords);
|
|
return $adwords[array_rand($adwords)];
|
|
}
|
|
}
|
|
|
|
if (!function_exists('parse_tag')) {
|
|
/**
|
|
* 生成参数列表,以数组形式返回
|
|
* @param string $tag 字符串
|
|
* @return array
|
|
*/
|
|
function parse_tag($tag)
|
|
{
|
|
if (is_array($tag)) {
|
|
return $tag;
|
|
}
|
|
|
|
$param = array(); //标签解析
|
|
$array = explode(';', $tag);
|
|
|
|
foreach ($array as $key => $val) {
|
|
if (!empty($val)) {
|
|
$temp = explode(':', trim($val));
|
|
if (!empty($temp[1])) {
|
|
$param[$temp[0]] = $temp[1];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!isset($param['status'])) { // 默认只查询已审核的
|
|
$param['status'] = 1;
|
|
}
|
|
return $param;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('mysql_comment')) {
|
|
/**
|
|
* 获取用户评论
|
|
* @param $param
|
|
* @param int $page
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
function mysql_comment($param, $page = 1)
|
|
{
|
|
|
|
$param = parse_tag($param);
|
|
$field = !empty($param['field']) ? $param['field'] : '*';
|
|
$limit = !empty($param['limit']) ? $param['limit'] : '2';
|
|
$order = !empty($param['order']) ? $param['order'] : 'id desc';
|
|
|
|
// 默认查询首屏
|
|
$where = [];
|
|
if (empty($param['pid'])) {
|
|
$where[] = ['pid', '=', 0];
|
|
} else if ($param['pid'] != 'self') {
|
|
$where[] = ['pid', '=', $param['pid']];
|
|
}
|
|
|
|
if (!empty($param['cid'])) {
|
|
$where[] = ['cid', '=', $param['cid']];
|
|
}
|
|
|
|
if (!empty($param['sid'])) {
|
|
$where[] = ['sid', '=', $param['sid']];
|
|
}
|
|
|
|
if (!empty($param['user_id'])) {
|
|
$where[] = ['user_id', '=', $param['user_id']];
|
|
}
|
|
|
|
// 默认为文章类型
|
|
if (empty($param['tid'])) {
|
|
$where[] = ['tid', '=', 1];
|
|
} else {
|
|
$where[] = ['tid', '=', $param['tid']];
|
|
}
|
|
|
|
// 获取总数
|
|
$count = Comment::where($where)->count('id');
|
|
|
|
// 设置分页
|
|
$page = ($count <= $limit) ? 1 : $page;
|
|
$totalPages = ceil($count / $limit);
|
|
$list = Comment::with(['user', 'child'])->where($where)->order($order)->limit($limit)->page($page)->select()->toArray();
|
|
return ['data' => $list, 'total' => $count, 'maxPage' => $totalPages, 'pages' => ''];
|
|
}
|
|
} |