BaseController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +---------------------------------------------------------------------
  9. // | Author: Dean <zxxjjforever@163.com>
  10. // +----------------------------------------------------------------------
  11. namespace cmf\controller;
  12. use huo\model\user\AdminLoginLogModel;
  13. use huo\model\user\AdminOperateLog;
  14. use huolib\tool\Ip;
  15. use think\Config;
  16. use think\Controller;
  17. use think\Cookie;
  18. use think\Request;
  19. use think\View;
  20. class BaseController extends Controller {
  21. protected $lang = 'zh-cn';
  22. /**
  23. * 构造函数
  24. *
  25. * @param Request $request Request对象
  26. *
  27. * @access public
  28. */
  29. public function __construct(Request $request = null) {
  30. if (is_null($request)) {
  31. $request = Request::instance();
  32. }
  33. $this->request = $request;
  34. $this->_initializeView();
  35. $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
  36. $this->_initLang();
  37. // 控制器初始化
  38. $this->_initialize();
  39. // 前置操作方法
  40. if ($this->beforeActionList) {
  41. foreach ($this->beforeActionList as $method => $options) {
  42. is_numeric($method)
  43. ?
  44. $this->beforeAction($options)
  45. :
  46. $this->beforeAction($method, $options);
  47. }
  48. }
  49. }
  50. // 初始化视图配置
  51. protected function _initializeView() {
  52. }
  53. private function _initLang() {
  54. $this->lang = $this->request->langset();
  55. Cookie::set('think_var', $this->lang);
  56. config('default_lang', $this->lang);
  57. }
  58. /**
  59. * 排序 排序字段为list_orders数组 POST 排序字段为:list_order
  60. *
  61. * @param Model $model
  62. * @param string $field
  63. *
  64. * @return bool
  65. */
  66. protected function listOrders($model, $field = 'list_order') {
  67. if (!is_object($model)) {
  68. return false;
  69. }
  70. $pk = $model->getPk(); //获取主键名称
  71. // $ids = $this->request->post("list_orders");
  72. $ids = $_REQUEST['list_orders'];
  73. if (!empty($ids)) {
  74. foreach ($ids as $key => $r) {
  75. $data[$field] = $r;
  76. $model->where([$pk => $key])->update($data);
  77. }
  78. }
  79. return true;
  80. }
  81. /**
  82. * 后台成功返回跳转
  83. *
  84. * @param string $msg
  85. * @param null $url
  86. * @param string $data
  87. * @param int $wait
  88. * @param array $header
  89. */
  90. protected function adminSuccess($msg = '', $url = null, $data = '', $wait = 3, array $header = []) {
  91. $this->insertActionLog(2, $msg);
  92. $this->success($msg, $data, 1, $header, $url, $wait);
  93. }
  94. /**
  95. * 插入后台操作记录
  96. *
  97. * @param int $type 操作类型,1操作成功, 2 操作失败
  98. * @param $remark
  99. */
  100. protected function insertActionLog($type = 0, $remark) {
  101. $_user_id = cmf_get_current_admin_id();
  102. if (empty($_user_id)) {
  103. $_user_id = 0;
  104. }
  105. $_data['user_id'] = $_user_id;
  106. $_data['username'] = !empty(session('name')) ? session('name') : '';
  107. $_data['type'] = $type;
  108. $_data['ip'] = $this->request->ip(0, true);
  109. $_data['addr'] = Ip::getIpHome($_data['ip']);
  110. $_data['action'] = $this->request->url(true);
  111. $_data['create_time'] = time();
  112. $_data['param'] = $this->request->method().':'.http_build_query($this->request->param());
  113. if ('saveprotocol' == $this->request->action()) {
  114. //保存文章时文章内容太多不需要保存操作参数
  115. $_data['param'] = '';
  116. }
  117. $_data['remark'] = $remark;
  118. (new AdminOperateLog())->addLog($_data);
  119. }
  120. /**
  121. * 插入登陆记录
  122. *
  123. * @param int $type 1 表示登陆 2输入网址再次登陆,3表示登出
  124. */
  125. protected function insertLoginLog($type = 0) {
  126. $_user_id = cmf_get_current_admin_id();
  127. if (empty($_user_id)) {
  128. $_user_id = 0;
  129. }
  130. $_data['user_id'] = $_user_id;
  131. $_data['username'] = !empty(session('name')) ? session('name') : '';
  132. $_data['type'] = $type;
  133. $_data['ip'] = $this->request->ip(0, true);
  134. $_data['addr'] = Ip::getIpHome($_data['ip']);
  135. $_data['device_info'] = $_SERVER["HTTP_USER_AGENT"];
  136. $_data['login_time'] = time();
  137. (new AdminLoginLogModel())->addLog($_data);
  138. }
  139. /**
  140. * 加载模板输出
  141. *
  142. * @access protected
  143. *
  144. * @param string $template 模板文件名
  145. * @param array $vars 模板输出变量
  146. * @param array $replace 模板替换
  147. * @param array $config 模板参数
  148. *
  149. * @return mixed
  150. */
  151. protected function fetch($template = '', $vars = [], $replace = [], $config = []) {
  152. $this->insertActionLog(2, '查询成功');
  153. return parent::fetch($template, $vars, $replace, $config);
  154. }
  155. /**
  156. * 后台失败返回跳转
  157. *
  158. * @param string $msg
  159. * @param null $url
  160. * @param string $data
  161. * @param int $wait
  162. * @param array $header
  163. */
  164. protected function adminError($msg = '', $url = null, $data = '', $wait = 3, array $header = []) {
  165. $this->insertActionLog(1, $msg);
  166. $this->error($msg, $data, 0, $header, $url, $wait);
  167. }
  168. }