PolicyController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace api\modules\v1\controllers;
  3. use api\common\controllers\Controller;
  4. use common\modules\config\models\Config;
  5. use common\models\Policy;
  6. use yii\helpers\ArrayHelper;
  7. use api\common\behaviors\QueryParamAuth;
  8. use Yii;
  9. use yii\base\Exception;
  10. use yii\data\ActiveDataProvider;
  11. use yii\web\NotFoundHttpException;
  12. class PolicyController extends Controller {
  13. public function behaviors()
  14. {
  15. return ArrayHelper::merge(parent::behaviors(), [
  16. [
  17. 'class' => QueryParamAuth::className(),
  18. 'tokenParam' => 'token',
  19. 'optional' => ['index','category','details']
  20. ]
  21. ]);
  22. }
  23. public function actionIndex()
  24. {
  25. $params = request()->get();
  26. $query = Policy::find()->select('id,title,author,created_at,summary');
  27. //过滤查询的政策
  28. $query->andWhere(new \yii\db\Expression('FIND_IN_SET("'. Policy::TYPE_SEARCH .'", type)'));
  29. if(!empty($params)){
  30. $arr = [];
  31. foreach ($params as $key=>$value) {
  32. if($key == 'page_size' || $key == 'page_no'){
  33. continue;
  34. }
  35. if ($key == 'cengci') {
  36. $query->andWhere(new \yii\db\Expression('FIND_IN_SET("'.$value.'", cengci)'));
  37. continue;
  38. }
  39. if($value){
  40. $expression = 'JSON_CONTAINS(JSON_EXTRACT(`dd_policy`.`filter`, "$.' . $key . '"), JSON_ARRAY("' . $value. '"))';
  41. $query->andWhere(new \yii\db\Expression($expression));
  42. }
  43. }
  44. }
  45. return new ActiveDataProvider([
  46. 'query' => $query,
  47. 'pagination' => [
  48. 'pageParam' => 'page_no',
  49. 'pageSizeParam' => 'page_size',
  50. ],
  51. 'sort' => [
  52. 'defaultOrder' => [
  53. 'sort' => SORT_ASC,
  54. ]
  55. ]
  56. ]);
  57. }
  58. public function actionCategory()
  59. {
  60. $lists = Config::find()->select(['name','value','extra','description','type'])->where(['group' => 'policy'])->asArray()->all();
  61. foreach ($lists as &$value) {
  62. $arr = [];
  63. $val = explode("\r\n",$value['value']);
  64. $extra = explode("\r\n",$value['extra']);
  65. foreach($extra as $k => $v){
  66. $arr[] = [
  67. 'name'=>$v,
  68. 'value'=>$val[$k] ?: (string)($k + 1)
  69. ];
  70. }
  71. $value['data'] = $arr;
  72. unset($value['extra'],$value['value'],$value['type']);
  73. }
  74. return ['data' => $lists];
  75. }
  76. public function actionDetails($id)
  77. {
  78. $model = Policy::find()->where(['id' => $id])->select('id,content,author,created_at,title')->one();
  79. return ['data' => $model];
  80. }
  81. }