123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Common extends CI_Controller {
-
- var $response;
- var $userinfo;
- var $version;
- public function __construct() {
- parent::__construct();
- $this->load->model('User_model');
- $this->response = $this->config->config['response_en'];
- }
- public function login() {
- $username = $this->input->post('username',true);
- $password = $this->input->post('password',true);
- $client_key = $this->input->post('client_key',true);
- $code = $this->input->post('code',true);
- $os = intval($this->input->post('os',true));
- if(empty($client_key)) exit(json_result('0003', $this->response['0003']));
- if(empty($username) || empty($password)) exit(json_result('0100', $this->response['0100']));
- $password = md5($password);
- $user = $this->User_model->get_one(array('username'=>$username));
- if(!empty($user)) {
- if ($user['password'] != $password) {
- exit(json_result('0101', $this->response['0101']));
- }
- $data = array(
- 'token' => generate_token($username, $password, $client_key),
- 'username' => $user['username'],
- 'id' => $user['id'],
- 'role' => intval($user['role']),
- 'name' => $user['name'],
- );
-
- $this->userinfo = $user;
- exit(json_result('0000', $this->response['0000'], $data));
- } else {
- exit(json_result('0102', $this->response['0102']));
- }
- }
- // 图片上传
- public function update_file(){
- $type = intval($this->input->post("fileType",true));
- if ($type == 0) {
- $path = '../upload/image';
- $config['file_name'] = md5(uniqid()); // 设置图片名字
- }else{
- $config['overwrite'] = true;
- $filename = trim($_FILES['file']['name']);
- if (strpos($filename, 'LampControl_') !== false) {
- $path = '../upload/firewalld';
- $config['file_name'] = $filename;
- }else{
- $path = '../upload/firewalld';
- $config['file_name'] = 'openwrt-'.md5(uniqid());
- }
-
- }
- if (!file_exists($path)) {
- mkdir($path);
- }
- $config['upload_path'] = $path.'/'; // 设置图片上传路径
- $config['allowed_types'] = '*'; // 设置图片上传格式
- $config['max_size'] = 10240; // 设置文件上传大小
-
- $this->load->library('upload', $config);
- if ( ! $this->upload->do_upload('file'))
- {
- $error = array('error' => $this->upload->display_errors('',''));
- // if (empty($this->version)) {
- // $data = array('error'=>transfer_error_tips($error['error']));
- // }else{
- $data = array('error'=>$error['error']);
- // }
- exit(json_result('0012',$this->response['0012'],$data));
- }
- else
- {
- $data = $this->upload->data();
- if ($type == 0) {
- $imagePath = '/upload/image/'.$data['file_name'];
- }else{
- $imagePath = '/upload/firewalld/'.$data['file_name'];
- }
-
- exit(json_result('0000',$this->response['0000'],array('path'=>base_url($imagePath))));
- }
- }
- public function get_country() {
- $this->load->model('Global_location_model');
- $version = $this->session->userdata('version');
- if (empty($version)) {
- $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');
- }else{
- $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');
- }
-
- exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
- }
- public function get_province() {
- $country_id = intval($this->input->post('countryId',true));
- $country_id = 92;
- $this->load->model('Global_location_model');
- $version = $this->session->userdata('version');
- if (empty($country_id)) {
- exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
- }else{
- $where = ['level'=>2, 'pid'=>$country_id];
- }
- if (empty($version)) {
- $data = $this->Global_location_model->get_list($where, 'id, chinese_name as name',null,null,'convert(chinese_name using gbk) ASC,id DESC');
- }else{
- $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
- }
- exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
- }
- public function get_city() {
- $province_id = intval($this->input->post('provinceId',true));
- $this->load->model('Global_location_model');
- $version = $this->session->userdata('version');
- if (empty($province_id)) {
- exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
- }else{
- $where = ['level'=>3, 'pid'=>$province_id];
- }
- if (empty($version)) {
- $data = $this->Global_location_model->get_list($where, 'id, chinese_name as name',null,null,'convert(chinese_name using gbk) ASC,id DESC');
- }else{
- $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
- }
- exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
- }
- public function get_area() {
- $cityId = intval($this->input->post('cityId',true));
- $this->load->model('Global_location_model');
- $version = $this->session->userdata('version');
- if (empty($cityId)) {
- exit(json_result('0000', $this->response['0000'], ['list'=>[]]));
- }else{
- $where = ['level'=>4, 'pid'=>$cityId];
- }
- if (empty($version)) {
- $data = $this->Global_location_model->get_list($where, 'id, chinese_name as name',null,null,'convert(chinese_name using gbk) ASC,id DESC');
- }else{
- $data = $this->Global_location_model->get_list($where, 'id, english_name as name',null,null,'convert(english_name using gbk) ASC,id DESC');
- }
- exit(json_result('0000', $this->response['0000'], ['list'=>$data]));
- }
- // 批量导入
- function test(){
- $indexArr = ['D'=>'area','F'=>'shen','G'=>'city'];
- // $config['file'] = md5(uniqid()); // 设置文件名字
- // $path = '../upload/file';
-
- // if (!file_exists($path)) {
- // mkdir($path);
- // }
- // $config['upload_path'] = $path.'/'; // 设置文件上传路径
- // $config['file_name'] = date('YmdHis',time()); // 设置文件上传路径
- // $config['allowed_types'] = 'xls|xlsx'; // 设置文件上传格式
- // $config['max_size'] = 1024*2; // 设置文件上传大小
-
- // $this->load->library('upload', $config);
- // if ( ! $this->upload->do_upload('file')){
- // // 文件上传失败
- // $error = array('error' => $this->upload->display_errors('',''));
- // if (empty($this->version)) {
- // exit(json_result('0012',$this->response['0012'],array('error'=>transfer_error_tips($error['error']))));
- // }elseif ($this->version == 2) {
- // exit(json_result('0012',$this->response['0012'],array('error'=>transfer_error_tips_ru($error['error']))));
- // }else{
- // exit(json_result('0012',$this->response['0012'],array('error'=>$error['error'])));
- // }
-
- // }
- // else{
- // 文件上传成功
- // $data = $this->upload->data();
- // $filePath = $data['full_path'];
- $filePath = '../upload/test.xls';
- $this->load->library('phpExcel/PHPExcel');
- $reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
- $PHPExcel = $reader->load($filePath); // 载入excel文件
- $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
- $highestRow = $sheet->getHighestRow(); // 取得总行数
- // $highestColumm = $sheet->getHighestColumn(); // 取得总列数
-
- /** 循环读取每个单元格的数据 */
- $k = 0;
- $data = array();
- for ($row = 1; $row <= $highestRow; $row++) //行号从1开始
- {
- if ($row < 2) continue; // 跳过标题行
- foreach ($indexArr as $key => $value) {
- $res = $sheet->getCell($key.$row)->getValue();
- if (is_object($res)) {
- $res= $res->__toString();
- }
- $data[$k][$value] = $res;
- }
- $k ++;
- }
- // var_dump($data);
- $temp = array();
- foreach ($data as $key => $value) {
- if (!isset($temp[$value['area']])) $temp[$value['area']] = $value;
- }
- // $temp = array_unique(array_column($data, 'shen'));
- foreach ($temp as $key => $value) {
- $res = $this->db->query('select id from global_location where english_name = "'.$value['city'].'" and level = 3')->row_array();
- if (!empty($res) && !empty($res['id'])){
- $res2 = $this->db->query('select id from global_location where english_name = "'.$value['area'].'" and level = 4')->row_array();
- if (!empty($res2) && !empty($res2['id'])) continue;
- $data = array('level'=>4,'english_name'=>$value['area'],'pid'=>$res['id'],'timezone'=>6);
- $this->db->insert('global_location', $data);
- }
-
- }
- var_dump($temp);
- // 批量写入数据
- // return $data;
- // }
- }
- }
|