Common.php 11 KB

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