Common.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. $username = $this->input->post('username',true);
  35. $password = $this->input->post('password',true);
  36. $client_key = $this->input->post('client_key',true);
  37. $role = $this->input->post('role',true);
  38. $code = $this->input->post('code',true);
  39. $os = intval($this->input->post('os',true));
  40. if(empty($client_key)) exit(json_result('0003', $this->response['0003']));
  41. if(empty($username) || empty($password)) exit(json_result('0100', $this->response['0100']));
  42. $password = md5($password);
  43. if(empty($os) && empty($code)) exit(json_result('0105', $this->response['0105']));
  44. if (empty($os) && !isset($_SESSION['login_code'])) exit(json_result('0106',$this->response['0106']));
  45. if (empty($os) && $_SESSION['login_code'] != $code) exit(json_result('0107',$this->response['0107']));
  46. $user = $this->User_model->get_one(array('username'=>$username,'role'=>$role));
  47. if(!empty($user)) {
  48. if ($user['password'] != $password) {
  49. exit(json_result('0101', $this->response['0101']));
  50. }
  51. $comInfo = $this->Company_model->get_one(['id'=>$user['companyid']],'name,no');
  52. $company = !empty($comInfo['no']) ? $comInfo['no'] : $comInfo['name'];
  53. $data = array(
  54. 'token' => generate_token($username, $password, $client_key),
  55. 'username' => $user['username'],
  56. 'id' => $user['id'],
  57. 'role' => intval($user['role']),
  58. 'name' => $user['name'],
  59. 'privilege' => $user['privilege'],
  60. 'avatar' => empty($user['avatar']) ? '' : base_url($user['avatar']),
  61. 'company' => $company
  62. );
  63. $this->userinfo = $user;
  64. $this->add_operation_log('login','用户登录',0);
  65. $this->add_operation_log('login','login',0,1);
  66. exit(json_result('0000', $this->response['0000'], $data));
  67. } else {
  68. exit(json_result('0102', $this->response['0102']));
  69. }
  70. }
  71. // 图片上传
  72. public function update_file(){
  73. $type = intval($this->input->post("fileType",true));
  74. $path = '../upload/image';
  75. $config['file_name'] = md5(uniqid()); // 设置图片名字
  76. if (!file_exists($path)) {
  77. mkdir($path);
  78. }
  79. $config['upload_path'] = $path.'/'; // 设置图片上传路径
  80. $config['allowed_types'] = '*'; // 设置图片上传格式
  81. $config['max_size'] = 10240; // 设置文件上传大小
  82. $this->load->library('upload', $config);
  83. if ( ! $this->upload->do_upload('file'))
  84. {
  85. $error = array('error' => $this->upload->display_errors('',''));
  86. // if (empty($this->version)) {
  87. // $data = array('error'=>transfer_error_tips($error['error']));
  88. // }else{
  89. $data = array('error'=>$error['error']);
  90. // }
  91. exit(json_result('0012',$this->response['0012'],$data));
  92. }
  93. else
  94. {
  95. $data = $this->upload->data();
  96. $imagePath = '/upload/image/'.$data['file_name'];
  97. exit(json_result('0000',$this->response['0000'],array('path'=>base_url($imagePath))));
  98. }
  99. }
  100. public function get_country() {
  101. $this->load->model('Global_location_model');
  102. $version = $this->session->userdata('version');
  103. if (empty($version)) {
  104. $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');
  105. }else{
  106. $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');
  107. }
  108. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  109. }
  110. public function get_province() {
  111. $country_id = intval($this->input->post('countryId',true));
  112. $country_id = 92;
  113. $this->load->model('Global_location_model');
  114. $version = $this->session->userdata('version');
  115. if (empty($country_id)) {
  116. exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
  117. }else{
  118. $where = ['level'=>2, 'pid'=>$country_id];
  119. }
  120. if (empty($version)) {
  121. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  122. }else{
  123. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  124. }
  125. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  126. }
  127. public function get_city() {
  128. $province_id = intval($this->input->post('provinceId',true));
  129. $this->load->model('Global_location_model');
  130. $version = $this->session->userdata('version');
  131. if (empty($province_id)) {
  132. exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
  133. }else{
  134. $where = ['level'=>3, 'pid'=>$province_id];
  135. }
  136. if (empty($version)) {
  137. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  138. }else{
  139. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  140. }
  141. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  142. }
  143. public function get_area() {
  144. $cityId = intval($this->input->post('cityId',true));
  145. $this->load->model('Global_location_model');
  146. $version = $this->session->userdata('version');
  147. if (empty($cityId)) {
  148. exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
  149. }else{
  150. $where = ['level'=>4, 'pid'=>$cityId];
  151. }
  152. if (empty($version)) {
  153. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  154. }else{
  155. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  156. }
  157. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  158. }
  159. public function get_code_image(){
  160. $type = intval($this->input->post('type',true));
  161. $this->load->helper('captcha');
  162. if (!file_exists('../upload')) mkdir('../upload');
  163. if (!file_exists('../upload/image')) mkdir('../upload/image');
  164. if (!file_exists('../upload/image/code')) mkdir('../upload/image/code');
  165. $url = '/upload/image/code';
  166. $code = rand(1000,9999);
  167. $vals = array(
  168. 'word' => $code,
  169. 'img_path' => '../upload/image/code/',
  170. 'img_url' => $url,
  171. 'img_width' => 120,
  172. 'font_path' => '../file/font/PingFangRegular.ttf',
  173. 'img_height' => 36,
  174. 'expiration' => 600,
  175. 'word_length' => 4,
  176. 'font_size' => 20,
  177. 'img_id' => 'Imageid',
  178. );
  179. $cap = create_captcha($vals);
  180. $filename = $cap['filename'];
  181. $url .= '/'.$filename;
  182. $this->load->library('session');
  183. if ($type == 0) {
  184. $this->session->set_userdata('login_code', $code);
  185. }else {
  186. $this->session->set_userdata('password_code', $code);
  187. }
  188. exit(json_result('0000',$this->response['0000'],array('path'=>base_url($url))));
  189. }
  190. public function model_nav(){
  191. $supId = intval($this->input->post('supId',true));
  192. if (empty($supId)) exit(json_result('0007',$this->response['0007']));
  193. $list = $this->db->query('select id,name from model_info where supId = '.$supId)->result_array();
  194. exit(json_result('0000',$this->response['0000'],['list'=>$list]));
  195. }
  196. public function workmode() {
  197. $version = $this->session->userdata('version');
  198. if (empty($version)) {
  199. $data = (object)array(
  200. 0 => '纯光控、光控开负载、光控关负载',
  201. 1 => '光控开负载,延时1小时后关闭负载',
  202. 2 => '光控开负载,延时2小时后关闭负载',
  203. 3 => '光控开负载,延时3小时后关闭负载',
  204. 4 => '光控开负载,延时4小时后关闭负载',
  205. 5 => '光控开负载,延时5小时后关闭负载',
  206. 6 => '光控开负载,延时6小时后关闭负载',
  207. 7 => '光控开负载,延时7小时后关闭负载',
  208. 8 => '光控开负载,延时8小时后关闭负载',
  209. 9 => '光控开负载,延时9小时后关闭负载',
  210. 10 => '光控开负载,延时10小时后关闭负载',
  211. 11 => '光控开负载,延时11小时后关闭负载',
  212. 12 => '光控开负载,延时12小时后关闭负载',
  213. 13 => '光控开负载,延时13小时后关闭负载',
  214. 14 => '光控开负载,延时14小时后关闭负载',
  215. 15 => '手动模式',
  216. 16 => '调试模式',
  217. 17 => '常开模式'
  218. );
  219. }else{
  220. $data = (object)array(
  221. 0 => 'Pure light control, light control open load, light control load',
  222. 1 => 'Light control open load, delay 1 hours to close the load',
  223. 2 => 'Light control open load, delay 2 hours to close the load',
  224. 3 => 'Light control open load, delay 3 hours to close the load',
  225. 4 => 'Light control open load, delay 4 hours to close the load',
  226. 5 => 'Light control open load, delay 5 hours to close the load',
  227. 6 => 'Light control open load, delay 6 hours to close the load',
  228. 7 => 'Light control open load, delay 7 hours to close the load',
  229. 8 => 'Light control open load, delay 8 hours to close the load',
  230. 9 => 'Light control open load, delay 9 hours to close the load',
  231. 10 => 'Light control open load, delay 10 hours to close the load',
  232. 11 => 'Light control open load, delay 11 hours to close the load',
  233. 12 => 'Light control open load, delay 12 hours to close the load',
  234. 13 => 'Light control open load, delay 13 hours to close the load',
  235. 14 => 'Light control open load, delay 14 hours to close the load',
  236. 15 => 'Manual mode',
  237. 16 => 'Debug mode',
  238. 17 => 'Open mode'
  239. );
  240. }
  241. exit(json_result('0000', $this->response['0000'], $data));
  242. }
  243. }