Base_Controller.php 10 KB

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