控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | cms函数文件
  13. // +----------------------------------------------------------------------
  14. use think\Db;
  15. use think\facade\Cache;
  16. use think\facade\Request;
  17. /**
  18. * 获取栏目相关信息
  19. * @param type $catid 栏目id或者栏目标识
  20. * @param type $field 返回的字段,默认返回全部,数组
  21. * @param type $newCache 是否强制刷新
  22. * @return boolean
  23. */
  24. function getCategory($cat, $fields = '', $newCache = false)
  25. {
  26. $url_mode = get_addon_config("cms")['site_url_mode'];
  27. if (empty($cat)) {
  28. return false;
  29. }
  30. $field = is_numeric($cat) ? 'id' : 'catdir';
  31. $key = 'getCategory_' . $cat;
  32. //强制刷新缓存
  33. if ($newCache) {
  34. Cache::rm($key, null);
  35. }
  36. $cache = Cache::get($key);
  37. if ($cache === 'false') {
  38. return false;
  39. }
  40. if (empty($cache)) {
  41. //读取数据
  42. $cache = Db::name('category')->where($field, $cat)->find();
  43. if (empty($cache)) {
  44. Cache::set($key, 'false', 60);
  45. return false;
  46. } else {
  47. //扩展配置
  48. $field = 1 == $url_mode ? 'id' : 'catdir';
  49. $cache['setting'] = unserialize($cache['setting']);
  50. $cache['url'] = buildCatUrl($cache[$field], $cache['url']);
  51. Cache::set($key, $cache, 3600);
  52. }
  53. }
  54. if ($fields) {
  55. //支持var.property,不过只支持一维数组
  56. if (false !== strpos($fields, '.')) {
  57. $vars = explode('.', $fields);
  58. return $cache[$vars[0]][$vars[1]];
  59. } else {
  60. return $cache[$fields];
  61. }
  62. } else {
  63. return $cache;
  64. }
  65. }
  66. /**
  67. * 当前路径
  68. * 返回指定栏目路径层级
  69. * @param $catid 栏目id
  70. * @param $symbol 栏目间隔符
  71. */
  72. function catpos($catid, $symbol = ' &gt; ')
  73. {
  74. if (getCategory($catid) == false) {
  75. return '';
  76. }
  77. //获取当前栏目的 父栏目列表
  78. $arrparentid = array_filter(explode(',', getCategory($catid, 'arrparentid') . ',' . $catid));
  79. foreach ($arrparentid as $cid) {
  80. $parsestr[] = '<a href="' . getCategory($cid, 'url') . '" >' . getCategory($cid, 'catname') . '</a>';
  81. }
  82. $parsestr = implode($symbol, $parsestr);
  83. return $parsestr;
  84. }
  85. /**
  86. * 生成分类信息中的筛选菜单
  87. */
  88. function filters($modelid, $catid)
  89. {
  90. $url_mode = get_addon_config("cms")['site_url_mode'];
  91. $data = get_filters_field($modelid);
  92. Request::filter('trim,strip_tags');
  93. $param = Request::param();
  94. unset($param['catid'], $param['catdir'], $param['page']);
  95. $conditionParam = [];
  96. foreach ($data as $name => $rs) {
  97. $data[$name]['options'][0] = '不限';
  98. //判断是否是单选条件
  99. $ifradio = 'checkbox' == $data[$name]['type'] ? false : true;
  100. if ($ifradio) {
  101. //单选选中参数
  102. if (!empty($param[$name])) {
  103. $conditionParam[$name]['options'][$param[$name]]['active'] = true;
  104. $nowParam = $param;
  105. $nowParam[$name] = '';
  106. $conditionParam[$name]['options'][$param[$name]]['param'] = paramencode($nowParam);
  107. unset($nowParam);
  108. }
  109. } else {
  110. //多选选中参数
  111. if (!empty($param[$name])) {
  112. $paramContent = explode('_', $param[$name]);
  113. foreach ($paramContent as $k => $v) {
  114. $nowParamContent = $paramContent;
  115. unset($nowParamContent[$k]);
  116. $nowParam = $param;
  117. $nowParam[$name] = implode('_', $nowParamContent);
  118. $conditionParam[$name]['options'][$v]['active'] = true;
  119. $conditionParam[$name]['options'][$v]['param'] = paramencode($nowParam);
  120. unset($nowParam);
  121. unset($nowParamContent);
  122. }
  123. unset($paramContent);
  124. }
  125. }
  126. $conditionParam[$name]['title'] = $rs['title'];
  127. $conditionParam[$name]['name'] = $rs['name'];
  128. //未选中 active param title url
  129. foreach ($data[$name]['options'] as $k => $v) {
  130. $conditionParam[$name]['options'][$k]['title'] = $v;
  131. //未选中条件参数生成
  132. if (!isset($conditionParam[$name]['options'][$k]['active'])) {
  133. //未选中条件参数生成
  134. $conditionParam[$name]['options'][$k]['active'] = 0;
  135. if ($ifradio) {
  136. $nowParam = $param;
  137. $nowParam[$name] = $k;
  138. $conditionParam[$name]['options'][$k]['param'] = paramencode($nowParam);
  139. } else {
  140. $nowParam = $param;
  141. $nowParam[$name] = empty($param[$name]) || !$k ? $k : $param[$name] . '_' . $k;
  142. $conditionParam[$name]['options'][$k]['param'] = paramencode($nowParam);
  143. }
  144. }
  145. if ($url_mode == 1) {
  146. $field = 'catid';
  147. } else {
  148. $field = 'catdir';
  149. $catid = getCategory($catid, 'catdir');
  150. }
  151. $newParam = $conditionParam[$name]['options'][$k]['param'];
  152. $conditionParam[$name]['options'][$k]['url'] = url('cms/index/lists', [$field => $catid]) . ($newParam ? '?' . $newParam : '');
  153. ksort($conditionParam[$name]['options']);
  154. }
  155. if (!isset($param[$rs['name']]) && empty($param[$rs['name']])) {
  156. $conditionParam[$name]['options'][0]['active'] = true;
  157. }
  158. }
  159. return $conditionParam;
  160. }
  161. function structure_filters_sql($modelid)
  162. {
  163. $data = get_filters_field($modelid);
  164. $fields_key = array_keys($data);
  165. $sql = '`status` = \'1\'';
  166. $param = Request::param();
  167. foreach ($param as $k => $r) {
  168. if (isset($data[$k]['type']) && in_array($k, $fields_key) && intval($r) != 0) {
  169. if ('radio' == $data[$k]['type']) {
  170. $sql .= " AND `$k` = '$r'";
  171. } elseif ('checkbox' == $data[$k]['type']) {
  172. if (strpos($r, '_')) {
  173. $r = explode('_', $r);
  174. $flag = [];
  175. foreach ($r as $key => $val) {
  176. $flag[] = "FIND_IN_SET($val,`$k`)";
  177. }
  178. $sql .= " AND (" . implode(' OR ', $flag) . ")";
  179. } else {
  180. $sql .= " AND FIND_IN_SET($r,`$k`)";
  181. }
  182. }
  183. }
  184. }
  185. return $sql;
  186. }
  187. function get_filters_field($modelid)
  188. {
  189. static $filters_data = [];
  190. if ($filters_data) {
  191. return $filters_data;
  192. }
  193. $options = cache('ModelField')[$modelid];
  194. foreach ($options as $_k => $_v) {
  195. if (isset($_v['filtertype']) && $_v['filtertype']) {
  196. $_v['options'] = parse_attr($_v['options']);
  197. } else {
  198. continue;
  199. }
  200. $filters_data[$_v['name']] = array_intersect_key($_v, array_flip(['name', 'title', 'type', 'options']));
  201. }
  202. return $filters_data;
  203. }
  204. /*function paramdecode($str)
  205. {
  206. $arr = [];
  207. $arr1 = explode('&', $str);
  208. foreach ($arr1 as $vo) {
  209. if (!empty($vo)) {
  210. $arr2 = explode('=', $vo);
  211. if (!empty($arr2[1])) {
  212. $arr[$arr2[0]] = $arr2[1];
  213. }
  214. }
  215. }
  216. return $arr;
  217. }*/
  218. function paramencode($arr)
  219. {
  220. $str = '';
  221. if (!empty($arr)) {
  222. foreach ($arr as $key => $vo) {
  223. if (!empty($vo)) {
  224. $str .= $key . '=' . $vo . '&';
  225. }
  226. }
  227. $str = $str ? substr($str, 0, -1) : '';
  228. }
  229. return $str;
  230. }
  231. /**
  232. * 生成SEO
  233. * @param $catid 栏目ID
  234. * @param $title 标题
  235. * @param $description 描述
  236. * @param $keyword 关键词
  237. */
  238. function seo($catid = '', $title = '', $description = '', $keyword = '')
  239. {
  240. if (!empty($title)) {
  241. $title = strip_tags($title);
  242. }
  243. if (!empty($description)) {
  244. $description = strip_tags($description);
  245. }
  246. if (!empty($keyword)) {
  247. $keyword = str_replace(' ', ',', strip_tags($keyword));
  248. }
  249. $site = get_addon_config("cms");
  250. if (!empty($catid)) {
  251. $cat = getCategory($catid);
  252. }
  253. $seo['site_title'] = isset($site['site_title']) && !empty($site['site_title']) ? $site['site_title'] : $site['site_name'];
  254. $seo['keyword'] = !empty($keyword) ? $keyword : $site['site_keyword'];
  255. $seo['description'] = isset($description) && !empty($description) ? $description : (isset($cat['setting']['meta_description']) && !empty($cat['setting']['meta_description']) ? $cat['setting']['meta_description'] : (isset($site['site_description']) && !empty($site['site_description']) ? $site['site_description'] : ''));
  256. $seo['title'] = (isset($title) && !empty($title) ? $title . ' - ' : '') . (isset($cat['setting']['meta_title']) && !empty($cat['setting']['meta_title']) ? $cat['setting']['meta_title'] . ' - ' : (isset($cat['catname']) && !empty($cat['catname']) ? $cat['catname'] . ' - ' : ''));
  257. foreach ($seo as $k => $v) {
  258. $seo[$k] = str_replace(["\n", "\r"], '', $v);
  259. }
  260. return $seo;
  261. }
  262. /**
  263. * 生成栏目URL
  264. */
  265. function buildCatUrl($cat, $url = '', $suffix = true, $domain = false)
  266. {
  267. $url_mode = get_addon_config("cms")['site_url_mode'];
  268. $category = cache("Category");
  269. if (is_numeric($cat)) {
  270. $field = 'catid';
  271. if ($url_mode == 2 && isset($category[$cat])) {
  272. $cat = $category[$cat]['catdir'];
  273. $field = 'catdir';
  274. }
  275. } else {
  276. $field = 'catdir';
  277. }
  278. return empty($url) ? url('cms/index/lists', [$field => $cat], $suffix, $domain) : ((strpos($url, '://') !== false) ? $url : url($url, '', $suffix, $domain));
  279. }
  280. //创建内容链接
  281. function buildContentUrl($cat, $id, $url = '', $suffix = true, $domain = false)
  282. {
  283. $url_mode = get_addon_config("cms")['site_url_mode'];
  284. $category = cache("Category");
  285. if (is_numeric($cat)) {
  286. $field = 'catid';
  287. if ($url_mode == 2 && isset($category[$cat])) {
  288. $cat = $category[$cat]['catdir'];
  289. $field = 'catdir';
  290. }
  291. } else {
  292. $field = 'catdir';
  293. }
  294. return empty($url) ? url('cms/index/shows', [$field => $cat, 'id' => $id], $suffix, $domain) : ((strpos($url, '://') !== false) ? $url : url($url, '', $suffix, $domain));
  295. }