海南旅游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.

74 lines
2.3 KiB

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