Admin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * 控制器
  7. */
  8. class Admin extends AdminControl
  9. {
  10. public function initialize()
  11. {
  12. parent::initialize();
  13. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/admin.lang.php');
  14. }
  15. /**
  16. * 管理员列表
  17. */
  18. public function admin()
  19. {
  20. $admin_mod = model('admin');
  21. $condition = array();
  22. $admin_list = $admin_mod->getAdminList($condition, 10);
  23. View::assign('admin_list', $admin_list);
  24. View::assign('show_page', $admin_mod->page_info->render());
  25. $this->setAdminCurItem('admin');
  26. return View::fetch('admin');
  27. }
  28. /**
  29. * 管理员删除
  30. */
  31. public function admin_del()
  32. {
  33. $admin_id = intval(input('param.admin_id'));
  34. if (!empty($admin_id)) {
  35. if ($admin_id == 1) {
  36. $this->error(lang('ds_common_save_fail'));
  37. }
  38. $admin_mod = model('admin');
  39. $admin_mod->delAdmin(array(array('admin_id', '=', $admin_id)));
  40. $this->log(lang('ds_del') . lang('limit_admin') . '[ID:' . $admin_id . ']', 1);
  41. ds_json_encode(10000, lang('ds_common_del_succ'));
  42. } else {
  43. ds_json_encode(10001, lang('ds_common_del_fail'));
  44. }
  45. }
  46. /**
  47. * 管理员添加
  48. */
  49. public function admin_add()
  50. {
  51. $admin_model = model('admin');
  52. if (!request()->isPost()) {
  53. //得到权限组
  54. $gadmin = $admin_model->getGadminList('gname,gid');
  55. View::assign('gadmin', $gadmin);
  56. return View::fetch('admin_form');
  57. } else {
  58. $data['admin_name'] = input('post.admin_name');
  59. $data['admin_gid'] = input('post.gid');
  60. $data['admin_password'] = md5(input('post.admin_password'));
  61. if (empty(input('post.admin_password'))) {
  62. $this->error(lang('admin_add_password_null'));
  63. }
  64. $admin_validate = ds_validate('admin');
  65. if (!$admin_validate->scene('admin_add')->check($data)) {
  66. $this->error($admin_validate->getError());
  67. }
  68. //判断是否重名
  69. $admin_info = $admin_model->getOneAdmin(array(array('admin_name', '=', $data['admin_name'])));
  70. if ($admin_info) {
  71. $this->error(lang('admin_add_admin_not_exists'));
  72. }
  73. $rs = $admin_model->addAdmin($data);
  74. if ($rs) {
  75. $this->log(lang('ds_add') . lang('limit_admin') . '[' . input('post.admin_name') . ']', 1);
  76. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  77. } else {
  78. $this->error(lang('ds_common_save_fail'));
  79. }
  80. }
  81. }
  82. /**
  83. * ajax操作
  84. */
  85. public function ajax()
  86. {
  87. $admin_model = model('admin');
  88. switch (input('get.branch')) {
  89. //管理人员名称验证
  90. case 'check_admin_name':
  91. $condition[] = array('admin_name', '=', input('get.admin_name'));
  92. $admin_info = $admin_model->infoAdmin($condition);
  93. if (!empty($admin_info)) {
  94. exit('false');
  95. } else {
  96. exit('true');
  97. }
  98. break;
  99. //权限组名称验证
  100. case 'check_gadmin_name':
  101. $condition = array();
  102. if (is_numeric(input('param.gid'))) {
  103. $condition[] = array('gid', '<>', intval(input('param.gid')));
  104. }
  105. $condition[] = array('gname', '=', input('get.gname'));
  106. $info = $admin_model->getOneGadmin($condition);
  107. if (!empty($info)) {
  108. exit('false');
  109. } else {
  110. exit('true');
  111. }
  112. break;
  113. }
  114. }
  115. /**
  116. * 设置管理员权限
  117. */
  118. public function admin_edit()
  119. {
  120. $admin_id = intval(input('param.admin_id'));
  121. if (request()->isPost()) {
  122. //没有更改密码
  123. if (input('post.admin_password') != '') {
  124. $data['admin_password'] = md5(input('post.admin_password'));
  125. }
  126. $data['admin_gid'] = intval(input('post.gid'));
  127. //查询管理员信息
  128. $admin_model = model('admin');
  129. $result = $admin_model->editAdmin($data, $admin_id);
  130. if ($result) {
  131. $this->log(lang('ds_edit') . lang('limit_admin') . '[ID:' . $admin_id . ']', 1);
  132. dsLayerOpenSuccess(lang('admin_edit_success'));
  133. } else {
  134. $this->error(lang('admin_edit_fail'));
  135. }
  136. } else {
  137. //查询用户信息
  138. $admin_model = model('admin');
  139. $admin = $admin_model->getOneAdmin(array(array('admin_id', '=', $admin_id)));
  140. if (!is_array($admin) || count($admin) <= 0) {
  141. $this->error(lang('admin_edit_admin_error'), (string)url('admin/admin'));
  142. }
  143. View::assign('admin', $admin);
  144. //得到权限组
  145. $gadmin = $admin_model->getGadminList('gname,gid');
  146. View::assign('gadmin', $gadmin);
  147. return View::fetch('admin_form');
  148. }
  149. }
  150. /**
  151. * 取得所有权限项
  152. *
  153. * @return array
  154. */
  155. private function permission()
  156. {
  157. $limit = $this->limitList();
  158. if (is_array($limit)) {
  159. foreach ($limit as $k => $v) {
  160. if (is_array($v['child'])) {
  161. $tmp = array();
  162. foreach ($v['child'] as $key => $value) {
  163. $controller = (!empty($value['controller'])) ? $value['controller'] : $v['controller'];
  164. if (strpos($controller, '|') == false) { //controller参数不带|
  165. $limit[$k]['child'][$key]['action'] = rtrim($controller . '.' . str_replace('|', '|' . $controller . '.', $value['action']), '.');
  166. } else { //controller参数带|
  167. $tmp_str = '';
  168. if (empty($value['action'])) {
  169. $limit[$k]['child'][$key]['action'] = $controller;
  170. } elseif (strpos($value['action'], '|') == false) { //action参数不带|
  171. foreach (explode('|', $controller) as $v1) {
  172. $tmp_str .= "$v1.{$value['action']}|";
  173. }
  174. $limit[$k]['child'][$key]['action'] = rtrim($tmp_str, '|');
  175. } elseif (strpos($value['action'], '|') != false && strpos($controller, '|') != false) { //action,controller都带|,交差权限
  176. foreach (explode('|', $controller) as $v1) {
  177. foreach (explode('|', $value['action']) as $v2) {
  178. $tmp_str .= "$v1.$v2|";
  179. }
  180. }
  181. $limit[$k]['child'][$key]['action'] = rtrim($tmp_str, '|');
  182. }
  183. }
  184. }
  185. }
  186. }
  187. return $limit;
  188. } else {
  189. return array();
  190. }
  191. }
  192. /**
  193. * 权限组
  194. */
  195. public function gadmin()
  196. {
  197. $admin_model = model('admin');
  198. $gadmin_list = $admin_model->getGadminList();
  199. View::assign('gadmin_list', $gadmin_list);
  200. $this->setAdminCurItem('gadmin');
  201. return View::fetch('gadmin');
  202. }
  203. /**
  204. * 添加权限组
  205. */
  206. public function gadmin_add()
  207. {
  208. if (!request()->isPost()) {
  209. View::assign('limit', $this->permission());
  210. return View::fetch('gadmin_add');
  211. } else {
  212. $limit_str = '';
  213. $permission_array = input('post.permission/a');
  214. if (is_array($permission_array)) {
  215. $limit_str = implode('|', $permission_array);
  216. }
  217. $data['glimits'] = ds_encrypt($limit_str, MD5_KEY . md5(input('post.gname')));
  218. $data['gname'] = input('post.gname');
  219. $admin_model = model('admin');
  220. if ($admin_model->addGadmin($data)) {
  221. $this->log(lang('ds_add') . lang('limit_gadmin') . '[' . input('post.gname') . ']', 1);
  222. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  223. } else {
  224. $this->error(lang('ds_common_save_fail'));
  225. }
  226. }
  227. }
  228. /**
  229. * 设置权限组权限
  230. */
  231. public function gadmin_set()
  232. {
  233. $gid = intval(input('param.gid'));
  234. $admin_model = model('admin');
  235. $ginfo = $admin_model->getOneGadmin(array(array('gid', '=', $gid)));
  236. if (empty($ginfo)) {
  237. $this->error(lang('admin_set_admin_not_exists'));
  238. }
  239. if (!request()->isPost()) {
  240. //解析已有权限
  241. $hlimit = ds_decrypt($ginfo['glimits'], MD5_KEY . md5($ginfo['gname']));
  242. $ginfo['glimits'] = explode('|', $hlimit);
  243. View::assign('ginfo', $ginfo);
  244. View::assign('limit', $this->permission());
  245. return View::fetch('gadmin_set');
  246. } else {
  247. $limit_str = '';
  248. $permission_array = input('post.permission/a');
  249. if (is_array($permission_array)) {
  250. $limit_str = implode('|', $permission_array);
  251. }
  252. $limit_str = ds_encrypt($limit_str, MD5_KEY . md5(input('post.gname')));
  253. $data['glimits'] = $limit_str;
  254. $data['gname'] = input('post.gname');
  255. $update = $admin_model->editGadmin(array(array('gid', '=', $gid)), $data);
  256. if ($update) {
  257. $this->log(lang('ds_edit') . lang('limit_gadmin') . '[' . input('post.gname') . ']', 1);
  258. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  259. } else {
  260. $this->error(lang('ds_common_save_succ'));
  261. }
  262. }
  263. }
  264. /**
  265. * 组删除
  266. */
  267. public function gadmin_del()
  268. {
  269. if (is_numeric(input('param.gid'))) {
  270. $admin_model = model('admin');
  271. $admin_model->delGadmin(array(array('gid', '=', intval(input('param.gid')))));
  272. $this->log(lang('ds_del') . lang('limit_gadmin') . '[ID' . intval(input('param.gid')) . ']', 1);
  273. ds_json_encode(10000, lang('ds_common_op_succ'));
  274. } else {
  275. ds_json_encode(10000, lang('ds_common_op_fail'));
  276. }
  277. }
  278. /**
  279. * 获取卖家栏目列表,针对控制器下的栏目
  280. */
  281. protected function getAdminItemList()
  282. {
  283. $menu_array = array(
  284. array(
  285. 'name' => 'admin',
  286. 'text' => lang('limit_admin'),
  287. 'url' => (string)url('admin/admin')
  288. ),
  289. array(
  290. 'name' => 'admin_add',
  291. 'text' => lang('admin_add_limit_admin'),
  292. 'url' => "javascript:dsLayerOpen('" . (string)url('admin/admin_add') . "','" . lang('admin_add_limit_admin') . "')"
  293. ),
  294. array(
  295. 'name' => 'gadmin',
  296. 'text' => lang('limit_gadmin'),
  297. 'url' => (string)url('admin/gadmin')
  298. ),
  299. array(
  300. 'name' => 'gadmin_add',
  301. 'text' => lang('admin_add_limit_gadmin'),
  302. 'url' => "javascript:dsLayerOpen('" . (string)url('admin/gadmin_add') . "','" . lang('admin_add_limit_gadmin') . "')"
  303. ),
  304. );
  305. return $menu_array;
  306. }
  307. }