Area.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Shop\Lib;
  3. use Dever;
  4. class Area
  5. {
  6. private $default = array
  7. (
  8. 'value' => -1,
  9. 'name' => '不限',
  10. );
  11. private $search_default = array
  12. (
  13. 'value' => -1,
  14. 'name' => '地区选择',
  15. );
  16. /**
  17. * 获取地区数据
  18. *
  19. * @return mixed
  20. */
  21. public function get_api()
  22. {
  23. # 联动总数,默认到县区
  24. $level_total = Dever::input('level_total', 5);
  25. # 当前联动级别
  26. $level_num = Dever::input('level_num');
  27. # 一般为id
  28. $level_id = Dever::input('level_id');
  29. # 是否是搜索列表页
  30. $level_search = Dever::input('level_search');
  31. if ($level_search) {
  32. $default = $this->search_default;
  33. if ($level_num == 1) {
  34. $default['name'] = '省份选择';
  35. } elseif ($level_num == 2) {
  36. $default['name'] = '城市选择';
  37. } elseif ($level_num == 3) {
  38. $default['name'] = '县区选择';
  39. } elseif ($level_num == 4) {
  40. $default['name'] = '街道选择';
  41. } else {
  42. $default['name'] = '门店选择';
  43. }
  44. } else {
  45. $default = $this->default;
  46. }
  47. if ($level_id < 0) {
  48. Dever::alert('error');
  49. }
  50. # 四级联动
  51. if ($level_num == 1) {
  52. $t = 'area/province';
  53. $data = Dever::db($t)->getAll();
  54. } elseif ($level_num == 2) {
  55. $t = 'area/city';
  56. $data = Dever::db($t)->getAll(array('province_id' => $level_id));
  57. } elseif ($level_num == 3) {
  58. $t = 'area/county';
  59. $data = Dever::db($t)->getAll(array('city_id' => $level_id));
  60. } elseif ($level_num == 4) {
  61. $t = 'area/town';
  62. $data = Dever::db($t)->getAll(array('county_id' => $level_id));
  63. } elseif ($level_num == 5) {
  64. if ($level_id > 0) {
  65. $data = Dever::db('shop/info')->getAll(array('town' => $level_id));
  66. } else {
  67. $data = false;
  68. }
  69. }
  70. if (!$data) {
  71. Dever::alert('error');
  72. }
  73. if ($level_search || $level_num >= 1) {
  74. array_unshift($data, $default);
  75. }
  76. $result['level_total'] = $level_total;
  77. $result['list'] = $data;
  78. return $result;
  79. }
  80. }