PolicyController.php 2.4 KB

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