Common.php 10 KB

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