// +---------------------------------------------------------------------- namespace cmf\controller; use huo\model\user\AdminLoginLogModel; use huo\model\user\AdminOperateLog; use huolib\tool\Ip; use think\Config; use think\Controller; use think\Cookie; use think\Request; use think\View; class BaseController extends Controller { protected $lang = 'zh-cn'; /** * 构造函数 * * @param Request $request Request对象 * * @access public */ public function __construct(Request $request = null) { if (is_null($request)) { $request = Request::instance(); } $this->request = $request; $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); } } } // 初始化视图配置 protected function _initializeView() { } private function _initLang() { $this->lang = $this->request->langset(); Cookie::set('think_var', $this->lang); config('default_lang', $this->lang); } /** * 排序 排序字段为list_orders数组 POST 排序字段为:list_order * * @param Model $model * @param string $field * * @return bool */ protected function listOrders($model, $field = 'list_order') { if (!is_object($model)) { return false; } $pk = $model->getPk(); //获取主键名称 // $ids = $this->request->post("list_orders"); $ids = $_REQUEST['list_orders']; if (!empty($ids)) { foreach ($ids as $key => $r) { $data[$field] = $r; $model->where([$pk => $key])->update($data); } } return true; } /** * 后台成功返回跳转 * * @param string $msg * @param null $url * @param string $data * @param int $wait * @param array $header */ protected function adminSuccess($msg = '', $url = null, $data = '', $wait = 3, array $header = []) { $this->insertActionLog(2, $msg); $this->success($msg, $data, 1, $header, $url, $wait); } /** * 插入后台操作记录 * * @param int $type 操作类型,1操作成功, 2 操作失败 * @param $remark */ protected function insertActionLog($type = 0, $remark) { $_user_id = cmf_get_current_admin_id(); if (empty($_user_id)) { $_user_id = 0; } $_data['user_id'] = $_user_id; $_data['username'] = !empty(session('name')) ? session('name') : ''; $_data['type'] = $type; $_data['ip'] = $this->request->ip(0, true); $_data['addr'] = Ip::getIpHome($_data['ip']); $_data['action'] = $this->request->url(true); $_data['create_time'] = time(); $_data['param'] = $this->request->method().':'.http_build_query($this->request->param()); if ('saveprotocol' == $this->request->action()) { //保存文章时文章内容太多不需要保存操作参数 $_data['param'] = ''; } $_data['remark'] = $remark; (new AdminOperateLog())->addLog($_data); } /** * 插入登陆记录 * * @param int $type 1 表示登陆 2输入网址再次登陆,3表示登出 */ protected function insertLoginLog($type = 0) { $_user_id = cmf_get_current_admin_id(); if (empty($_user_id)) { $_user_id = 0; } $_data['user_id'] = $_user_id; $_data['username'] = !empty(session('name')) ? session('name') : ''; $_data['type'] = $type; $_data['ip'] = $this->request->ip(0, true); $_data['addr'] = Ip::getIpHome($_data['ip']); $_data['device_info'] = $_SERVER["HTTP_USER_AGENT"]; $_data['login_time'] = time(); (new AdminLoginLogModel())->addLog($_data); } /** * 加载模板输出 * * @access protected * * @param string $template 模板文件名 * @param array $vars 模板输出变量 * @param array $replace 模板替换 * @param array $config 模板参数 * * @return mixed */ protected function fetch($template = '', $vars = [], $replace = [], $config = []) { $this->insertActionLog(2, '查询成功'); return parent::fetch($template, $vars, $replace, $config); } /** * 后台失败返回跳转 * * @param string $msg * @param null $url * @param string $data * @param int $wait * @param array $header */ protected function adminError($msg = '', $url = null, $data = '', $wait = 3, array $header = []) { $this->insertActionLog(1, $msg); $this->error($msg, $data, 0, $header, $url, $wait); } }