作者 Karson

新增Auth的getGroupIds、getChildrenAdminIds和getChildrenGroupIds方法

移除require-table.js的label方法
修复commonsearch普通搜索中queryParmas的BUG
修复operate为小写字母导致的BUG
优化管理员、管理组的代码
@@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
2 2
3 namespace app\admin\controller\auth; 3 namespace app\admin\controller\auth;
4 4
  5 +use app\admin\model\AuthGroup;
  6 +use app\admin\model\AuthGroupAccess;
5 use app\common\controller\Backend; 7 use app\common\controller\Backend;
6 use fast\Random; 8 use fast\Random;
7 use fast\Tree; 9 use fast\Tree;
@@ -16,33 +18,21 @@ class Admin extends Backend @@ -16,33 +18,21 @@ class Admin extends Backend
16 { 18 {
17 19
18 protected $model = null; 20 protected $model = null;
19 - //当前登录管理员所有子节点组别  
20 - protected $childrenIds = []; 21 + protected $childrenGroupIds = [];
  22 + protected $childrenAdminIds = [];
21 23
22 public function _initialize() 24 public function _initialize()
23 { 25 {
24 parent::_initialize(); 26 parent::_initialize();
25 $this->model = model('Admin'); 27 $this->model = model('Admin');
26 28
27 - $groups = $this->auth->getGroups(); 29 + $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
  30 + $this->childrenGroupIds = $this->auth->getChildrenGroupIds();
28 31
29 - // 取出所有分组  
30 - $grouplist = model('AuthGroup')->all(['status' => 'normal']);  
31 - $objlist = [];  
32 - foreach ($groups as $K => $v)  
33 - {  
34 - // 取出包含自己的所有子节点  
35 - $childrenlist = Tree::instance()->init($grouplist)->getChildren($v['id'], TRUE);  
36 - $obj = Tree::instance()->init($childrenlist)->getTreeArray($v['pid']);  
37 - $objlist = array_merge($objlist, Tree::instance()->getTreeList($obj));  
38 - }  
39 - $groupdata = [];  
40 - foreach ($objlist as $k => $v)  
41 - {  
42 - $groupdata[$v['id']] = $v['name'];  
43 - }  
44 - $this->childrenIds = array_keys($groupdata);  
45 - $this->view->assign('groupdata', $groupdata); 32 + $groupName = AuthGroup::where('id', 'in', $this->childrenGroupIds)
  33 + ->column('id,name');
  34 +
  35 + $this->view->assign('groupdata', $groupName);
46 $this->assignconfig("admin", ['id' => $this->auth->id]); 36 $this->assignconfig("admin", ['id' => $this->auth->id]);
47 } 37 }
48 38
@@ -53,31 +43,31 @@ class Admin extends Backend @@ -53,31 +43,31 @@ class Admin extends Backend
53 { 43 {
54 if ($this->request->isAjax()) 44 if ($this->request->isAjax())
55 { 45 {
56 - $groupData = model('AuthGroup')->where('status', 'normal')->column('id,name');  
57 46
58 - $childrenAdminIds = [];  
59 - $authGroupList = model('AuthGroupAccess') 47 + $childrenGroupIds = $this->auth->getChildrenAdminIds(true);
  48 + $groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
  49 + ->column('id,name');
  50 + $authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
60 ->field('uid,group_id') 51 ->field('uid,group_id')
61 - ->where('group_id', 'in', $this->childrenIds)  
62 ->select(); 52 ->select();
63 53
64 $adminGroupName = []; 54 $adminGroupName = [];
65 foreach ($authGroupList as $k => $v) 55 foreach ($authGroupList as $k => $v)
66 { 56 {
67 - $childrenAdminIds[] = $v['uid'];  
68 - if (isset($groupData[$v['group_id']]))  
69 - $adminGroupName[$v['uid']][$v['group_id']] = $groupData[$v['group_id']]; 57 + if (isset($groupName[$v['group_id']]))
  58 + $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
70 } 59 }
  60 +
71 list($where, $sort, $order, $offset, $limit) = $this->buildparams(); 61 list($where, $sort, $order, $offset, $limit) = $this->buildparams();
72 $total = $this->model 62 $total = $this->model
73 ->where($where) 63 ->where($where)
74 - ->where('id', 'in', $childrenAdminIds) 64 + ->where('id', 'in', $this->childrenAdminIds)
75 ->order($sort, $order) 65 ->order($sort, $order)
76 ->count(); 66 ->count();
77 67
78 $list = $this->model 68 $list = $this->model
79 ->where($where) 69 ->where($where)
80 - ->where('id', 'in', $childrenAdminIds) 70 + ->where('id', 'in', $this->childrenAdminIds)
81 ->field(['password', 'salt', 'token'], true) 71 ->field(['password', 'salt', 'token'], true)
82 ->order($sort, $order) 72 ->order($sort, $order)
83 ->limit($offset, $limit) 73 ->limit($offset, $limit)
@@ -88,6 +78,7 @@ class Admin extends Backend @@ -88,6 +78,7 @@ class Admin extends Backend
88 $v['groups'] = implode(',', array_keys($groups)); 78 $v['groups'] = implode(',', array_keys($groups));
89 $v['groups_text'] = implode(',', array_values($groups)); 79 $v['groups_text'] = implode(',', array_values($groups));
90 } 80 }
  81 + unset($v);
91 $result = array("total" => $total, "rows" => $list); 82 $result = array("total" => $total, "rows" => $list);
92 83
93 return json($result); 84 return json($result);
@@ -113,7 +104,7 @@ class Admin extends Backend @@ -113,7 +104,7 @@ class Admin extends Backend
113 $group = $this->request->post("group/a"); 104 $group = $this->request->post("group/a");
114 105
115 //过滤不允许的组别,避免越权 106 //过滤不允许的组别,避免越权
116 - $group = array_intersect($this->childrenIds, $group); 107 + $group = array_intersect($this->childrenGroupIds, $group);
117 $dataset = []; 108 $dataset = [];
118 foreach ($group as $value) 109 foreach ($group as $value)
119 { 110 {
@@ -157,7 +148,7 @@ class Admin extends Backend @@ -157,7 +148,7 @@ class Admin extends Backend
157 $group = $this->request->post("group/a"); 148 $group = $this->request->post("group/a");
158 149
159 // 过滤不允许的组别,避免越权 150 // 过滤不允许的组别,避免越权
160 - $group = array_intersect($this->childrenIds, $group); 151 + $group = array_intersect($this->childrenGroupIds, $group);
161 152
162 $dataset = []; 153 $dataset = [];
163 foreach ($group as $value) 154 foreach ($group as $value)
@@ -188,7 +179,7 @@ class Admin extends Backend @@ -188,7 +179,7 @@ class Admin extends Backend
188 if ($ids) 179 if ($ids)
189 { 180 {
190 // 避免越权删除管理员 181 // 避免越权删除管理员
191 - $childrenGroupIds = $this->childrenIds; 182 + $childrenGroupIds = $this->childrenGroupIds;
192 $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) { 183 $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) {
193 $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid'); 184 $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
194 })->select(); 185 })->select();
@@ -2,8 +2,8 @@ @@ -2,8 +2,8 @@
2 2
3 namespace app\admin\controller\auth; 3 namespace app\admin\controller\auth;
4 4
  5 +use app\admin\model\AuthGroup;
5 use app\common\controller\Backend; 6 use app\common\controller\Backend;
6 -use fast\Tree;  
7 7
8 /** 8 /**
9 * 管理员日志 9 * 管理员日志
@@ -15,33 +15,21 @@ class Adminlog extends Backend @@ -15,33 +15,21 @@ class Adminlog extends Backend
15 { 15 {
16 16
17 protected $model = null; 17 protected $model = null;
18 - //当前登录管理员所有子节点组别  
19 - protected $childrenIds = []; 18 + protected $childrenGroupIds = [];
  19 + protected $childrenAdminIds = [];
20 20
21 public function _initialize() 21 public function _initialize()
22 { 22 {
23 parent::_initialize(); 23 parent::_initialize();
24 $this->model = model('AdminLog'); 24 $this->model = model('AdminLog');
25 25
26 - $groups = $this->auth->getGroups(); 26 + $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
  27 + $this->childrenGroupIds = $this->auth->getChildrenGroupIds();
27 28
28 - // 取出所有分组  
29 - $grouplist = model('AuthGroup')->all(['status' => 'normal']);  
30 - $objlist = [];  
31 - foreach ($groups as $K => $v)  
32 - {  
33 - // 取出包含自己的所有子节点  
34 - $childrenlist = Tree::instance()->init($grouplist)->getChildren($v['id'], TRUE);  
35 - $obj = Tree::instance()->init($childrenlist)->getTreeArray($v['pid']);  
36 - $objlist = array_merge($objlist, Tree::instance()->getTreeList($obj));  
37 - }  
38 - $groupdata = [];  
39 - foreach ($objlist as $k => $v)  
40 - {  
41 - $groupdata[$v['id']] = $v['name'];  
42 - }  
43 - $this->childrenIds = array_keys($groupdata);  
44 - $this->view->assign('groupdata', $groupdata); 29 + $groupName = AuthGroup::where('id', 'in', $this->childrenGroupIds)
  30 + ->column('id,name');
  31 +
  32 + $this->view->assign('groupdata', $groupName);
45 } 33 }
46 34
47 /** 35 /**
@@ -51,20 +39,16 @@ class Adminlog extends Backend @@ -51,20 +39,16 @@ class Adminlog extends Backend
51 { 39 {
52 if ($this->request->isAjax()) 40 if ($this->request->isAjax())
53 { 41 {
54 - $childrenAdminIds = model('AuthGroupAccess')  
55 - ->field('uid')  
56 - ->where('group_id', 'in', $this->childrenIds)  
57 - ->column('uid');  
58 list($where, $sort, $order, $offset, $limit) = $this->buildparams(); 42 list($where, $sort, $order, $offset, $limit) = $this->buildparams();
59 $total = $this->model 43 $total = $this->model
60 ->where($where) 44 ->where($where)
61 - ->where('admin_id', 'in', $childrenAdminIds) 45 + ->where('admin_id', 'in', $this->childrenAdminIds)
62 ->order($sort, $order) 46 ->order($sort, $order)
63 ->count(); 47 ->count();
64 48
65 $list = $this->model 49 $list = $this->model
66 ->where($where) 50 ->where($where)
67 - ->where('admin_id', 'in', $childrenAdminIds) 51 + ->where('admin_id', 'in', $this->childrenAdminIds)
68 ->order($sort, $order) 52 ->order($sort, $order)
69 ->limit($offset, $limit) 53 ->limit($offset, $limit)
70 ->select(); 54 ->select();
@@ -112,7 +96,7 @@ class Adminlog extends Backend @@ -112,7 +96,7 @@ class Adminlog extends Backend
112 { 96 {
113 if ($ids) 97 if ($ids)
114 { 98 {
115 - $childrenGroupIds = $this->childrenIds; 99 + $childrenGroupIds = $this->childrenGroupIds;
116 $adminList = $this->model->where('id', 'in', $ids)->where('admin_id', 'in', function($query) use($childrenGroupIds) { 100 $adminList = $this->model->where('id', 'in', $ids)->where('admin_id', 'in', function($query) use($childrenGroupIds) {
117 $query->name('auth_group_access')->field('uid'); 101 $query->name('auth_group_access')->field('uid');
118 })->select(); 102 })->select();
@@ -142,5 +126,10 @@ class Adminlog extends Backend @@ -142,5 +126,10 @@ class Adminlog extends Backend
142 // 管理员禁止批量操作 126 // 管理员禁止批量操作
143 $this->error(); 127 $this->error();
144 } 128 }
  129 +
  130 + public function selectpage()
  131 + {
  132 + return parent::selectpage();
  133 + }
145 134
146 } 135 }
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 namespace app\admin\controller\auth; 3 namespace app\admin\controller\auth;
4 4
  5 +use app\admin\model\AuthGroup;
5 use app\common\controller\Backend; 6 use app\common\controller\Backend;
6 use fast\Tree; 7 use fast\Tree;
7 8
@@ -15,8 +16,8 @@ class Group extends Backend @@ -15,8 +16,8 @@ class Group extends Backend
15 { 16 {
16 17
17 protected $model = null; 18 protected $model = null;
18 - //当前登录管理员所有子节点组别  
19 - protected $childrenIds = []; 19 + //当前登录管理员所有子组别
  20 + protected $childrenGroupIds = [];
20 //当前组别列表数据 21 //当前组别列表数据
21 protected $groupdata = []; 22 protected $groupdata = [];
22 //无需要权限判断的方法 23 //无需要权限判断的方法
@@ -27,30 +28,15 @@ class Group extends Backend @@ -27,30 +28,15 @@ class Group extends Backend
27 parent::_initialize(); 28 parent::_initialize();
28 $this->model = model('AuthGroup'); 29 $this->model = model('AuthGroup');
29 30
30 - $groups = $this->auth->getGroups(); 31 + $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
31 32
32 - // 取出所有分组  
33 - $grouplist = model('AuthGroup')->all(['status' => 'normal']);  
34 - $objlist = [];  
35 - $group_ids = [];  
36 - foreach ($groups as $K => $v)  
37 - {  
38 - // 取出包含自己的所有子节点  
39 - $childrenlist = Tree::instance()->init($grouplist)->getChildren($v['id'], TRUE);  
40 - $obj = Tree::instance()->init($childrenlist)->getTreeArray($v['pid']);  
41 - $objlist = array_merge($objlist, Tree::instance()->getTreeList($obj));  
42 - $group_ids[] = (int) $v['group_id'];  
43 - } 33 + $groupName = AuthGroup::where('id', 'in', $this->childrenGroupIds)
  34 + ->column('id,name');
44 35
45 - $groupdata = [];  
46 - foreach ($objlist as $k => $v)  
47 - {  
48 - $groupdata[$v['id']] = $v['name'];  
49 - }  
50 - $this->groupdata = $groupdata;  
51 - $this->assignconfig("admin", ['id' => $this->auth->id, 'group_ids' => $group_ids]);  
52 - $this->childrenIds = array_keys($groupdata);  
53 - $this->view->assign('groupdata', $groupdata); 36 + $this->groupdata = $groupName;
  37 + $this->assignconfig("admin", ['id' => $this->auth->id, 'group_ids' => $this->auth->getGroupIds()]);
  38 +
  39 + $this->view->assign('groupdata', $this->groupdata);
54 } 40 }
55 41
56 /** 42 /**
@@ -84,7 +70,7 @@ class Group extends Backend @@ -84,7 +70,7 @@ class Group extends Backend
84 { 70 {
85 $params = $this->request->post("row/a", [], 'strip_tags'); 71 $params = $this->request->post("row/a", [], 'strip_tags');
86 $params['rules'] = explode(',', $params['rules']); 72 $params['rules'] = explode(',', $params['rules']);
87 - if (!in_array($params['pid'], $this->childrenIds)) 73 + if (!in_array($params['pid'], $this->childrenGroupIds))
88 { 74 {
89 $this->error(__('The parent group can not be its own child')); 75 $this->error(__('The parent group can not be its own child'));
90 } 76 }
@@ -125,7 +111,7 @@ class Group extends Backend @@ -125,7 +111,7 @@ class Group extends Backend
125 { 111 {
126 $params = $this->request->post("row/a", [], 'strip_tags'); 112 $params = $this->request->post("row/a", [], 'strip_tags');
127 // 父节点不能是它自身的子节点 113 // 父节点不能是它自身的子节点
128 - if (!in_array($params['pid'], $this->childrenIds)) 114 + if (!in_array($params['pid'], $this->childrenGroupIds))
129 { 115 {
130 $this->error(__('The parent group can not be its own child')); 116 $this->error(__('The parent group can not be its own child'));
131 } 117 }
@@ -91,6 +91,7 @@ return [ @@ -91,6 +91,7 @@ return [
91 'Wechat manager' => '微信管理', 91 'Wechat manager' => '微信管理',
92 'Common search' => '普通搜索', 92 'Common search' => '普通搜索',
93 'Search %s' => '搜索 %s', 93 'Search %s' => '搜索 %s',
  94 + 'View %s' => '查看 %s',
94 '%d second%s ago' => '%d秒前', 95 '%d second%s ago' => '%d秒前',
95 '%d minute%s ago' => '%d分钟前', 96 '%d minute%s ago' => '%d分钟前',
96 '%d hour%s ago' => '%d小时前', 97 '%d hour%s ago' => '%d小时前',
@@ -205,6 +205,95 @@ class Auth extends \fast\Auth @@ -205,6 +205,95 @@ class Auth extends \fast\Auth
205 } 205 }
206 206
207 /** 207 /**
  208 + * 获取管理员所属于的分组ID
  209 + * @param int $uid
  210 + * @return array
  211 + */
  212 + public function getGroupIds($uid = null)
  213 + {
  214 + $groups = $this->getGroups($uid);
  215 + $groupIds = [];
  216 + foreach ($groups as $K => $v)
  217 + {
  218 + $groupIds[] = (int) $v['group_id'];
  219 + }
  220 + return $groupIds;
  221 + }
  222 +
  223 + /**
  224 + * 取出当前管理员所拥有权限的分组
  225 + * @param boolean $withself 是否包含当前所在的分组
  226 + * @return array
  227 + */
  228 + public function getChildrenGroupIds($withself = false)
  229 + {
  230 + //取出当前管理员所有的分组
  231 + $groups = $this->getGroups();
  232 + $groupIds = [];
  233 + foreach ($groups as $k => $v)
  234 + {
  235 + $groupIds[] = $v['id'];
  236 + }
  237 + // 取出所有分组
  238 + $groupList = model('AuthGroup')->all(['status' => 'normal']);
  239 + $objList = [];
  240 + foreach ($groups as $K => $v)
  241 + {
  242 + if ($v['rules'] === '*')
  243 + {
  244 + $objList = $groupList;
  245 + break;
  246 + }
  247 + // 取出包含自己的所有子节点
  248 + $childrenList = Tree::instance()->init($groupList)->getChildren($v['id'], true);
  249 + $obj = Tree::instance()->init($childrenList)->getTreeArray($v['pid']);
  250 + $objList = array_merge($objList, Tree::instance()->getTreeList($obj));
  251 + }
  252 + $childrenGroupIds = [];
  253 + foreach ($objList as $k => $v)
  254 + {
  255 + $childrenGroupIds[] = $v['id'];
  256 + }
  257 + if (!$withself)
  258 + {
  259 + $childrenGroupIds = array_diff($childrenGroupIds, $groupIds);
  260 + }
  261 + return $childrenGroupIds;
  262 + }
  263 +
  264 + /**
  265 + * 取出当前管理员所拥有权限的管理员
  266 + * @param boolean $withself 是否包含自身
  267 + * @return array
  268 + */
  269 + public function getChildrenAdminIds($withself = false)
  270 + {
  271 + $groupIds = $this->getChildrenGroupIds(false);
  272 + $childrenAdminIds = [];
  273 + $authGroupList = model('AuthGroupAccess')
  274 + ->field('uid,group_id')
  275 + ->where('group_id', 'in', $groupIds)
  276 + ->select();
  277 +
  278 + foreach ($authGroupList as $k => $v)
  279 + {
  280 + $childrenAdminIds[] = $v['uid'];
  281 + }
  282 + if ($withself)
  283 + {
  284 + if (!in_array($this->id, $childrenAdminIds))
  285 + {
  286 + $childrenAdminIds[] = $this->id;
  287 + }
  288 + }
  289 + else
  290 + {
  291 + $childrenAdminIds = array_diff($childrenAdminIds, [$this->id]);
  292 + }
  293 + return $childrenAdminIds;
  294 + }
  295 +
  296 + /**
208 * 获得面包屑导航 297 * 获得面包屑导航
209 * @param string $path 298 * @param string $path
210 * @return array 299 * @return array
@@ -239,14 +239,18 @@ class Backend extends Controller @@ -239,14 +239,18 @@ class Backend extends Controller
239 { 239 {
240 $k = $tableName . $k; 240 $k = $tableName . $k;
241 } 241 }
242 - $sym = isset($op[$k]) ? $op[$k] : $sym; 242 + $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
243 switch ($sym) 243 switch ($sym)
244 { 244 {
245 case '=': 245 case '=':
246 case '!=': 246 case '!=':
  247 + $where[] = [$k, $sym, (string) $v];
  248 + break;
247 case 'LIKE': 249 case 'LIKE':
248 case 'NOT LIKE': 250 case 'NOT LIKE':
249 - $where[] = [$k, $sym, (string) $v]; 251 + case 'LIKE %...%':
  252 + case 'NOT LIKE %...%':
  253 + $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
250 break; 254 break;
251 case '>': 255 case '>':
252 case '>=': 256 case '>=':
@@ -348,7 +352,7 @@ class Backend extends Controller @@ -348,7 +352,7 @@ class Backend extends Controller
348 $field = $field ? $field : 'name'; 352 $field = $field ? $field : 'name';
349 353
350 //如果有primaryvalue,说明当前是初始化传值 354 //如果有primaryvalue,说明当前是初始化传值
351 - if ($primaryvalue) 355 + if ($primaryvalue !== null)
352 { 356 {
353 $where = [$primarykey => ['in', $primaryvalue]]; 357 $where = [$primarykey => ['in', $primaryvalue]];
354 } 358 }
@@ -23,7 +23,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin @@ -23,7 +23,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
23 {field: 'state', checkbox: true, }, 23 {field: 'state', checkbox: true, },
24 {field: 'id', title: 'ID', operate: false}, 24 {field: 'id', title: 'ID', operate: false},
25 {field: 'username', title: __('Username'), formatter: Table.api.formatter.search}, 25 {field: 'username', title: __('Username'), formatter: Table.api.formatter.search},
26 - {field: 'title', title: __('Title'), operate: 'LIKE %...%', placeholder: '模糊搜索,*表示任意字符', style: 'width:200px'}, 26 + {field: 'title', title: __('Title'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
27 {field: 'url', title: __('Url'), align: 'left', formatter: Table.api.formatter.url}, 27 {field: 'url', title: __('Url'), align: 'left', formatter: Table.api.formatter.url},
28 {field: 'ip', title: __('IP'), events: Table.api.events.ip, formatter: Table.api.formatter.search}, 28 {field: 'ip', title: __('IP'), events: Table.api.events.ip, formatter: Table.api.formatter.search},
29 {field: 'browser', title: __('Browser'), operate: false, formatter: Controller.api.formatter.browser}, 29 {field: 'browser', title: __('Browser'), operate: false, formatter: Controller.api.formatter.browser},
@@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
3 * @version: v0.0.1 3 * @version: v0.0.1
4 * 4 *
5 * @update 2017-05-07 <http://git.oschina.net/pp/fastadmin> 5 * @update 2017-05-07 <http://git.oschina.net/pp/fastadmin>
6 - * @update 2017-09-03 <http://git.oschina.net/karson/fastadmin> 6 + * @update 2017-09-09 <http://git.oschina.net/karson/fastadmin>
7 */ 7 */
8 8
9 !function ($) { 9 !function ($) {
@@ -178,41 +178,43 @@ @@ -178,41 +178,43 @@
178 return true; 178 return true;
179 }; 179 };
180 180
181 - var getSearchQuery = function (that) { 181 + var getSearchQuery = function (that, removeempty) {
182 var op = {}; 182 var op = {};
183 var filter = {}; 183 var filter = {};
184 - $("form.form-commonsearch input.operate", that.$container).each(function (i) { 184 + var value = '';
  185 + $("form.form-commonsearch input.operate", that.$commonsearch).each(function (i) {
185 var name = $(this).data("name"); 186 var name = $(this).data("name");
186 var sym = $(this).val(); 187 var sym = $(this).val();
187 - var obj = $("[name='" + name + "']"); 188 + var obj = $("[name='" + name + "']", that.$commonsearch);
188 if (obj.size() == 0) 189 if (obj.size() == 0)
189 return true; 190 return true;
190 var vObjCol = ColumnsForSearch[i]; 191 var vObjCol = ColumnsForSearch[i];
191 if (obj.size() > 1) { 192 if (obj.size() > 1) {
192 if (/BETWEEN$/.test(sym)) { 193 if (/BETWEEN$/.test(sym)) {
193 - var value_begin = $.trim($("[name='" + name + "']:first").val()), value_end = $.trim($("[name='" + name + "']:last").val());  
194 - if (!value_begin.length && !value_end.length) {  
195 - return true;  
196 - }  
197 - if (typeof vObjCol.process === 'function') {  
198 - value_begin = vObjCol.process(value_begin, 'begin');  
199 - value_end = vObjCol.process(value_end, 'end');  
200 - } else if ($("[name='" + name + "']:first").attr('type') === 'datetime') { //datetime类型字段转换成时间戳  
201 - var Hms = Moment(value_begin).format("HH:mm:ss");  
202 - value_begin = value_begin ? parseInt(Moment(value_begin) / 1000) : '';  
203 - value_end = value_end ? parseInt(Moment(value_end) / 1000) : '';  
204 - if (value_begin === value_end && '00:00:00' === Hms) {  
205 - value_end += 86399; 194 + var value_begin = $.trim($("[name='" + name + "']:first", that.$commonsearch).val()), value_end = $.trim($("[name='" + name + "']:last", that.$commonsearch).val());
  195 + if (value_begin.length || value_end.length) {
  196 + if (typeof vObjCol.process === 'function') {
  197 + value_begin = vObjCol.process(value_begin, 'begin');
  198 + value_end = vObjCol.process(value_end, 'end');
  199 + } else if ($("[name='" + name + "']:first", that.$commonsearch).attr('type') === 'datetime') { //datetime类型字段转换成时间戳
  200 + var Hms = Moment(value_begin).format("HH:mm:ss");
  201 + value_begin = value_begin ? parseInt(Moment(value_begin) / 1000) : '';
  202 + value_end = value_end ? parseInt(Moment(value_end) / 1000) : '';
  203 + if (value_begin === value_end && '00:00:00' === Hms) {
  204 + value_end += 86399;
  205 + }
206 } 206 }
  207 + value = value_begin + ',' + value_end;
  208 + } else {
  209 + value = '';
207 } 210 }
208 - var value = value_begin + ',' + value_end;  
209 } else { 211 } else {
210 - var value = $("[name='" + name + "']:checked").val(); 212 + value = $("[name='" + name + "']:checked", that.$commonsearch).val();
211 } 213 }
212 } else { 214 } else {
213 - var value = (typeof vObjCol.process === 'function') ? vObjCol.process(obj.val()) : (sym == 'LIKE %...%' ? obj.val().replace(/\*/g, '%') : obj.val()); 215 + value = (typeof vObjCol.process === 'function') ? vObjCol.process(obj.val()) : (sym == 'LIKE %...%' ? obj.val().replace(/\*/g, '%') : obj.val());
214 } 216 }
215 - if (value == '' && sym.indexOf("NULL") == -1) { 217 + if (removeempty && value == '' && sym.indexOf("NULL") == -1) {
216 return true; 218 return true;
217 } 219 }
218 220
@@ -222,6 +224,25 @@ @@ -222,6 +224,25 @@
222 return {op: op, filter: filter}; 224 return {op: op, filter: filter};
223 }; 225 };
224 226
  227 + var getQueryParams = function (params, searchQuery, removeempty) {
  228 + params.filter = typeof params.filter === 'Object' ? params.filter : (params.filter ? JSON.parse(params.filter) : {});
  229 + params.op = typeof params.op === 'Object' ? params.op : (params.op ? JSON.parse(params.op) : {});
  230 + params.filter = $.extend(params.filter, searchQuery.filter);
  231 + params.op = $.extend(params.op, searchQuery.op);
  232 + //移除empty的值
  233 + if (removeempty) {
  234 + $.each(params.filter, function (i, j) {
  235 + if (j === '') {
  236 + delete params.filter[i];
  237 + delete params.op[i];
  238 + }
  239 + });
  240 + }
  241 + params.filter = JSON.stringify(params.filter);
  242 + params.op = JSON.stringify(params.op);
  243 + return params;
  244 + };
  245 +
225 $.extend($.fn.bootstrapTable.defaults, { 246 $.extend($.fn.bootstrapTable.defaults, {
226 commonSearch: false, 247 commonSearch: false,
227 titleForm: "Common search", 248 titleForm: "Common search",
@@ -289,32 +310,39 @@ @@ -289,32 +310,39 @@
289 310
290 initCommonSearch(that.columns, that); 311 initCommonSearch(that.columns, that);
291 312
292 - var searchContainer = $(".commonsearch-table", that.$container);  
293 -  
294 that.$toolbar.find('button[name="commonSearch"]') 313 that.$toolbar.find('button[name="commonSearch"]')
295 .off('click').on('click', function () { 314 .off('click').on('click', function () {
296 - searchContainer.toggleClass("hidden"); 315 + that.$commonsearch.toggleClass("hidden");
297 return; 316 return;
298 }); 317 });
299 318
300 that.$container.on("click", "." + that.options.searchClass, function () { 319 that.$container.on("click", "." + that.options.searchClass, function () {
301 - var obj = $("form [name='" + $(this).data("field") + "']", searchContainer); 320 + var obj = $("form [name='" + $(this).data("field") + "']", that.$commonsearch);
302 if (obj.size() > 0) { 321 if (obj.size() > 0) {
303 obj.val($(this).data("value")); 322 obj.val($(this).data("value"));
304 - $("form", searchContainer).trigger("submit"); 323 + $("form", that.$commonsearch).trigger("submit");
305 } 324 }
306 }); 325 });
  326 + var searchQuery = getSearchQuery(that, true);
  327 + var queryParams = that.options.queryParams;
  328 + //匹配默认搜索值
  329 + this.options.queryParams = function (params) {
  330 + var params = getQueryParams(queryParams(params), searchQuery);
  331 + return params;
  332 + };
  333 + this.trigger('post-common-search', that);
307 334
  335 + };
  336 +
  337 + BootstrapTable.prototype.onCommonSearch = function () {
308 var searchQuery = getSearchQuery(this); 338 var searchQuery = getSearchQuery(this);
309 - var queryParams = this.options.queryParams; 339 + var params = getQueryParams(this.options.queryParams({}), searchQuery, true);
  340 + this.trigger('common-search', this, params, searchQuery);
  341 + this.options.pageNumber = 1;
310 this.options.queryParams = function () { 342 this.options.queryParams = function () {
311 - var params = queryParams.apply(this, arguments);  
312 - params.filter = JSON.stringify($.extend(params.filter || {}, searchQuery.filter));  
313 - params.op = JSON.stringify($.extend(params.op || {}, searchQuery.op));  
314 return params; 343 return params;
315 }; 344 };
316 - this.trigger('post-common-search', that);  
317 - 345 + this.refresh({query: params});
318 }; 346 };
319 347
320 BootstrapTable.prototype.load = function (data) { 348 BootstrapTable.prototype.load = function (data) {
@@ -351,24 +379,4 @@ @@ -351,24 +379,4 @@
351 return true; 379 return true;
352 }) : this.data; 380 }) : this.data;
353 }; 381 };
354 -  
355 - BootstrapTable.prototype.onCommonSearch = function () {  
356 - var searchquery = getSearchQuery(this);  
357 - this.trigger('common-search', this, searchquery);  
358 -  
359 - // 追加查询关键字  
360 - this.options.pageNumber = 1;  
361 - this.options.queryParams = function (params) {  
362 - return {  
363 - search: params.search,  
364 - sort: params.sort,  
365 - order: params.order,  
366 - filter: JSON.stringify(searchquery.filter),  
367 - op: JSON.stringify(searchquery.op),  
368 - offset: params.offset,  
369 - limit: params.limit,  
370 - };  
371 - };  
372 - this.refresh({query: {filter: JSON.stringify(searchquery.filter), op: JSON.stringify(searchquery.op)}});  
373 - };  
374 }(jQuery); 382 }(jQuery);
@@ -302,6 +302,7 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table @@ -302,6 +302,7 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
302 return '<img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" />'; 302 return '<img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" />';
303 }, 303 },
304 images: function (value, row, index) { 304 images: function (value, row, index) {
  305 + value = value.toString();
305 var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center'; 306 var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
306 var arr = value.split(','); 307 var arr = value.split(',');
307 var html = []; 308 var html = [];
@@ -332,12 +333,12 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table @@ -332,12 +333,12 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
332 }, 333 },
333 addtabs: function (value, row, index) { 334 addtabs: function (value, row, index) {
334 var url = Table.api.replaceurl(this.url, value, row, this.table); 335 var url = Table.api.replaceurl(this.url, value, row, this.table);
335 - var title = this.title ? this.title : __("Search %s", value); 336 + var title = this.atitle ? this.atitle : __("Search %s", value);
336 return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>'; 337 return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
337 }, 338 },
338 dialog: function (value, row, index) { 339 dialog: function (value, row, index) {
339 var url = Table.api.replaceurl(this.url, value, row, this.table); 340 var url = Table.api.replaceurl(this.url, value, row, this.table);
340 - var title = this.title ? this.title : value; 341 + var title = this.atitle ? this.atitle : __("View %s", value);
341 return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>'; 342 return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
342 }, 343 },
343 flag: function (value, row, index) { 344 flag: function (value, row, index) {
@@ -360,20 +361,7 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table @@ -360,20 +361,7 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
360 return html.join(' '); 361 return html.join(' ');
361 }, 362 },
362 label: function (value, row, index) { 363 label: function (value, row, index) {
363 - var colorArr = ['success', 'warning', 'danger', 'info'];  
364 - //如果字段列有定义custom  
365 - if (typeof this.custom !== 'undefined') {  
366 - colorArr = $.merge(colorArr, this.custom);  
367 - }  
368 - //渲染Flag  
369 - var html = [];  
370 - var arr = value.split(',');  
371 - $.each(arr, function (i, value) {  
372 - value = value.toString();  
373 - var color = colorArr[i % colorArr.length];  
374 - html.push('<span class="label label-' + color + '">' + __(value) + '</span>');  
375 - });  
376 - return html.join(' '); 364 + return Table.api.formatter.flag.call(this, value, row, index);
377 }, 365 },
378 datetime: function (value, row, index) { 366 datetime: function (value, row, index) {
379 return value ? Moment(parseInt(value) * 1000).format("YYYY-MM-DD HH:mm:ss") : __('None'); 367 return value ? Moment(parseInt(value) * 1000).format("YYYY-MM-DD HH:mm:ss") : __('None');