AdminController.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. namespace Admin\Controller;
  3. use Think\Controller;
  4. class AdminController extends Controller
  5. {
  6. ////OK
  7. protected function _initialize(){
  8. }
  9. ////OK
  10. public function __construct(){
  11. parent::__construct();
  12. defined('APP_DEMO') || define('APP_DEMO', 0);
  13. if (!session('admin_id')) {
  14. $this->redirect('Admin/Login/index');
  15. }
  16. define('UID', session('admin_id'));
  17. $access = $this->accessControl();
  18. if ($access === false) {
  19. $this->error('403:禁止访问');
  20. } else if ($access === null) {
  21. $dynamic = $this->checkDynamic();
  22. if ($dynamic === null) {
  23. $rule = strtolower(MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME);
  24. if (!$this->checkRule($rule, array('in', '1,2'))) {
  25. $this->error('未授权访问!');
  26. }
  27. } else if ($dynamic === false) {
  28. $this->error('未授权访问!');
  29. }
  30. }
  31. $this->assign('__LANG__', array('en-us'=>'English','zh-cn'=>'简体中文'));
  32. $this->assign('__MENU__', $this->getMenus());
  33. }
  34. ////OK
  35. public function index()
  36. {
  37. $this->redirect('Admin/Index/index');
  38. }
  39. ////OK
  40. final protected function checkRule($rule, $type = \Common\Model\AuthRuleModel::RULE_URL, $mode = 'url')
  41. {
  42. if (IS_ROOT) {
  43. return true;
  44. }
  45. static $Auth;
  46. if (!$Auth) {
  47. $Auth = new \Think\Auth();
  48. }
  49. if (!$Auth->check($rule, UID, $type, $mode)) {
  50. return false;
  51. }
  52. return true;
  53. }
  54. ////OK
  55. function addtime($time = NULL, $type = NULL){
  56. if (empty($time)) {
  57. return '---';
  58. }
  59. if (($time < 2545545) && (1893430861 < $time)) {
  60. return '---';
  61. }
  62. if (empty($type)) {
  63. $type = 'Y-m-d H:i:s';
  64. }
  65. return date($type, $time);
  66. }
  67. final protected function editRow($model, $data, $where, $msg)
  68. {
  69. $id = array_unique((array)I('id', 0));
  70. $id = (is_array($id) ? implode(',', $id) : $id);
  71. $where = array_merge(array(
  72. 'id' => array('in', $id)
  73. ), (array)$where);
  74. $msg = array_merge(array('success' => '操作成功!', 'error' => '操作失败!', 'url' => '', 'ajax' => IS_AJAX), (array)$msg);
  75. if (M($model)->where($where)->save($data) !== false) {
  76. $this->success($msg['success'], $msg['url'], $msg['ajax']);
  77. } else {
  78. $this->error($msg['error'], $msg['url'], $msg['ajax']);
  79. }
  80. }
  81. protected function forbid($model, $where = array(), $msg = array('success' => '状态禁用成功!', 'error' => '状态禁用失败!'))
  82. {
  83. $data = array('status' => 0);
  84. $this->editRow($model, $data, $where, $msg);
  85. }
  86. protected function resume($model, $where = array(), $msg = array('success' => '状态恢复成功!', 'error' => '状态恢复失败!'))
  87. {
  88. $data = array('status' => 1);
  89. $this->editRow($model, $data, $where, $msg);
  90. }
  91. protected function restore($model, $where = array(), $msg = array('success' => '状态还原成功!', 'error' => '状态还原失败!'))
  92. {
  93. $data = array('status' => 1);
  94. $where = array_merge(array('status' => -1), $where);
  95. $this->editRow($model, $data, $where, $msg);
  96. }
  97. protected function delete($model, $where = array(), $msg = array('success' => '删除成功!', 'error' => '删除失败!'))
  98. {
  99. $data['status'] = -1;
  100. $data['update_time'] = NOW_TIME;
  101. $this->editRow($model, $data, $where, $msg);
  102. }
  103. public function setStatus($Model = CONTROLLER_NAME)
  104. {
  105. $ids = I('request.ids');
  106. $status = I('request.status');
  107. if (empty($ids)) {
  108. $this->error('请选择要操作的数据');
  109. }
  110. $map['id'] = array('in', $ids);
  111. switch ($status) {
  112. case -1:
  113. $this->delete($Model, $map, array('success' => '删除成功', 'error' => '删除失败'));
  114. break;
  115. case 0:
  116. $this->forbid($Model, $map, array('success' => '禁用成功', 'error' => '禁用失败'));
  117. break;
  118. case 1:
  119. $this->resume($Model, $map, array('success' => '启用成功', 'error' => '启用失败'));
  120. break;
  121. default:
  122. $this->error('参数错误');
  123. break;
  124. }
  125. }
  126. protected function checkDynamic()
  127. {
  128. if (IS_ROOT) {
  129. return true;
  130. }
  131. return null;
  132. }
  133. ////OK
  134. final protected function accessControl()
  135. {
  136. if (IS_ROOT) {
  137. return true;
  138. }
  139. $allow = C('ALLOW_VISIT');
  140. $deny = C('DENY_VISIT');
  141. $check = strtolower(CONTROLLER_NAME . '/' . ACTION_NAME);
  142. if (!empty($deny) && in_array_case($check, $deny)) {
  143. return false;
  144. }
  145. if (!empty($allow) && in_array_case($check, $allow)) {
  146. return true;
  147. }
  148. return null;
  149. }
  150. final public function getMenus($controller = CONTROLLER_NAME)
  151. {
  152. if (empty($menus)) {
  153. $where['pid'] = 0;
  154. $where['hide'] = 0;
  155. if (!C('DEVELOP_MODE')) {
  156. $where['is_dev'] = 0;
  157. }
  158. $menus['main'] = M('Menu')->where($where)->order('sort asc')->select();
  159. $menus['child'] = array();
  160. $current = M('Menu')->where('url like \'' . $controller . '/' . ACTION_NAME . '%\'')->field('id')->find();
  161. if (!$current) {
  162. $current = M('Menu')->where('url like \'' . $controller . '/%\'')->field('id')->find();
  163. }
  164. if ($current) {
  165. $nav = D('Menu')->getPath($current['id']);
  166. $nav_first_title = $nav[0]['title'];
  167. foreach ($menus['main'] as $key => $item) {
  168. if (!is_array($item) || empty($item['title']) || empty($item['url'])) {
  169. $this->error('控制器基类$menus属性元素配置有误');
  170. }
  171. if (stripos($item['url'], MODULE_NAME) !== 0) {
  172. $item['url'] = MODULE_NAME . '/' . $item['url'];
  173. }
  174. if (!IS_ROOT && !$this->checkRule($item['url'], \Common\Model\AuthRuleModel::RULE_MAIN, null)) {
  175. unset($menus['main'][$key]);
  176. continue;
  177. }
  178. if ($item['title'] == $nav_first_title) {
  179. $menus['main'][$key]['class'] = 'current';
  180. $groups = M('Menu')->where('pid = ' . $item['id'])->distinct(true)->field('`group`')->select();
  181. if ($groups) {
  182. $groups = array_column($groups, 'group');
  183. } else {
  184. $groups = array();
  185. }
  186. $where = array();
  187. $where['pid'] = $item['id'];
  188. $where['hide'] = 0;
  189. if (!C('DEVELOP_MODE')) {
  190. $where['is_dev'] = 0;
  191. }
  192. $second_urls = M('Menu')->where($where)->getField('id,url');
  193. if (!IS_ROOT) {
  194. $to_check_urls = array();
  195. foreach ($second_urls as $key => $to_check_url) {
  196. if (stripos($to_check_url, MODULE_NAME) !== 0) {
  197. $rule = MODULE_NAME . '/' . $to_check_url;
  198. } else {
  199. $rule = $to_check_url;
  200. }
  201. if ($this->checkRule($rule, \Common\Model\AuthRuleModel::RULE_URL, null)) {
  202. $to_check_urls[] = $to_check_url;
  203. }
  204. }
  205. }
  206. foreach ($groups as $g) {
  207. $map = array('group' => $g);
  208. if (isset($to_check_urls)) {
  209. if (empty($to_check_urls)) {
  210. continue;
  211. } else {
  212. $map['url'] = array('in', $to_check_urls);
  213. }
  214. }
  215. $map['pid'] = $item['id'];
  216. $map['hide'] = 0;
  217. if (!C('DEVELOP_MODE')) {
  218. $map['is_dev'] = 0;
  219. }
  220. $menuList = M('Menu')->where($map)->field('id,pid,title,url,tip,ico_name')->order('sort asc')->select();
  221. $menus['child'][$g] = list_to_tree($menuList, 'id', 'pid', 'operater', $item['id']);
  222. }
  223. if ($menus['child'] === array()) {}
  224. }
  225. }
  226. }
  227. }
  228. return $menus;
  229. }
  230. final protected function returnNodes($tree = true)
  231. {
  232. static $tree_nodes = array();
  233. if ($tree && !empty($tree_nodes[(int)$tree])) {
  234. return $tree_nodes[$tree];
  235. }
  236. if ((int)$tree) {
  237. $list = M('Menu')->field('id,pid,title,url,tip,hide')->order('sort asc')->select();
  238. foreach ($list as $key => $value) {
  239. if (stripos($value['url'], MODULE_NAME) !== 0) {
  240. $list[$key]['url'] = MODULE_NAME . '/' . $value['url'];
  241. }
  242. }
  243. $nodes = list_to_tree($list, $pk = 'id', $pid = 'pid', $child = 'operator', $root = 0);
  244. foreach ($nodes as $key => $value) {
  245. if (!empty($value['operator'])) {
  246. $nodes[$key]['child'] = $value['operator'];
  247. unset($nodes[$key]['operator']);
  248. }
  249. }
  250. } else {
  251. $nodes = M('Menu')->field('title,url,tip,pid')->order('sort asc')->select();
  252. foreach ($nodes as $key => $value) {
  253. if (stripos($value['url'], MODULE_NAME) !== 0) {
  254. $nodes[$key]['url'] = MODULE_NAME . '/' . $value['url'];
  255. }
  256. }
  257. }
  258. $tree_nodes[(int)$tree] = $nodes;
  259. return $nodes;
  260. }
  261. protected function lists($model, $where = array(), $order = '', $base = array('status' => array('egt', 0)), $field = true)
  262. {
  263. $options = array();
  264. $REQUEST = (array)I('request.');
  265. if (is_string($model)) {
  266. $model = M($model);
  267. }
  268. $OPT = new \ReflectionProperty($model, 'options');
  269. $OPT->setAccessible(true);
  270. $pk = $model->getPk();
  271. if ($order === null) {
  272. } else if (isset($REQUEST['_order']) && isset($REQUEST['_field']) && in_array(strtolower($REQUEST['_order']), array('desc', 'asc'))) {
  273. $options['order'] = '`' . $REQUEST['_field'] . '` ' . $REQUEST['_order'];
  274. } else if (($order === '') && empty($options['order']) && !empty($pk)) {
  275. $options['order'] = $pk . ' desc';
  276. } else if ($order) {
  277. $options['order'] = $order;
  278. }
  279. unset($REQUEST['_order']);
  280. unset($REQUEST['_field']);
  281. $options['where'] = array_filter(array_merge((array)$base, (array)$where), function ($val) {
  282. if (($val === '') || ($val === null)) {
  283. return false;
  284. } else {
  285. return true;
  286. }
  287. });
  288. if (empty($options['where'])) {
  289. unset($options['where']);
  290. }
  291. $options = array_merge((array)$OPT->getValue($model), $options);
  292. $total = $model->where($options['where'])->count();
  293. if (isset($REQUEST['r'])) {
  294. $listRows = (int)$REQUEST['r'];
  295. } else {
  296. $listRows = (0 < C('LIST_ROWS') ? C('LIST_ROWS') : 10);
  297. }
  298. $page = new \Think\Page($total, $listRows, $REQUEST);
  299. if ($listRows < $total) {
  300. $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
  301. }
  302. $p = $page->show();
  303. $this->assign('_page', $p ? $p : '');
  304. $this->assign('_total', $total);
  305. $options['limit'] = $page->firstRow . ',' . $page->listRows;
  306. $model->setProperty('options', $options);
  307. return $model->field($field)->select();
  308. }
  309. // 提现导出表处理
  310. public function exportExcel($expTitle, $expCellName, $expTableData)
  311. {
  312. import('Org.Util.PHPExcel');
  313. import('Org.Util.PHPExcel.Writer.Excel5');
  314. import('Org.Util.PHPExcel.IOFactory.php');
  315. $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);
  316. $fileName = $_SESSION['loginAccount'] . date('_YmdHis');
  317. $cellNum = count($expCellName);
  318. $dataNum = count($expTableData);
  319. // $objPHPExcel = new PHPExcel();
  320. $objPHPExcel = new \PHPExcel();
  321. $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
  322. $objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');
  323. $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', date('Y-m-d H:i:s') . '提现记录');
  324. $i = 0;
  325. for (; $i < $cellNum; $i++) {
  326. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '2', $expCellName[$i][2]);
  327. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($cellName[$i])->setWidth(12);
  328. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  329. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(30);
  330. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('M')->setWidth(30);
  331. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('O')->setWidth(20);
  332. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('L')->setWidth(30);
  333. }
  334. $i = 0;
  335. for (; $i < $dataNum; $i++) {
  336. $j = 0;
  337. for (; $j < $cellNum; $j++) {
  338. $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 3), (string)$expTableData[$i][$expCellName[$j][0]]);
  339. }
  340. }
  341. ob_end_clean();
  342. header('pragma:public');
  343. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
  344. header('Content-Disposition:attachment;filename=' . $fileName . '.xls');
  345. // $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  346. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  347. $objWriter->save('php://output');
  348. exit();
  349. }
  350. // 充值记录导出表处理
  351. public function cz_exportExcel($expTitle, $expCellName, $expTableData)
  352. {
  353. import('Org.Util.PHPExcel');
  354. import('Org.Util.PHPExcel.Writer.Excel5');
  355. import('Org.Util.PHPExcel.IOFactory.php');
  356. $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);
  357. $fileName = $_SESSION['loginAccount'] . date('_YmdHis');
  358. $cellNum = count($expCellName);
  359. $dataNum = count($expTableData);
  360. // $objPHPExcel = new PHPExcel();
  361. $objPHPExcel = new \PHPExcel();
  362. $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
  363. $objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');
  364. $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', date('Y-m-d H:i:s') . '充值记录');
  365. $i = 0;
  366. for (; $i < $cellNum; $i++) {
  367. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '2', $expCellName[$i][2]);
  368. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($cellName[$i])->setWidth(12);
  369. // $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
  370. // $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(30);
  371. // $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('M')->setWidth(30);
  372. // $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('O')->setWidth(20);
  373. // $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('L')->setWidth(30);
  374. }
  375. $i = 0;
  376. for (; $i < $dataNum; $i++) {
  377. $j = 0;
  378. for (; $j < $cellNum; $j++) {
  379. $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 3), (string)$expTableData[$i][$expCellName[$j][0]]);
  380. }
  381. }
  382. ob_end_clean();
  383. header('pragma:public');
  384. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
  385. header('Content-Disposition:attachment;filename=' . $fileName . '.xls');
  386. // $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  387. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  388. $objWriter->save('php://output');
  389. exit();
  390. }
  391. // 用户表导出处理
  392. public function us_exportExcel($expTitle, $expCellName, $expTableData)
  393. {
  394. import('Org.Util.PHPExcel');
  395. import('Org.Util.PHPExcel.Writer.Excel5');
  396. import('Org.Util.PHPExcel.IOFactory.php');
  397. $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);
  398. $fileName = $_SESSION['loginAccount'] . date('_YmdHis');
  399. $cellNum = count($expCellName);
  400. $dataNum = count($expTableData);
  401. $objPHPExcel = new \PHPExcel();
  402. $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
  403. $objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');
  404. $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', date('Y-m-d H:i:s') . '充值记录');
  405. $i = 0;
  406. for (; $i < $cellNum; $i++) {
  407. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '2', $expCellName[$i][2]);
  408. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($cellName[$i])->setWidth(12);
  409. }
  410. $i = 0;
  411. for (; $i < $dataNum; $i++) {
  412. $j = 0;
  413. for (; $j < $cellNum; $j++) {
  414. $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 3), (string)$expTableData[$i][$expCellName[$j][0]]);
  415. }
  416. }
  417. ob_end_clean();
  418. header('pragma:public');
  419. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
  420. header('Content-Disposition:attachment;filename=' . $fileName . '.xls');
  421. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  422. $objWriter->save('php://output');
  423. exit();
  424. }
  425. }
  426. ?>