Common.php 9.7 KB

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