Base_Controller.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. // if ($version === null) {
  11. // $version = $this->session->userdata('version');
  12. // }
  13. $this->response = $this->config->config['response_en'];
  14. $this->load->model('Operation_model');
  15. $this->load->model('Room_model');
  16. $this->load->model('Build_model');
  17. $this->load->model('Hotel_model');
  18. $this->load->model('Global_location_model');
  19. $this->load->model('User_model');
  20. $this->load->model('Floor_model');
  21. $this->load->model('Sensorlog_model');
  22. $this->load->model('DevInfo_model');
  23. $this->load->model('TempDevInfo_model');
  24. $this->load->model('AlarmInfo_model');
  25. $this->load->model('LampLog_model');
  26. $this->load->model('AirPurifier_model');
  27. $this->load->model('Network_model');
  28. $this->check_base_params();
  29. // 判断用户权限
  30. // $this->check_auth();
  31. }
  32. // 检测用户权限
  33. // private function check_auth(){
  34. // if ($this->userinfo['role'] != 1) {
  35. // $action = uri_string();
  36. // // 设置基本权限
  37. // $def = array();
  38. // if (!in_array($action, $def)) {
  39. // // 获取用户权限
  40. // $authid = $this->get_user_info('privilege_list');
  41. // if (empty($authid)) exit(json_result('0013',$this->response['0013']));
  42. // $authid = explode(',', $authid);
  43. // $this->load->model('Privilnode_model');
  44. // $actions = $this->Privilnode_model->get_list(['id'=>$authid],'action');
  45. // $actions = explode(',', implode(array_column($actions, 'action'), ','));
  46. // if (!in_array($action, $actions)) {
  47. // $asy_actions = ['group/inspectionSet','lamp/dimming','lamp/getParam','lamp/setParam','lamp/reset','group/set','lamp/update_lamp_info'];
  48. // if (in_array($action, $asy_actions)) {
  49. // $data = ['code'=>'0013','msg'=>$this->response['0013'],'data'=>['status'=>2]];
  50. // $clientid = $this->input->post('clientid',true);
  51. // exit(send_websocket($clientid,$data));
  52. // }
  53. // exit(json_result('0013',$this->response['0013']));
  54. // }
  55. // }
  56. // }
  57. // }
  58. protected function check_base_params() {
  59. $username = $this->input->post('username',true);
  60. $token = $this->input->post('token',true);
  61. $client_key = $this->input->post('client_key',true);
  62. $os = intval($this->input->post('os',true));
  63. if(empty($username)){
  64. exit(json_result('0001', $this->response['0001']));
  65. }
  66. $userinfo = $this->User_model->get_one(array('username'=>$username));
  67. if(empty($userinfo)){
  68. exit(json_result('0102', $this->response['0102']));
  69. }
  70. if(empty($token)){
  71. exit(json_result('0002', $this->response['0002']));
  72. }
  73. if(empty($client_key)){
  74. exit(json_result('0003', $this->response['0003']));
  75. }
  76. if ($token != generate_token($username, $userinfo['password'], $client_key)) {
  77. exit(json_result('0004', $this->response['0004']));
  78. }
  79. if ($userinfo['role'] != SYSTEM_ADMIN && $userinfo['status'] == 1) {
  80. exit(json_result('0103',$this->response['0103']));
  81. }
  82. $this->userinfo = $userinfo;
  83. $this->os = $os;
  84. }
  85. protected function get_user_info($key){
  86. return $this->userinfo[$key];
  87. }
  88. protected function add_opertaion_log($remark = '',$type = 0,$operaType=0,$devtype=0,$deviceId=0,$floor='',$room='',$time = '',$roomId=0) {
  89. $new = [
  90. 'time' => $time,
  91. 'acount' => $this->get_user_info('username'),
  92. 'name' => $this->get_user_info('name'),
  93. 'remark' => $remark,
  94. 'os' => $this->os,
  95. 'type' => $type,
  96. 'operaType' => $operaType,
  97. 'devtype' => $devtype,
  98. 'userid' => $this->userinfo['id'],
  99. 'deviceId' => $deviceId,
  100. 'floor' => empty($floor) ? 0 : $floor,
  101. 'room' => empty($room) ? 0 : $room,
  102. 'roomId' => $roomId,
  103. ];
  104. $this->Operation_model->add($new);
  105. }
  106. /**
  107. * type 0 增加,1 减少
  108. * deviceType 设备类型(0 lights,1 circadian light,2 sensor,3 purifiers)
  109. */
  110. protected function change_room_device_count($roomId,$deviceType,$type,$count = 1){
  111. if (empty($roomId)) return;
  112. $roomData = $this->Room_model->get_one(array('id'=>$roomId),'deviceCount,lightCount,cicLightCount,sensorCount,airPurCount');
  113. if (empty($roomData)) return;
  114. if ($type == 1) {
  115. $roomData['deviceCount'] = $roomData['deviceCount'] > $count ? $roomData['deviceCount'] - $count : 0;
  116. if ($deviceType == 0) {
  117. $roomData['lightCount'] = $roomData['lightCount'] > $count ? $roomData['lightCount'] - $count : 0;
  118. }elseif ($deviceType == 1) {
  119. $roomData['cicLightCount'] = $roomData['cicLightCount'] > $count ? $roomData['cicLightCount'] - $count : 0;
  120. }elseif ($deviceType == 2) {
  121. $roomData['sensorCount'] = $roomData['sensorCount'] > $count ? $roomData['sensorCount'] - $count : 0;
  122. }else{
  123. $roomData['airPurCount'] = $roomData['airPurCount'] > $count ? $roomData['airPurCount'] - $count : 0;
  124. }
  125. }else{
  126. $roomData['deviceCount'] = $roomData['deviceCount'] + $count;
  127. if ($deviceType == 0) {
  128. $roomData['lightCount'] = $roomData['lightCount'] + $count;
  129. }elseif ($deviceType == 1) {
  130. $roomData['cicLightCount'] = $roomData['cicLightCount'] + $count;
  131. }elseif ($deviceType == 2) {
  132. $roomData['sensorCount'] = $roomData['sensorCount'] + $count;
  133. }else{
  134. $roomData['airPurCount'] = $roomData['airPurCount'] + $count;
  135. }
  136. }
  137. $this->Room_model->update($roomData,array('id'=>$roomId));
  138. }
  139. // 获取时区,可根据房间、楼房、酒店、城市获取时区数据
  140. protected function get_timezone($id = 0,$type = 0){
  141. if (empty($id)) return 8;
  142. $join = array(
  143. ['table'=>'dev_info AS D','cond'=>'D.roomId = R.id','type'=>'left'],
  144. ['table'=>'floor AS F','cond'=>'F.id = R.floorId','type'=>'left'],
  145. ['table'=>'build AS B','cond'=>'F.buildId = B.id','type'=>'left'],
  146. ['table'=>'hotel AS H','cond'=>'B.hotelId = H.id','type'=>'left'],
  147. ['table'=>'global_location AS G','cond'=>'G.id = H.cityId','type'=>'left']
  148. );
  149. $where = array();
  150. if ($type == ROOM) {
  151. $where['R.id'] = $id;
  152. }elseif ($type == HOTEL) {
  153. $where['H.id'] = $id;
  154. }elseif ($type == FLOOR) {
  155. $where['F.id'] = $id;
  156. }elseif ($type == BUILD) {
  157. $where['B.id'] = $id;
  158. }elseif ($type == CITY) {
  159. $where['G.id'] = $id;
  160. }elseif ($type == DEVICE) {
  161. $where['D.id'] = $id;
  162. }else{
  163. return 8;
  164. }
  165. $res = $this->Room_model->get_list_by_multi_join($where, 'G.timezone',NULL, NULL, $join, NULL, NUll, 'R', true);
  166. return empty($res) ? 8 : $res['timezone'];
  167. }
  168. /**
  169. * type 0 房间,1 楼房
  170. */
  171. protected function update_user_personnel($id,$pid,$type=0){
  172. if ($type == BUILD) {
  173. $userList = $this->User_model->get_list(array('buildId|'=>$id),'hotelId,id');
  174. if (!empty($userList)) {
  175. foreach ($userList as $user) {
  176. if (empty($user['hotelId'])) {
  177. $this->User_model->update(array('hotelId'=>$pid),array('id'=>$user['id']));
  178. }else{
  179. $hotelArr = explode(',', $user['hotelId']);
  180. if (!in_array($pid, $hotelArr)) {
  181. $hotelArr[] = $pid;
  182. $ids = implode(',', $hotelArr);
  183. $this->User_model->update(array('hotelId'=>$ids),array('id'=>$user['id']));
  184. }
  185. }
  186. }
  187. }
  188. }elseif ($type == ROOM){
  189. $buildData = $this->Build_model->get_one(array('id'=>$pid),'hotelId');
  190. if (empty($buildData)) return;
  191. $userList = $this->User_model->get_list(array('room|'=>$id),'hotelId,buildId,id');
  192. if (!empty($userList)) {
  193. foreach ($userList as $user) {
  194. if (empty($user['buildId'])) {
  195. $this->User_model->update(array('buildId'=>$pid),array('id'=>$user['id']));
  196. }else{
  197. $buildArr = explode(',', $user['buildId']);
  198. if (!in_array($pid, $buildArr)) {
  199. $buildArr[] = $pid;
  200. $ids = implode(',', $buildArr);
  201. $this->User_model->update(array('buildId'=>$ids),array('id'=>$user['id']));
  202. }
  203. }
  204. if (empty($user['hotelId'])) {
  205. $this->User_model->update(array('hotelId'=>$buildData['hotelId']),array('id'=>$user['id']));
  206. }else{
  207. $hotelArr = explode(',', $user['hotelId']);
  208. if (!in_array($buildData['hotelId'], $hotelArr)) {
  209. $hotelArr[] = $buildData['hotelId'];
  210. $ids = implode(',', $hotelArr);
  211. $this->User_model->update(array('hotelId'=>$ids),array('id'=>$user['id']));
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. /**
  219. * type ROOM 房间,BUILD 楼房,HOTEL 酒店,FLOOR 楼层
  220. */
  221. protected function del_user_personnel($id,$type=0){
  222. if ($type == BUILD) {
  223. $userList = $this->User_model->get_list(array('buildId|'=>$id),'buildId,id');
  224. if (!empty($userList)) {
  225. foreach ($userList as $user) {
  226. if (!empty($user['buildId'])) {
  227. $buildArr = explode(',', $user['buildId']);
  228. if (in_array($id, $buildArr)) {
  229. $temp = array();
  230. foreach ($buildArr as $bId) {
  231. if ($id != $bId) $temp[] = $bId;
  232. }
  233. $ids = empty($temp) ? '' : implode(',', $temp);
  234. $this->User_model->update(array('buildId'=>$ids),array('id'=>$user['id']));
  235. }
  236. }
  237. }
  238. }
  239. }elseif ($type == HOTEL) {
  240. $userList = $this->User_model->get_list(array('hotelId|'=>$id),'hotelId,id');
  241. if (!empty($userList)) {
  242. foreach ($userList as $user) {
  243. if (!empty($user['hotelId'])) {
  244. $hotelArr = explode(',', $user['hotelId']);
  245. if (in_array($id, $hotelArr)) {
  246. $temp = array();
  247. foreach ($hotelArr as $bId) {
  248. if ($id != $bId) $temp[] = $bId;
  249. }
  250. $ids = empty($temp) ? '' : implode(',', $temp);
  251. $this->User_model->update(array('hotelId'=>$ids),array('id'=>$user['id']));
  252. }
  253. }
  254. }
  255. }
  256. }elseif ($type == ROOM) {
  257. $userList = $this->User_model->get_list(array('roomId|'=>$id),'roomId,id');
  258. if (!empty($userList)) {
  259. foreach ($userList as $user) {
  260. if (!empty($user['roomId'])) {
  261. $roomArr = explode(',', $user['roomId']);
  262. if (in_array($id, $roomArr)) {
  263. $temp = array();
  264. foreach ($roomArr as $bId) {
  265. if ($id != $bId) $temp[] = $bId;
  266. }
  267. $ids = empty($temp) ? '' : implode(',', $temp);
  268. $this->User_model->update(array('roomId'=>$ids),array('id'=>$user['id']));
  269. }
  270. }
  271. }
  272. }
  273. }elseif ($type == FLOOR) {
  274. $userList = $this->User_model->get_list(array('floorId|'=>$id),'floorId,id');
  275. if (!empty($userList)) {
  276. foreach ($userList as $user) {
  277. if (!empty($user['floorId'])) {
  278. $roomArr = explode(',', $user['floorId']);
  279. if (in_array($id, $roomArr)) {
  280. $temp = array();
  281. foreach ($roomArr as $bId) {
  282. if ($id != $bId) $temp[] = $bId;
  283. }
  284. $ids = empty($temp) ? '' : implode(',', $temp);
  285. $this->User_model->update(array('floorId'=>$ids),array('id'=>$user['id']));
  286. }
  287. }
  288. }
  289. }
  290. }
  291. }
  292. // 获取酒店id
  293. protected function get_hotel_ids(){
  294. if ($this->get_user_info('role') == SYSTEM_ADMIN) {
  295. $roomId = $this->get_user_info('roomId');
  296. if (empty($roomId)) return array();
  297. $join = array();
  298. $join[] = ['table'=>'build as B','cond'=>'B.id = R.buildId','type'=>'left'];
  299. $join[] = ['table'=>'hotel as H','cond'=>'H.id = B.hotelId','type'=>'left'];
  300. $hotelList = $this->Room_model->get_list_by_join(array('R.id'=>explode(',', $roomId)), 'H.id as id',NULL, NULL, $join, NULL, NUll, 'R');
  301. }else{
  302. $hotelList = $this->Hotel_model->get_list(array(),'id');
  303. }
  304. $hotelIds = array_column($hotelList, 'id');
  305. return $hotelIds;
  306. }
  307. // 导出数据
  308. protected function export($fileName = '导出数据', $titles = [], $data= []){
  309. $this->load->library('phpExcel/PHPExcel');
  310. $objPHPExcel = new PHPExcel();
  311. //设置基本信息
  312. $objPHPExcel->setActiveSheetIndex(0);
  313. $objPHPExcel->getActiveSheet()->setTitle($fileName);//设置sheet的名字
  314. $A_ASCII = 65;//A字母ASCII码
  315. //设置标题栏
  316. if($titles){
  317. foreach($titles as $index=>$title){
  318. $cur_code = chr($A_ASCII+$index);
  319. $objPHPExcel->getActiveSheet()->getStyle($cur_code)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
  320. // $objPHPExcel->getActiveSheet()->getStyle('C')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
  321. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cur_code.'1',$title);
  322. //设置列宽
  323. $objPHPExcel->getActiveSheet()->getColumnDimension($cur_code)->setWidth(20);
  324. }
  325. }
  326. $line_length = count($data)+1;
  327. $objPHPExcel->getActiveSheet()->getStyle('A1:R'.$line_length)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//水平居中
  328. $objPHPExcel->getActiveSheet()->getStyle('A1:R'.$line_length)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中
  329. //设置数据
  330. foreach($data as $k => $item){
  331. if($item){
  332. $k = $k +2;
  333. $objPHPExcel->setActiveSheetIndex(0);
  334. foreach($item as $index=>$value){
  335. $cur_code = chr($A_ASCII+$index);
  336. // $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cur_code.$k,$value);
  337. $objPHPExcel->setActiveSheetIndex(0)->setCellValueExplicit($cur_code.$k,$value,PHPExcel_Cell_DataType::TYPE_STRING);
  338. }
  339. }
  340. }
  341. //创建一个新的工作空间(sheet)
  342. setcookie('revenue_total_flag', 0); //防止用户重复点击
  343. // header('Content-Type: application/vnd.ms-excel');
  344. // header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
  345. // header('Cache-Control: max-age=0');
  346. // $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
  347. // $objWriter->save('php://output');
  348. // exit;
  349. $filePath = '../upload/file/';
  350. if (!file_exists('../upload/file')) {
  351. mkdir('../upload/file');
  352. }
  353. $objWrite = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  354. $_fileName = iconv("utf-8", "gb2312", $fileName); //转码
  355. $_savePath = $filePath.$_fileName.'.xls';
  356. $objWrite->save($_savePath);
  357. return base_url('upload/file/'.$fileName.'.xls');
  358. }
  359. // 批量导入
  360. protected function batch_import($indexArr = []){
  361. $config['file'] = md5(uniqid()); // 设置文件名字
  362. $path = '../upload/file';
  363. if (!file_exists($path)) {
  364. mkdir($path);
  365. }
  366. $config['upload_path'] = $path.'/'; // 设置文件上传路径
  367. $config['file_name'] = date('YmdHis',time()); // 设置文件上传路径
  368. $config['allowed_types'] = 'xls|xlsx'; // 设置文件上传格式
  369. $config['max_size'] = 1024*2; // 设置文件上传大小
  370. $this->load->library('upload', $config);
  371. if ( ! $this->upload->do_upload('file')){
  372. // 文件上传失败
  373. $error = array('error' => $this->upload->display_errors('',''));
  374. if (empty($this->version)) {
  375. exit(json_result('0012',$this->response['0012'],array('error'=>transfer_error_tips($error['error']))));
  376. }elseif ($this->version == 2) {
  377. exit(json_result('0012',$this->response['0012'],array('error'=>transfer_error_tips_ru($error['error']))));
  378. }else{
  379. exit(json_result('0012',$this->response['0012'],array('error'=>$error['error'])));
  380. }
  381. }
  382. else{
  383. // 文件上传成功
  384. $data = $this->upload->data();
  385. $filePath = $data['full_path'];
  386. $this->load->library('phpExcel/PHPExcel');
  387. $reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
  388. $PHPExcel = $reader->load($filePath); // 载入excel文件
  389. $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
  390. $highestRow = $sheet->getHighestRow(); // 取得总行数
  391. // $highestColumm = $sheet->getHighestColumn(); // 取得总列数
  392. /** 循环读取每个单元格的数据 */
  393. $k = 0;
  394. $data = array();
  395. for ($row = 1; $row <= $highestRow; $row++) //行号从1开始
  396. {
  397. if ($row < 2) continue; // 跳过标题行
  398. foreach ($indexArr as $key => $value) {
  399. $res = $sheet->getCell($key.$row)->getValue();
  400. if (is_object($res)) {
  401. $res= $res->__toString();
  402. }
  403. $data[$k][$value] = $res;
  404. }
  405. $k ++;
  406. }
  407. // 批量写入数据
  408. return $data;
  409. }
  410. }
  411. }