123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- <?php
- namespace wap\common\controller;
- use huo\controller\agent\Agent;
- use huo\controller\common\HuoSession;
- use huolib\constant\CommonConst;
- use huolib\constant\FormatConst;
- use huolib\status\CommonStatus;
- use think\Config;
- use think\Controller;
- use think\Db;
- use think\exception\HttpResponseException;
- use think\exception\ValidateException;
- use think\Loader;
- use think\Log;
- use think\Response;
- use think\View;
- class V2BaseController extends Controller {
-
- protected $lang = 'en';
-
- protected $response_type = 'html';
- protected $allowed_device_types = ['mobile', 'android', 'iphone', 'ipad', 'web', 'pc', 'mac', 'wxapp'];
-
- protected $request;
- protected $agent_site;
-
- protected $fail_exception = false;
-
- protected $batch_validate = false;
- protected $rq_data = [];
- protected $sess_config
- = [
- 'id' => '',
- 'prefix' => '',
- 'type' => '',
- 'expire' => CommonConst::CONST_DAY_SECONDS
- ];
-
- protected $before_action_list = [];
-
- private function _initLang() {
- $_lang = $this->request->header('HS-Lang/s', 'en-us');
- $this->lang = $_lang;
- config('default_lang', $this->lang);
- }
- protected function _initialize() {
- if(!$this->request->isMobile()) {
- $this->redirect(WEBSITE);
- }
- $this->response_type = $this->request->param('format/s', FormatConst::FORMAT_HTML);
- if (FormatConst::FORMAT_HTML == $this->response_type) {
- $this->_initializeView();
- $_site = $this->request->domain();
- $_agent_id = (new Agent())->getAgentIdBySite($_site);
- if (empty($_agent_id)) {
- $_agent_id = $this->request->request('agent_id', 0);
- }
- if (empty($_agent_id)) {
- $_agent_id = (new HuoSession($this->mem_id))->getAgentId();
- } else {
- (new HuoSession($this->mem_id))->setAgentId($_agent_id);
- }
- $siteInfo = cmf_get_site_info($_agent_id);
- View::share('site_info', $siteInfo);
- View::share('agent_id', $_agent_id);
- View::share('agent_site', $_site);
- }
- Config::set('default_return_type', $this->response_type);
- $this->_initLang();
- $this->checkParam();
-
- Log::write($this->request->getContent(), Log::LOG);
- Log::write($this->rq_data, Log::LOG);
- if ($this->request->isOptions()) {
- $this->success();
- }
- }
-
- protected function beforeAction($method, $options = []) {
- if (isset($options['only'])) {
- if (is_string($options['only'])) {
- $options['only'] = explode(',', $options['only']);
- }
- if (!in_array($this->request->action(), $options['only'])) {
- return;
- }
- } elseif (isset($options['except'])) {
- if (is_string($options['except'])) {
- $options['except'] = explode(',', $options['except']);
- }
- if (in_array($this->request->action(), $options['except'])) {
- return;
- }
- }
- call_user_func([$this, $method]);
- }
-
- protected function returnData($data = []) {
- $_msg = $data['msg'];
- $_code = $data['code'];
- $_data = $data['data'];
- if (CommonStatus::NO_ERROR != $_code) {
- $this->error($_msg, $_data, $_code);
- }
- $this->success($_msg, $_data, $_code);
- }
-
- protected function error($msg = '', $data = '', $code = 400, array $header = [], $url = null, $wait = 3) {
- $type = $this->getResponseType();
- if ('html' == $type) {
- parent::error($msg, $data, $code, $header, $url, $wait);
- }
- if (empty($data)) {
- $data = null;
- }
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'data' => $data,
- ];
- $header['Access-Control-Allow-Origin'] = '*';
- $header['Access-Control-Allow-Headers'] = 'X-Requested-With,Content-Type,HS-Device-Type,HS-Token,HS-Lang';
- $header['Access-Control-Allow-Methods'] = 'GET,POST,PATCH,PUT,DELETE,OPTIONS';
- $response = Response::create($result, $type)->header($header);
- throw new HttpResponseException($response);
- }
-
- protected function getResponseType() {
- return $this->response_type;
- }
-
- public function getMemId() {
- if (empty($this->mem_id)) {
- $this->error(lang('NO_LOGIN'), '', 1002);
- }
- return $this->mem_id;
- }
-
- protected function validateFailException($fail = true) {
- $this->fail_exception = $fail;
- return $this;
- }
-
- protected function validate($data, $validate, $message = [], $batch = false, $callback = null) {
- if (is_array($validate)) {
- $_valid_class = Loader::validate();
- $_valid_class->rule($validate);
- } else {
- if (strpos($validate, '.')) {
-
- list($validate, $scene) = explode('.', $validate);
- }
- $_valid_class = Loader::validate($validate);
- if (!empty($scene)) {
- $_valid_class->scene($scene);
- }
- }
-
- if ($batch || $this->batch_validate) {
- $_valid_class->batch(true);
- }
- if (is_array($message)) {
- $_valid_class->message($message);
- }
- if ($callback && is_callable($callback)) {
- call_user_func_array($callback, [$_valid_class, &$data]);
- }
- if (!$_valid_class->check($data)) {
- if ($this->fail_exception) {
- throw new ValidateException($_valid_class->getError());
- } else {
- return $_valid_class->getError();
- }
- } else {
- return true;
- }
- }
-
- protected function success($msg = '', $data = '', $code = 200, array $header = [], $url = null, $wait = 3) {
- $type = $this->getResponseType();
- if ('html' == $type) {
- parent::success($msg, $data, $code, $header, $url, $wait);
- }
- if (empty($data)) {
- $data = null;
- }
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'data' => $data,
- ];
- $header['Access-Control-Allow-Origin'] = '*';
- $header['Access-Control-Allow-Headers'] = 'X-Requested-With,Content-Type,HS-Device-Type,HS-Token,HS-Lang';
- $header['Access-Control-Allow-Methods'] = 'GET,POST,PATCH,PUT,DELETE,OPTIONS';
- $response = Response::create($result, $type)->header($header);
- throw new HttpResponseException($response);
- }
- protected function checkParam() {
- $_is_json = false;
- $_params = $_REQUEST;
- if ($this->request->isPost()) {
- }
- $this->rq_data = $this->getParam($_params, $_is_json);
- if (empty($this->rq_data)) {
- $this->rq_data = [];
- }
- $this->checkTime();
- }
-
- public function getParam($param, $is_json = false) {
- if (empty($param)) {
- return '';
- }
- if ($is_json) {
- $_param = json_decode($param, true);
- if (JSON_ERROR_NONE != json_last_error()) {
- return false;
- }
- } else {
- foreach ($param as $_k => $_v) {
- if (strpos($_k, '-')) {
- list($_k1, $_k2) = explode('-', $_k);
- if (is_array($_k2)) {
- return false;
- }
- $_param[$_k1][$_k2] = $_v;
- } else {
- $_param[$_k] = $_v;
- }
- }
- }
- if (empty($_param)) {
- return false;
- }
- return $_param;
- }
-
- protected function checkTime() {
- $_ts = get_val($this->rq_data, 'ts', 0);
- if (abs($_ts - time()) < 5) {
- $this->error('time is error');
- }
- }
- public function _initializeView() {
- $cmfThemePath = config('cmf_theme_path');
- $cmfDefaultTheme = cmf_get_current_theme();
- $themePath = "{$cmfThemePath}{$cmfDefaultTheme}";
- $root = cmf_get_root();
-
- $cdnSettings = cmf_get_option('cdn_settings');
- if (empty($cdnSettings['cdn_static_root'])) {
- $viewReplaceStr = [
- '__ROOT__' => $root,
- '__TMPL__' => "{$root}/{$themePath}",
- '__STATIC__' => "{$root}/static",
- '__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);
- }
- }
-
- protected function fetch($template = '', $vars = [], $replace = [], $config = []) {
- $template = $this->parseTemplate($template);
- $more = $this->getThemeFileMore($template);
- $this->assign('theme_vars', $more['vars']);
- $this->assign('theme_widgets', $more['widgets']);
- return parent::fetch($template, $vars, $replace, $config);
- }
-
- private function parseTemplate($template) {
-
- $request = $this->request;
-
- if (strpos($template, '@')) {
-
- list($module, $template) = explode('@', $template);
- }
- $viewBase = config('template.view_base');
- if ($viewBase) {
-
- $module = isset($module) ? $module : $request->module();
- $path = $viewBase.($module ? $module.DS : '');
- } else {
- $path = isset($module) ? APP_PATH.$module.DS.'view'.DS : config('template.view_path');
- }
- $depr = config('template.view_depr');
- if (0 !== strpos($template, '/')) {
- $template = str_replace(['/', ':'], $depr, $template);
- $controller = cmf_parse_name($request->controller());
- if ($controller) {
- if ('' == $template) {
-
- $template = str_replace('.', DS, $controller).$depr.$request->action();
- } elseif (false === strpos($template, $depr)) {
- $template = str_replace('.', DS, $controller).$depr.$template;
- }
- }
- } else {
- $template = str_replace(['/', ':'], $depr, substr($template, 1));
- }
- return $path.ltrim($template, '/').'.'.ltrim(config('template.view_suffix'), '.');
- }
-
- private function getThemeFileMore($file, $theme = "") {
-
- $theme = empty($theme) ? cmf_get_current_theme() : $theme;
- $themePath = config('cmf_theme_path');
- $file = str_replace('\\', '/', $file);
- $file = str_replace('//', '/', $file);
- $file = str_replace(['.html', '.php', $themePath.$theme."/"], '', $file);
- $files = Db::name('theme_file')->field('more')->where(['theme' => $theme])->where(
- function ($query) use ($file) {
- $query->where(['is_public' => 1])->whereOr(['file' => $file]);
- }
- )->select();
- $vars = [];
- $widgets = [];
- foreach ($files as $file) {
- $oldMore = json_decode($file['more'], true);
- if (!empty($oldMore['vars'])) {
- foreach ($oldMore['vars'] as $varName => $var) {
- $vars[$varName] = $var['value'];
- }
- }
- if (!empty($oldMore['widgets'])) {
- foreach ($oldMore['widgets'] as $widgetName => $widget) {
- $widgetVars = [];
- if (!empty($widget['vars'])) {
- foreach ($widget['vars'] as $varName => $var) {
- $widgetVars[$varName] = $var['value'];
- }
- }
- $widget['vars'] = $widgetVars;
- $widgets[$widgetName] = $widget;
- }
- }
- }
- return ['vars' => $vars, 'widgets' => $widgets];
- }
- }
|