DataList.php 16 KB

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