Base_Controller.php 8.7 KB

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