Area.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace common\widgets\area;
  3. use Yii;
  4. use yii\base\Widget;
  5. use common\helpers\StringHelper;
  6. /**
  7. * Class Area
  8. * @package common\widgets\area
  9. * @author jianyan74 <751393839@qq.com>
  10. */
  11. class Area extends Widget
  12. {
  13. /**
  14. * 省字段名
  15. *
  16. * @var
  17. */
  18. public $provincesName = 'province_ids';
  19. /**
  20. * 市字段名
  21. *
  22. * @var
  23. */
  24. public $cityName = 'city_ids';
  25. /**
  26. * 区字段名
  27. *
  28. * @var
  29. */
  30. public $areaName = 'area_ids';
  31. /**
  32. * 不可选市数组
  33. *
  34. * @var
  35. */
  36. public $notChooseProvinceIds = [];
  37. /**
  38. * 不可选区数组
  39. *
  40. * @var
  41. */
  42. public $notChooseCityIds = [];
  43. /**
  44. * 不可选省数组
  45. *
  46. * @var
  47. */
  48. public $notChooseAreaIds = [];
  49. /**
  50. * 级别
  51. *
  52. * @var int
  53. */
  54. public $level = 3;
  55. /**
  56. * 模型
  57. *
  58. * @var array
  59. */
  60. public $model;
  61. /**
  62. * 表单
  63. * @var
  64. */
  65. public $form;
  66. public function init()
  67. {
  68. parent::init();
  69. $this->level = (int)$this->level;
  70. $this->level < 1 && $this->level = 1;
  71. $this->level > 3 && $this->level = 3;
  72. }
  73. /**
  74. * @return string
  75. */
  76. public function run()
  77. {
  78. $provincesName = $this->provincesName;
  79. $cityName = $this->cityName;
  80. $areaName = $this->areaName;
  81. $provinceIds = StringHelper::parseAttr($this->model->$provincesName);
  82. $cityIds = $this->level >= 2 ? StringHelper::parseAttr($this->model->$cityName) : [];
  83. $areaIds = $this->level == 3 ? StringHelper::parseAttr($this->model->$areaName) : [];
  84. // 获取选中数据
  85. $provinceIds = array_merge(array_diff($this->notChooseProvinceIds, $provinceIds));
  86. $cityIds = array_merge(array_diff($this->notChooseCityIds, $cityIds));
  87. $areaIds = array_merge(array_diff($this->notChooseAreaIds, $areaIds));
  88. $addressList = Yii::$app->services->provinces->getAreaTree($provinceIds, $cityIds, $areaIds);
  89. return $this->render('area', [
  90. 'form' => $this->form,
  91. 'model' => $this->model,
  92. 'provincesName' => $provincesName,
  93. 'cityName' => $cityName,
  94. 'areaName' => $areaName,
  95. 'addressList' => $addressList,
  96. 'level' => $this->level,
  97. ]);
  98. }
  99. }
  100. ?>
粤ICP备19079148号