Base_Controller.php 8.6 KB

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