Admin.php 12 KB

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