PolicyController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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' || $key == 'page_no'){
  33. continue;
  34. }
  35. if($value){
  36. $fd = $key.'_' . $value;
  37. $query->orWhere(new \yii\db\Expression('FIND_IN_SET("'.$fd.'", filter)'));
  38. }
  39. }
  40. }
  41. return new ActiveDataProvider([
  42. 'query' => $query,
  43. 'pagination' => [
  44. 'pageParam' => 'page_no',
  45. 'pageSizeParam' => 'page_size',
  46. ],
  47. 'sort' => [
  48. 'defaultOrder' => [
  49. 'created_at' => SORT_ASC,
  50. ]
  51. ]
  52. ]);
  53. }
  54. public function actionCategory()
  55. {
  56. $lists = Config::find()->select(['name','value','extra','description','type'])->where(['group' => 'policy'])->asArray()->all();
  57. foreach ($lists as &$value) {
  58. $arr = [];
  59. $val = explode("\r\n",$value['value']);
  60. $extra = explode("\r\n",$value['extra']);
  61. foreach($extra as $k => $v){
  62. $arr[] = [
  63. 'name'=>$v,
  64. 'value'=>$val[$k] ?: (string)($k + 1)
  65. ];
  66. }
  67. $value['data'] = $arr;
  68. unset($value['extra'],$value['value'],$value['type']);
  69. }
  70. return ['data' => $lists];
  71. }
  72. public function actionDetails($id)
  73. {
  74. $model = Policy::find()->where(['id' => $id])->select('id,content,author,created_at,title')->one();
  75. return ['data' => $model];
  76. }
  77. }