DataList.php 17 KB

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