Common.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. public function login() {
  15. $username = $this->input->post('username',true);
  16. $password = $this->input->post('password',true);
  17. $client_key = $this->input->post('client_key',true);
  18. $role = $this->input->post('role',true);
  19. $code = $this->input->post('code',true);
  20. $os = intval($this->input->post('os',true));
  21. if(empty($client_key)) exit(json_result('0003', $this->response['0003']));
  22. if(empty($username) || empty($password)) exit(json_result('0100', $this->response['0100']));
  23. $password = md5($password);
  24. if(empty($os) && empty($code)) exit(json_result('0105', $this->response['0105']));
  25. if (empty($os) && !isset($_SESSION['login_code'])) exit(json_result('0106',$this->response['0106']));
  26. if (empty($os) && $_SESSION['login_code'] != $code) exit(json_result('0107',$this->response['0107']));
  27. $user = $this->User_model->get_one(array('username'=>$username,'role'=>$role));
  28. if(!empty($user)) {
  29. if ($user['password'] != $password) {
  30. exit(json_result('0101', $this->response['0101']));
  31. }
  32. $data = array(
  33. 'token' => generate_token($username, $password, $client_key),
  34. 'username' => $user['username'],
  35. 'id' => $user['id'],
  36. 'role' => intval($user['role']),
  37. 'name' => $user['name'],
  38. 'privilege' => $user['privilege'],
  39. 'avatar' => empty($user['avatar']) ? '' : base_url($user['avatar'])
  40. );
  41. $this->userinfo = $user;
  42. exit(json_result('0000', $this->response['0000'], $data));
  43. } else {
  44. exit(json_result('0102', $this->response['0102']));
  45. }
  46. }
  47. // 图片上传
  48. public function update_file(){
  49. $type = intval($this->input->post("fileType",true));
  50. $path = '../upload/image';
  51. $config['file_name'] = md5(uniqid()); // 设置图片名字
  52. if (!file_exists($path)) {
  53. mkdir($path);
  54. }
  55. $config['upload_path'] = $path.'/'; // 设置图片上传路径
  56. $config['allowed_types'] = '*'; // 设置图片上传格式
  57. $config['max_size'] = 10240; // 设置文件上传大小
  58. $this->load->library('upload', $config);
  59. if ( ! $this->upload->do_upload('file'))
  60. {
  61. $error = array('error' => $this->upload->display_errors('',''));
  62. // if (empty($this->version)) {
  63. // $data = array('error'=>transfer_error_tips($error['error']));
  64. // }else{
  65. $data = array('error'=>$error['error']);
  66. // }
  67. exit(json_result('0012',$this->response['0012'],$data));
  68. }
  69. else
  70. {
  71. $data = $this->upload->data();
  72. $imagePath = '/upload/image/'.$data['file_name'];
  73. exit(json_result('0000',$this->response['0000'],array('path'=>base_url($imagePath))));
  74. }
  75. }
  76. public function get_country() {
  77. $this->load->model('Global_location_model');
  78. $version = $this->session->userdata('version');
  79. if (empty($version)) {
  80. $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');
  81. }else{
  82. $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');
  83. }
  84. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  85. }
  86. public function get_province() {
  87. $country_id = intval($this->input->post('countryId',true));
  88. $country_id = 92;
  89. $this->load->model('Global_location_model');
  90. $version = $this->session->userdata('version');
  91. if (empty($country_id)) {
  92. exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
  93. }else{
  94. $where = ['level'=>2, 'pid'=>$country_id];
  95. }
  96. if (empty($version)) {
  97. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  98. }else{
  99. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  100. }
  101. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  102. }
  103. public function get_city() {
  104. $province_id = intval($this->input->post('provinceId',true));
  105. $this->load->model('Global_location_model');
  106. $version = $this->session->userdata('version');
  107. if (empty($province_id)) {
  108. exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
  109. }else{
  110. $where = ['level'=>3, 'pid'=>$province_id];
  111. }
  112. if (empty($version)) {
  113. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  114. }else{
  115. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  116. }
  117. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  118. }
  119. public function get_area() {
  120. $cityId = intval($this->input->post('cityId',true));
  121. $this->load->model('Global_location_model');
  122. $version = $this->session->userdata('version');
  123. if (empty($cityId)) {
  124. exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
  125. }else{
  126. $where = ['level'=>4, 'pid'=>$cityId];
  127. }
  128. if (empty($version)) {
  129. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  130. }else{
  131. $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
  132. }
  133. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  134. }
  135. public function get_code_image(){
  136. $type = intval($this->input->post('type',true));
  137. $this->load->helper('captcha');
  138. if (!file_exists('../upload')) mkdir('../upload');
  139. if (!file_exists('../upload/image')) mkdir('../upload/image');
  140. if (!file_exists('../upload/image/code')) mkdir('../upload/image/code');
  141. $url = '/upload/image/code';
  142. $code = rand(1000,9999);
  143. $vals = array(
  144. 'word' => $code,
  145. 'img_path' => '../upload/image/code/',
  146. 'img_url' => $url,
  147. 'img_width' => 120,
  148. 'font_path' => '../file/font/PingFangRegular.ttf',
  149. 'img_height' => 36,
  150. 'expiration' => 600,
  151. 'word_length' => 4,
  152. 'font_size' => 20,
  153. 'img_id' => 'Imageid',
  154. );
  155. $cap = create_captcha($vals);
  156. $filename = $cap['filename'];
  157. $url .= '/'.$filename;
  158. $this->load->library('session');
  159. if ($type == 0) {
  160. $this->session->set_userdata('login_code', $code);
  161. }else {
  162. $this->session->set_userdata('password_code', $code);
  163. }
  164. exit(json_result('0000',$this->response['0000'],array('path'=>base_url($url))));
  165. }
  166. public function workmode() {
  167. $version = $this->session->userdata('version');
  168. if (empty($version)) {
  169. $data = (object)array(
  170. 0 => '纯光控、光控开负载、光控关负载',
  171. 1 => '光控开负载,延时1小时后关闭负载',
  172. 2 => '光控开负载,延时2小时后关闭负载',
  173. 3 => '光控开负载,延时3小时后关闭负载',
  174. 4 => '光控开负载,延时4小时后关闭负载',
  175. 5 => '光控开负载,延时5小时后关闭负载',
  176. 6 => '光控开负载,延时6小时后关闭负载',
  177. 7 => '光控开负载,延时7小时后关闭负载',
  178. 8 => '光控开负载,延时8小时后关闭负载',
  179. 9 => '光控开负载,延时9小时后关闭负载',
  180. 10 => '光控开负载,延时10小时后关闭负载',
  181. 11 => '光控开负载,延时11小时后关闭负载',
  182. 12 => '光控开负载,延时12小时后关闭负载',
  183. 13 => '光控开负载,延时13小时后关闭负载',
  184. 14 => '光控开负载,延时14小时后关闭负载',
  185. 15 => '手动模式',
  186. 16 => '调试模式',
  187. 17 => '常开模式'
  188. );
  189. }else{
  190. $data = (object)array(
  191. 0 => 'Pure light control, light control open load, light control load',
  192. 1 => 'Light control open load, delay 1 hours to close the load',
  193. 2 => 'Light control open load, delay 2 hours to close the load',
  194. 3 => 'Light control open load, delay 3 hours to close the load',
  195. 4 => 'Light control open load, delay 4 hours to close the load',
  196. 5 => 'Light control open load, delay 5 hours to close the load',
  197. 6 => 'Light control open load, delay 6 hours to close the load',
  198. 7 => 'Light control open load, delay 7 hours to close the load',
  199. 8 => 'Light control open load, delay 8 hours to close the load',
  200. 9 => 'Light control open load, delay 9 hours to close the load',
  201. 10 => 'Light control open load, delay 10 hours to close the load',
  202. 11 => 'Light control open load, delay 11 hours to close the load',
  203. 12 => 'Light control open load, delay 12 hours to close the load',
  204. 13 => 'Light control open load, delay 13 hours to close the load',
  205. 14 => 'Light control open load, delay 14 hours to close the load',
  206. 15 => 'Manual mode',
  207. 16 => 'Debug mode',
  208. 17 => 'Open mode'
  209. );
  210. }
  211. exit(json_result('0000', $this->response['0000'], $data));
  212. }
  213. // 批量导入
  214. function test(){
  215. $indexArr = ['D'=>'area','F'=>'shen','G'=>'city'];
  216. // $config['file'] = md5(uniqid()); // 设置文件名字
  217. // $path = '../upload/file';
  218. // if (!file_exists($path)) {
  219. // mkdir($path);
  220. // }
  221. // $config['upload_path'] = $path.'/'; // 设置文件上传路径
  222. // $config['file_name'] = date('YmdHis',time()); // 设置文件上传路径
  223. // $config['allowed_types'] = 'xls|xlsx'; // 设置文件上传格式
  224. // $config['max_size'] = 1024*2; // 设置文件上传大小
  225. // $this->load->library('upload', $config);
  226. // if ( ! $this->upload->do_upload('file')){
  227. // // 文件上传失败
  228. // $error = array('error' => $this->upload->display_errors('',''));
  229. // if (empty($this->version)) {
  230. // exit(json_result('0012',$this->response['0012'],array('error'=>transfer_error_tips($error['error']))));
  231. // }elseif ($this->version == 2) {
  232. // exit(json_result('0012',$this->response['0012'],array('error'=>transfer_error_tips_ru($error['error']))));
  233. // }else{
  234. // exit(json_result('0012',$this->response['0012'],array('error'=>$error['error'])));
  235. // }
  236. // }
  237. // else{
  238. // 文件上传成功
  239. // $data = $this->upload->data();
  240. // $filePath = $data['full_path'];
  241. $filePath = '../upload/test.xls';
  242. $this->load->library('phpExcel/PHPExcel');
  243. $reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
  244. $PHPExcel = $reader->load($filePath); // 载入excel文件
  245. $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
  246. $highestRow = $sheet->getHighestRow(); // 取得总行数
  247. // $highestColumm = $sheet->getHighestColumn(); // 取得总列数
  248. /** 循环读取每个单元格的数据 */
  249. $k = 0;
  250. $data = array();
  251. for ($row = 1; $row <= $highestRow; $row++) //行号从1开始
  252. {
  253. if ($row < 2) continue; // 跳过标题行
  254. foreach ($indexArr as $key => $value) {
  255. $res = $sheet->getCell($key.$row)->getValue();
  256. if (is_object($res)) {
  257. $res= $res->__toString();
  258. }
  259. $data[$k][$value] = $res;
  260. }
  261. $k ++;
  262. }
  263. // var_dump($data);
  264. $temp = array();
  265. foreach ($data as $key => $value) {
  266. if (!isset($temp[$value['area']])) $temp[$value['area']] = $value;
  267. }
  268. // $temp = array_unique(array_column($data, 'shen'));
  269. foreach ($temp as $key => $value) {
  270. $res = $this->db->query('select id from global_location where english_name = "'.$value['city'].'" and level = 3')->row_array();
  271. if (!empty($res) && !empty($res['id'])){
  272. $res2 = $this->db->query('select id from global_location where english_name = "'.$value['area'].'" and level = 4')->row_array();
  273. if (!empty($res2) && !empty($res2['id'])) continue;
  274. $data = array('level'=>4,'english_name'=>$value['area'],'pid'=>$res['id'],'timezone'=>6);
  275. $this->db->insert('global_location', $data);
  276. }
  277. }
  278. var_dump($temp);
  279. // 批量写入数据
  280. // return $data;
  281. // }
  282. }
  283. }