AuthArea.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace backend\models;
  3. use common\models\Area;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%auth_area}}".
  7. *
  8. * @property string $uid
  9. * @property integer $area_id
  10. * @property string $father_area_id
  11. * @property string $name
  12. */
  13. class AuthArea extends \yii\db\ActiveRecord
  14. {
  15. /**
  16. * @inheritdoc
  17. */
  18. public static function tableName()
  19. {
  20. return '{{%auth_area}}';
  21. }
  22. /**
  23. * @inheritdoc
  24. */
  25. public function rules()
  26. {
  27. return [
  28. [['uid', 'area_id'], 'required'],
  29. [['uid', 'father_area_id'], 'integer'],
  30. [['name'], 'string'],
  31. ];
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function attributeLabels()
  37. {
  38. return [
  39. 'uid' => 'uid',
  40. 'area_id' => 'area_id',
  41. 'father_area_id' => 'father_area_id',
  42. 'name' => 'name',
  43. ];
  44. }
  45. public function settingAuth($uid,$post)
  46. {
  47. //先清除已有权限
  48. if($this::find()->where(['uid'=>$uid])->count() > 0 )
  49. {
  50. $this::deleteAll(['uid'=>$uid]);
  51. }
  52. if(!empty($post['all']))
  53. {
  54. $model = new AuthArea();
  55. $model->uid = $uid;
  56. $model->area_id = 'all';
  57. $model->save();
  58. }elseif (!empty($post['citys']))
  59. {
  60. $datas = array();
  61. foreach ($post['citys'] as $val)
  62. {
  63. $area = Area::queryAdminCityInfo($val);
  64. $datas[] = [$uid,$val,$area['father_area_id'],$area['area']];
  65. }
  66. Yii::$app->db->createCommand()->batchInsert("{{%auth_area}}",['uid','area_id','father_area_id','name'],$datas)->execute();
  67. }
  68. }
  69. /**
  70. * 获取选中城市
  71. */
  72. public function queryCheckbox($uid)
  73. {
  74. $result = ['list'=>array(),'all'=>false];
  75. if($this::find()->where(['uid'=>$uid,'area_id'=>'all'])->count()>0){
  76. $result['all'] = true;
  77. return $result;
  78. }
  79. $list = $this::find()->where(['uid'=>$uid])->select('area_id')->all();
  80. if(!empty($list))
  81. {
  82. foreach ($list as $value)
  83. $result['list'][] =$value->area_id;
  84. }
  85. return $result;
  86. }
  87. /**
  88. * 获取权限
  89. */
  90. static public function queryAuth()
  91. {
  92. if(AuthArea::find()->where(['uid'=>Yii::$app->user->id,'area_id'=>'all'])->count()>0)
  93. return 'all';
  94. $result = "";
  95. $list = AuthArea::find()->where(['uid'=>Yii::$app->user->id])->select('area_id')->all();
  96. if(!empty($list))
  97. {
  98. foreach ($list as $value)
  99. $datas[] =$value->area_id;
  100. $result = implode(",",$datas);
  101. }
  102. return $result;
  103. }
  104. }