Common.php 9.7 KB

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