Common.php 9.8 KB

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