海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Models\Supplier;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\DB;
  6. /**
  7. * 仅用于测试
  8. * Class TController
  9. * @package App\Http\Controllers\Api
  10. */
  11. class TestController
  12. {
  13. public function index()
  14. {
  15. dd(Supplier::class);
  16. }
  17. /**
  18. * 模拟登录
  19. * @param $user_id
  20. * @return string
  21. */
  22. private function login($user_id)
  23. {
  24. $token_key = md5($user_id . env('APP_KEY'));
  25. Cache::put($token_key, $user_id);
  26. return $token_key;
  27. }
  28. public function index2()
  29. {
  30. $handle = fopen(base_path('area.txt'), 'r');
  31. if ($handle) {
  32. DB::statement('TRUNCATE `areas`;');
  33. while (($line = fgets($handle, 4096)) !== false) {
  34. $line = trim($line);
  35. $last2 = substr($line, 4, 2); //区划码后2位
  36. $front2 = substr($line, 0, 2); //区划码前2位
  37. $front4 = substr($line, 0, 4); //区划码前4位
  38. //判断是否是直辖市,是直辖市则再插入一次上条记录作为二级联动
  39. if (isset($last4, $arr, $parent_arr[$front2]) && $last4 == '0000' && $last2 != '00') {
  40. $insert_id = DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => $parent_arr[$front2], 'name' => $arr[1]]);
  41. $parent_arr[$front4] = $insert_id;
  42. //对直辖市不做省直辖县处理,如:重庆市
  43. $parent_arr[$front2] = $insert_id;
  44. }
  45. $last4 = substr($line, 2, 4); //区划码后4位
  46. $arr = preg_split('/\s+/', $line);
  47. if (count($arr) !== 2) continue;
  48. if ($last4 == '0000') {
  49. $insert_id = DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => 0, 'name' => $arr[1]]);
  50. $parent_arr[$front2] = $insert_id;
  51. } else if ($last2 == '00') {
  52. $insert_id = DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => $parent_arr[$front2], 'name' => $arr[1]]);
  53. $parent_arr[$front4] = $insert_id;
  54. } else {
  55. //考虑到省直辖县级市情况,如:海南琼海市、湖北仙桃市等,但重庆市的省辖县除外(已在上面判断直辖市逻辑中做处理)
  56. $parent_id = $parent_arr[$front4] ?? $parent_arr[$front2];
  57. DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => $parent_id, 'name' => $arr[1]]);
  58. }
  59. }
  60. } else {
  61. return 'open file fail!';
  62. }
  63. return 'success';
  64. }}