Linkage.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace common\widgets\linkage;
  3. use common\helpers\StringHelper;
  4. use Yii;
  5. use yii\base\Widget;
  6. /**
  7. * Class Linkage
  8. * @package common\widgets\provinces
  9. * @author jianyan74 <751393839@qq.com>
  10. */
  11. class Linkage extends Widget
  12. {
  13. /**
  14. * 一级
  15. *
  16. * @var array
  17. */
  18. public $one = [
  19. 'name' => 'province_id', // 字段名称
  20. 'title' => '请选择省', //默认选择
  21. 'list' => [], // 默认数据
  22. ];
  23. /**
  24. * 二级
  25. *
  26. * @var array
  27. */
  28. public $two = [
  29. 'name' => 'city_id', // 字段名称
  30. 'title' => '请选择市', //默认选择
  31. 'list' => [], // 默认数据
  32. ];
  33. /**
  34. * 二级
  35. *
  36. * @var array
  37. */
  38. public $three = [
  39. 'name' => 'area_id', // 字段名称
  40. 'title' => '请选择区', //默认选择
  41. 'list' => [], // 默认数据
  42. ];
  43. /**
  44. * 四级
  45. *
  46. * @var array
  47. */
  48. public $four = [
  49. 'name' => 'township_id', // 字段名称
  50. 'title' => '请选择乡/镇', //默认选择
  51. 'list' => [], // 默认数据
  52. ];
  53. /**
  54. * 五级
  55. *
  56. * @var array
  57. */
  58. public $five = [
  59. 'name' => 'village_id', // 字段名称
  60. 'title' => '请选择村/社区', //默认选择
  61. 'list' => [], // 默认数据
  62. ];
  63. /**
  64. * 显示类型
  65. *
  66. * long/short
  67. *
  68. * @var string
  69. */
  70. public $template = 'long';
  71. /**
  72. * 关联的ajax url
  73. *
  74. * @var
  75. */
  76. public $url;
  77. /**
  78. * 级别
  79. *
  80. * @var int
  81. */
  82. public $level = 3;
  83. /**
  84. * 模型
  85. *
  86. * @var array
  87. */
  88. public $model;
  89. /**
  90. * 表单
  91. * @var
  92. */
  93. public $form;
  94. /**
  95. * @inheritdoc
  96. */
  97. public function init()
  98. {
  99. parent::init();
  100. empty($this->url) && $this->url = Yii::$app->urlManager->createUrl(['/provinces/child']);
  101. }
  102. /**
  103. * @return string
  104. */
  105. public function run()
  106. {
  107. if ($this->level >= 1 && isset($this->one['list']) && empty($this->one['list'])) {
  108. $this->one['list'] = Yii::$app->services->provinces->getCityMapByPid();
  109. }
  110. if ($this->level >= 2 && isset($this->two['list']) && empty($this->two['list'])) {
  111. $oneName = $this->one['name'];
  112. $this->two['list'] = Yii::$app->services->provinces->getCityMapByPid($this->model->$oneName ?? 0, 2);
  113. }
  114. if ($this->level >= 3 && isset($this->three['list']) && empty($this->three['list'])) {
  115. $twoName = $this->two['name'];
  116. $this->three['list'] = Yii::$app->services->provinces->getCityMapByPid($this->model->$twoName ?? 0, 3);
  117. }
  118. if ($this->level >= 4 && isset($this->four['list']) && empty($this->four['list'])) {
  119. $threeName = $this->three['name'];
  120. $this->four['list'] = Yii::$app->services->provinces->getCityMapByPid($this->model->$threeName ?? 0, 4);
  121. }
  122. if ($this->level >= 5 && isset($this->five['list']) && empty($this->five['list'])) {
  123. $foueName = $this->four['name'];
  124. $this->five['list'] = Yii::$app->services->provinces->getCityMapByPid($this->model->$foueName ?? 0, 5);
  125. }
  126. return $this->render('index', [
  127. 'form' => $this->form,
  128. 'model' => $this->model,
  129. 'random' => StringHelper::random(20),
  130. 'one' => $this->one,
  131. 'two' => $this->two,
  132. 'three' => $this->three,
  133. 'four' => $this->four,
  134. 'five' => $this->five,
  135. 'url' => $this->url,
  136. 'level' => $this->level,
  137. 'template' => $this->template,
  138. ]);
  139. }
  140. }
粤ICP备19079148号