123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Base_Controller extends CI_Controller {
- var $response;
- var $userinfo;
- var $os;
- var $version;
- public function __construct() {
- parent::__construct();
- $this->os = intval($this->input->post('os',true));
- $this->response = $this->config->config['response_en'];
- // $version = intval($this->input->post('version',true));
- $version = 1;
- $this->session->set_userdata('version', $version);
- $this->version = $version;
- $this->load->model('User_model');
- $this->check_base_params();
- // 判断用户权限
- // $this->check_auth();
- if (empty($this->userinfo['lastLoginTime']) || date('Y-m-d',time()) != date('Y-m-d',strtotime($this->userinfo['lastLoginTime']))) {
- $logCount = $this->get_user_info('logCount');
- $logCount += 1;
- $this->User_model->update(['logCount'=>$logCount,'lastLoginTime'=>date('Y-m-d H:i:s',time())],['id'=>$this->userinfo['id']]);
- }
- }
- 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));
- $role = intval($this->input->post('role',true));
- if(empty($username)){
- exit(json_result('0001', $this->response['0001']));
- }
- $userinfo = $this->User_model->get_one(array('username'=>$username,'role'=>$role));
- 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);
- }
-
- // 导出数据
- 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;
- }
- }
- // 系统日志
- protected function add_operation_log($optype, $content, $logtype = 1,$language = 0){
- $role = $this->get_user_info('role');
- $company = $this->get_user_info('company');
- $this->load->model('Syslog_model');
- $userid = $this->get_user_info('id');
- $data = array(
- 'time' => date('Y-m-d H:i:s',time()),
- 'logtype' => $logtype,
- 'optype' => $optype,
- 'userid' => $userid,
- 'content' => $content,
- 'language' => $language
- );
- if ($role != SYSTEM_ADMIN) {
- $data['company'] = $company;
- }
- $this->Syslog_model->add($data);
- }
- // 下发指令
- protected function send_cmd($funcode,$address,$data,$manucode='0000000000',$version='0100',$timeout = 30){
- // $address = strtolower($address);
- $sendData = $funcode.$version.'01'.$manucode.$data;
- $sendData .= crc16(pack('H*',$sendData));
- // var_dump($sendData);die;
- $seq = '9'.substr($funcode,-1);
- $seq = strtolower($seq);
- return lampMqttCmd('/IDCOL/CmdInput/'.$address,'/IDCOL/CmdOutput/'.$address,pack('H*',$sendData),$timeout,$seq);
- }
- // 根据设备地址获取制造商
- protected function get_manu_by_address($address){
- $id = substr($address, 0,2);
- $res = $this->db->query('select id from company where companyId like "'.$id.'%" order by id desc')->row_array();
- if (empty($res) || empty($res['id'])) {
- return 0;
- }
- return $res['id'];
- }
- }
|