AdminController.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. namespace Admin\Controller;
  3. /**
  4. * 管理员
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2016-12-01T21:51:08+0800
  9. */
  10. class AdminController extends CommonController
  11. {
  12. /**
  13. * [_initialize 前置操作-继承公共前置方法]
  14. * @author Devil
  15. * @blog http://gong.gg/
  16. * @version 0.0.1
  17. * @datetime 2016-12-03T12:39:08+0800
  18. */
  19. public function _initialize()
  20. {
  21. // 调用父类前置方法
  22. parent::_initialize();
  23. }
  24. /**
  25. * [Index 管理员列表]
  26. * @author Devil
  27. * @blog http://gong.gg/
  28. * @version 0.0.1
  29. * @datetime 2016-12-06T21:31:53+0800
  30. */
  31. public function Index()
  32. {
  33. // 登录校验
  34. $this->Is_Login();
  35. // 权限校验
  36. $this->Is_Power();
  37. // 参数
  38. $param = array_merge($_POST, $_GET);
  39. // 模型对象
  40. $m = M('Admin');
  41. // 条件
  42. $where = $this->GetIndexWhere();
  43. // 分页
  44. $number = MyC('admin_page_number');
  45. $page_param = array(
  46. 'number' => $number,
  47. 'total' => $m->where($where)->count(),
  48. 'where' => $param,
  49. 'url' => U('Admin/Admin/Index'),
  50. );
  51. $page = new \My\Page($page_param);
  52. // 获取管理员列表
  53. $list = $m->field(array('id', 'username', 'mobile', 'gender', 'login_total', 'login_time', 'add_time'))->where($where)->limit($page->GetPageStarNumber(), $number)->select();
  54. $role = M('Role')->field(array('id', 'name'))->where(array('is_enable'=>1))->select();
  55. $this->assign('role', $role);
  56. $this->assign('param', $param);
  57. $this->assign('page_html', $page->GetPageHtml());
  58. $this->assign('list', $list);
  59. $this->display('Index');
  60. }
  61. /**
  62. * [GetIndexWhere 管理员列表条件]
  63. * @author Devil
  64. * @blog http://gong.gg/
  65. * @version 0.0.1
  66. * @datetime 2016-12-10T22:16:29+0800
  67. */
  68. private function GetIndexWhere()
  69. {
  70. $where = array();
  71. if(!empty($_REQUEST['username']))
  72. {
  73. $where['username'] = array('like', '%'.I('username').'%');
  74. }
  75. $role_id = empty($_REQUEST['role_id']) ? 0 : intval(I('role_id'));
  76. if($role_id > 0)
  77. {
  78. $where['role_id'] = $role_id;
  79. }
  80. return $where;
  81. }
  82. /**
  83. * [SaveInfo 管理员添加/编辑页面]
  84. * @author Devil
  85. * @blog http://gong.gg/
  86. * @version 0.0.1
  87. * @datetime 2016-12-06T21:31:53+0800
  88. */
  89. public function SaveInfo()
  90. {
  91. // 登录校验
  92. $this->Is_Login();
  93. // 不是操作自己的情况下
  94. if(I('id') != $this->admin['id'])
  95. {
  96. // 权限校验
  97. $this->Is_Power();
  98. }
  99. // 用户编辑
  100. $id = I('id');
  101. if($id > 0)
  102. {
  103. $user = M('Admin')->where(array('id'=>$id))->field(array('id', 'username', 'mobile', 'gender', 'role_id'))->find();
  104. if(empty($user))
  105. {
  106. $this->error(L('login_username_no_exist'), U('Admin/Index/Index'));
  107. }
  108. $this->assign('data', $user);
  109. }
  110. $role = M('Role')->field(array('id', 'name'))->where(array('is_enable'=>1, 'id'=>array('gt', 1)))->select();
  111. $this->assign('role', $role);
  112. $this->assign('id', $id);
  113. $this->assign('common_gender_list', L('common_gender_list'));
  114. $this->display('SaveInfo');
  115. }
  116. /**
  117. * [Save 管理员添加/编辑]
  118. * @author Devil
  119. * @blog http://gong.gg/
  120. * @version 0.0.1
  121. * @datetime 2016-12-07T21:58:19+0800
  122. */
  123. public function Save()
  124. {
  125. // 登录校验
  126. $this->Is_Login();
  127. if(!IS_AJAX)
  128. {
  129. $this->error(L('common_unauthorized_access'));
  130. }
  131. // 不是操作自己的情况下
  132. if(I('id') != $this->admin['id'])
  133. {
  134. // 权限校验
  135. $this->Is_Power();
  136. }
  137. // id为空则表示是新增
  138. if(empty($_POST['id']))
  139. {
  140. $this->AdminAdd();
  141. } else {
  142. $this->AdminEdit();
  143. }
  144. }
  145. /**
  146. * [AdminAdd 管理员添加]
  147. * @author Devil
  148. * @blog http://gong.gg/
  149. * @version 0.0.1
  150. * @datetime 2016-12-24T22:44:28+0800
  151. */
  152. private function AdminAdd()
  153. {
  154. $m = D('Admin');
  155. if($m->create($_POST, 1))
  156. {
  157. // 额外数据处理
  158. $m->login_salt = GetNumberCode(6);
  159. $m->login_pwd = LoginPwdEncryption($m->login_pwd, $m->login_salt);
  160. $m->add_time = time();
  161. // 写入数据库
  162. if($m->add())
  163. {
  164. $this->ajaxReturn(L('common_operation_add_success'));
  165. } else {
  166. $this->ajaxReturn(L('common_operation_add_error'), -100);
  167. }
  168. } else {
  169. $this->ajaxReturn($m->getError(), -1);
  170. }
  171. }
  172. /**
  173. * [AdminEdit 管理员编辑]
  174. * @author Devil
  175. * @blog http://gong.gg/
  176. * @version 0.0.1
  177. * @datetime 2016-12-24T22:46:03+0800
  178. */
  179. private function AdminEdit()
  180. {
  181. $m = D('Admin');
  182. if($m->create($_POST, 2))
  183. {
  184. // 不能修改自身所属角色组
  185. if(I('id') == $this->admin['id'])
  186. {
  187. unset($m->role_id);
  188. }
  189. // 有密码,则更新密码
  190. if(!empty($_POST['login_pwd']))
  191. {
  192. $m->login_salt = GetNumberCode(6);
  193. $m->login_pwd = LoginPwdEncryption($m->login_pwd, $m->login_salt);
  194. } else {
  195. unset($m->login_pwd);
  196. }
  197. // 移除username,不允许更新用户名
  198. unset($m->username);
  199. // 更新数据库
  200. if($m->where(array('id'=>I('id')))->save())
  201. {
  202. // 编辑自身则退出重新登录
  203. if(!empty($_POST['login_pwd']) && I('id') == $this->admin['id'])
  204. {
  205. session_destroy();
  206. }
  207. $this->ajaxReturn(L('common_operation_edit_success'));
  208. } else {
  209. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  210. }
  211. } else {
  212. $this->ajaxReturn($m->getError(), -1);
  213. }
  214. }
  215. /**
  216. * [Delete 管理员删除]
  217. * @author Devil
  218. * @blog http://gong.gg/
  219. * @version 0.0.1
  220. * @datetime 2016-12-09T21:13:47+0800
  221. */
  222. public function Delete()
  223. {
  224. // 登录校验
  225. $this->Is_Login();
  226. // 权限校验
  227. $this->Is_Power();
  228. if(!IS_AJAX)
  229. {
  230. $this->error(L('common_unauthorized_access'));
  231. }
  232. $m = D('Admin');
  233. if($m->create($_POST, 5))
  234. {
  235. if($m->delete($id))
  236. {
  237. $this->ajaxReturn(L('common_operation_delete_success'));
  238. } else {
  239. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  240. }
  241. } else {
  242. $this->ajaxReturn($m->getError(), -1);
  243. }
  244. }
  245. /**
  246. * [LoginInfo 登录页面]
  247. * @author Devil
  248. * @blog http://gong.gg/
  249. * @version 0.0.1
  250. * @datetime 2016-12-03T12:55:53+0800
  251. */
  252. public function LoginInfo()
  253. {
  254. // 是否已登录
  255. if(!empty($_SESSION['admin']))
  256. {
  257. redirect(U('Admin/Index/Index'));
  258. }
  259. $this->display('LoginInfo');
  260. }
  261. /**
  262. * [Login 管理员登录]
  263. * @author Devil
  264. * @blog http://gong.gg/
  265. * @version 0.0.1
  266. * @datetime 2016-12-03T21:46:49+0800
  267. */
  268. public function Login()
  269. {
  270. // 是否ajax请求
  271. if(!IS_AJAX)
  272. {
  273. $this->error(L('common_unauthorized_access'));
  274. }
  275. // 登录业务处理
  276. $m = D('Admin');
  277. if($m->create($_POST, 4))
  278. {
  279. // 获取管理员
  280. $user = $m->field(array('id', 'username', 'login_pwd', 'login_salt', 'mobile', 'login_total', 'role_id'))->where(array('username'=>I('username')))->find();
  281. if(empty($user))
  282. {
  283. $this->ajaxReturn(L('login_username_no_exist'), -2);
  284. }
  285. // 密码校验
  286. $login_pwd = LoginPwdEncryption(I('login_pwd'), $user['login_salt']);
  287. if($login_pwd != $user['login_pwd'])
  288. {
  289. $this->ajaxReturn(L('login_login_pwd_error'), -3);
  290. }
  291. // 校验成功
  292. // session存储
  293. unset($user['login_pwd'], $user['login_salt']);
  294. $_SESSION['admin'] = $user;
  295. // 返回数据,更新数据库
  296. if(!empty($_SESSION['admin']))
  297. {
  298. $login_salt = GetNumberCode(6);
  299. $data = array(
  300. 'login_salt' => $login_salt,
  301. 'login_pwd' => LoginPwdEncryption(I('login_pwd'), $login_salt),
  302. 'login_total' => $user['login_total']+1,
  303. 'login_time' => time(),
  304. );
  305. if($m->where(array('id'=>$user['id']))->save($data))
  306. {
  307. // 清空缓存目录下的数据
  308. EmptyDir(C('DATA_CACHE_PATH'));
  309. $this->ajaxReturn(L('login_login_success'));
  310. }
  311. }
  312. // 失败
  313. unset($_SESSION['admin']);
  314. $this->ajaxReturn(L('login_login_error'), -100);
  315. } else {
  316. // 自动验证失败
  317. $this->ajaxReturn($m->getError(), -1);
  318. }
  319. }
  320. /**
  321. * [Logout 退出]
  322. * @author Devil
  323. * @blog http://gong.gg/
  324. * @version 0.0.1
  325. * @datetime 2016-12-05T14:31:23+0800
  326. */
  327. public function Logout()
  328. {
  329. session_destroy();
  330. redirect(U('Admin/Admin/LoginInfo'));
  331. }
  332. }
  333. ?>