BaseHome.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use app\BaseController;
  5. /*
  6. * 基类
  7. */
  8. /**
  9. *
  10. *
  11. * ----------------------------------------------------------------------------
  12. *
  13. * 控制器
  14. */
  15. class BaseHome extends BaseController
  16. {
  17. public function initialize()
  18. {
  19. parent::initialize();
  20. //自动加入配置
  21. $config_list = rkcache('config', true);
  22. config($config_list, 'ds_config');
  23. $controller = request()->controller();
  24. $action = request()->action();
  25. if (
  26. !config('ds_config.site_state') &&
  27. (!($controller == 'Index' && $action == 'josn_class') &&
  28. !($controller == 'Index' && $action == 'flea_area') &&
  29. !($controller == 'Index' && $action == 'josn_flea_class') &&
  30. !($controller == 'Index' && $action == 'json_area') &&
  31. !($controller == 'Index' && $action == 'json_area_show') &&
  32. !($controller == 'Index' && $action == 'getweekofmonth') &&
  33. !($controller == 'Payment' && $action == 'allinpay_notify') &&
  34. !($controller == 'Payment' && $action == 'unionpay_notify') &&
  35. !($controller == 'Payment' && $action == 'wxpay_minipro_notify') &&
  36. !($controller == 'Payment' && $action == 'wxpay_jsapi_notify') &&
  37. !($controller == 'Payment' && $action == 'wxpay_h5_notify') &&
  38. !($controller == 'Payment' && $action == 'wxpay_app_notify') &&
  39. !($controller == 'Payment' && $action == 'alipay_notify') &&
  40. !($controller == 'Payment' && $action == 'alipay_app_notify') &&
  41. !($controller == 'Payment' && $action == 'alipay_h5_notify'))
  42. ) {
  43. echo '<div style="height:100%;display:flex;flex-direction:column;align-items:center;justify-content:center">';
  44. echo '<img src="' . ds_get_pic(ATTACH_COMMON, config('ds_config.site_logo')) . '"/>';
  45. echo '<div style="font-size:30px;font-weight:bold;padding-top:40px;color:#999;max-width:1200px;text-align:center;">' . (config('ds_config.closed_reason') ? config('ds_config.closed_reason') : '站点已关闭') . '</div>';
  46. echo '</div>';
  47. exit;
  48. }
  49. $this->checkMessage(); //短消息检查
  50. $this->showArticle();
  51. $this->showCartCount();
  52. //顶部广告
  53. $prefix = 'home-adv';
  54. $result = rcache(21, $prefix);
  55. if (empty($result)) {
  56. $condition = array();
  57. $condition[] = ['ap_id', '=', 21];
  58. $condition[] = ['adv_enabled', '=', 1];
  59. $condition[] = ['adv_startdate', '<', strtotime(date('Y-m-d H:00:00'))];
  60. $condition[] = ['adv_enddate', '>', strtotime(date('Y-m-d H:00:00'))];
  61. $adv_list = model('adv')->getAdvList($condition, '', 1, 'adv_sort asc,adv_id asc');
  62. if (!empty($adv_list)) {
  63. $result = $adv_list[0];
  64. }
  65. wcache(21, $result, $prefix, 3600);
  66. }
  67. View::assign('adv_top', $result);
  68. View::assign('hot_search', @explode(',', config('ds_config.hot_search'))); //热门搜索
  69. // 自定义导航
  70. View::assign('navs', $this->_get_navs());
  71. //获取所有分类
  72. View::assign('header_categories', $this->_get_header_categories());
  73. }
  74. //SEO 赋值
  75. function _assign_seo($seo)
  76. {
  77. View::assign('html_title', $seo['html_title']);
  78. View::assign('seo_keywords', $seo['seo_keywords']);
  79. View::assign('seo_description', $seo['seo_description']);
  80. }
  81. /**
  82. * 检查短消息数量
  83. *
  84. */
  85. protected function checkMessage()
  86. {
  87. if (session('member_id') == '')
  88. return;
  89. //判断cookie是否存在
  90. $cookie_name = 'msgnewnum' . session('member_id');
  91. if (cookie($cookie_name) != null) {
  92. $countnum = intval(cookie($cookie_name));
  93. } else {
  94. $message_model = model('message');
  95. $countnum = $message_model->getNewMessageCount(session('member_id'));
  96. cookie($cookie_name, $countnum, 2 * 3600); //保存2小时
  97. }
  98. View::assign('message_num', $countnum);
  99. }
  100. public function _get_navs()
  101. {
  102. $data = array(
  103. 'header' => array(), 'middle' => array(), 'footer' => array(),
  104. );
  105. $rows = rkcache('nav', true);
  106. foreach ($rows as $row) {
  107. $data[$row['nav_location']][] = $row;
  108. }
  109. return $data;
  110. }
  111. public function _get_header_categories()
  112. {
  113. $goodsclass_model = model('goodsclass');
  114. $goods_class = $goodsclass_model->get_all_category();
  115. return $goods_class;
  116. }
  117. /**
  118. * 显示购物车数量
  119. */
  120. protected function showCartCount()
  121. {
  122. if (cookie('cart_goods_num') != null) {
  123. $cart_num = intval(cookie('cart_goods_num'));
  124. } else {
  125. //已登录状态,存入数据库,未登录时,优先存入缓存,否则存入COOKIE
  126. if (session('member_id')) {
  127. $save_type = 'db';
  128. } else {
  129. $save_type = 'cookie';
  130. }
  131. $cart_num = model('cart')->getCartNum($save_type, array('buyer_id' => session('member_id'))); //查询购物车商品种类
  132. }
  133. View::assign('cart_goods_num', $cart_num);
  134. }
  135. /**
  136. * 输出会员等级
  137. * @param bool $is_return 是否返回会员信息,返回为true,输出会员信息为false
  138. */
  139. protected function getMemberAndGradeInfo($is_return = false)
  140. {
  141. $member_info = array();
  142. //会员详情及会员级别处理
  143. if (session('member_id')) {
  144. $member_model = model('member');
  145. $member_info = $member_model->getMemberInfoByID(session('member_id'));
  146. if ($member_info) {
  147. $member_gradeinfo = $member_model->getOneMemberGrade(intval($member_info['member_exppoints']));
  148. $member_info = array_merge($member_info, $member_gradeinfo);
  149. }
  150. }
  151. if ($is_return == true) { //返回会员信息
  152. return $member_info;
  153. } else { //输出会员信息
  154. View::assign('member_info', $member_info);
  155. }
  156. }
  157. /**
  158. * 验证会员是否登录
  159. *
  160. */
  161. protected function checkLogin()
  162. {
  163. if (session('is_login') !== '1') {
  164. if (trim(request()->action()) == 'favoritesgoods' || trim(request()->action()) == 'favoritesstore') {
  165. echo json_encode(array('done' => false, 'msg' => lang('no_login')));
  166. die;
  167. }
  168. $ref_url = request_uri();
  169. session('ref_url', $ref_url);
  170. if (input('get.inajax')) {
  171. ds_show_dialog('', '', 'js', "login_dialog();", 200);
  172. } else {
  173. @header("location: " . HOME_SITE_URL . "/Login/logon.html");
  174. }
  175. exit;
  176. }
  177. }
  178. /**
  179. * 添加到任务队列
  180. *
  181. * @param array $goods_array
  182. * @param boolean $ifdel 是否删除以原记录
  183. */
  184. protected function addcron($data = array(), $ifdel = false)
  185. {
  186. $cron_model = model('cron');
  187. if (isset($data[0])) { // 批量插入
  188. $where = array();
  189. foreach ($data as $k => $v) {
  190. // 删除原纪录条件
  191. if ($ifdel) {
  192. $where[] = '(cron_type = "' . $data['cron_type'] . '" and cron_value = "' . $data['cron_value'] . '")';
  193. }
  194. }
  195. // 删除原纪录
  196. if ($ifdel) {
  197. $cron_model->delCron(implode(',', $where));
  198. }
  199. $cron_model->addCronAll($data);
  200. } else { // 单条插入
  201. // 删除原纪录
  202. if ($ifdel) {
  203. $cron_model->delCron(array('cron_type' => $data['cron_type'], 'cron_value' => $data['cron_value']));
  204. }
  205. $cron_model->addCron($data);
  206. }
  207. }
  208. //文章输出
  209. public function showArticle()
  210. {
  211. $article = rcache("index_article");
  212. if (!empty($article)) {
  213. View::assign('show_article', $article['show_article']);
  214. View::assign('article_list', $article['article_list']);
  215. } else {
  216. $articleclass_model = model('articleclass');
  217. $article_model = model('article');
  218. $show_article = array(); //商城公告
  219. $article_list = array(); //下方文章
  220. $notice_class = array('notice');
  221. $code_array = array('member', 'store', 'payment', 'sold', 'service', 'about');
  222. $notice_limit = 5;
  223. $faq_limit = 5;
  224. $class_condition = array();
  225. $class_condition[] = array('ac_id', '<=', 7);
  226. $class_order = 'ac_sort asc';
  227. $article_class = $articleclass_model->getArticleclassList($class_condition, $class_order);
  228. $class_list = array();
  229. if (!empty($article_class) && is_array($article_class)) {
  230. foreach ($article_class as $key => $val) {
  231. $ac_code = $val['ac_code'];
  232. $ac_id = $val['ac_id'];
  233. $val['list'] = array(); //文章
  234. $class_list[$ac_id] = $val;
  235. }
  236. }
  237. //首页系统文章
  238. $article_where = "article.article_show = '1' and (article_class.ac_id <= 7 or (article_class.ac_parent_id > 0 and article_class.ac_parent_id <= 7))";
  239. $article_field = 'article.article_id,article.ac_id,article.article_url,article.article_title,article.article_time,article_class.ac_name,article_class.ac_parent_id';
  240. $article_order = 'article_sort asc,article_time desc';
  241. $article_array = $article_model->getJoinArticleList($article_where, 300, $article_field, $article_order);
  242. if (!empty($article_array) && is_array($article_array)) {
  243. foreach ($article_array as $key => $val) {
  244. $ac_id = $val['ac_id'];
  245. $ac_parent_id = $val['ac_parent_id'];
  246. if ($ac_parent_id == 0) { //顶级分类
  247. $class_list[$ac_id]['list'][] = $val;
  248. } else {
  249. $class_list[$ac_parent_id]['list'][] = $val;
  250. }
  251. }
  252. }
  253. if (!empty($class_list) && is_array($class_list)) {
  254. foreach ($class_list as $key => $val) {
  255. $ac_code = @$val['ac_code'];
  256. if (in_array($ac_code, $notice_class)) {
  257. $list = $val['list'];
  258. array_splice($list, $notice_limit);
  259. $val['list'] = $list;
  260. $show_article[$ac_code] = $val;
  261. }
  262. if (in_array($ac_code, $code_array)) {
  263. $list = $val['list'];
  264. $val['class']['ac_name'] = $val['ac_name'];
  265. array_splice($list, $faq_limit);
  266. $val['list'] = $list;
  267. $article_list[] = $val;
  268. }
  269. }
  270. }
  271. wcache('index_article', array('show_article' => $show_article, 'article_list' => $article_list,));
  272. View::assign('show_article', $show_article);
  273. View::assign('article_list', $article_list);
  274. }
  275. }
  276. /**
  277. * 自动登录
  278. */
  279. protected function auto_login()
  280. {
  281. $data = cookie('auto_login');
  282. if (empty($data)) {
  283. return false;
  284. }
  285. $member_model = model('member');
  286. if (session('is_login')) {
  287. $member_model->auto_login();
  288. }
  289. $member_id = intval(ds_decrypt($data, MD5_KEY));
  290. if ($member_id <= 0) {
  291. return false;
  292. }
  293. $member_info = $member_model->getMemberInfoByID($member_id);
  294. if (!$member_info['member_state']) {
  295. return false;
  296. }
  297. $member_model->createSession($member_info);
  298. }
  299. }