DataList.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. LoggerTool::error($this->listData);
  227. foreach($this->listData['list'] as $key=>$row){
  228. foreach($this->columns as $hk => $column){
  229. if(is_string($column)){
  230. if($key == 0){
  231. $this->headers[] = $this->isExport ? Tool::textConvert($column) : $column;
  232. $this->columnsShow[] = [
  233. 'header' => $column,
  234. 'index' => $hk,
  235. 'other' => [],
  236. ];
  237. if(isset($this->filterTypes[$hk])){
  238. $this->filterTypesShow[$hk] = $this->filterTypes[$hk];
  239. }
  240. }
  241. if($this->isExport){
  242. $data[$key][$hk] = $row[$hk];
  243. } else {
  244. $data[$key][$hk] = [
  245. 'value' => $row[$hk],
  246. 'other' => [],
  247. ];
  248. }
  249. } elseif(is_array($column)) {
  250. if(!isset($column['header'])){
  251. //throw new Exception('column数组中必须存在header');
  252. $this->addError('header', \Yii::t('ctx', 'columnArrayMustExistHeader')); // 提交场景不存在
  253. return null;
  254. }
  255. if($key == 0){
  256. if(!isset($column['hidden']) || !$column['hidden']){
  257. if(is_callable($column['header'])){
  258. $header = $column['header']($row);
  259. } else {
  260. $header = $column['header'];
  261. }
  262. if(isset($column['headerOther'])){
  263. if(is_callable($column['headerOther'])){
  264. $headerOther = $column['headerOther']($row);
  265. } else {
  266. $headerOther = $column['headerOther'];
  267. }
  268. } else {
  269. $headerOther = [];
  270. }
  271. $this->headers[] = $this->isExport ? Tool::textConvert($header) : $header;
  272. $this->columnsShow[] = [
  273. 'header' => $header,
  274. 'index' => $hk,
  275. 'other' => $headerOther,
  276. ];
  277. }
  278. if(isset($this->filterTypes[$hk])){
  279. $this->filterTypesShow[$hk] = $this->filterTypes[$hk];
  280. }
  281. }
  282. if(isset($column['hidden'])&&$column['hidden']) continue;
  283. // 如果是回调函数,则直接调用回调函数,否则直接赋值
  284. $tempValue = null;
  285. if(!isset($column['value'])){
  286. $tempValue = $row[$hk];
  287. } else {
  288. if(is_callable($column['value'])){
  289. $tempValue = $column['value']($row);
  290. } else {
  291. $tempValue = $column['value'];
  292. }
  293. }
  294. if(!$this->isExport && isset($column['showValue'])){
  295. if(is_callable($column['showValue'])){
  296. $tempValue = $column['showValue']($row);
  297. } else {
  298. $tempValue = $column['showValue'];
  299. }
  300. }
  301. if(!isset($column['valueOther'])){
  302. $tempListValueOther = [];
  303. } else {
  304. if(is_callable($column['valueOther'])){
  305. $tempListValueOther = $column['valueOther']($row);
  306. } else {
  307. $tempListValueOther = $column['valueOther'];
  308. }
  309. }
  310. if($this->isExport){
  311. $data[$key][$hk] = $tempValue;
  312. } else {
  313. $data[$key][$hk] = [
  314. 'value' => $tempValue,
  315. 'other' => $tempListValueOther,
  316. ];
  317. }
  318. } elseif (is_callable($column)) {
  319. if($key == 0){
  320. $this->headers[] = $this->isExport ? Tool::textConvert($column($row)) : $column($row);
  321. $this->columnsShow[] = [
  322. 'header' => $column($row),
  323. 'index' => $hk,
  324. 'other' => [],
  325. ];
  326. if(isset($this->filterTypes[$hk])){
  327. $this->filterTypesShow[$hk] = $this->filterTypes[$hk];
  328. }
  329. }
  330. if($this->isExport){
  331. $data[$key][$hk] = $row[$hk];
  332. } else {
  333. $data[$key][$hk] = [
  334. 'value' => $row[$hk],
  335. 'other' => [],
  336. ];
  337. }
  338. } elseif ($column === null){
  339. // 这种直接加入到list中,但是header和columns里面没有
  340. if(!$this->isExport){
  341. $data[$key][$hk] = $row[$hk];
  342. }
  343. }
  344. }
  345. }
  346. $this->listData['listName'] = $this->getListName();
  347. $this->listData['list'] = $data;
  348. if($this->isExport){
  349. $this->listData['headers'] = $this->headers;
  350. } else {
  351. if(empty($this->listData['list'])){
  352. $this->getShowHeaders();
  353. }
  354. $this->listData['columnsShow'] = $this->columnsShow;
  355. $this->listData['filterTypes'] = $this->filterTypesShow;
  356. }
  357. unset($data);
  358. }
  359. /**
  360. * 列表为空时需要调用此方法获取一下显示给前端的表头
  361. * @throws Exception
  362. */
  363. public function getShowHeaders(){
  364. foreach($this->columns as $hk => $column){
  365. if(is_string($column)){
  366. $this->headers[] = Tool::textConvert($column);
  367. if(isset($this->filterTypes[$hk])){
  368. $this->filterTypesShow[$hk] = $this->filterTypes[$hk];
  369. }
  370. } elseif(is_array($column)) {
  371. if(!isset($column['header'])){
  372. throw new Exception('column数组中必须存在header');
  373. }
  374. if(isset($column['hidden'])&&$column['hidden']) continue;
  375. if(is_callable($column['header'])){
  376. $header = $column['header']([]);
  377. } else {
  378. $header = $column['header'];
  379. }
  380. if(isset($column['headerOther'])){
  381. if(is_callable($column['headerOther'])){
  382. $headerOther = $column['headerOther']([]);
  383. } else {
  384. $headerOther = $column['headerOther'];
  385. }
  386. } else {
  387. $headerOther = [];
  388. }
  389. $this->columnsShow[] = [
  390. 'header' => $header,
  391. 'index' => $hk,
  392. 'other' => $headerOther,
  393. ];
  394. if(isset($this->filterTypes[$hk])){
  395. $this->filterTypesShow[$hk] = $this->filterTypes[$hk];
  396. }
  397. } elseif (is_callable($column)) {
  398. $this->columnsShow[] = [
  399. 'header' => $column([]),
  400. 'index' => $hk,
  401. 'other' => [],
  402. ];
  403. if(isset($this->filterTypes[$hk])){
  404. $this->filterTypesShow[$hk] = $this->filterTypes[$hk];
  405. }
  406. }
  407. }
  408. }
  409. /**
  410. * 按照权限处理哪些字段显示哪些字段不显示
  411. * @param null $userId
  412. * @throws Exception
  413. */
  414. protected function permissionColumn($userId = null){
  415. return;
  416. $allRole = AdminRole::getFromCache();
  417. if($userId === null){
  418. $roleId = \Yii::$app->user->userInfo['roleId'];
  419. } else {
  420. $admin = Admin::findOneAsArray('ID=:ID', [':ID'=>$userId]);
  421. if(!$admin){
  422. //throw new Exception('管理员不存在');
  423. $this->addError('admin', \Yii::t('ctx', 'adminUserDoesNotExist')); // 提交场景不存在
  424. return null;
  425. }
  426. $roleId = $admin['ROLE_ID'];
  427. if(!$roleId){
  428. //throw new Exception('管理组不存在');
  429. $this->addError('roleid', \Yii::t('ctx', 'adminUserGroupDoesNotExist')); // 提交场景不存在
  430. return null;
  431. }
  432. }
  433. // 如果是超级管理员直接返回不做处理
  434. if($roleId == \Yii::$app->params['superAdminRoleId']){
  435. return ;
  436. }
  437. $role = isset($allRole[$roleId]) ? $allRole[$roleId] : null;
  438. if($role){
  439. $className = get_class($this);
  440. $listModuleArr = explode('\\', $className);
  441. $listModuleArrCount = count($listModuleArr);
  442. $listModule = $listModuleArr[$listModuleArrCount-2].'/'.$listModuleArr[$listModuleArrCount-1];
  443. // 把列里面没有的内容过滤掉
  444. if($this->columns){
  445. foreach($this->columns as $key=>$column){
  446. if($column !== null){
  447. if(!in_array($key, $role['COLUMN_PERMISSION'][$listModule])){
  448. unset($this->columns[$key]);
  449. // 把筛选里面没有的内容也过滤掉
  450. if(isset($this->filterTypes[$key])) unset($this->filterTypes[$key]);
  451. }
  452. }
  453. }
  454. }
  455. } else {
  456. $this->columns = [];
  457. }
  458. }
  459. }