123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <?php
- /**
- * WebBaseController.php UTF-8
- * 网页公用控制器
- *
- * @date : 2018/1/19 11:33
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : linjiebin <ljb@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace web\common\controller;
- use think\Controller;
- use think\Config;
- use think\Cookie;
- use think\Request;
- use think\View;
- use think\Db;
- class WebBaseController extends Controller {
- protected $web_info = [];
- /**
- * 构造函数
- *
- * @param Request $request Request对象
- *
- * @access public
- */
- public function __construct(Request $request = null) {
- // Cache::clear();
- if (is_null($request)) {
- $request = Request::instance();
- }
- $this->request = $request;
- if($this->request->isMobile()) {
- $this->redirect(MOBILESITE);
- }
- $this->_initializeView();
- $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
- $this->_initLang();
- // 控制器初始化
- $this->_initialize();
- // 前置操作方法
- if ($this->beforeActionList) {
- foreach ($this->beforeActionList as $method => $options) {
- is_numeric($method)
- ?
- $this->beforeAction($options)
- :
- $this->beforeAction($method, $options);
- }
- }
- View::share('api_site', APISITE);
- $this->web_info = $this->getWebInfo();
- $this->getMemberInfo();
- $this->common_assign();
- }
- // 初始化视图配置
- protected function _initializeView() {
- $cmfThemePath = config('cmf_theme_path');
- $cmfDefaultTheme = cmf_get_current_theme();
- $themePath = "{$cmfThemePath}{$cmfDefaultTheme}";
- $root = cmf_get_root();
- //使cdn设置生效
- $cdnSettings = cmf_get_option('cdn_settings');
- if (empty($cdnSettings['cdn_static_root'])) {
- $viewReplaceStr = [
- '__ROOT__' => $root,
- '__TMPL__' => "{$root}/{$themePath}",
- '__STATIC__' => STATICSITE,
- '__WEB_ROOT__' => $root
- ];
- } else {
- $cdnStaticRoot = rtrim($cdnSettings['cdn_static_root'], '/');
- $viewReplaceStr = [
- '__ROOT__' => $root,
- '__TMPL__' => "{$cdnStaticRoot}/{$themePath}",
- '__STATIC__' => "{$cdnStaticRoot}/static",
- '__WEB_ROOT__' => $cdnStaticRoot
- ];
- }
- $viewReplaceStr = array_merge(config('view_replace_str'), $viewReplaceStr);
- config('template.view_base', "{$themePath}/");
- config('view_replace_str', $viewReplaceStr);
- $themeErrorTmpl = "{$themePath}/error.html";
- if (file_exists_case($themeErrorTmpl)) {
- config('dispatch_error_tmpl', $themeErrorTmpl);
- }
- $themeSuccessTmpl = "{$themePath}/success.html";
- if (file_exists_case($themeSuccessTmpl)) {
- config('dispatch_success_tmpl', $themeSuccessTmpl);
- }
- }
- private function _initLang() {
- $this->lang = $this->request->langset();
- Cookie::set('think_var', $this->lang);
- config('default_lang', $this->lang);
- }
- /**
- * 获取seo信息
- *
- * @return array|mixed|string
- */
- public function getSeoInfo() {
- $_option_class = new \web\pc\logic\OptionLogic();
- $_ca_name = request()->controller().'/'.request()->action();
- if (strpos($_ca_name, 'Game/index')) {//游戏首页
- $_name = 'web_gamelist_seo';
- } elseif (strpos($_ca_name, 'Server/index')) {//开服首页
- $_name = 'web_server_seo';
- } elseif (strpos($_ca_name, 'News/index')) {//资讯首页
- $_name = 'web_newslist_seo';
- } elseif (strpos($_ca_name, 'Gift/index')) {//礼包首页
- $_name = 'web_giftindex_seo';
- } elseif (strpos($_ca_name, 'Page/index')) {//页面首页
- $_name = 'web_service_seo';
- } elseif (strpos($_ca_name, 'Game/detail')) {//游戏详情
- $_name = 'web_gamedetails_seo';
- } elseif (strpos($_ca_name, 'News/detail')) {//资讯详情
- $_name = 'web_newsdetails_seo';
- } else {//默认首页
- $_name = 'web_index_seo';
- }
- $_seo_setting = $_option_class->getValue($_name);
- if (empty($_seo_setting)) {
- $_seo_setting = [
- 'title' => '游戏官网',
- 'keyword' => '游戏官网',
- 'description' => '游戏官网'
- ];
- }
- return $_seo_setting;
- }
- public function conversionSeo(
- $seo_info, $webname = '', $gamename = '', $newname = '', $giftname = '', $gametype = ''
- ) {
- /*title转化*/
- if (strpos($seo_info['title'], '%webname%') !== false) {
- $seo_info['title'] = str_replace("%webname%", $webname, $seo_info['title']);
- }
- if (strpos($seo_info['title'], '%gamename%') !== false) {
- $seo_info['title'] = str_replace("%gamename%", $gamename, $seo_info['title']);
- }
- if (strpos($seo_info['title'], '%newsname%') !== false) {
- $seo_info['title'] = str_replace("%newsname%", $newname, $seo_info['title']);
- }
- if (strpos($seo_info['title'], '%giftname%') !== false) {
- $seo_info['title'] = str_replace("%giftname%", $giftname, $seo_info['title']);
- }
- if (strpos($seo_info['title'], '%gametype%') !== false) {
- $seo_info['title'] = str_replace("%gametype%", $gametype, $seo_info['title']);
- }
- /*keyword转化*/
- if (strpos($seo_info['keyword'], '%webname%') !== false) {
- $seo_info['keyword'] = str_replace("%webname%", $webname, $seo_info['keyword']);
- }
- if (strpos($seo_info['keyword'], '%gamename%') !== false) {
- $seo_info['keyword'] = str_replace("%gamename%", $gamename, $seo_info['keyword']);
- }
- if (strpos($seo_info['keyword'], '%newsname%') !== false) {
- $seo_info['keyword'] = str_replace("%newsname%", $newname, $seo_info['keyword']);
- }
- if (strpos($seo_info['keyword'], '%giftname%') !== false) {
- $seo_info['keyword'] = str_replace("%giftname%", $giftname, $seo_info['keyword']);
- }
- if (strpos($seo_info['keyword'], '%gametype%') !== false) {
- $seo_info['keyword'] = str_replace("%gametype%", $gametype, $seo_info['keyword']);
- }
- /*description转化*/
- if (strpos($seo_info['description'], '%webname%') !== false) {
- $seo_info['description'] = str_replace("%webname%", $webname, $seo_info['description']);
- }
- if (strpos($seo_info['description'], '%gamename%') !== false) {
- $seo_info['description'] = str_replace("%gamename%", $gamename, $seo_info['description']);
- }
- if (strpos($seo_info['description'], '%newsname%') !== false) {
- $seo_info['description'] = str_replace("%newsname%", $newname, $seo_info['description']);
- }
- if (strpos($seo_info['description'], '%giftname%') !== false) {
- $seo_info['description'] = str_replace("%giftname%", $giftname, $seo_info['description']);
- }
- if (strpos($seo_info['description'], '%gametype%') !== false) {
- $seo_info['description'] = str_replace("%gametype%", $gametype, $seo_info['description']);
- }
- return $seo_info;
- }
- /**
- * 设置通用内容
- *
- * @param $seo_info
- */
- public function common_assign($seo_info = []) {
- $_web_info = $this->web_info;
- $_default = $_web_info['web_basic']['company_name'].'游戏';
- if (empty($seo_info)) {
- $this->assign('seo_title', $_default);
- $this->assign('seo_keywords', $_default);
- $this->assign('seo_description', $_default);
- } else {
- $seo_title = isset($seo_info['title']) ? $seo_info['title'] : $_default;
- $seo_keywords = isset($seo_info['keyword']) ? $seo_info['keyword'] : $_default;
- $seo_description = isset($seo_info['description']) ? $seo_info['description'] : $_default;
- $this->assign('seo_title', $seo_title);
- $this->assign('seo_keywords', $seo_keywords);
- $this->assign('seo_description', $seo_description);
- }
- }
- public function set_seo($webname = '', $gamename = '', $newname = '') {
- $_seo_info = $this->getSeoInfo();
- $_seo_info = $this->conversionSeo($_seo_info, $webname, $gamename, $newname);
- $this->common_assign($_seo_info);
- }
- /**
- * 查询玩家是否登录
- *
- * @return bool
- */
- public function isLogin() {
- $_mem_id = \think\Session::get('user.id');
- if (empty($_mem_id)) {
- return false;
- }
- return true;
- }
- /**
- * 获取玩家信息
- *
- * @return array|false|\PDOStatement|string|\think\Model
- */
- public function getMemberInfo() {
- $_mem_id = \think\Session::get('user.id');
- if (empty($_mem_id)) {
- $this->assign('member', []);
- return [];
- }
- $_members_class = new \web\pc\logic\MemberLogic($_mem_id);
- $_member_info = $_members_class->getMemberInfo();
- $this->assign('member', $_member_info);
- return $_member_info;
- }
- /**
- * 获取网页配置信息
- */
- public function getWebInfo() {
- $_option_class = new \web\pc\logic\OptionLogic();
- $_option_names = [
- 'web_basic', 'web_icon', 'web_member', 'web_assistant', 'web_index_seo', 'web_gamelist_seo',
- 'web_gamedetails_seo', 'web_giftindex_seo', 'web_giftlist_seo', 'web_giftdetails_seo', 'web_newslist_seo',
- 'web_newsdetails_seo', 'web_pay_seo', 'web_service_seo', 'web_search', 'web_content'
- ];
- $web_info = $_option_class->getValues($_option_names);
- $web_info['app_url'] = $this->getAndDownurl(100);
- $this->assign($web_info);
- return $web_info;
- }
- public function getAndDownurl($app_id) {
- $_gv_map['is_delete'] = 2;
- $_gv_map['app_id'] = $app_id;
- $_gv_info = Db::name('game_version')->where($_gv_map)->order('id desc')->limit(1)->select();
- if (!empty($_gv_info)) {
- $_downurl = isset($_gv_info[0]) ? $_gv_info[0]['package_url'] :'';
- }
- if (empty($_downurl)) {
- return '';
- }
- return DOWNSITE.$_downurl;
- }
- //获取验证码图片
- public function getCaptchaImg() {
- $length = 4;
- if (isset($_GET['length']) && intval($_GET['length'])) {
- $length = intval($_GET['length']);
- }
- //设置验证码字符库
- $code_set = "";
- if (isset($_GET['charset'])) {
- $code_set = trim($_GET['charset']);
- }
- $use_noise = 0;
- if (isset($_GET['use_noise'])) {
- $use_noise = intval($_GET['use_noise']);
- }
- $use_curve = 1;
- if (isset($_GET['use_curve'])) {
- $use_curve = intval($_GET['use_curve']);
- }
- $font_size = 25;
- if (isset($_GET['font_size']) && intval($_GET['font_size'])) {
- $font_size = intval($_GET['font_size']);
- }
- $width = 0;
- if (isset($_GET['width']) && intval($_GET['width'])) {
- $width = intval($_GET['width']);
- }
- $height = 0;
- if (isset($_GET['height']) && intval($_GET['height'])) {
- $height = intval($_GET['height']);
- }
- $config = array(
- 'seKey' => \think\Config::get('database.authcode'),
- 'codeSet' => !empty($code_set) ? $code_set : "2345678abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY",
- // 验证码字符集合
- 'expire' => 1800, // 验证码过期时间(s)
- 'useImgBg' => false, // 使用背景图片
- 'fontSize' => !empty($font_size) ? $font_size : 25, // 验证码字体大小(px)
- 'useCurve' => $use_curve === 0 ? false : true, // 是否画混淆曲线
- 'useNoise' => $use_noise === 0 ? false : true, // 是否添加杂点
- 'imageH' => $height, // 验证码图片高度
- 'imageW' => $width, // 验证码图片宽度
- 'length' => !empty($length) ? $length : 4, // 验证码位数
- 'bg' => array(243, 251, 254), // 背景颜色
- 'reset' => true, // 验证成功后是否重置
- );
- $Verify = new \think\captcha\Captcha($config);
- return $Verify->entry();
- }
- //校验验证码
- public function checkCaptchaVerify($code, $id = '') {
- $length = 4;
- if (isset($_GET['length']) && intval($_GET['length'])) {
- $length = intval($_GET['length']);
- }
- //设置验证码字符库
- $code_set = "";
- if (isset($_GET['charset'])) {
- $code_set = trim($_GET['charset']);
- }
- $use_noise = 0;
- if (isset($_GET['use_noise'])) {
- $use_noise = intval($_GET['use_noise']);
- }
- $use_curve = 1;
- if (isset($_GET['use_curve'])) {
- $use_curve = intval($_GET['use_curve']);
- }
- $font_size = 25;
- if (isset($_GET['font_size']) && intval($_GET['font_size'])) {
- $font_size = intval($_GET['font_size']);
- }
- $width = 0;
- if (isset($_GET['width']) && intval($_GET['width'])) {
- $width = intval($_GET['width']);
- }
- $height = 0;
- if (isset($_GET['height']) && intval($_GET['height'])) {
- $height = intval($_GET['height']);
- }
- $config = array(
- 'seKey' => \think\Config::get('database.authcode'),
- 'codeSet' => !empty($code_set) ? $code_set : "2345678abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY",
- // 验证码字符集合
- 'expire' => 1800, // 验证码过期时间(s)
- 'useImgBg' => false, // 使用背景图片
- 'fontSize' => !empty($font_size) ? $font_size : 25, // 验证码字体大小(px)
- 'useCurve' => $use_curve === 0 ? false : true, // 是否画混淆曲线
- 'useNoise' => $use_noise === 0 ? false : true, // 是否添加杂点
- 'imageH' => $height, // 验证码图片高度
- 'imageW' => $width, // 验证码图片宽度
- 'length' => !empty($length) ? $length : 4, // 验证码位数
- 'bg' => array(243, 251, 254), // 背景颜色
- 'reset' => true, // 验证成功后是否重置
- );
- $captcha = new \think\captcha\Captcha($config);
- return $captcha->check($code, $id);
- }
- }
|