AgentController.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace Agent\Controller;
  3. use Think\Controller;
  4. class AgentController extends Controller
  5. {
  6. ////OK
  7. public function __construct(){
  8. parent::__construct();
  9. if (!session('agent_id')) {
  10. $this->redirect('Agent/Login/index');
  11. }
  12. define('AID', session('agent_id'));
  13. $access = $this->accessControl();
  14. if ($access === false) {
  15. $this->error('403:禁止访问');
  16. } else if ($access === null) {
  17. $dynamic = $this->checkDynamic();
  18. if ($dynamic === null) {
  19. $rule = strtolower(MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME);
  20. if (!$this->checkRule($rule, array('in', '1,2'))) {
  21. $this->error('未授权访问!');
  22. }
  23. } else if ($dynamic === false) {
  24. $this->error('未授权访问!');
  25. }
  26. }
  27. }
  28. ////OK
  29. public function index()
  30. {
  31. $this->redirect('Agent/Index/index');
  32. }
  33. ////OK
  34. final protected function checkRule($rule, $type = \Common\Model\AuthRuleModel::RULE_URL, $mode = 'url')
  35. {
  36. if (IS_ROOT) {
  37. return true;
  38. }
  39. static $Auth;
  40. if (!$Auth) {
  41. $Auth = new \Think\Auth();
  42. }
  43. if (!$Auth->check($rule, UID, $type, $mode)) {
  44. return false;
  45. }
  46. return true;
  47. }
  48. ////OK
  49. function addtime($time = NULL, $type = NULL){
  50. if (empty($time)) {
  51. return '---';
  52. }
  53. if (($time < 2545545) && (1893430861 < $time)) {
  54. return '---';
  55. }
  56. if (empty($type)) {
  57. $type = 'Y-m-d H:i:s';
  58. }
  59. return date($type, $time);
  60. }
  61. final protected function editRow($model, $data, $where, $msg)
  62. {
  63. $id = array_unique((array)I('id', 0));
  64. $id = (is_array($id) ? implode(',', $id) : $id);
  65. $where = array_merge(array(
  66. 'id' => array('in', $id)
  67. ), (array)$where);
  68. $msg = array_merge(array('success' => '操作成功!', 'error' => '操作失败!', 'url' => '', 'ajax' => IS_AJAX), (array)$msg);
  69. if (M($model)->where($where)->save($data) !== false) {
  70. $this->success($msg['success'], $msg['url'], $msg['ajax']);
  71. } else {
  72. $this->error($msg['error'], $msg['url'], $msg['ajax']);
  73. }
  74. }
  75. protected function forbid($model, $where = array(), $msg = array('success' => '状态禁用成功!', 'error' => '状态禁用失败!'))
  76. {
  77. $data = array('status' => 0);
  78. $this->editRow($model, $data, $where, $msg);
  79. }
  80. protected function resume($model, $where = array(), $msg = array('success' => '状态恢复成功!', 'error' => '状态恢复失败!'))
  81. {
  82. $data = array('status' => 1);
  83. $this->editRow($model, $data, $where, $msg);
  84. }
  85. protected function restore($model, $where = array(), $msg = array('success' => '状态还原成功!', 'error' => '状态还原失败!'))
  86. {
  87. $data = array('status' => 1);
  88. $where = array_merge(array('status' => -1), $where);
  89. $this->editRow($model, $data, $where, $msg);
  90. }
  91. protected function delete($model, $where = array(), $msg = array('success' => '删除成功!', 'error' => '删除失败!'))
  92. {
  93. $data['status'] = -1;
  94. $data['update_time'] = NOW_TIME;
  95. $this->editRow($model, $data, $where, $msg);
  96. }
  97. public function setStatus($Model = CONTROLLER_NAME)
  98. {
  99. $ids = I('request.ids');
  100. $status = I('request.status');
  101. if (empty($ids)) {
  102. $this->error('请选择要操作的数据');
  103. }
  104. $map['id'] = array('in', $ids);
  105. switch ($status) {
  106. case -1:
  107. $this->delete($Model, $map, array('success' => '删除成功', 'error' => '删除失败'));
  108. break;
  109. case 0:
  110. $this->forbid($Model, $map, array('success' => '禁用成功', 'error' => '禁用失败'));
  111. break;
  112. case 1:
  113. $this->resume($Model, $map, array('success' => '启用成功', 'error' => '启用失败'));
  114. break;
  115. default:
  116. $this->error('参数错误');
  117. break;
  118. }
  119. }
  120. protected function checkDynamic()
  121. {
  122. if (IS_ROOT) {
  123. return true;
  124. }
  125. return null;
  126. }
  127. ////OK
  128. final protected function accessControl()
  129. {
  130. if (IS_ROOT) {
  131. return true;
  132. }
  133. $allow = C('ALLOW_VISIT');
  134. $deny = C('DENY_VISIT');
  135. $check = strtolower(CONTROLLER_NAME . '/' . ACTION_NAME);
  136. if (!empty($deny) && in_array_case($check, $deny)) {
  137. return false;
  138. }
  139. if (!empty($allow) && in_array_case($check, $allow)) {
  140. return true;
  141. }
  142. return null;
  143. }
  144. final public function getMenus($controller = CONTROLLER_NAME)
  145. {
  146. if (empty($menus)) {
  147. $where['pid'] = 0;
  148. $where['hide'] = 0;
  149. if (!C('DEVELOP_MODE')) {
  150. $where['is_dev'] = 0;
  151. }
  152. $menus['main'] = M('Menu')->where($where)->order('sort asc')->select();
  153. $menus['child'] = array();
  154. $current = M('Menu')->where('url like \'' . $controller . '/' . ACTION_NAME . '%\'')->field('id')->find();
  155. if (!$current) {
  156. $current = M('Menu')->where('url like \'' . $controller . '/%\'')->field('id')->find();
  157. }
  158. if ($current) {
  159. $nav = D('Menu')->getPath($current['id']);
  160. $nav_first_title = $nav[0]['title'];
  161. foreach ($menus['main'] as $key => $item) {
  162. if (!is_array($item) || empty($item['title']) || empty($item['url'])) {
  163. $this->error('控制器基类$menus属性元素配置有误');
  164. }
  165. if (stripos($item['url'], MODULE_NAME) !== 0) {
  166. $item['url'] = MODULE_NAME . '/' . $item['url'];
  167. }
  168. if (!IS_ROOT && !$this->checkRule($item['url'], \Common\Model\AuthRuleModel::RULE_MAIN, null)) {
  169. unset($menus['main'][$key]);
  170. continue;
  171. }
  172. if ($item['title'] == $nav_first_title) {
  173. $menus['main'][$key]['class'] = 'current';
  174. $groups = M('Menu')->where('pid = ' . $item['id'])->distinct(true)->field('`group`')->select();
  175. if ($groups) {
  176. $groups = array_column($groups, 'group');
  177. } else {
  178. $groups = array();
  179. }
  180. $where = array();
  181. $where['pid'] = $item['id'];
  182. $where['hide'] = 0;
  183. if (!C('DEVELOP_MODE')) {
  184. $where['is_dev'] = 0;
  185. }
  186. $second_urls = M('Menu')->where($where)->getField('id,url');
  187. if (!IS_ROOT) {
  188. $to_check_urls = array();
  189. foreach ($second_urls as $key => $to_check_url) {
  190. if (stripos($to_check_url, MODULE_NAME) !== 0) {
  191. $rule = MODULE_NAME . '/' . $to_check_url;
  192. } else {
  193. $rule = $to_check_url;
  194. }
  195. if ($this->checkRule($rule, \Common\Model\AuthRuleModel::RULE_URL, null)) {
  196. $to_check_urls[] = $to_check_url;
  197. }
  198. }
  199. }
  200. foreach ($groups as $g) {
  201. $map = array('group' => $g);
  202. if (isset($to_check_urls)) {
  203. if (empty($to_check_urls)) {
  204. continue;
  205. } else {
  206. $map['url'] = array('in', $to_check_urls);
  207. }
  208. }
  209. $map['pid'] = $item['id'];
  210. $map['hide'] = 0;
  211. if (!C('DEVELOP_MODE')) {
  212. $map['is_dev'] = 0;
  213. }
  214. $menuList = M('Menu')->where($map)->field('id,pid,title,url,tip,ico_name')->order('sort asc')->select();
  215. $menus['child'][$g] = list_to_tree($menuList, 'id', 'pid', 'operater', $item['id']);
  216. }
  217. if ($menus['child'] === array()) {}
  218. }
  219. }
  220. }
  221. }
  222. return $menus;
  223. }
  224. protected function lists($model, $where = array(), $order = '', $base = array('status' => array('egt', 0)), $field = true)
  225. {
  226. $options = array();
  227. $REQUEST = (array)I('request.');
  228. if (is_string($model)) {
  229. $model = M($model);
  230. }
  231. $OPT = new \ReflectionProperty($model, 'options');
  232. $OPT->setAccessible(true);
  233. $pk = $model->getPk();
  234. if ($order === null) {
  235. } else if (isset($REQUEST['_order']) && isset($REQUEST['_field']) && in_array(strtolower($REQUEST['_order']), array('desc', 'asc'))) {
  236. $options['order'] = '`' . $REQUEST['_field'] . '` ' . $REQUEST['_order'];
  237. } else if (($order === '') && empty($options['order']) && !empty($pk)) {
  238. $options['order'] = $pk . ' desc';
  239. } else if ($order) {
  240. $options['order'] = $order;
  241. }
  242. unset($REQUEST['_order']);
  243. unset($REQUEST['_field']);
  244. $options['where'] = array_filter(array_merge((array)$base, (array)$where), function ($val) {
  245. if (($val === '') || ($val === null)) {
  246. return false;
  247. } else {
  248. return true;
  249. }
  250. });
  251. if (empty($options['where'])) {
  252. unset($options['where']);
  253. }
  254. $options = array_merge((array)$OPT->getValue($model), $options);
  255. $total = $model->where($options['where'])->count();
  256. if (isset($REQUEST['r'])) {
  257. $listRows = (int)$REQUEST['r'];
  258. } else {
  259. $listRows = (0 < C('LIST_ROWS') ? C('LIST_ROWS') : 10);
  260. }
  261. $page = new \Think\Page($total, $listRows, $REQUEST);
  262. if ($listRows < $total) {
  263. $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
  264. }
  265. $p = $page->show();
  266. $this->assign('_page', $p ? $p : '');
  267. $this->assign('_total', $total);
  268. $options['limit'] = $page->firstRow . ',' . $page->listRows;
  269. $model->setProperty('options', $options);
  270. return $model->field($field)->select();
  271. }
  272. }
  273. ?>