WebBaseController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /**
  3. * WebBaseController.php UTF-8
  4. * 网页公用控制器
  5. *
  6. * @date : 2018/1/19 11:33
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : linjiebin <ljb@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace web\common\controller;
  13. use think\Controller;
  14. use think\Config;
  15. use think\Cookie;
  16. use think\Request;
  17. use think\View;
  18. use think\Db;
  19. class WebBaseController extends Controller {
  20. protected $web_info = [];
  21. /**
  22. * 构造函数
  23. *
  24. * @param Request $request Request对象
  25. *
  26. * @access public
  27. */
  28. public function __construct(Request $request = null) {
  29. // Cache::clear();
  30. if (is_null($request)) {
  31. $request = Request::instance();
  32. }
  33. $this->request = $request;
  34. if($this->request->isMobile()) {
  35. $this->redirect(MOBILESITE);
  36. }
  37. $this->_initializeView();
  38. $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
  39. $this->_initLang();
  40. // 控制器初始化
  41. $this->_initialize();
  42. // 前置操作方法
  43. if ($this->beforeActionList) {
  44. foreach ($this->beforeActionList as $method => $options) {
  45. is_numeric($method)
  46. ?
  47. $this->beforeAction($options)
  48. :
  49. $this->beforeAction($method, $options);
  50. }
  51. }
  52. View::share('api_site', APISITE);
  53. $this->web_info = $this->getWebInfo();
  54. $this->getMemberInfo();
  55. $this->common_assign();
  56. }
  57. // 初始化视图配置
  58. protected function _initializeView() {
  59. $cmfThemePath = config('cmf_theme_path');
  60. $cmfDefaultTheme = cmf_get_current_theme();
  61. $themePath = "{$cmfThemePath}{$cmfDefaultTheme}";
  62. $root = cmf_get_root();
  63. //使cdn设置生效
  64. $cdnSettings = cmf_get_option('cdn_settings');
  65. if (empty($cdnSettings['cdn_static_root'])) {
  66. $viewReplaceStr = [
  67. '__ROOT__' => $root,
  68. '__TMPL__' => "{$root}/{$themePath}",
  69. '__STATIC__' => STATICSITE,
  70. '__WEB_ROOT__' => $root
  71. ];
  72. } else {
  73. $cdnStaticRoot = rtrim($cdnSettings['cdn_static_root'], '/');
  74. $viewReplaceStr = [
  75. '__ROOT__' => $root,
  76. '__TMPL__' => "{$cdnStaticRoot}/{$themePath}",
  77. '__STATIC__' => "{$cdnStaticRoot}/static",
  78. '__WEB_ROOT__' => $cdnStaticRoot
  79. ];
  80. }
  81. $viewReplaceStr = array_merge(config('view_replace_str'), $viewReplaceStr);
  82. config('template.view_base', "{$themePath}/");
  83. config('view_replace_str', $viewReplaceStr);
  84. $themeErrorTmpl = "{$themePath}/error.html";
  85. if (file_exists_case($themeErrorTmpl)) {
  86. config('dispatch_error_tmpl', $themeErrorTmpl);
  87. }
  88. $themeSuccessTmpl = "{$themePath}/success.html";
  89. if (file_exists_case($themeSuccessTmpl)) {
  90. config('dispatch_success_tmpl', $themeSuccessTmpl);
  91. }
  92. }
  93. private function _initLang() {
  94. $this->lang = $this->request->langset();
  95. Cookie::set('think_var', $this->lang);
  96. config('default_lang', $this->lang);
  97. }
  98. /**
  99. * 获取seo信息
  100. *
  101. * @return array|mixed|string
  102. */
  103. public function getSeoInfo() {
  104. $_option_class = new \web\pc\logic\OptionLogic();
  105. $_ca_name = request()->controller().'/'.request()->action();
  106. if (strpos($_ca_name, 'Game/index')) {//游戏首页
  107. $_name = 'web_gamelist_seo';
  108. } elseif (strpos($_ca_name, 'Server/index')) {//开服首页
  109. $_name = 'web_server_seo';
  110. } elseif (strpos($_ca_name, 'News/index')) {//资讯首页
  111. $_name = 'web_newslist_seo';
  112. } elseif (strpos($_ca_name, 'Gift/index')) {//礼包首页
  113. $_name = 'web_giftindex_seo';
  114. } elseif (strpos($_ca_name, 'Page/index')) {//页面首页
  115. $_name = 'web_service_seo';
  116. } elseif (strpos($_ca_name, 'Game/detail')) {//游戏详情
  117. $_name = 'web_gamedetails_seo';
  118. } elseif (strpos($_ca_name, 'News/detail')) {//资讯详情
  119. $_name = 'web_newsdetails_seo';
  120. } else {//默认首页
  121. $_name = 'web_index_seo';
  122. }
  123. $_seo_setting = $_option_class->getValue($_name);
  124. if (empty($_seo_setting)) {
  125. $_seo_setting = [
  126. 'title' => '游戏官网',
  127. 'keyword' => '游戏官网',
  128. 'description' => '游戏官网'
  129. ];
  130. }
  131. return $_seo_setting;
  132. }
  133. public function conversionSeo(
  134. $seo_info, $webname = '', $gamename = '', $newname = '', $giftname = '', $gametype = ''
  135. ) {
  136. /*title转化*/
  137. if (strpos($seo_info['title'], '%webname%') !== false) {
  138. $seo_info['title'] = str_replace("%webname%", $webname, $seo_info['title']);
  139. }
  140. if (strpos($seo_info['title'], '%gamename%') !== false) {
  141. $seo_info['title'] = str_replace("%gamename%", $gamename, $seo_info['title']);
  142. }
  143. if (strpos($seo_info['title'], '%newsname%') !== false) {
  144. $seo_info['title'] = str_replace("%newsname%", $newname, $seo_info['title']);
  145. }
  146. if (strpos($seo_info['title'], '%giftname%') !== false) {
  147. $seo_info['title'] = str_replace("%giftname%", $giftname, $seo_info['title']);
  148. }
  149. if (strpos($seo_info['title'], '%gametype%') !== false) {
  150. $seo_info['title'] = str_replace("%gametype%", $gametype, $seo_info['title']);
  151. }
  152. /*keyword转化*/
  153. if (strpos($seo_info['keyword'], '%webname%') !== false) {
  154. $seo_info['keyword'] = str_replace("%webname%", $webname, $seo_info['keyword']);
  155. }
  156. if (strpos($seo_info['keyword'], '%gamename%') !== false) {
  157. $seo_info['keyword'] = str_replace("%gamename%", $gamename, $seo_info['keyword']);
  158. }
  159. if (strpos($seo_info['keyword'], '%newsname%') !== false) {
  160. $seo_info['keyword'] = str_replace("%newsname%", $newname, $seo_info['keyword']);
  161. }
  162. if (strpos($seo_info['keyword'], '%giftname%') !== false) {
  163. $seo_info['keyword'] = str_replace("%giftname%", $giftname, $seo_info['keyword']);
  164. }
  165. if (strpos($seo_info['keyword'], '%gametype%') !== false) {
  166. $seo_info['keyword'] = str_replace("%gametype%", $gametype, $seo_info['keyword']);
  167. }
  168. /*description转化*/
  169. if (strpos($seo_info['description'], '%webname%') !== false) {
  170. $seo_info['description'] = str_replace("%webname%", $webname, $seo_info['description']);
  171. }
  172. if (strpos($seo_info['description'], '%gamename%') !== false) {
  173. $seo_info['description'] = str_replace("%gamename%", $gamename, $seo_info['description']);
  174. }
  175. if (strpos($seo_info['description'], '%newsname%') !== false) {
  176. $seo_info['description'] = str_replace("%newsname%", $newname, $seo_info['description']);
  177. }
  178. if (strpos($seo_info['description'], '%giftname%') !== false) {
  179. $seo_info['description'] = str_replace("%giftname%", $giftname, $seo_info['description']);
  180. }
  181. if (strpos($seo_info['description'], '%gametype%') !== false) {
  182. $seo_info['description'] = str_replace("%gametype%", $gametype, $seo_info['description']);
  183. }
  184. return $seo_info;
  185. }
  186. /**
  187. * 设置通用内容
  188. *
  189. * @param $seo_info
  190. */
  191. public function common_assign($seo_info = []) {
  192. $_web_info = $this->web_info;
  193. $_default = $_web_info['web_basic']['company_name'].'游戏';
  194. if (empty($seo_info)) {
  195. $this->assign('seo_title', $_default);
  196. $this->assign('seo_keywords', $_default);
  197. $this->assign('seo_description', $_default);
  198. } else {
  199. $seo_title = isset($seo_info['title']) ? $seo_info['title'] : $_default;
  200. $seo_keywords = isset($seo_info['keyword']) ? $seo_info['keyword'] : $_default;
  201. $seo_description = isset($seo_info['description']) ? $seo_info['description'] : $_default;
  202. $this->assign('seo_title', $seo_title);
  203. $this->assign('seo_keywords', $seo_keywords);
  204. $this->assign('seo_description', $seo_description);
  205. }
  206. }
  207. public function set_seo($webname = '', $gamename = '', $newname = '') {
  208. $_seo_info = $this->getSeoInfo();
  209. $_seo_info = $this->conversionSeo($_seo_info, $webname, $gamename, $newname);
  210. $this->common_assign($_seo_info);
  211. }
  212. /**
  213. * 查询玩家是否登录
  214. *
  215. * @return bool
  216. */
  217. public function isLogin() {
  218. $_mem_id = \think\Session::get('user.id');
  219. if (empty($_mem_id)) {
  220. return false;
  221. }
  222. return true;
  223. }
  224. /**
  225. * 获取玩家信息
  226. *
  227. * @return array|false|\PDOStatement|string|\think\Model
  228. */
  229. public function getMemberInfo() {
  230. $_mem_id = \think\Session::get('user.id');
  231. if (empty($_mem_id)) {
  232. $this->assign('member', []);
  233. return [];
  234. }
  235. $_members_class = new \web\pc\logic\MemberLogic($_mem_id);
  236. $_member_info = $_members_class->getMemberInfo();
  237. $this->assign('member', $_member_info);
  238. return $_member_info;
  239. }
  240. /**
  241. * 获取网页配置信息
  242. */
  243. public function getWebInfo() {
  244. $_option_class = new \web\pc\logic\OptionLogic();
  245. $_option_names = [
  246. 'web_basic', 'web_icon', 'web_member', 'web_assistant', 'web_index_seo', 'web_gamelist_seo',
  247. 'web_gamedetails_seo', 'web_giftindex_seo', 'web_giftlist_seo', 'web_giftdetails_seo', 'web_newslist_seo',
  248. 'web_newsdetails_seo', 'web_pay_seo', 'web_service_seo', 'web_search', 'web_content'
  249. ];
  250. $web_info = $_option_class->getValues($_option_names);
  251. $web_info['app_url'] = $this->getAndDownurl(100);
  252. $this->assign($web_info);
  253. return $web_info;
  254. }
  255. public function getAndDownurl($app_id) {
  256. $_gv_map['is_delete'] = 2;
  257. $_gv_map['app_id'] = $app_id;
  258. $_gv_info = Db::name('game_version')->where($_gv_map)->order('id desc')->limit(1)->select();
  259. if (!empty($_gv_info)) {
  260. $_downurl = isset($_gv_info[0]) ? $_gv_info[0]['package_url'] :'';
  261. }
  262. if (empty($_downurl)) {
  263. return '';
  264. }
  265. return DOWNSITE.$_downurl;
  266. }
  267. //获取验证码图片
  268. public function getCaptchaImg() {
  269. $length = 4;
  270. if (isset($_GET['length']) && intval($_GET['length'])) {
  271. $length = intval($_GET['length']);
  272. }
  273. //设置验证码字符库
  274. $code_set = "";
  275. if (isset($_GET['charset'])) {
  276. $code_set = trim($_GET['charset']);
  277. }
  278. $use_noise = 0;
  279. if (isset($_GET['use_noise'])) {
  280. $use_noise = intval($_GET['use_noise']);
  281. }
  282. $use_curve = 1;
  283. if (isset($_GET['use_curve'])) {
  284. $use_curve = intval($_GET['use_curve']);
  285. }
  286. $font_size = 25;
  287. if (isset($_GET['font_size']) && intval($_GET['font_size'])) {
  288. $font_size = intval($_GET['font_size']);
  289. }
  290. $width = 0;
  291. if (isset($_GET['width']) && intval($_GET['width'])) {
  292. $width = intval($_GET['width']);
  293. }
  294. $height = 0;
  295. if (isset($_GET['height']) && intval($_GET['height'])) {
  296. $height = intval($_GET['height']);
  297. }
  298. $config = array(
  299. 'seKey' => \think\Config::get('database.authcode'),
  300. 'codeSet' => !empty($code_set) ? $code_set : "2345678abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY",
  301. // 验证码字符集合
  302. 'expire' => 1800, // 验证码过期时间(s)
  303. 'useImgBg' => false, // 使用背景图片
  304. 'fontSize' => !empty($font_size) ? $font_size : 25, // 验证码字体大小(px)
  305. 'useCurve' => $use_curve === 0 ? false : true, // 是否画混淆曲线
  306. 'useNoise' => $use_noise === 0 ? false : true, // 是否添加杂点
  307. 'imageH' => $height, // 验证码图片高度
  308. 'imageW' => $width, // 验证码图片宽度
  309. 'length' => !empty($length) ? $length : 4, // 验证码位数
  310. 'bg' => array(243, 251, 254), // 背景颜色
  311. 'reset' => true, // 验证成功后是否重置
  312. );
  313. $Verify = new \think\captcha\Captcha($config);
  314. return $Verify->entry();
  315. }
  316. //校验验证码
  317. public function checkCaptchaVerify($code, $id = '') {
  318. $length = 4;
  319. if (isset($_GET['length']) && intval($_GET['length'])) {
  320. $length = intval($_GET['length']);
  321. }
  322. //设置验证码字符库
  323. $code_set = "";
  324. if (isset($_GET['charset'])) {
  325. $code_set = trim($_GET['charset']);
  326. }
  327. $use_noise = 0;
  328. if (isset($_GET['use_noise'])) {
  329. $use_noise = intval($_GET['use_noise']);
  330. }
  331. $use_curve = 1;
  332. if (isset($_GET['use_curve'])) {
  333. $use_curve = intval($_GET['use_curve']);
  334. }
  335. $font_size = 25;
  336. if (isset($_GET['font_size']) && intval($_GET['font_size'])) {
  337. $font_size = intval($_GET['font_size']);
  338. }
  339. $width = 0;
  340. if (isset($_GET['width']) && intval($_GET['width'])) {
  341. $width = intval($_GET['width']);
  342. }
  343. $height = 0;
  344. if (isset($_GET['height']) && intval($_GET['height'])) {
  345. $height = intval($_GET['height']);
  346. }
  347. $config = array(
  348. 'seKey' => \think\Config::get('database.authcode'),
  349. 'codeSet' => !empty($code_set) ? $code_set : "2345678abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY",
  350. // 验证码字符集合
  351. 'expire' => 1800, // 验证码过期时间(s)
  352. 'useImgBg' => false, // 使用背景图片
  353. 'fontSize' => !empty($font_size) ? $font_size : 25, // 验证码字体大小(px)
  354. 'useCurve' => $use_curve === 0 ? false : true, // 是否画混淆曲线
  355. 'useNoise' => $use_noise === 0 ? false : true, // 是否添加杂点
  356. 'imageH' => $height, // 验证码图片高度
  357. 'imageW' => $width, // 验证码图片宽度
  358. 'length' => !empty($length) ? $length : 4, // 验证码位数
  359. 'bg' => array(243, 251, 254), // 背景颜色
  360. 'reset' => true, // 验证成功后是否重置
  361. );
  362. $captcha = new \think\captcha\Captcha($config);
  363. return $captcha->check($code, $id);
  364. }
  365. }