os = intval($this->input->post('os',true)); // if ($version === null) { // $version = $this->session->userdata('version'); // } $this->response = $this->config->config['response_en']; $this->load->model('Operation_model'); $this->load->model('Room_model'); $this->load->model('Build_model'); $this->load->model('Hotel_model'); $this->load->model('Global_location_model'); $this->load->model('User_model'); $this->load->model('Floor_model'); $this->load->model('Sensorlog_model'); $this->load->model('DevInfo_model'); $this->load->model('TempDevInfo_model'); $this->load->model('AlarmInfo_model'); $this->load->model('LampLog_model'); $this->load->model('AirPurifier_model'); $this->load->model('Network_model'); $this->check_base_params(); // 判断用户权限 // $this->check_auth(); } // 检测用户权限 // private function check_auth(){ // if ($this->userinfo['role'] != 1) { // $action = uri_string(); // // 设置基本权限 // $def = array(); // if (!in_array($action, $def)) { // // 获取用户权限 // $authid = $this->get_user_info('privilege_list'); // if (empty($authid)) exit(json_result('0013',$this->response['0013'])); // $authid = explode(',', $authid); // $this->load->model('Privilnode_model'); // $actions = $this->Privilnode_model->get_list(['id'=>$authid],'action'); // $actions = explode(',', implode(array_column($actions, 'action'), ',')); // if (!in_array($action, $actions)) { // $asy_actions = ['group/inspectionSet','lamp/dimming','lamp/getParam','lamp/setParam','lamp/reset','group/set','lamp/update_lamp_info']; // if (in_array($action, $asy_actions)) { // $data = ['code'=>'0013','msg'=>$this->response['0013'],'data'=>['status'=>2]]; // $clientid = $this->input->post('clientid',true); // exit(send_websocket($clientid,$data)); // } // exit(json_result('0013',$this->response['0013'])); // } // } // } // } protected function check_base_params() { $username = $this->input->post('username',true); $token = $this->input->post('token',true); $client_key = $this->input->post('client_key',true); $os = intval($this->input->post('os',true)); if(empty($username)){ exit(json_result('0001', $this->response['0001'])); } $userinfo = $this->User_model->get_one(array('username'=>$username)); if(empty($userinfo)){ exit(json_result('0102', $this->response['0102'])); } if(empty($token)){ exit(json_result('0002', $this->response['0002'])); } if(empty($client_key)){ exit(json_result('0003', $this->response['0003'])); } if ($token != generate_token($username, $userinfo['password'], $client_key)) { exit(json_result('0004', $this->response['0004'])); } if ($userinfo['role'] != SYSTEM_ADMIN && $userinfo['status'] == 1) { exit(json_result('0103',$this->response['0103'])); } $this->userinfo = $userinfo; $this->os = $os; } protected function get_user_info($key){ return $this->userinfo[$key]; } protected function add_opertaion_log($remark = '',$type = 0,$operaType=0,$devtype=0,$deviceId=0,$floor='',$room='',$time = '',$roomId=0) { $new = [ 'time' => $time, 'acount' => $this->get_user_info('username'), 'name' => $this->get_user_info('name'), 'remark' => $remark, 'os' => $this->os, 'type' => $type, 'operaType' => $operaType, 'devtype' => $devtype, 'userid' => $this->userinfo['id'], 'deviceId' => $deviceId, 'floor' => empty($floor) ? 0 : $floor, 'room' => empty($room) ? 0 : $room, 'roomId' => $roomId, ]; $this->Operation_model->add($new); } /** * type 0 增加,1 减少 * deviceType 设备类型(0 lights,1 circadian light,2 sensor,3 purifiers) */ protected function change_room_device_count($roomId,$deviceType,$type,$count = 1){ if (empty($roomId)) return; $roomData = $this->Room_model->get_one(array('id'=>$roomId),'deviceCount,lightCount,cicLightCount,sensorCount,airPurCount'); if (empty($roomData)) return; if ($type == 1) { $roomData['deviceCount'] = $roomData['deviceCount'] > $count ? $roomData['deviceCount'] - $count : 0; if ($deviceType == 0) { $roomData['lightCount'] = $roomData['lightCount'] > $count ? $roomData['lightCount'] - $count : 0; }elseif ($deviceType == 1) { $roomData['cicLightCount'] = $roomData['cicLightCount'] > $count ? $roomData['cicLightCount'] - $count : 0; }elseif ($deviceType == 2) { $roomData['sensorCount'] = $roomData['sensorCount'] > $count ? $roomData['sensorCount'] - $count : 0; }else{ $roomData['airPurCount'] = $roomData['airPurCount'] > $count ? $roomData['airPurCount'] - $count : 0; } }else{ $roomData['deviceCount'] = $roomData['deviceCount'] + $count; if ($deviceType == 0) { $roomData['lightCount'] = $roomData['lightCount'] + $count; }elseif ($deviceType == 1) { $roomData['cicLightCount'] = $roomData['cicLightCount'] + $count; }elseif ($deviceType == 2) { $roomData['sensorCount'] = $roomData['sensorCount'] + $count; }else{ $roomData['airPurCount'] = $roomData['airPurCount'] + $count; } } $this->Room_model->update($roomData,array('id'=>$roomId)); } // 获取时区,可根据房间、楼房、酒店、城市获取时区数据 protected function get_timezone($id = 0,$type = 0){ if (empty($id)) return 8; $join = array( ['table'=>'dev_info AS D','cond'=>'D.roomId = R.id','type'=>'left'], ['table'=>'floor AS F','cond'=>'F.id = R.floorId','type'=>'left'], ['table'=>'build AS B','cond'=>'F.buildId = B.id','type'=>'left'], ['table'=>'hotel AS H','cond'=>'B.hotelId = H.id','type'=>'left'], ['table'=>'global_location AS G','cond'=>'G.id = H.cityId','type'=>'left'] ); $where = array(); if ($type == ROOM) { $where['R.id'] = $id; }elseif ($type == HOTEL) { $where['H.id'] = $id; }elseif ($type == FLOOR) { $where['F.id'] = $id; }elseif ($type == BUILD) { $where['B.id'] = $id; }elseif ($type == CITY) { $where['G.id'] = $id; }elseif ($type == DEVICE) { $where['D.id'] = $id; }else{ return 8; } $res = $this->Room_model->get_list_by_multi_join($where, 'G.timezone',NULL, NULL, $join, NULL, NUll, 'R', true); return empty($res) ? 8 : $res['timezone']; } /** * type 0 房间,1 楼房 */ protected function update_user_personnel($id,$pid,$type=0){ if ($type == BUILD) { $userList = $this->User_model->get_list(array('buildId|'=>$id),'hotelId,id'); if (!empty($userList)) { foreach ($userList as $user) { if (empty($user['hotelId'])) { $this->User_model->update(array('hotelId'=>$pid),array('id'=>$user['id'])); }else{ $hotelArr = explode(',', $user['hotelId']); if (!in_array($pid, $hotelArr)) { $hotelArr[] = $pid; $ids = implode(',', $hotelArr); $this->User_model->update(array('hotelId'=>$ids),array('id'=>$user['id'])); } } } } }elseif ($type == ROOM){ $buildData = $this->Build_model->get_one(array('id'=>$pid),'hotelId'); if (empty($buildData)) return; $userList = $this->User_model->get_list(array('room|'=>$id),'hotelId,buildId,id'); if (!empty($userList)) { foreach ($userList as $user) { if (empty($user['buildId'])) { $this->User_model->update(array('buildId'=>$pid),array('id'=>$user['id'])); }else{ $buildArr = explode(',', $user['buildId']); if (!in_array($pid, $buildArr)) { $buildArr[] = $pid; $ids = implode(',', $buildArr); $this->User_model->update(array('buildId'=>$ids),array('id'=>$user['id'])); } } if (empty($user['hotelId'])) { $this->User_model->update(array('hotelId'=>$buildData['hotelId']),array('id'=>$user['id'])); }else{ $hotelArr = explode(',', $user['hotelId']); if (!in_array($buildData['hotelId'], $hotelArr)) { $hotelArr[] = $buildData['hotelId']; $ids = implode(',', $hotelArr); $this->User_model->update(array('hotelId'=>$ids),array('id'=>$user['id'])); } } } } } } /** * type ROOM 房间,BUILD 楼房,HOTEL 酒店,FLOOR 楼层 */ protected function del_user_personnel($id,$type=0){ if ($type == BUILD) { $userList = $this->User_model->get_list(array('buildId|'=>$id),'buildId,id'); if (!empty($userList)) { foreach ($userList as $user) { if (!empty($user['buildId'])) { $buildArr = explode(',', $user['buildId']); if (in_array($id, $buildArr)) { $temp = array(); foreach ($buildArr as $bId) { if ($id != $bId) $temp[] = $bId; } $ids = empty($temp) ? '' : implode(',', $temp); $this->User_model->update(array('buildId'=>$ids),array('id'=>$user['id'])); } } } } }elseif ($type == HOTEL) { $userList = $this->User_model->get_list(array('hotelId|'=>$id),'hotelId,id'); if (!empty($userList)) { foreach ($userList as $user) { if (!empty($user['hotelId'])) { $hotelArr = explode(',', $user['hotelId']); if (in_array($id, $hotelArr)) { $temp = array(); foreach ($hotelArr as $bId) { if ($id != $bId) $temp[] = $bId; } $ids = empty($temp) ? '' : implode(',', $temp); $this->User_model->update(array('hotelId'=>$ids),array('id'=>$user['id'])); } } } } }elseif ($type == ROOM) { $userList = $this->User_model->get_list(array('roomId|'=>$id),'roomId,id'); if (!empty($userList)) { foreach ($userList as $user) { if (!empty($user['roomId'])) { $roomArr = explode(',', $user['roomId']); if (in_array($id, $roomArr)) { $temp = array(); foreach ($roomArr as $bId) { if ($id != $bId) $temp[] = $bId; } $ids = empty($temp) ? '' : implode(',', $temp); $this->User_model->update(array('roomId'=>$ids),array('id'=>$user['id'])); } } } } }elseif ($type == FLOOR) { $userList = $this->User_model->get_list(array('floorId|'=>$id),'floorId,id'); if (!empty($userList)) { foreach ($userList as $user) { if (!empty($user['floorId'])) { $roomArr = explode(',', $user['floorId']); if (in_array($id, $roomArr)) { $temp = array(); foreach ($roomArr as $bId) { if ($id != $bId) $temp[] = $bId; } $ids = empty($temp) ? '' : implode(',', $temp); $this->User_model->update(array('floorId'=>$ids),array('id'=>$user['id'])); } } } } } } // 获取酒店id protected function get_hotel_ids(){ if ($this->get_user_info('role') == SYSTEM_ADMIN) { $roomId = $this->get_user_info('roomId'); if (empty($roomId)) return array(); $join = array(); $join[] = ['table'=>'build as B','cond'=>'B.id = R.buildId','type'=>'left']; $join[] = ['table'=>'hotel as H','cond'=>'H.id = B.hotelId','type'=>'left']; $hotelList = $this->Room_model->get_list_by_join(array('R.id'=>explode(',', $roomId)), 'H.id as id',NULL, NULL, $join, NULL, NUll, 'R'); }else{ $hotelList = $this->Hotel_model->get_list(array(),'id'); } $hotelIds = array_column($hotelList, 'id'); return $hotelIds; } // 导出数据 protected function export($fileName = '导出数据', $titles = [], $data= []){ $this->load->library('phpExcel/PHPExcel'); $objPHPExcel = new PHPExcel(); //设置基本信息 $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->setTitle($fileName);//设置sheet的名字 $A_ASCII = 65;//A字母ASCII码 //设置标题栏 if($titles){ foreach($titles as $index=>$title){ $cur_code = chr($A_ASCII+$index); $objPHPExcel->getActiveSheet()->getStyle($cur_code)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT); // $objPHPExcel->getActiveSheet()->getStyle('C')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT); $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cur_code.'1',$title); //设置列宽 $objPHPExcel->getActiveSheet()->getColumnDimension($cur_code)->setWidth(20); } } $line_length = count($data)+1; $objPHPExcel->getActiveSheet()->getStyle('A1:R'.$line_length)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//水平居中 $objPHPExcel->getActiveSheet()->getStyle('A1:R'.$line_length)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中 //设置数据 foreach($data as $k => $item){ if($item){ $k = $k +2; $objPHPExcel->setActiveSheetIndex(0); foreach($item as $index=>$value){ $cur_code = chr($A_ASCII+$index); // $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cur_code.$k,$value); $objPHPExcel->setActiveSheetIndex(0)->setCellValueExplicit($cur_code.$k,$value,PHPExcel_Cell_DataType::TYPE_STRING); } } } //创建一个新的工作空间(sheet) setcookie('revenue_total_flag', 0); //防止用户重复点击 // header('Content-Type: application/vnd.ms-excel'); // header('Content-Disposition: attachment;filename="'.$filename.'.xls"'); // header('Cache-Control: max-age=0'); // $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); // $objWriter->save('php://output'); // exit; $filePath = '../upload/file/'; if (!file_exists('../upload/file')) { mkdir('../upload/file'); } $objWrite = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $_fileName = iconv("utf-8", "gb2312", $fileName); //转码 $_savePath = $filePath.$_fileName.'.xls'; $objWrite->save($_savePath); return base_url('upload/file/'.$fileName.'.xls'); } // 批量导入 protected function batch_import($indexArr = []){ $config['file'] = md5(uniqid()); // 设置文件名字 $path = '../upload/file'; if (!file_exists($path)) { mkdir($path); } $config['upload_path'] = $path.'/'; // 设置文件上传路径 $config['file_name'] = date('YmdHis',time()); // 设置文件上传路径 $config['allowed_types'] = 'xls|xlsx'; // 设置文件上传格式 $config['max_size'] = 1024*2; // 设置文件上传大小 $this->load->library('upload', $config); if ( ! $this->upload->do_upload('file')){ // 文件上传失败 $error = array('error' => $this->upload->display_errors('','')); if (empty($this->version)) { exit(json_result('0012',$this->response['0012'],array('error'=>transfer_error_tips($error['error'])))); }elseif ($this->version == 2) { exit(json_result('0012',$this->response['0012'],array('error'=>transfer_error_tips_ru($error['error'])))); }else{ exit(json_result('0012',$this->response['0012'],array('error'=>$error['error']))); } } else{ // 文件上传成功 $data = $this->upload->data(); $filePath = $data['full_path']; $this->load->library('phpExcel/PHPExcel'); $reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿) $PHPExcel = $reader->load($filePath); // 载入excel文件 $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表 $highestRow = $sheet->getHighestRow(); // 取得总行数 // $highestColumm = $sheet->getHighestColumn(); // 取得总列数 /** 循环读取每个单元格的数据 */ $k = 0; $data = array(); for ($row = 1; $row <= $highestRow; $row++) //行号从1开始 { if ($row < 2) continue; // 跳过标题行 foreach ($indexArr as $key => $value) { $res = $sheet->getCell($key.$row)->getValue(); if (is_object($res)) { $res= $res->__toString(); } $data[$k][$value] = $res; } $k ++; } // 批量写入数据 return $data; } } }