Common.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\common\controller;
  10. use think\Controller;
  11. /**
  12. * 项目公共控制器
  13. * @package app\common\controller
  14. */
  15. class Common extends Controller
  16. {
  17. /**
  18. * 初始化
  19. * @author 蔡伟明 <314013107@qq.com>
  20. */
  21. protected function initialize()
  22. {
  23. // 后台公共模板
  24. $this->assign('_admin_base_layout', config('admin_base_layout'));
  25. // 当前配色方案
  26. $this->assign('system_color', config('system_color'));
  27. // 输出弹出层参数
  28. $this->assign('_pop', $this->request->param('_pop'));
  29. }
  30. /**
  31. * 获取筛选条件
  32. * @author 蔡伟明 <314013107@qq.com>
  33. * @alter 小乌 <82950492@qq.com>
  34. * @return array
  35. */
  36. final protected function getMap()
  37. {
  38. $search_field = input('param.search_field/s', '', 'trim');
  39. $keyword = input('param.keyword/s', '', 'trim');
  40. $filter = input('param._filter/s', '', 'trim');
  41. $filter_content = input('param._filter_content/s', '', 'trim');
  42. $filter_time = input('param._filter_time/s', '', 'trim');
  43. $filter_time_from = input('param._filter_time_from/s', '', 'trim');
  44. $filter_time_to = input('param._filter_time_to/s', '', 'trim');
  45. $select_field = input('param._select_field/s', '', 'trim');
  46. $select_value = input('param._select_value/s', '', 'trim');
  47. $search_area = input('param._s', '', 'trim');
  48. $search_area_op = input('param._o', '', 'trim');
  49. $map = [];
  50. // 搜索框搜索
  51. if ($search_field != '' && $keyword !== '') {
  52. $map[] = [$search_field, 'like', "%$keyword%"];
  53. }
  54. // 下拉筛选
  55. if ($select_field != '') {
  56. $select_field = array_filter(explode('|', $select_field), 'strlen');
  57. $select_value = array_filter(explode('|', $select_value), 'strlen');
  58. foreach ($select_field as $key => $item) {
  59. if ($select_value[$key] != '_all') {
  60. $map[] = [$item, '=', $select_value[$key]];
  61. }
  62. }
  63. }
  64. // 时间段搜索
  65. if ($filter_time != '' && $filter_time_from != '' && $filter_time_to != '') {
  66. $map[] = [$filter_time, 'between time', [$filter_time_from.' 00:00:00', $filter_time_to.' 23:59:59']];
  67. }
  68. // 表头筛选
  69. if ($filter != '') {
  70. $filter = array_filter(explode('|', $filter), 'strlen');
  71. $filter_content = array_filter(explode('|', $filter_content), 'strlen');
  72. foreach ($filter as $key => $item) {
  73. if (isset($filter_content[$key])) {
  74. $map[] = [$item, 'in', $filter_content[$key]];
  75. }
  76. }
  77. }
  78. // 搜索区域
  79. if ($search_area != '') {
  80. $search_area = explode('|', $search_area);
  81. $search_area_op = explode('|', $search_area_op);
  82. foreach ($search_area as $key => $item) {
  83. list($field, $value) = explode('=', $item);
  84. $value = trim($value);
  85. $op = explode('=', $search_area_op[$key]);
  86. if ($value != '') {
  87. switch ($op[1]) {
  88. case 'like':
  89. $map[] = [$field, 'like', "%$value%"];
  90. break;
  91. case 'between time':
  92. case 'not between time':
  93. $value = explode(' - ', $value);
  94. if ($value[0] == $value[1]) {
  95. $value[0] = date('Y-m-d', strtotime($value[0])). ' 00:00:00';
  96. $value[1] = date('Y-m-d', strtotime($value[1])). ' 23:59:59';
  97. }
  98. default:
  99. $map[] = [$field, $op[1], $value];
  100. }
  101. }
  102. }
  103. }
  104. return $map;
  105. }
  106. /**
  107. * 获取字段排序
  108. * @param string $extra_order 额外的排序字段
  109. * @param bool $before 额外排序字段是否前置
  110. * @author 蔡伟明 <314013107@qq.com>
  111. * @return string
  112. */
  113. final protected function getOrder($extra_order = '', $before = false)
  114. {
  115. $order = input('param._order/s', '');
  116. $by = input('param._by/s', '');
  117. if ($order == '' || $by == '') {
  118. return $extra_order;
  119. }
  120. if ($extra_order == '') {
  121. return $order. ' '. $by;
  122. }
  123. if ($before) {
  124. return $extra_order. ',' .$order. ' '. $by;
  125. } else {
  126. return $order. ' '. $by . ',' . $extra_order;
  127. }
  128. }
  129. /**
  130. * 渲染插件模板
  131. * @param string $template 模板文件名
  132. * @param string $suffix 模板后缀
  133. * @param array $vars 模板输出变量
  134. * @param array $config 模板参数
  135. * @author 蔡伟明 <314013107@qq.com>
  136. * @return mixed
  137. */
  138. final protected function pluginView($template = '', $suffix = '', $vars = [], $config = [])
  139. {
  140. $plugin_name = input('param.plugin_name');
  141. if ($plugin_name != '') {
  142. $plugin = $plugin_name;
  143. $action = 'index';
  144. } else {
  145. $plugin = input('param._plugin');
  146. $action = input('param._action');
  147. }
  148. $suffix = $suffix == '' ? 'html' : $suffix;
  149. $template = $template == '' ? $action : $template;
  150. $template_path = config('plugin_path'). "{$plugin}/view/{$template}.{$suffix}";
  151. return parent::fetch($template_path, $vars, $config);
  152. }
  153. }