Common.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Common extends CI_Controller {
  3. var $response;
  4. var $userinfo;
  5. var $version;
  6. public function __construct() {
  7. parent::__construct();
  8. $this->load->model('User_model');
  9. $this->response = $this->config->config['response_en'];
  10. $version = 1;
  11. $this->session->set_userdata('version', $version);
  12. $this->version = $version;
  13. }
  14. // 系统日志
  15. protected function add_operation_log($optype, $content, $logtype = 1,$language=0){
  16. $role = $this->userinfo['role'];
  17. $companyid = $this->userinfo['company'];
  18. $this->load->model('Syslog_model');
  19. $userid = $this->userinfo['id'];
  20. $data = array(
  21. 'time' => date('Y-m-d H:i:s',time()),
  22. 'logtype' => $logtype,
  23. 'optype' => $optype,
  24. 'userid' => $userid,
  25. 'content' => $content,
  26. 'language' => $language
  27. );
  28. if ($role != SYSTEM_ADMIN) {
  29. $data['company'] = $companyid;
  30. }
  31. $this->Syslog_model->add($data);
  32. }
  33. public function login() {
  34. $this->load->model('Company_model');
  35. $username = $this->input->post('username',true);
  36. $password = $this->input->post('password',true);
  37. $client_key = $this->input->post('client_key',true);
  38. $role = $this->input->post('role',true);
  39. $code = $this->input->post('code',true);
  40. $os = intval($this->input->post('os',true));
  41. if(empty($client_key)) exit(json_result('0003', $this->response['0003']));
  42. if(empty($username) || empty($password)) exit(json_result('0100', $this->response['0100']));
  43. $password = md5($password);
  44. if(empty($os) && empty($code)) exit(json_result('0105', $this->response['0105']));
  45. if (empty($os) && !isset($_SESSION['login_code'])) exit(json_result('0106',$this->response['0106']));
  46. if (empty($os) && $_SESSION['login_code'] != $code) exit(json_result('0107',$this->response['0107']));
  47. $user = $this->User_model->get_one(array('username'=>$username,'role'=>$role));
  48. if(!empty($user)) {
  49. if ($user['password'] != $password) {
  50. exit(json_result('0101', $this->response['0101']));
  51. }
  52. $comInfo = $this->Company_model->get_one(['id'=>$user['company']],'name,no');
  53. $company = !empty($comInfo['no']) ? $comInfo['no'] : $comInfo['name'];
  54. $data = array(
  55. 'token' => generate_token($username, $password, $client_key),
  56. 'username' => $user['username'],
  57. 'id' => $user['id'],
  58. 'role' => intval($user['role']),
  59. 'name' => $user['name'],
  60. 'privilege' => $user['privilege'],
  61. 'avatar' => empty($user['avatar']) ? '' : base_url($user['avatar']),
  62. 'company' => $company
  63. );
  64. $this->userinfo = $user;
  65. $this->add_operation_log('login','用户登录',0);
  66. $this->add_operation_log('login','login',0,1);
  67. exit(json_result('0000', $this->response['0000'], $data));
  68. } else {
  69. exit(json_result('0102', $this->response['0102']));
  70. }
  71. }
  72. // 图片上传
  73. public function update_file(){
  74. $type = intval($this->input->post("fileType",true));
  75. $path = '../upload/image';
  76. $config['file_name'] = md5(uniqid()); // 设置图片名字
  77. if (!file_exists($path)) {
  78. mkdir($path);
  79. }
  80. $config['upload_path'] = $path.'/'; // 设置图片上传路径
  81. $config['allowed_types'] = '*'; // 设置图片上传格式
  82. $config['max_size'] = 10240; // 设置文件上传大小
  83. $this->load->library('upload', $config);
  84. if ( ! $this->upload->do_upload('file'))
  85. {
  86. $error = array('error' => $this->upload->display_errors('',''));
  87. // if (empty($this->version)) {
  88. // $data = array('error'=>transfer_error_tips($error['error']));
  89. // }else{
  90. $data = array('error'=>$error['error']);
  91. // }
  92. exit(json_result('0012',$this->response['0012'],$data));
  93. }
  94. else
  95. {
  96. $data = $this->upload->data();
  97. $imagePath = '/upload/image/'.$data['file_name'];
  98. exit(json_result('0000',$this->response['0000'],array('path'=>base_url($imagePath))));
  99. }
  100. }
  101. public function get_country() {
  102. $this->load->model('Global_location_model');
  103. $version = $this->session->userdata('version');
  104. if (empty($version)) {
  105. $data = $this->Global_location_model->get_list(['level'=>1,'id'=>92], 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  106. }else{
  107. $data = $this->Global_location_model->get_list(['level'=>1,'id'=>92], 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  108. }
  109. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  110. }
  111. public function get_province() {
  112. $country_id = intval($this->input->post('countryId',true));
  113. $country_id = 92;
  114. $this->load->model('Global_location_model');
  115. $version = $this->session->userdata('version');
  116. if (empty($country_id)) {
  117. exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
  118. }else{
  119. $where = ['level'=>2, 'pid'=>$country_id];
  120. }
  121. if (empty($version)) {
  122. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  123. }else{
  124. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  125. }
  126. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  127. }
  128. public function get_city() {
  129. $province_id = intval($this->input->post('provinceId',true));
  130. $this->load->model('Global_location_model');
  131. $version = $this->session->userdata('version');
  132. if (empty($province_id)) {
  133. exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
  134. }else{
  135. $where = ['level'=>3, 'pid'=>$province_id];
  136. }
  137. if (empty($version)) {
  138. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  139. }else{
  140. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  141. }
  142. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  143. }
  144. public function get_area() {
  145. $cityId = intval($this->input->post('cityId',true));
  146. $this->load->model('Global_location_model');
  147. $version = $this->session->userdata('version');
  148. if (empty($cityId)) {
  149. exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
  150. }else{
  151. $where = ['level'=>4, 'pid'=>$cityId];
  152. }
  153. if (empty($version)) {
  154. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  155. }else{
  156. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  157. }
  158. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  159. }
  160. public function get_code_image(){
  161. $type = intval($this->input->post('type',true));
  162. $this->load->helper('captcha');
  163. if (!file_exists('../upload')) mkdir('../upload/');
  164. if (!file_exists('../upload/image')) mkdir('../upload/image/');
  165. if (!file_exists('../upload/image/code')) mkdir('../upload/image/code/');
  166. $url = '/upload/image/code';
  167. $code = rand(1000,9999);
  168. $vals = array(
  169. 'word' => $code,
  170. 'img_path' => '../upload/image/code/',
  171. 'img_url' => $url,
  172. 'img_width' => 120,
  173. 'font_path' => '../file/font/PingFangRegular.ttf',
  174. 'img_height' => 36,
  175. 'expiration' => 600,
  176. 'word_length' => 4,
  177. 'font_size' => 20,
  178. 'img_id' => 'Imageid',
  179. );
  180. $cap = create_captcha($vals);
  181. $filename = $cap['filename'];
  182. $url .= '/'.$filename;
  183. $this->load->library('session');
  184. if ($type == 0) {
  185. $this->session->set_userdata('login_code', $code);
  186. }else {
  187. $this->session->set_userdata('password_code', $code);
  188. }
  189. exit(json_result('0000',$this->response['0000'],array('path'=>base_url($url))));
  190. }
  191. public function model_nav(){
  192. $supId = intval($this->input->post('supId',true));
  193. if (empty($supId)) exit(json_result('0000',$this->response['0000'],['list'=>array()]));
  194. $list = $this->db->query('select id,name from model_info where supId = '.$supId)->result_array();
  195. exit(json_result('0000',$this->response['0000'],['list'=>$list]));
  196. }
  197. // 图片上传
  198. public function update_firewalld(){
  199. // var_dump($_FILES);
  200. // $type = intval($this->input->post("fileType",true));
  201. $filename = trim($_FILES['file']['name']);
  202. $path = '../upload/firewalld';
  203. $config['file_name'] = $filename;
  204. if (!file_exists('../upload')) mkdir('../upload');
  205. if (!file_exists('../upload/firewalld')) mkdir('../upload/firewalld');
  206. $config['upload_path'] = $path.'/'; // 设置图片上传路径
  207. $config['allowed_types'] = '*'; // 设置图片上传格式
  208. $config['max_size'] = 10240; // 设置文件上传大小
  209. $this->load->library('upload', $config);
  210. if ( ! $this->upload->do_upload('file'))
  211. {
  212. $error = array('error' => $this->upload->display_errors('',''));
  213. // if (empty($this->version)) {
  214. // $data = array('error'=>transfer_error_tips($error['error']));
  215. // }else{
  216. $data = array('error'=>$error['error']);
  217. // }
  218. exit(json_result('0012',$this->response['0012'],$data));
  219. }
  220. else
  221. {
  222. $data = $this->upload->data();
  223. $imagePath = '/upload/firewalld/'.$data['file_name'];
  224. exit(json_result('0000',$this->response['0000'],array('path'=>$imagePath)));
  225. }
  226. }
  227. public function firewalld(){
  228. $this->load->view('common/index');
  229. }
  230. // 获取制造商下拉列表
  231. public function manu_nav(){
  232. $list = $this->db->query('select id,name,no,companyId from company where type = 2 and companyId != "" order by companyId DESC')->result_array();
  233. foreach ($list as $key => $value) {
  234. if (!empty($value['no'])) {
  235. $list[$key]['name'] = $value['no'];
  236. }
  237. $list[$key]['name'] = substr($value['companyId'], 0,2).'---'.$list[$key]['name'];
  238. }
  239. exit(json_result('0000',$this->response['0000'],['list'=>$list]));
  240. }
  241. // 通过设备地址获取制造商id
  242. public function get_manu_by_address(){
  243. $address = $this->input->post('address',true);
  244. $name = '';
  245. if ($address === '' || $address === null){
  246. $id = 0;
  247. }else{
  248. $companyId = substr($address,0,2);
  249. $res = $this->db->query('select id,name,no from company where companyId like "'.$companyId.'%" order by id desc')->row_array();
  250. if (empty($res) || empty($res['id'])) {
  251. $id = 0;
  252. }else{
  253. $id = $res['id'];
  254. if (!empty($res['no'])) {
  255. $name = $res['no'];
  256. }else{
  257. $name = $res['name'];
  258. }
  259. }
  260. }
  261. exit(json_result('0000',$this->response['0000'],array('id'=>$id,'name'=>$name)));
  262. }
  263. // 上传固件文件
  264. public function uploadFilewalld(){
  265. $body = file_get_contents('php://input');
  266. $body = json_decode($body,true);
  267. $path = $body['path'];
  268. $version = $body['version'];
  269. $manuId = $body['manuId'];
  270. $infodata = $this->db->query('select * from firmware_info where manuId = '.$manuId)->row_array();
  271. $data = ['version'=>$version,'path'=>$path,'manuId'=>$manuId];
  272. if (empty($infodata)) {
  273. $this->db->insert('firmware_info', $data);
  274. }else{
  275. $this->db->where('id', $infodata['id']);
  276. $this->db->update('firmware_info', $data);
  277. }
  278. exit(json_result('0000',$this->response['0000']));
  279. }
  280. public function workmode() {
  281. $version = $this->session->userdata('version');
  282. if (empty($version)) {
  283. $data = (object)array(
  284. 0 => '纯光控、光控开负载、光控关负载',
  285. 1 => '光控开负载,延时1小时后关闭负载',
  286. 2 => '光控开负载,延时2小时后关闭负载',
  287. 3 => '光控开负载,延时3小时后关闭负载',
  288. 4 => '光控开负载,延时4小时后关闭负载',
  289. 5 => '光控开负载,延时5小时后关闭负载',
  290. 6 => '光控开负载,延时6小时后关闭负载',
  291. 7 => '光控开负载,延时7小时后关闭负载',
  292. 8 => '光控开负载,延时8小时后关闭负载',
  293. 9 => '光控开负载,延时9小时后关闭负载',
  294. 10 => '光控开负载,延时10小时后关闭负载',
  295. 11 => '光控开负载,延时11小时后关闭负载',
  296. 12 => '光控开负载,延时12小时后关闭负载',
  297. 13 => '光控开负载,延时13小时后关闭负载',
  298. 14 => '光控开负载,延时14小时后关闭负载',
  299. 15 => '手动模式',
  300. 16 => '调试模式',
  301. 17 => '常开模式'
  302. );
  303. }else{
  304. $data = (object)array(
  305. 0 => 'Pure light control, light control open load, light control load',
  306. 1 => 'Light control open load, delay 1 hours to close the load',
  307. 2 => 'Light control open load, delay 2 hours to close the load',
  308. 3 => 'Light control open load, delay 3 hours to close the load',
  309. 4 => 'Light control open load, delay 4 hours to close the load',
  310. 5 => 'Light control open load, delay 5 hours to close the load',
  311. 6 => 'Light control open load, delay 6 hours to close the load',
  312. 7 => 'Light control open load, delay 7 hours to close the load',
  313. 8 => 'Light control open load, delay 8 hours to close the load',
  314. 9 => 'Light control open load, delay 9 hours to close the load',
  315. 10 => 'Light control open load, delay 10 hours to close the load',
  316. 11 => 'Light control open load, delay 11 hours to close the load',
  317. 12 => 'Light control open load, delay 12 hours to close the load',
  318. 13 => 'Light control open load, delay 13 hours to close the load',
  319. 14 => 'Light control open load, delay 14 hours to close the load',
  320. 15 => 'Manual mode',
  321. 16 => 'Debug mode',
  322. 17 => 'Open mode'
  323. );
  324. }
  325. exit(json_result('0000', $this->response['0000'], $data));
  326. }
  327. }