Files
orico-official-website-old/app/common/controller/BaseController.php
2024-10-29 17:57:44 +08:00

312 lines
10 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\controller;
use think\Controller;
use think\Lang;
use think\Db;
use think\Cache;
use think\Config;
class BaseController extends Controller {
//当前网站ID
protected $siteid = 32267;
//当前用户
protected $member_id;
protected $lang;
protected $module;
protected $controller;
protected $action;
protected $langid;
public function __construct()
{
parent::__construct();
}
/**
* 初始化操作
* @access protected
*/
protected function _initialize()
{
//parent::_initialize();
//$this->siteid = Config::get('siteid', 0);
$this->module = $this->request->module();
$this->controller = $this->request->controller();
$this->action = $this->request->action();
$this->view->assign('siteid', $this->siteid);
$this->view->assign('module', $this->module);
$this->view->assign('controller', $this->controller);
$this->view->assign('action', $this->action);
$this->view->assign('request', $this->request);
$this->langid = 1;
$this->lang = 'zh-cn';
$this->view->assign('lang', $this->lang);
$this->view->assign('langid', $this->langid);
$uuid = createGuid();
$this->view->assign('uuid', $uuid);
// 该服务停用了,导致访问慢,暂时注释
// setMomitorFeed($this->request->controller(), $uuid);
$this->check_url();
}
public function check_url()
{
// echo $this->module, '<hr>';
// echo $this->controller, '<hr>';
// echo $this->action, '<hr>';
// echo $this->request->scheme(), '<hr>';
// echo $this->request->domain(), '<hr>';
// echo $this->request->baseUrl(), '<hr>';
// echo $this->request->url(true), '<hr>';
// die;
$module = strtolower($this->module);
$controller = strtolower($this->controller);
$action = strtolower($this->action);
$scheme = $this->request->scheme();
$domain = $this->request->domain();
$base_url = $this->request->baseUrl();
$url = $this->request->url(true);
if ($url == 'https://www.orico.com.cn/index.php')
return $this->redirect('https://www.orico.com.cn');
if ($this->request->isMobile() && strpos($base_url, 'mobile') === false)
{
$ctrl = \think\helper\Str::snake($this->controller);
if ($ctrl == 'tops_nas') {
return $this->redirect(url("@mobile/$ctrl/$action"));
}
if (strpos($domain, 'orico.cc'))
{
// 英文
if ($base_url == '/')
return $this->redirect($scheme . '://www.orico.cc/usmobile.html'); // 首页
else if ($controller == 'product')
{
// 产品
$id = $this->request->param('id');
if ($action == 'catelists')
return $this->redirect($scheme . '://www.orico.cc/usmobile/product/catelists/id/' . $id . '.html');
else if ($action == 'subcatelists')
return $this->redirect($scheme . '://www.orico.cc/usmobile/product/subcatelists/id/' . $id . '.html');
else if ($action == 'detail')
return $this->redirect($scheme . '://www.orico.cc/usmobile/product/detail/id/' . $id . '.html');
else if ($action == 'new_arrival')
return $this->redirect($scheme . '://www.orico.cc/usmobile/product/new_arrival.html');
}
else if ($controller == 'article')
{
// 新闻
$id = $this->request->param('id');
if ($action == 'catelists')
{
$page = $this->request->param('page');
$page = isset($page) ? $page : 1;
}
else if ($action == 'detail')
return $this->redirect($scheme . '://www.orico.cc/usmobile/article/detail/id/' . $id . '.html');
}
else if ($controller == 'group')
return $this->redirect($scheme . '://www.orico.cc/usmobile/' . $controller . '/' . $action . '.html');
}
else
{
// 中文
if ($base_url == '/')
return $this->redirect($scheme . '://www.orico.com.cn/mobile.html'); // 首页
else if ($controller == 'product')
{
// 产品
$id = $this->request->param('id');
if ($action == 'catelists')
return $this->redirect($scheme . '://www.orico.com.cn/mobile/product/catelists/id/' . $id . '.html');
else if ($action == 'subcatelists')
return $this->redirect($scheme . '://www.orico.com.cn/mobile/product/subcatelists/id/' . $id . '.html');
else if ($action == 'detail')
return $this->redirect($scheme . '://www.orico.com.cn/mobile/product/detail/id/' . $id . '.html');
}
else if ($controller == 'article')
{
// 新闻
$id = $this->request->param('id');
if ($action == 'catelists')
{
$page = $this->request->param('page');
$page = isset($page) ? $page : 1;
}
else if ($action == 'detail')
return $this->redirect($scheme . '://www.orico.com.cn/mobile/article/detail/id/' . $id . '.html');
}
else if ($controller == 'group'){
/*if($action == 'oricoindex'){
return $this->redirect($scheme . '://www.orico.com.cn/mobile');
}*/
return $this->redirect($scheme . '://www.orico.com.cn/mobile/' . $controller . '/' . $action . '.html');
}
}
}
}
//空操作
public function _empty() {
if ($_SERVER['HTTP_HOST']=="www.orico.com.cn" && $_SERVER['HTTP_HOST']=="orico.com.cn") {
return $this->redirect("http://orico.com.cn");
} elseif ($_SERVER['HTTP_HOST']=="www.orico.cc" || $_SERVER['HTTP_HOST']=="orico.cc") {
return $this->redirect("http://orico.cc");
}
return exception(_lang_('This operation not valid'));
}
/**
* 检测验证码
* @param $code 验证码ID
* @param integer $id id
* @return bool 检测结果
*/
protected function verify_check($code, $id = '') {
$verify = new \verify\Verify();
return $verify->check($code, $id);
}
protected function verify_build($id = '', $cfg = []) {
//$verify = new \verify\Verify((array) Config::get('captcha'));
$config = [
'expire' => 900, // 验证码过期时间s
'codeSet' => '0123456789', // 验证码字符集合
'length' => 4, // 验证码位数
'fontSize' => 18, // 验证码字体大小(px)
'useCurve' => true, // 是否画混淆曲线
'useNoise' => true, // 是否添加杂点
'imageH' => 35, // 验证码图片高度
'imageW' => 120, // 验证码图片宽度
];
$config = array_merge($config, $cfg);
$verify = new \verify\Verify($config);
return $verify->entry($id);
}
/**
* 判断缓存是否存在
* @access public
* @param string $name 缓存变量名
* @return bool
*/
public function cacheHas($name) {
return Cache::has($name);
}
/**
* 写入缓存
* @access public
* @param string $name 缓存标识
* @param mixed $value 存储数据
* @param int|null $expire 有效时间 0为永久
* @return boolean
*/
public function cacheSet($name, $value, $expire = null) {
return Cache::set($name, $value, $expire);
}
/**
* 读取缓存
* @access public
* @param string $name 缓存标识
* @param mixed $default 默认值
* @return mixed
*/
public function cacheGet($name, $default = false) {
return Cache::get($name, $default);
}
/**
* 删除缓存
* @access public
* @param string $name 缓存标识
* @return boolean
*/
public function cacheDelete($name) {
return Cache::rm($name);
}
/**
* 清除缓存
* @access public
* @param string $tag 标签名
* @return boolean
*/
public function cacheClear($tag = null) {
return Cache::clear($tag);
}
/**
* 读取缓存并删除
* @access public
* @param string $name 缓存变量名
* @return mixed
*/
public function cachePull($name) {
return Cache::pull($name);
}
/**
* 如果不存在则写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param int $expire 有效时间 0为永久
* @return mixed
*/
public function cacheRemember($name, $value, $expire = null) {
return Cache::remember($name, $value, $expire);
}
/**
* 缓存标签
* @access public
* @param string $name 标签名
* @param string|array $keys 缓存标识
* @param bool $overlay 是否覆盖
* @return Driver
*/
public function cacheTag($name, $keys = null, $overlay = false) {
return Cache::tag($name, $keys, $overlay);
}
/**
* json格式返回数据
* @access public
* @param int $code 返回码
* @param string $msg 返回信息
* @param array $data 返回数据
* @return json
*/
public function json($code, $msg, $data=null) {
$result = [
'code' => $code,
'msg' => $msg,
];
if (!empty($data)) {
$result['data'] = $data;
}
echo json_encode($result);
exit;
}
}