Base_Controller.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Base_Controller extends CI_Controller {
  3. var $response;
  4. var $userinfo;
  5. var $os;
  6. var $version;
  7. public function __construct() {
  8. parent::__construct();
  9. $this->os = intval($this->input->post('os',true));
  10. $this->response = $this->config->config['response_en'];
  11. $this->load->model('User_model');
  12. $this->check_base_params();
  13. // 判断用户权限
  14. // $this->check_auth();
  15. }
  16. protected function check_base_params() {
  17. $username = $this->input->post('username',true);
  18. $token = $this->input->post('token',true);
  19. $client_key = $this->input->post('client_key',true);
  20. $os = intval($this->input->post('os',true));
  21. if(empty($username)){
  22. exit(json_result('0001', $this->response['0001']));
  23. }
  24. $userinfo = $this->User_model->get_one(array('username'=>$username));
  25. if(empty($userinfo)){
  26. exit(json_result('0102', $this->response['0102']));
  27. }
  28. if(empty($token)){
  29. exit(json_result('0002', $this->response['0002']));
  30. }
  31. if(empty($client_key)){
  32. exit(json_result('0003', $this->response['0003']));
  33. }
  34. if ($token != generate_token($username, $userinfo['password'], $client_key)) {
  35. exit(json_result('0004', $this->response['0004']));
  36. }
  37. if ($userinfo['role'] != SYSTEM_ADMIN && $userinfo['status'] == 1) {
  38. exit(json_result('0103',$this->response['0103']));
  39. }
  40. $this->userinfo = $userinfo;
  41. $this->os = $os;
  42. }
  43. protected function get_user_info($key){
  44. return $this->userinfo[$key];
  45. }
  46. protected function add_opertaion_log($remark = '',$type = 0,$operaType=0,$devtype=0,$deviceId=0,$floor='',$room='',$time = '',$roomId=0) {
  47. $new = [
  48. 'time' => $time,
  49. 'acount' => $this->get_user_info('username'),
  50. 'name' => $this->get_user_info('name'),
  51. 'remark' => $remark,
  52. 'os' => $this->os,
  53. 'type' => $type,
  54. 'operaType' => $operaType,
  55. 'devtype' => $devtype,
  56. 'userid' => $this->userinfo['id'],
  57. 'deviceId' => $deviceId,
  58. 'floor' => empty($floor) ? 0 : $floor,
  59. 'room' => empty($room) ? 0 : $room,
  60. 'roomId' => $roomId,
  61. ];
  62. $this->Operation_model->add($new);
  63. }
  64. // 导出数据
  65. protected function export($fileName = '导出数据', $titles = [], $data= []){
  66. $this->load->library('phpExcel/PHPExcel');
  67. $objPHPExcel = new PHPExcel();
  68. //设置基本信息
  69. $objPHPExcel->setActiveSheetIndex(0);
  70. $objPHPExcel->getActiveSheet()->setTitle($fileName);//设置sheet的名字
  71. $A_ASCII = 65;//A字母ASCII码
  72. //设置标题栏
  73. if($titles){
  74. foreach($titles as $index=>$title){
  75. $cur_code = chr($A_ASCII+$index);
  76. $objPHPExcel->getActiveSheet()->getStyle($cur_code)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
  77. // $objPHPExcel->getActiveSheet()->getStyle('C')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
  78. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cur_code.'1',$title);
  79. //设置列宽
  80. $objPHPExcel->getActiveSheet()->getColumnDimension($cur_code)->setWidth(20);
  81. }
  82. }
  83. $line_length = count($data)+1;
  84. $objPHPExcel->getActiveSheet()->getStyle('A1:R'.$line_length)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//水平居中
  85. $objPHPExcel->getActiveSheet()->getStyle('A1:R'.$line_length)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中
  86. //设置数据
  87. foreach($data as $k => $item){
  88. if($item){
  89. $k = $k +2;
  90. $objPHPExcel->setActiveSheetIndex(0);
  91. foreach($item as $index=>$value){
  92. $cur_code = chr($A_ASCII+$index);
  93. // $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cur_code.$k,$value);
  94. $objPHPExcel->setActiveSheetIndex(0)->setCellValueExplicit($cur_code.$k,$value,PHPExcel_Cell_DataType::TYPE_STRING);
  95. }
  96. }
  97. }
  98. //创建一个新的工作空间(sheet)
  99. setcookie('revenue_total_flag', 0); //防止用户重复点击
  100. // header('Content-Type: application/vnd.ms-excel');
  101. // header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
  102. // header('Cache-Control: max-age=0');
  103. // $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
  104. // $objWriter->save('php://output');
  105. // exit;
  106. $filePath = '../upload/file/';
  107. if (!file_exists('../upload/file')) {
  108. mkdir('../upload/file');
  109. }
  110. $objWrite = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  111. $_fileName = iconv("utf-8", "gb2312", $fileName); //转码
  112. $_savePath = $filePath.$_fileName.'.xls';
  113. $objWrite->save($_savePath);
  114. return base_url('upload/file/'.$fileName.'.xls');
  115. }
  116. // 批量导入
  117. protected function batch_import($indexArr = []){
  118. $config['file'] = md5(uniqid()); // 设置文件名字
  119. $path = '../upload/file';
  120. if (!file_exists($path)) {
  121. mkdir($path);
  122. }
  123. $config['upload_path'] = $path.'/'; // 设置文件上传路径
  124. $config['file_name'] = date('YmdHis',time()); // 设置文件上传路径
  125. $config['allowed_types'] = 'xls|xlsx'; // 设置文件上传格式
  126. $config['max_size'] = 1024*2; // 设置文件上传大小
  127. $this->load->library('upload', $config);
  128. if ( ! $this->upload->do_upload('file')){
  129. // 文件上传失败
  130. $error = array('error' => $this->upload->display_errors('',''));
  131. if (empty($this->version)) {
  132. exit(json_result('0012',$this->response['0012'],array('error'=>transfer_error_tips($error['error']))));
  133. }elseif ($this->version == 2) {
  134. exit(json_result('0012',$this->response['0012'],array('error'=>transfer_error_tips_ru($error['error']))));
  135. }else{
  136. exit(json_result('0012',$this->response['0012'],array('error'=>$error['error'])));
  137. }
  138. }
  139. else{
  140. // 文件上传成功
  141. $data = $this->upload->data();
  142. $filePath = $data['full_path'];
  143. $this->load->library('phpExcel/PHPExcel');
  144. $reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
  145. $PHPExcel = $reader->load($filePath); // 载入excel文件
  146. $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
  147. $highestRow = $sheet->getHighestRow(); // 取得总行数
  148. // $highestColumm = $sheet->getHighestColumn(); // 取得总列数
  149. /** 循环读取每个单元格的数据 */
  150. $k = 0;
  151. $data = array();
  152. for ($row = 1; $row <= $highestRow; $row++) //行号从1开始
  153. {
  154. if ($row < 2) continue; // 跳过标题行
  155. foreach ($indexArr as $key => $value) {
  156. $res = $sheet->getCell($key.$row)->getValue();
  157. if (is_object($res)) {
  158. $res= $res->__toString();
  159. }
  160. $data[$k][$value] = $res;
  161. }
  162. $k ++;
  163. }
  164. // 批量写入数据
  165. return $data;
  166. }
  167. }
  168. }