Admin.php 12 KB

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