You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

377 lines
13 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Controllers\Home;
  3. use Illuminate\Support\Facades\DB;
  4. class IndexController extends BaseController
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. }
  10. //首页
  11. public function index()
  12. {
  13. //商品列表
  14. $where['status'] = 0;
  15. $data['goods_list'] = object_to_array(DB::table('goods')->where($where)->select('id', 'title', 'price', 'litpic', 'description', 'shipping_fee', 'market_price', 'goods_number', 'sale')->take(30)->orderBy('pubdate', 'desc')->get());
  16. $data['goods_type_list'] = object_to_array(DB::table('goods_type')->where(['pid' => 0, 'status' => 1])->select('id', 'name')->take(30)->orderBy('listorder', 'asc')->get());
  17. $data['slide_list'] = object_to_array(DB::table('slide')->where(['group_id' => 0, 'type' => 0, 'is_show' => 0])->take(30)->orderBy('listorder', 'asc')->get());
  18. $data['ztad_list'] = object_to_array(DB::table('slide')->where(['group_id' => 1, 'type' => 0, 'is_show' => 0])->take(30)->orderBy('listorder', 'asc')->get());
  19. return view('home.index.index', $data);
  20. }
  21. //列表页
  22. public function category($cat, $page = 0)
  23. {
  24. $pagenow = $page;
  25. if (empty($cat) || !preg_match('/[0-9]+/', $cat)) {
  26. return redirect()->route('page404');
  27. }
  28. if (cache("catid$cat")) {
  29. $post = cache("catid$cat");
  30. } else {
  31. $post = object_to_array(DB::table('arctype')->where('id', $cat)->first(), 1);
  32. if (empty($post)) {
  33. return redirect()->route('page404');
  34. }
  35. cache(["catid$cat" => $post], \Carbon\Carbon::now()->addMinutes(2592000));
  36. }
  37. $data['post'] = $post;
  38. $subcat = "";
  39. $sql = "";
  40. $post2 = object_to_array(DB::table('arctype')->select('id')->where('pid', $cat)->get());
  41. if (!empty($post2)) {
  42. foreach ($post2 as $row) {
  43. $subcat = $subcat . "typeid=" . $row["id"] . " or ";
  44. }
  45. }
  46. $subcat = $subcat . "typeid=" . $cat;
  47. $sql = $subcat . " or typeid2 in (" . $cat . ")";//echo $subcat2;exit;
  48. $data['sql'] = $sql;
  49. $counts = DB::table("article")->whereRaw($sql)->count();
  50. if ($counts > sysconfig('CMS_MAXARC')) {
  51. $counts = sysconfig('CMS_MAXARC');
  52. dd($counts);
  53. }
  54. $pagesize = sysconfig('CMS_PAGESIZE');
  55. $page = 0;
  56. if ($counts % $pagesize) {//取总数据量除以每页数的余数
  57. $pages = intval($counts / $pagesize) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一,如果没有余数,则页数等于总数据量除以每页数的结果
  58. } else {
  59. $pages = $counts / $pagesize;
  60. }
  61. if (!empty($pagenow)) {
  62. if ($pagenow == 1 || $pagenow > $pages) {
  63. return redirect()->route('page404');
  64. }
  65. $page = $pagenow - 1;
  66. $nextpage = $pagenow + 1;
  67. $previouspage = $pagenow - 1;
  68. } else {
  69. $page = 0;
  70. $nextpage = 2;
  71. $previouspage = 0;
  72. }
  73. $data['page'] = $page;
  74. $data['pages'] = $pages;
  75. $data['counts'] = $counts;
  76. $start = $page * $pagesize;
  77. $data['posts'] = arclist(array("sql" => $sql, "limit" => "$start,$pagesize")); //获取列表
  78. $data['pagenav'] = get_listnav(array("counts" => $counts, "pagesize" => $pagesize, "pagenow" => $page + 1, "catid" => $cat)); //获取分页列表
  79. if ($post['templist'] == 'category2') {
  80. if (!empty($pagenow)) {
  81. return redirect()->route('page404');
  82. }
  83. }
  84. return view('home.index.' . $post['templist'], $data);
  85. }
  86. //文章详情页
  87. public function detail($id)
  88. {
  89. if (empty($id) || !preg_match('/[0-9]+/', $id)) {
  90. return redirect()->route('page404');
  91. }
  92. if (cache("detailid$id")) {
  93. $post = cache("detailid$id");
  94. } else {
  95. $post = object_to_array(DB::table('article')->where('id', $id)->first(), 1);
  96. if (empty($post)) {
  97. return redirect()->route('page404');
  98. }
  99. $post['name'] = DB::table('arctype')->where('id', $post['typeid'])->value('name');
  100. cache(["detailid$id" => $post], \Carbon\Carbon::now()->addMinutes(2592000));
  101. }
  102. if ($post) {
  103. $cat = $post['typeid'];
  104. $post['body'] = ReplaceKeyword($post['body']);
  105. if (!empty($post['writer'])) {
  106. $post['writertitle'] = $post['title'] . ' ' . $post['writer'];
  107. }
  108. $data['post'] = $post;
  109. $data['pre'] = get_article_prenext(array('aid' => $post["id"], 'typeid' => $post["typeid"], 'type' => "pre"));
  110. } else {
  111. return redirect()->route('page404');
  112. }
  113. if (cache("catid$cat")) {
  114. $post = cache("catid$cat");
  115. } else {
  116. $post = object_to_array(DB::table('arctype')->where('id', $cat)->first(), 1);
  117. cache(["catid$cat" => $post], \Carbon\Carbon::now()->addMinutes(2592000));
  118. }
  119. return view('home.index.' . $post['temparticle'], $data);
  120. }
  121. //标签详情页,共有3种显示方式,1正常列表,2列表显示文章,3显示描述
  122. public function tag($tag, $page = 0)
  123. {
  124. $pagenow = $page;
  125. if (empty($tag) || !preg_match('/[0-9]+/', $tag)) {
  126. return redirect()->route('page404');
  127. }
  128. $post = object_to_array(DB::table('tagindex')->where('id', $tag)->first(), 1);
  129. $data['post'] = $post;
  130. $counts = DB::table("taglist")->where('tid', $tag)->count('aid');
  131. if ($counts > sysconfig('CMS_MAXARC')) {
  132. $counts = sysconfig('CMS_MAXARC');
  133. }
  134. $pagesize = sysconfig('CMS_PAGESIZE');
  135. $page = 0;
  136. if ($counts % $pagesize) {//取总数据量除以每页数的余数
  137. $pages = intval($counts / $pagesize) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一,如果没有余数,则页数等于总数据量除以每页数的结果
  138. } else {
  139. $pages = $counts / $pagesize;
  140. }
  141. if (!empty($pagenow)) {
  142. if ($pagenow == 1 || $pagenow > $pages) {
  143. return redirect()->route('page404');
  144. }
  145. $page = $pagenow - 1;
  146. $nextpage = $pagenow + 1;
  147. $previouspage = $pagenow - 1;
  148. } else {
  149. $page = 0;
  150. $nextpage = 2;
  151. $previouspage = 0;
  152. }
  153. $data['page'] = $page;
  154. $data['pages'] = $pages;
  155. $data['counts'] = $counts;
  156. $start = $page * $pagesize;
  157. $posts = object_to_array(DB::table("taglist")->where('tid', $tag)->orderBy('aid', 'desc')->skip($start)->take($pagesize)->get());
  158. foreach ($posts as $row) {
  159. $aid[] = $row["aid"];
  160. }
  161. $aid = isset($aid) ? implode(',', $aid) : "";
  162. if ($aid != "") {
  163. if ($post['template'] == 'tag2') {
  164. $data['posts'] = arclist(array("sql" => "id in ($aid)", "orderby" => ['id', 'desc'], "row" => "$pagesize", "field" => "title,body")); //获取列表
  165. } else {
  166. $data['posts'] = arclist(array("sql" => "id in ($aid)", "orderby" => ['id', 'desc'], "row" => "$pagesize")); //获取列表
  167. }
  168. } else {
  169. $data['posts'] = ''; //获取列表
  170. }
  171. $data['pagenav'] = get_listnav(array("counts" => $counts, "pagesize" => $pagesize, "pagenow" => $page + 1, "catid" => $tag, "urltype" => "tag")); //获取分页列表
  172. if ($post['template'] == 'tag2' || $post['template'] == 'tag3') {
  173. if (!empty($pagenow)) {
  174. return redirect()->route('page404');
  175. }
  176. }
  177. return view('home.index.' . $post['template'], $data);
  178. }
  179. //标签页
  180. public function tags()
  181. {
  182. return view('home.index.tags');
  183. }
  184. //搜索页
  185. public function search($keyword)
  186. {
  187. if (empty($keyword)) {
  188. echo '请输入正确的关键词';
  189. exit;
  190. }
  191. if (strstr($keyword, "&")) exit;
  192. $data['posts'] = object_to_array(DB::table("article")->where("title", "like", "%$keyword%")->orderBy('id', 'desc')->take(30)->get());
  193. $data['keyword'] = $keyword;
  194. return view('home.index.search', $data);
  195. }
  196. //单页面
  197. public function page($id)
  198. {
  199. $data = [];
  200. if (!empty($id) && preg_match('/[a-z0-9]+/', $id)) {
  201. $map['filename'] = $id;
  202. if (cache("pageid$id")) {
  203. $post = cache("pageid$id");
  204. } else {
  205. $post = object_to_array(DB::table('page')->where($map)->first(), 1);
  206. cache("pageid$id", $post, 2592000);
  207. cache(["pageid$id" => $post], \Carbon\Carbon::now()->addMinutes(2592000));
  208. }
  209. if ($post) {
  210. $data['post'] = $post;
  211. } else {
  212. return redirect()->route('page404');
  213. }
  214. } else {
  215. return redirect()->route('page404');
  216. }
  217. $data['posts'] = object_to_array(DB::table('page')->orderBy(\DB::raw('rand()'))->take(5)->get());
  218. return view('home.index.' . $post['template'], $data);
  219. }
  220. //商品列表页
  221. public function goodstype($cat, $page = 0)
  222. {
  223. $pagenow = $page;
  224. if (empty($cat) || !preg_match('/[0-9]+/', $cat)) {
  225. return redirect()->route('page404');
  226. }
  227. $post = object_to_array(DB::table('goods_type')->where('id', $cat)->first(), 1);
  228. if (empty($post)) {
  229. return redirect()->route('page404');
  230. }
  231. $data['post'] = $post;
  232. $subcat = "";
  233. $post2 = object_to_array(DB::table('goods_type')->select('id')->where('pid', $cat)->get());
  234. if (!empty($post2)) {
  235. foreach ($post2 as $row) {
  236. $subcat = $subcat . "typeid=" . $row["id"] . " or ";
  237. }
  238. }
  239. $subcat = $subcat . "typeid=" . $cat;
  240. $data['sql'] = $subcat;
  241. $counts = DB::table("goods")->whereRaw($subcat)->count();
  242. if ($counts > sysconfig('CMS_MAXARC')) {
  243. $counts = sysconfig('CMS_MAXARC');
  244. dd($counts);
  245. }
  246. $pagesize = sysconfig('CMS_PAGESIZE');
  247. $page = 0;
  248. if ($counts % $pagesize) {//取总数据量除以每页数的余数
  249. $pages = intval($counts / $pagesize) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一,如果没有余数,则页数等于总数据量除以每页数的结果
  250. } else {
  251. $pages = $counts / $pagesize;
  252. }
  253. if (!empty($pagenow)) {
  254. if ($pagenow == 1 || $pagenow > $pages) {
  255. return redirect()->route('page404');
  256. }
  257. $page = $pagenow - 1;
  258. $nextpage = $pagenow + 1;
  259. $previouspage = $pagenow - 1;
  260. } else {
  261. $page = 0;
  262. $nextpage = 2;
  263. $previouspage = 0;
  264. }
  265. $data['page'] = $page;
  266. $data['pages'] = $pages;
  267. $data['counts'] = $counts;
  268. $start = $page * $pagesize;
  269. $data['posts'] = arclist(array("table" => "goods", "sql" => $subcat, "limit" => "$start,$pagesize")); //获取列表
  270. $data['pagenav'] = get_listnav(array("counts" => $counts, "pagesize" => $pagesize, "pagenow" => $page + 1, "catid" => $cat, "urltype" => "goods")); //获取分页列表
  271. if ($post['templist'] == 'category2') {
  272. if (!empty($pagenow)) {
  273. return redirect()->route('page404');
  274. }
  275. }
  276. return view('home.index.' . $post['templist'], $data);
  277. }
  278. //商品详情页
  279. public function goods($id)
  280. {
  281. if (empty($id) || !preg_match('/[0-9]+/', $id)) {
  282. return redirect()->route('page404');
  283. }
  284. $post = object_to_array(DB::table('goods')->where('id', $id)->first(), 1);
  285. if (empty($post)) {
  286. return redirect()->route('page404');
  287. }
  288. $post['name'] = DB::table('goods_type')->where('id', $post['typeid'])->value('name');
  289. if ($post) {
  290. $cat = $post['typeid'];
  291. if (!empty($post['writer'])) {
  292. $post['writertitle'] = $post['title'] . ' ' . $post['writer'];
  293. }
  294. $data['post'] = $post;
  295. $data['pre'] = get_article_prenext(array('aid' => $post["id"], 'typeid' => $post["typeid"], 'type' => "pre"));
  296. } else {
  297. return redirect()->route('page404');
  298. }
  299. $post = object_to_array(DB::table('goods_type')->where('id', $cat)->first(), 1);
  300. return view('home.index.goods', $data);
  301. }
  302. //sitemap页面
  303. public function sitemap()
  304. {
  305. return view('home.index.sitemap');
  306. }
  307. //404页面
  308. public function page404()
  309. {
  310. return view('home.404');
  311. }
  312. //测试页面
  313. public function test()
  314. {
  315. return view('home.index.test');
  316. //return base_path('resources/org');
  317. //$qrcode = new \SimpleSoftwareIO\QrCode\BaconQrCodeGenerator;
  318. //return $qrcode->size(500)->generate('Make a qrcode without Laravel!');
  319. //return '<img src="data:image/png;base64,'.base64_encode(\QrCode::format('png')->encoding('UTF-8')->size(200)->generate('http://www.72p.org/')).'">';
  320. //set_exception_handler('myException');
  321. //return uniqid();
  322. //return \App\Common\Helper::formatPrice(1.2346);
  323. }
  324. }