DataList.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. namespace common\libs\dataList;
  3. use backendApi\modules\v1\models\Admin;
  4. use backendApi\modules\v1\models\AdminRole;
  5. use common\components\Model;
  6. use common\helpers\Tool;
  7. use yii\base\Exception;
  8. /**
  9. * Class DataList
  10. * @package common\libs\dataList
  11. * @method getListName
  12. * 列表名称
  13. * @method dataHandle
  14. * 需要子类继承的方法实现数据的获取
  15. * @method getColumn
  16. * 需要子类继承的方法实现column的获取
  17. * 传null是指:这种传输方式主要是用于索引,因为过滤后的字段可能没有这种ID,但是一些功能的操作还需要用这种ID去关联,例如前台会员列表中的勾选批量状态管理,这里需要的就是USER_ID
  18. * 例如:
  19. * $this->columns = [
  20. * 'USER_ID' => null,
  21. * 'USER_NAME' => [
  22. * 'header' => '会员编号',
  23. * 'headerOther' => ['width' => '150'],
  24. * ],
  25. * 'IS_DEC' => [
  26. * 'header' => '是否报单中心',
  27. * 'value' => function($row) {
  28. * return (new YesNo([
  29. * 'value' => $row['IS_DEC'],
  30. * ]))->result();
  31. * },
  32. * 'show' => function($row) {
  33. * return '<div>列表展示的值结果</div>';
  34. * }
  35. * 'headerOther' => function($row) {
  36. * return [
  37. * 'width' => '120',
  38. * 'tag'=>['type'=>$row['IS_DEC'] ? 'success' : 'info', 'size' => 'small']
  39. * ];
  40. * },
  41. * ],
  42. * ]
  43. * @method getFilterTypes
  44. */
  45. class DataList extends Model
  46. {
  47. /**
  48. * 导出
  49. * @var bool
  50. */
  51. public $isExport = false;
  52. /**
  53. * 列表当前页码
  54. * @var null
  55. */
  56. protected $page = null;
  57. /**
  58. * 列表每页数量
  59. * @var int
  60. */
  61. protected $pageSize = 0;
  62. /**
  63. * 列表查询条件
  64. * @var string
  65. */
  66. protected $condition = '';
  67. /**
  68. * 列表查询条件参数
  69. * @var array
  70. */
  71. protected $params = [];
  72. /**
  73. * 列表查询其他参数
  74. * @var array
  75. */
  76. protected $others = [];
  77. /**
  78. * 列表数据
  79. * @var
  80. */
  81. protected $listData;
  82. /**
  83. * 列表字段限制条件
  84. * @var
  85. */
  86. protected $columns;
  87. /**
  88. * 用于筛选的字段
  89. * @var
  90. */
  91. protected $filterTypes;
  92. /**
  93. * 主要是给导出时候的表头使用
  94. * @var array
  95. */
  96. protected $headers = [];
  97. /**
  98. * 主要是给页面输出是展示用的就是columns去掉value的结果,让vue方便循环表头和表头样式
  99. * @var array
  100. */
  101. protected $columnsShow = [];
  102. /**
  103. * 主要是给前端的筛选类型
  104. * @var array
  105. */
  106. protected $filterTypesShow = [];
  107. /**
  108. * 获取列表
  109. * @param null $params
  110. * @return mixed
  111. * @throws Exception
  112. */
  113. public function getList($params = null){
  114. if($params !== null) {
  115. $this->prepare($params);
  116. }
  117. // 获取数据
  118. $this->dataHandle();
  119. // 获取列信息
  120. $this->getColumn();
  121. // 获取筛选字段的信息,用于前端筛选
  122. if(!$this->isExport){
  123. $this->getFilterTypes();
  124. }
  125. // 利用权限处理掉不需要的字段(只需要从$this->columns中去掉就可以)
  126. $userId = isset($params['userId']) && $params['userId'] ? $params['userId'] : null;
  127. $this->permissionColumn($userId);
  128. // 处理数据
  129. $this->_formatDataByColumn();
  130. return $this->listData;
  131. }
  132. /**
  133. * 获取导出用到的列标题
  134. * @param $userId
  135. * @return array
  136. * @throws Exception
  137. */
  138. public function getExportHeaders($userId){
  139. if(!$this->headers && $this->isExport){
  140. $this->getColumn();
  141. // 利用权限处理掉不需要的字段(只需要从$this->columns中去掉就可以)
  142. $this->permissionColumn($userId);
  143. foreach($this->columns as $hk => $column){
  144. if(is_string($column)){
  145. $this->headers[] = Tool::textConvert($column);
  146. } elseif(is_array($column)) {
  147. if(!isset($column['header'])){
  148. throw new Exception('column数组中必须存在header');
  149. }
  150. if(isset($column['hidden'])&&$column['hidden']) continue;
  151. if(is_callable($column['header'])){
  152. $header = $column['header']([]);
  153. } else {
  154. $header = $column['header'];
  155. }
  156. $this->headers[] = Tool::textConvert($header);
  157. } elseif (is_callable($column)) {
  158. $this->headers[] = Tool::textConvert($column([]));
  159. }
  160. }
  161. }
  162. return $this->headers;
  163. }
  164. /**
  165. * 获取全部列及列名,主要用于后台权限的筛选
  166. * @return array
  167. * @throws Exception
  168. */
  169. public function getAllColumnHeaderName(){
  170. $result = [];
  171. foreach($this->getColumn() as $hk => $column){
  172. if(is_string($column)){
  173. $result[] = [
  174. 'header' => $column,
  175. 'index' => $hk,
  176. ];
  177. } elseif(is_array($column)) {
  178. if(!isset($column['header'])){
  179. throw new Exception('column数组中必须存在header');
  180. }
  181. //if(isset($column['hidden'])&&$column['hidden']) continue;
  182. if(is_callable($column['header'])){
  183. $header = $column['header']([]);
  184. } else {
  185. $header = $column['header'];
  186. }
  187. $result[] = [
  188. 'header' => $header,
  189. 'index' => $hk,
  190. ];
  191. } elseif (is_callable($column)) {
  192. $result[] = [
  193. 'header' => $column([]),
  194. 'index' => $hk,
  195. ];
  196. }
  197. }
  198. return $result;
  199. }
  200. /**
  201. * 整理参数
  202. * @param $params
  203. */
  204. protected function prepare($params){
  205. $default = [
  206. 'condition' => '',
  207. 'params' => [],
  208. 'others' => [],
  209. 'page' => null,
  210. 'pageSize' => 0,
  211. ];
  212. $params = Tool::deepParse($params ,$default);
  213. $this->condition = $params['condition'];
  214. $this->params = $params['params'];
  215. $this->others = $params['others'];
  216. $this->page = $params['page'];
  217. $this->pageSize = $params['pageSize'];
  218. }
  219. /**
  220. * 格式化输出的数据
  221. * @throws Exception
  222. */
  223. private function _formatDataByColumn(){
  224. $data = [];
  225. foreach($this->listData['list'] as $key=>$row){
  226. foreach($this->columns as $hk => $column){
  227. if(is_string($column)){
  228. if($key == 0){
  229. $this->headers[] = $this->isExport ? Tool::textConvert($column) : $column;
  230. $this->columnsShow[] = [
  231. 'header' => $column,
  232. 'index' => $hk,
  233. 'other' => [],
  234. ];
  235. if(isset($this->filterTypes[$hk])){
  236. $this->filterTypesShow[$hk] = $this->filterTypes[$hk];
  237. }
  238. }
  239. if($this->isExport){
  240. $data[$key][$hk] = $row[$hk];
  241. } else {
  242. $data[$key][$hk] = [
  243. 'value' => $row[$hk],
  244. 'other' => [],
  245. ];
  246. }
  247. } elseif(is_array($column)) {
  248. if(!isset($column['header'])){
  249. //throw new Exception('column数组中必须存在header');
  250. $this->addError('header', \Yii::t('ctx', 'columnArrayMustExistHeader')); // 提交场景不存在
  251. return null;
  252. }
  253. if($key == 0){
  254. if(!isset($column['hidden']) || !$column['hidden']){
  255. if(is_callable($column['header'])){
  256. $header = $column['header']($row);
  257. } else {
  258. $header = $column['header'];
  259. }
  260. if(isset($column['headerOther'])){
  261. if(is_callable($column['headerOther'])){
  262. $headerOther = $column['headerOther']($row);
  263. } else {
  264. $headerOther = $column['headerOther'];
  265. }
  266. } else {
  267. $headerOther = [];
  268. }
  269. $this->headers[] = $this->isExport ? Tool::textConvert($header) : $header;
  270. $this->columnsShow[] = [
  271. 'header' => $header,
  272. 'index' => $hk,
  273. 'other' => $headerOther,
  274. ];
  275. }
  276. if(isset($this->filterTypes[$hk])){
  277. $this->filterTypesShow[$hk] = $this->filterTypes[$hk];
  278. }
  279. }
  280. if(isset($column['hidden'])&&$column['hidden']) continue;
  281. // 如果是回调函数,则直接调用回调函数,否则直接赋值
  282. $tempValue = null;
  283. if(!isset($column['value'])){
  284. $tempValue = $row[$hk];
  285. } else {
  286. if(is_callable($column['value'])){
  287. $tempValue = $column['value']($row);
  288. } else {
  289. $tempValue = $column['value'];
  290. }
  291. }
  292. if(!$this->isExport && isset($column['showValue'])){
  293. if(is_callable($column['showValue'])){
  294. $tempValue = $column['showValue']($row);
  295. } else {
  296. $tempValue = $column['showValue'];
  297. }
  298. }
  299. if(!isset($column['valueOther'])){
  300. $tempListValueOther = [];
  301. } else {
  302. if(is_callable($column['valueOther'])){
  303. $tempListValueOther = $column['valueOther']($row);
  304. } else {
  305. $tempListValueOther = $column['valueOther'];
  306. }
  307. }
  308. if($this->isExport){
  309. $data[$key][$hk] = $tempValue;
  310. } else {
  311. $data[$key][$hk] = [
  312. 'value' => $tempValue,
  313. 'other' => $tempListValueOther,
  314. ];
  315. }
  316. } elseif (is_callable($column)) {
  317. if($key == 0){
  318. $this->headers[] = $this->isExport ? Tool::textConvert($column($row)) : $column($row);
  319. $this->columnsShow[] = [
  320. 'header' => $column($row),
  321. 'index' => $hk,
  322. 'other' => [],
  323. ];
  324. if(isset($this->filterTypes[$hk])){
  325. $this->filterTypesShow[$hk] = $this->filterTypes[$hk];
  326. }
  327. }
  328. if($this->isExport){
  329. $data[$key][$hk] = $row[$hk];
  330. } else {
  331. $data[$key][$hk] = [
  332. 'value' => $row[$hk],
  333. 'other' => [],
  334. ];
  335. }
  336. } elseif ($column === null){
  337. // 这种直接加入到list中,但是header和columns里面没有
  338. if(!$this->isExport){
  339. $data[$key][$hk] = $row[$hk];
  340. }
  341. }
  342. }
  343. }
  344. $this->listData['listName'] = $this->getListName();
  345. $this->listData['list'] = $data;
  346. if($this->isExport){
  347. $this->listData['headers'] = $this->headers;
  348. } else {
  349. if(empty($this->listData['list'])){
  350. $this->getShowHeaders();
  351. }
  352. $this->listData['columnsShow'] = $this->columnsShow;
  353. $this->listData['filterTypes'] = $this->filterTypesShow;
  354. }
  355. unset($data);
  356. }
  357. /**
  358. * 列表为空时需要调用此方法获取一下显示给前端的表头
  359. * @throws Exception
  360. */
  361. public function getShowHeaders(){
  362. foreach($this->columns as $hk => $column){
  363. if(is_string($column)){
  364. $this->headers[] = Tool::textConvert($column);
  365. if(isset($this->filterTypes[$hk])){
  366. $this->filterTypesShow[$hk] = $this->filterTypes[$hk];
  367. }
  368. } elseif(is_array($column)) {
  369. if(!isset($column['header'])){
  370. throw new Exception('column数组中必须存在header');
  371. }
  372. if(isset($column['hidden'])&&$column['hidden']) continue;
  373. if(is_callable($column['header'])){
  374. $header = $column['header']([]);
  375. } else {
  376. $header = $column['header'];
  377. }
  378. if(isset($column['headerOther'])){
  379. if(is_callable($column['headerOther'])){
  380. $headerOther = $column['headerOther']([]);
  381. } else {
  382. $headerOther = $column['headerOther'];
  383. }
  384. } else {
  385. $headerOther = [];
  386. }
  387. $this->columnsShow[] = [
  388. 'header' => $header,
  389. 'index' => $hk,
  390. 'other' => $headerOther,
  391. ];
  392. if(isset($this->filterTypes[$hk])){
  393. $this->filterTypesShow[$hk] = $this->filterTypes[$hk];
  394. }
  395. } elseif (is_callable($column)) {
  396. $this->columnsShow[] = [
  397. 'header' => $column([]),
  398. 'index' => $hk,
  399. 'other' => [],
  400. ];
  401. if(isset($this->filterTypes[$hk])){
  402. $this->filterTypesShow[$hk] = $this->filterTypes[$hk];
  403. }
  404. }
  405. }
  406. }
  407. /**
  408. * 按照权限处理哪些字段显示哪些字段不显示
  409. * @param null $userId
  410. * @throws Exception
  411. */
  412. protected function permissionColumn($userId = null){
  413. return ;
  414. $allRole = AdminRole::getFromCache();
  415. if($userId === null){
  416. $roleId = \Yii::$app->user->userInfo['roleId'];
  417. } else {
  418. $admin = Admin::findOneAsArray('ID=:ID', [':ID'=>$userId]);
  419. if(!$admin){
  420. //throw new Exception('管理员不存在');
  421. $this->addError('admin', \Yii::t('ctx', 'adminUserDoesNotExist')); // 提交场景不存在
  422. return null;
  423. }
  424. $roleId = $admin['ROLE_ID'];
  425. if(!$roleId){
  426. //throw new Exception('管理组不存在');
  427. $this->addError('roleid', \Yii::t('ctx', 'adminUserGroupDoesNotExist')); // 提交场景不存在
  428. return null;
  429. }
  430. }
  431. // 如果是超级管理员直接返回不做处理
  432. if($roleId == \Yii::$app->params['superAdminRoleId']){
  433. return ;
  434. }
  435. $role = isset($allRole[$roleId]) ? $allRole[$roleId] : null;
  436. if($role){
  437. $className = get_class($this);
  438. $listModuleArr = explode('\\', $className);
  439. $listModuleArrCount = count($listModuleArr);
  440. $listModule = $listModuleArr[$listModuleArrCount-2].'/'.$listModuleArr[$listModuleArrCount-1];
  441. // 把列里面没有的内容过滤掉
  442. if($this->columns){
  443. foreach($this->columns as $key=>$column){
  444. if($column !== null){
  445. if(!in_array($key, $role['COLUMN_PERMISSION'][$listModule])){
  446. unset($this->columns[$key]);
  447. // 把筛选里面没有的内容也过滤掉
  448. if(isset($this->filterTypes[$key])) unset($this->filterTypes[$key]);
  449. }
  450. }
  451. }
  452. }
  453. } else {
  454. $this->columns = [];
  455. }
  456. }
  457. }