Common.php 11 KB

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