CommonController.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. /**
  5. * 前台
  6. * @author Devil
  7. * @blog http://gong.gg/
  8. * @version 0.0.1
  9. * @datetime 2016-12-01T21:51:08+0800
  10. */
  11. class CommonController extends Controller
  12. {
  13. // 顶部导航
  14. protected $nav_header;
  15. // 底部导航
  16. protected $nav_footer;
  17. // 用户信息
  18. protected $user;
  19. /**
  20. * [__construt 构造方法]
  21. * @author Devil
  22. * @blog http://gong.gg/
  23. * @version 0.0.1
  24. * @datetime 2016-12-03T12:29:53+0800
  25. * @param [string] $msg [提示信息]
  26. * @param [int] $code [状态码]
  27. * @param [mixed] $data [数据]
  28. */
  29. protected function _initialize()
  30. {
  31. // 配置信息初始化
  32. MyConfigInit();
  33. // 公共数据初始化
  34. $this->CommonInit();
  35. // 菜单
  36. $this->NavInit();
  37. // 视图初始化
  38. $this->ViewInit();
  39. // 站点状态校验
  40. $this->SiteStateCheck();
  41. }
  42. /**
  43. * [ajaxReturn 重写ajax返回方法]
  44. * @author Devil
  45. * @blog http://gong.gg/
  46. * @version 0.0.1
  47. * @datetime 2016-12-07T22:03:40+0800
  48. * @param [string] $msg [提示信息]
  49. * @param [int] $code [状态码]
  50. * @param [mixed] $data [数据]
  51. * @return [json] [json数据]
  52. */
  53. protected function ajaxReturn($msg = '', $code = 0, $data = '')
  54. {
  55. // ajax的时候,success和error错误由当前方法接收
  56. if(IS_AJAX)
  57. {
  58. if(isset($msg['info']))
  59. {
  60. // success模式下code=0, error模式下code参数-1
  61. $result = array('msg'=>$msg['info'], 'code'=>-1, 'data'=>'');
  62. }
  63. }
  64. // 默认情况下,手动调用当前方法
  65. if(empty($result))
  66. {
  67. $result = array('msg'=>$msg, 'code'=>$code, 'data'=>$data);
  68. }
  69. // 错误情况下,防止提示信息为空
  70. if($result['code'] != 0 && empty($result['msg']))
  71. {
  72. $result['msg'] = L('common_operation_error');
  73. }
  74. exit(json_encode($result));
  75. }
  76. /**
  77. * [Is_Login 登录校验]
  78. * @author Devil
  79. * @blog http://gong.gg/
  80. * @version 0.0.1
  81. * @datetime 2017-03-09T11:43:48+0800
  82. */
  83. protected function Is_Login()
  84. {
  85. if(empty($_SESSION['user']))
  86. {
  87. $this->error(L('common_login_invalid'), U('Home/User/LoginInfo'));
  88. }
  89. }
  90. /**
  91. * [CommonInit 公共数据初始化]
  92. * @author Devil
  93. * @blog http://gong.gg/
  94. * @version 0.0.1
  95. * @datetime 2017-03-09T11:43:48+0800
  96. */
  97. private function CommonInit()
  98. {
  99. // 用户数据
  100. if(!empty($_SESSION['user']))
  101. {
  102. $this->user = I('session.user');
  103. }
  104. }
  105. /**
  106. * [ViewInit 视图初始化]
  107. * @author Devil
  108. * @blog http://gong.gg/
  109. * @version 0.0.1
  110. * @datetime 2016-12-03T12:30:06+0800
  111. */
  112. public function ViewInit()
  113. {
  114. // 主题
  115. $default_theme = C('DEFAULT_THEME');
  116. $this->assign('default_theme', $default_theme);
  117. // 控制器静态文件状态css,js
  118. $module_css = MODULE_NAME.DS.$default_theme.DS.'Css'.DS.CONTROLLER_NAME.'.css';
  119. $this->assign('module_css', file_exists(ROOT_PATH.'Public'.DS.$module_css) ? $module_css : '');
  120. $module_js = MODULE_NAME.DS.$default_theme.DS.'Js'.DS.CONTROLLER_NAME.'.js';
  121. $this->assign('module_js', file_exists(ROOT_PATH.'Public'.DS.$module_js) ? $module_js : '');
  122. // 导航
  123. $this->assign('nav_header', $this->nav_header);
  124. $this->assign('nav_footer', $this->nav_footer);
  125. // 当前页面选择导航状态
  126. $nav_pid = 0;
  127. $nav_id = 0;
  128. foreach($this->nav_header as $v)
  129. {
  130. if(I('viewid') == $v['id'])
  131. {
  132. $nav_id = $v['id'];
  133. }
  134. if(!empty($v['item']))
  135. {
  136. foreach($v['item'] as $vs)
  137. {
  138. if(I('viewid') == $vs['id'])
  139. {
  140. $nav_pid = $v['id'];
  141. $nav_id = $vs['id'];
  142. }
  143. }
  144. }
  145. }
  146. $this->assign('nav_pid', $nav_pid);
  147. $this->assign('nav_id', $nav_id);
  148. // 图片host地址
  149. $this->assign('image_host', C('IMAGE_HOST'));
  150. // 标题
  151. $this->assign('home_seo_site_title', MyC('home_seo_site_title'));
  152. // 页面最大宽度
  153. $max_width = MyC('home_content_max_width', 0, true);
  154. $max_width_style = ($max_width == 0) ? '' : 'max-width:'.$max_width.'px;';
  155. $this->assign('max_width_style', $max_width_style);
  156. // 用户数据
  157. $this->assign('user', $this->user);
  158. // 用户中心菜单
  159. $this->assign('user_left_menu', L('user_left_menu'));
  160. // 用户顶部菜单
  161. $this->assign('user_nav_menu', L('user_nav_menu'));
  162. }
  163. /**
  164. * [NavInit 导航初始化]
  165. * @author Devil
  166. * @blog http://gong.gg/
  167. * @version 0.0.1
  168. * @datetime 2016-12-19T22:41:20+0800
  169. */
  170. private function NavInit()
  171. {
  172. // 读取缓存数据
  173. $this->nav_header = S(C('cache_common_home_nav_header_key'));
  174. $this->nav_footer = S(C('cache_common_home_nav_footer_key'));
  175. // 导航模型
  176. $m = M('Navigation');
  177. $field = array('id', 'pid', 'name', 'url', 'value', 'data_type', 'is_new_window_open');
  178. // 缓存没数据则从数据库重新读取,顶部菜单
  179. if(empty($this->nav_header))
  180. {
  181. $this->nav_header = NavDataDealWith($m->field($field)->where(array('nav_type'=>'header', 'is_show'=>1, 'pid'=>0))->order('sort')->select());
  182. if(!empty($this->nav_header))
  183. {
  184. foreach($this->nav_header as $k=>$v)
  185. {
  186. $this->nav_header[$k]['item'] = NavDataDealWith($m->field($field)->where(array('nav_type'=>'header', 'is_show'=>1, 'pid'=>$v['id']))->order('sort')->select());
  187. }
  188. }
  189. S(C('cache_common_home_nav_header_key'), $this->nav_header);
  190. }
  191. // 底部导航
  192. if(empty($this->nav_footer))
  193. {
  194. $this->nav_footer = NavDataDealWith($m->field($field)->where(array('nav_type'=>'footer', 'is_show'=>1))->order('sort')->select());
  195. S(C('cache_common_home_nav_footer_key'), $this->nav_footer);
  196. }
  197. }
  198. /**
  199. * [GetClassList 获取班级列表,二级]
  200. * @author Devil
  201. * @blog http://gong.gg/
  202. * @version 0.0.1
  203. * @datetime 2016-12-30T13:26:00+0800
  204. * @return [array] [班级列表]
  205. */
  206. protected function GetClassList()
  207. {
  208. $m = M('Class');
  209. $data = $m->field(array('id', 'name'))->where(array('is_enable'=>1, 'pid'=>0))->select();
  210. if(!empty($data))
  211. {
  212. foreach($data as $k=>$v)
  213. {
  214. $data[$k]['item'] = $m->field(array('id', 'name'))->where(array('is_enable'=>1, 'pid'=>$v['id']))->select();
  215. }
  216. }
  217. return $data;
  218. }
  219. /**
  220. * [GetRoomList 获取教室列表,二级]
  221. * @author Devil
  222. * @blog http://gong.gg/
  223. * @version 0.0.1
  224. * @datetime 2016-12-30T13:26:00+0800
  225. * @return [array] [班级列表]
  226. */
  227. protected function GetRoomList()
  228. {
  229. $m = M('Room');
  230. $data = $m->field(array('id', 'name'))->where(array('is_enable'=>1, 'pid'=>0))->select();
  231. if(!empty($data))
  232. {
  233. foreach($data as $k=>$v)
  234. {
  235. $data[$k]['item'] = $m->field(array('id', 'name'))->where(array('is_enable'=>1, 'pid'=>$v['id']))->select();
  236. }
  237. }
  238. return $data;
  239. }
  240. /**
  241. * [GetLayoutList 获取布局-模块列表]
  242. * @author Devil
  243. * @blog http://gong.gg/
  244. * @version 0.0.1
  245. * @datetime 2017-02-22T10:15:40+0800
  246. * @param [string] $type [布局类型(home, channel, detail)]
  247. * @return [array] [布局+模块数据]
  248. */
  249. protected function GetLayoutList($type = 'home')
  250. {
  251. // 布局+模块列表
  252. $data = M('Layout')->field(array('id', 'value'))->where(array('is_enable'=>1, 'type'=>$type))->order('sort asc, id desc')->select();
  253. if(!empty($data))
  254. {
  255. // 布局模块处理驱动
  256. $lay = \My\LayoutModule::SetInstance();
  257. // 开始处理布局数据
  258. foreach($data as $k=>$v)
  259. {
  260. // 模块
  261. $item = M('LayoutModule')->where(array('layout_id'=>$v['id']))->select();
  262. if(!empty($item))
  263. {
  264. foreach($item as $ik=>$iv)
  265. {
  266. // 获取文章数据
  267. $article = LayoutArticleList($lay->GetLayoutMouleWhere($iv), $iv);
  268. // 模块数据生成
  269. $fun = GetViewTitleStyleFun($iv['title_style']);
  270. if(method_exists($lay, $fun))
  271. {
  272. $html = $lay->$fun($article, $iv);
  273. $iv = $lay->GetRules();
  274. } else {
  275. $html = '';
  276. }
  277. // 重新赋值
  278. $item[$ik] = $html;
  279. // 模板赋值
  280. $this->assign('data_'.$iv['id'], $article);
  281. $this->assign('rules_'.$iv['id'], $iv);
  282. }
  283. }
  284. $data[$k]['item'] = $item;
  285. }
  286. }
  287. return $data;
  288. }
  289. /**
  290. * [GetBrowserSeoTitle 获取浏览器seo标题]
  291. * @author Devil
  292. * @blog http://gong.gg/
  293. * @version 0.0.1
  294. * @datetime 2017-02-25T14:21:21+0800
  295. * @param [string] $title [标题]
  296. * @param [int] $type [页面类型 0, 1, 2]
  297. * @return [string] [浏览器seo标题]
  298. */
  299. protected function GetBrowserSeoTitle($title, $type)
  300. {
  301. switch($type)
  302. {
  303. case 0:
  304. break;
  305. case 1:
  306. $site_name = MyC('home_site_name');
  307. break;
  308. default:
  309. $site_name = MyC('home_seo_site_title');
  310. }
  311. return empty($title) ? $site_name : $title.' - '.$site_name;
  312. }
  313. /**
  314. * [_empty 空方法操作]
  315. * @author Devil
  316. * @blog http://gong.gg/
  317. * @version 0.0.1
  318. * @datetime 2017-02-25T15:47:50+0800
  319. * @param [string] $name [方法名称]
  320. */
  321. protected function _empty($name)
  322. {
  323. $this->assign('msg', L('common_unauthorized_access'));
  324. $this->assign('is_footer', 0);
  325. $this->display('/Public/Error');
  326. }
  327. /**
  328. * [SiteStateCheck 站点状态校验]
  329. * @author Devil
  330. * @blog http://gong.gg/
  331. * @version 0.0.1
  332. * @datetime 2017-02-25T21:43:07+0800
  333. */
  334. private function SiteStateCheck()
  335. {
  336. if(MyC('home_site_state') == 0)
  337. {
  338. $this->assign('msg', MyC('home_site_close_reason', L('common_site_maintenance_tips'), true));
  339. $this->assign('is_footer', 0);
  340. $this->display('/Public/Error');
  341. exit;
  342. }
  343. }
  344. /**
  345. * [CommonIsImaVerify 是否开启图片验证码校验]
  346. * @author Devil
  347. * @blog http://gong.gg/
  348. * @version 0.0.1
  349. * @datetime 2017-03-22T15:48:31+0800
  350. * @param [array] $verify_param [配置参数]
  351. * @return [object] [图片验证码类对象]
  352. */
  353. protected function CommonIsImaVerify($verify_param)
  354. {
  355. if(MyC('home_img_verify_state') == 1)
  356. {
  357. if(empty($_POST['verify']))
  358. {
  359. $this->ajaxReturn(L('common_param_error'), -10);
  360. }
  361. $verify = new \My\Verify($verify_param);
  362. if(!$verify->CheckExpire())
  363. {
  364. $this->ajaxReturn(L('common_verify_expire'), -11);
  365. }
  366. if(!$verify->CheckCorrect(I('verify')))
  367. {
  368. $this->ajaxReturn(L('common_verify_error'), -12);
  369. }
  370. return $verify;
  371. }
  372. }
  373. /**
  374. * [CommonVerifyEntry 验证码显示]
  375. * @author Devil
  376. * @blog http://gong.gg/
  377. * @version 0.0.1
  378. * @datetime 2017-03-05T15:10:21+0800
  379. * @param [string] $type [验证码类型]
  380. */
  381. protected function CommonVerifyEntry($type = 'schoolcms')
  382. {
  383. $param = array(
  384. 'width' => 100,
  385. 'height' => 32,
  386. 'key_prefix' => $type,
  387. );
  388. $verify = new \My\Verify($param);
  389. $verify->Entry();
  390. }
  391. /**
  392. * [UserLoginRecord 用户登录记录]
  393. * @author Devil
  394. * @blog http://gong.gg/
  395. * @version 0.0.1
  396. * @datetime 2017-03-09T11:37:43+0800
  397. * @param [int] $user_id [用户id]
  398. * @return [boolean] [记录成功true, 失败false]
  399. */
  400. protected function UserLoginRecord($user_id = 0)
  401. {
  402. if(!empty($user_id))
  403. {
  404. $field = array('id', 'mobile', 'email', 'nickname', 'gender', 'signature', 'describe', 'birthday', 'add_time', 'upd_time');
  405. $user = M('User')->field($field)->find($user_id);
  406. if(!empty($user))
  407. {
  408. // 基础数据处理
  409. $user['add_time_text'] = date('Y-m-d H:i:s', $user['add_time']);
  410. $user['upd_time_text'] = date('Y-m-d H:i:s', $user['upd_time']);
  411. $user['gender_text'] = L('common_gender_list')[$user['gender']]['name'];
  412. $user['birthday_text'] = empty($user['birthday']) ? '' : date('Y-m-d', $user['birthday']);
  413. $user['mobile_security']= empty($user['mobile']) ? '' : substr($user['mobile'], 0, 3).'***'.substr($user['mobile'], -3);
  414. $user['email_security'] = empty($user['email']) ? '' : substr($user['email'], 0, 3).'***'.substr($user['email'], -3);
  415. // 存储session
  416. $_SESSION['user'] = $user;
  417. return !empty($_SESSION['user']);
  418. }
  419. }
  420. return false;
  421. }
  422. protected function http($url, $method = 'GET', $postfields = null, $headers = array(), $debug = false) {
  423. $ci = curl_init();
  424. /* Curl settings */
  425. curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  426. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
  427. curl_setopt($ci, CURLOPT_TIMEOUT, 30);
  428. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  429. switch ($method) {
  430. case 'POST':
  431. curl_setopt($ci, CURLOPT_POST, true);
  432. if (!empty($postfields)) {
  433. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  434. $this->postdata = $postfields;
  435. }
  436. break;
  437. }
  438. curl_setopt($ci, CURLOPT_URL, $url);
  439. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  440. curl_setopt($ci, CURLINFO_HEADER_OUT, true);
  441. $response = curl_exec($ci);
  442. $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  443. if ($debug) {
  444. echo '=====$header=====' . "\r\n";
  445. print_r($headers);
  446. echo "=====post data======\r\n";
  447. var_dump($postfields);
  448. echo '=====info=====' . "\r\n";
  449. print_r(curl_getinfo($ci));
  450. echo '=====$response=====' . "\r\n";
  451. print_r($response);
  452. }
  453. curl_close($ci);
  454. return array($http_code, $response);
  455. }
  456. }