Common.php 9.9 KB

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