Common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. }
  11. public function login() {
  12. $username = $this->input->post('username',true);
  13. $password = $this->input->post('password',true);
  14. $client_key = $this->input->post('client_key',true);
  15. $code = $this->input->post('code',true);
  16. $os = intval($this->input->post('os',true));
  17. if(empty($client_key)) exit(json_result('0003', $this->response['0003']));
  18. if(empty($username) || empty($password)) exit(json_result('0100', $this->response['0100']));
  19. $password = md5($password);
  20. $user = $this->User_model->get_one(array('username'=>$username));
  21. if(!empty($user)) {
  22. if ($user['password'] != $password) {
  23. exit(json_result('0101', $this->response['0101']));
  24. }
  25. $data = array(
  26. 'token' => generate_token($username, $password, $client_key),
  27. 'username' => $user['username'],
  28. 'id' => $user['id'],
  29. 'role' => intval($user['role']),
  30. 'name' => $user['name'],
  31. );
  32. $this->userinfo = $user;
  33. exit(json_result('0000', $this->response['0000'], $data));
  34. } else {
  35. exit(json_result('0102', $this->response['0102']));
  36. }
  37. }
  38. // 图片上传
  39. public function update_file(){
  40. $type = intval($this->input->post("fileType",true));
  41. if ($type == 0) {
  42. $path = '../upload/image';
  43. $config['file_name'] = md5(uniqid()); // 设置图片名字
  44. }else{
  45. $config['overwrite'] = true;
  46. $filename = trim($_FILES['file']['name']);
  47. if (strpos($filename, 'LampControl_') !== false) {
  48. $path = '../upload/firewalld';
  49. $config['file_name'] = $filename;
  50. }else{
  51. $path = '../upload/firewalld';
  52. $config['file_name'] = 'openwrt-'.md5(uniqid());
  53. }
  54. }
  55. if (!file_exists($path)) {
  56. mkdir($path);
  57. }
  58. $config['upload_path'] = $path.'/'; // 设置图片上传路径
  59. $config['allowed_types'] = '*'; // 设置图片上传格式
  60. $config['max_size'] = 10240; // 设置文件上传大小
  61. $this->load->library('upload', $config);
  62. if ( ! $this->upload->do_upload('file'))
  63. {
  64. $error = array('error' => $this->upload->display_errors('',''));
  65. // if (empty($this->version)) {
  66. // $data = array('error'=>transfer_error_tips($error['error']));
  67. // }else{
  68. $data = array('error'=>$error['error']);
  69. // }
  70. exit(json_result('0012',$this->response['0012'],$data));
  71. }
  72. else
  73. {
  74. $data = $this->upload->data();
  75. if ($type == 0) {
  76. $imagePath = '/upload/image/'.$data['file_name'];
  77. }else{
  78. $imagePath = '/upload/firewalld/'.$data['file_name'];
  79. }
  80. exit(json_result('0000',$this->response['0000'],array('path'=>base_url($imagePath))));
  81. }
  82. }
  83. public function get_country() {
  84. $this->load->model('Global_location_model');
  85. $version = $this->session->userdata('version');
  86. if (empty($version)) {
  87. $data = $this->Global_location_model->get_list(['level'=>1,'id'=>92], 'id, chinese_name as name',null,null,'convert(chinese_name using gbk) ASC,id DESC');
  88. }else{
  89. $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');
  90. }
  91. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  92. }
  93. public function get_province() {
  94. $country_id = intval($this->input->post('countryId',true));
  95. $country_id = 92;
  96. $this->load->model('Global_location_model');
  97. $version = $this->session->userdata('version');
  98. if (empty($country_id)) {
  99. exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
  100. }else{
  101. $where = ['level'=>2, 'pid'=>$country_id];
  102. }
  103. if (empty($version)) {
  104. $data = $this->Global_location_model->get_list($where, 'id, chinese_name as name',null,null,'convert(chinese_name using gbk) ASC,id DESC');
  105. }else{
  106. $data = $this->Global_location_model->get_list($where, '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_city() {
  111. $province_id = intval($this->input->post('provinceId',true));
  112. $this->load->model('Global_location_model');
  113. $version = $this->session->userdata('version');
  114. if (empty($province_id)) {
  115. exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
  116. }else{
  117. $where = ['level'=>3, 'pid'=>$province_id];
  118. }
  119. if (empty($version)) {
  120. $data = $this->Global_location_model->get_list($where, 'id, chinese_name as name',null,null,'convert(chinese_name using gbk) ASC,id DESC');
  121. }else{
  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. }
  124. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  125. }
  126. public function get_area() {
  127. $cityId = intval($this->input->post('cityId',true));
  128. $this->load->model('Global_location_model');
  129. $version = $this->session->userdata('version');
  130. if (empty($cityId)) {
  131. exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
  132. }else{
  133. $where = ['level'=>4, 'pid'=>$cityId];
  134. }
  135. if (empty($version)) {
  136. $data = $this->Global_location_model->get_list($where, 'id, chinese_name as name',null,null,'convert(chinese_name using gbk) ASC,id DESC');
  137. }else{
  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. }
  140. exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
  141. }
  142. // 批量导入
  143. function test(){
  144. $indexArr = ['D'=>'area','F'=>'shen','G'=>'city'];
  145. // $config['file'] = md5(uniqid()); // 设置文件名字
  146. // $path = '../upload/file';
  147. // if (!file_exists($path)) {
  148. // mkdir($path);
  149. // }
  150. // $config['upload_path'] = $path.'/'; // 设置文件上传路径
  151. // $config['file_name'] = date('YmdHis',time()); // 设置文件上传路径
  152. // $config['allowed_types'] = 'xls|xlsx'; // 设置文件上传格式
  153. // $config['max_size'] = 1024*2; // 设置文件上传大小
  154. // $this->load->library('upload', $config);
  155. // if ( ! $this->upload->do_upload('file')){
  156. // // 文件上传失败
  157. // $error = array('error' => $this->upload->display_errors('',''));
  158. // if (empty($this->version)) {
  159. // exit(json_result('0012',$this->response['0012'],array('error'=>transfer_error_tips($error['error']))));
  160. // }elseif ($this->version == 2) {
  161. // exit(json_result('0012',$this->response['0012'],array('error'=>transfer_error_tips_ru($error['error']))));
  162. // }else{
  163. // exit(json_result('0012',$this->response['0012'],array('error'=>$error['error'])));
  164. // }
  165. // }
  166. // else{
  167. // 文件上传成功
  168. // $data = $this->upload->data();
  169. // $filePath = $data['full_path'];
  170. $filePath = '../upload/test.xls';
  171. $this->load->library('phpExcel/PHPExcel');
  172. $reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
  173. $PHPExcel = $reader->load($filePath); // 载入excel文件
  174. $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
  175. $highestRow = $sheet->getHighestRow(); // 取得总行数
  176. // $highestColumm = $sheet->getHighestColumn(); // 取得总列数
  177. /** 循环读取每个单元格的数据 */
  178. $k = 0;
  179. $data = array();
  180. for ($row = 1; $row <= $highestRow; $row++) //行号从1开始
  181. {
  182. if ($row < 2) continue; // 跳过标题行
  183. foreach ($indexArr as $key => $value) {
  184. $res = $sheet->getCell($key.$row)->getValue();
  185. if (is_object($res)) {
  186. $res= $res->__toString();
  187. }
  188. $data[$k][$value] = $res;
  189. }
  190. $k ++;
  191. }
  192. // var_dump($data);
  193. $temp = array();
  194. foreach ($data as $key => $value) {
  195. if (!isset($temp[$value['area']])) $temp[$value['area']] = $value;
  196. }
  197. // $temp = array_unique(array_column($data, 'shen'));
  198. foreach ($temp as $key => $value) {
  199. $res = $this->db->query('select id from global_location where english_name = "'.$value['city'].'" and level = 3')->row_array();
  200. if (!empty($res) && !empty($res['id'])){
  201. $res2 = $this->db->query('select id from global_location where english_name = "'.$value['area'].'" and level = 4')->row_array();
  202. if (!empty($res2) && !empty($res2['id'])) continue;
  203. $data = array('level'=>4,'english_name'=>$value['area'],'pid'=>$res['id'],'timezone'=>6);
  204. $this->db->insert('global_location', $data);
  205. }
  206. }
  207. var_dump($temp);
  208. // 批量写入数据
  209. // return $data;
  210. // }
  211. }
  212. }