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

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