BaseHome.php 12 KB

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