Page.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <?php namespace Manage\Lib;
  2. use Dever;
  3. # 通用页面
  4. class Page extends Auth
  5. {
  6. protected $db;
  7. protected $key;
  8. protected $id = 0;
  9. protected $input = false;
  10. protected $recycler = false;
  11. protected $menu = array();
  12. protected $config = array();
  13. protected $field = array();
  14. public $info = array();
  15. public function __construct($key = '', $load = '', $input = true, $config = array())
  16. {
  17. parent::__construct();
  18. $this->key = $key;
  19. $this->input = $input;
  20. $this->id = $this->input('id', 0);
  21. if (!$load) {
  22. $load = Dever::input('load');
  23. }
  24. list($this->db, $this->menu) = Dever::load('common', 'manage')->db($load);
  25. if ($this->menu && $this->menu['show'] == 1) {
  26. $this->checkMenu($this->menu['id'], false);
  27. }
  28. $this->config = $this->db->config['manage'][$key] ?? array();
  29. if ($config) {
  30. $this->config = array_merge($this->config, $config);
  31. }
  32. if ($this->id && !strstr($this->id, ',')) {
  33. $this->info = $this->db->find($this->id);
  34. }
  35. }
  36. public function setting($key, &$data, $struct = true, $type = 'show', $disable = false)
  37. {
  38. if (empty($this->config[$key]) && $struct && isset($this->db->config['struct']) && $this->db->config['struct']) {
  39. $this->config[$key] = $this->db->config['struct'];
  40. }
  41. if (empty($this->config[$key])) {
  42. return;
  43. }
  44. $setting = $this->config[$key];
  45. if (is_string($setting)) {
  46. $setting = explode(',', $setting);
  47. }
  48. $field = $this->input('field', '');
  49. return $this->setData($setting, $data, $field, $type, $disable);
  50. }
  51. # 获取某个数据的具体展示值
  52. public function getValue($key, $value, $data, $field = array())
  53. {
  54. if ($show = Dever::issets($field, 'show')) {
  55. $value = $this->getShow($show, $data);
  56. } elseif ($value && isset($this->db->config['struct'][$key]['value']) && $this->db->config['struct'][$key]['value']) {
  57. $value = $this->db->value($key, $value);
  58. } elseif ($key == 'cdate') {
  59. if (strstr($value, 'T')) {
  60. $value = date('Y-m-d H:i:s', strtotime($value));
  61. } elseif ($value) {
  62. $value = date('Y-m-d H:i:s', $value);
  63. } else {
  64. $value = '-';
  65. }
  66. }
  67. return $value;
  68. }
  69. # 获取关联数据
  70. public function getOther($key, $set, $data)
  71. {
  72. $where = array();
  73. foreach ($set as $k => $v) {
  74. if (!is_array($v) && isset($data[$v])) {
  75. $where[$k] = $data[$v];
  76. } else {
  77. $where[$k] = $v;
  78. }
  79. }
  80. if ($where) {
  81. return Dever::db($key)->select($where);
  82. }
  83. return array();
  84. }
  85. public function getShow($show, $data)
  86. {
  87. if (strpos($show, '{') !== false && strpos($show, '{"') === false) {
  88. $func = function ($r) use ($data) {
  89. if (isset($data[$r[1]])) {
  90. return $data[$r[1]];
  91. }
  92. return false;
  93. };
  94. $show = preg_replace_callback('/{(.*?)}/', $func, $show);
  95. }
  96. $eval = '$show = ' . $show . ';';
  97. @eval($eval);
  98. return $show;
  99. }
  100. # 获取菜单标题
  101. public function getTitle()
  102. {
  103. return $this->menu['name'];
  104. }
  105. # 获取左侧分栏
  106. protected function column(&$data, $name = '左侧分栏')
  107. {
  108. $data['column'] = false;
  109. if (isset($this->config['column'])) {
  110. $data['column'] = $this->config['column'];
  111. if (isset($this->config['column']['add'])) {
  112. $data['column']['add'] = array('name' => $this->config['column']['add'], 'func' => $this->getFunc('column_add', $name . '-' . $this->config['column']['add'], 101));
  113. }
  114. if (isset($this->config['column']['edit'])) {
  115. $data['column']['edit'] = array('name' => '编辑', 'func' => $this->getFunc('column_edit', $name . '-编辑', 102));
  116. }
  117. if (isset($this->config['column']['delete'])) {
  118. $data['column']['delete'] = array('name' => '删除', 'func' => $this->getFunc('column_delete', $name . '-删除', 103));
  119. }
  120. $data['column']['data'] = Dever::call($this->config['column']['data']);
  121. $data['height'] = '100%';
  122. }
  123. }
  124. # 通用的规则验证 一般为更新数据时使用
  125. protected function checkRules($set, $data)
  126. {
  127. if ($set['rules']) {
  128. if (!is_array($set['rules'])) {
  129. $set['rules'] = array
  130. (
  131. array
  132. (
  133. 'required' => true,
  134. 'trigger' => 'blur',
  135. 'message' => $set['name'] . '不能为空',
  136. ),
  137. );
  138. }
  139. foreach ($set['rules'] as $k => $v) {
  140. if (isset($v['required']) && $v['required'] && !$data && $data !== 0) {
  141. Dever::error($v['message']);
  142. }
  143. if ($data || $data === 0) {
  144. if (isset($v['pattern']) && $v['pattern'] && !preg_match('/' . $v['pattern'] . '/', $data)) {
  145. Dever::error($v['message']);
  146. }
  147. if (isset($v['type']) && $v['type']) {
  148. if ($v['type'] == 'number' && !is_numeric($data)) {
  149. Dever::error($v['message']);
  150. } elseif ($v['type'] == 'array' && !is_array($data)) {
  151. Dever::error($v['message']);
  152. } elseif ($v['type'] == 'integer' && !is_int($data)) {
  153. Dever::error($v['message']);
  154. } elseif ($v['type'] == 'float' && !is_float($data)) {
  155. Dever::error($v['message']);
  156. } elseif ($v['type'] == 'string' && !is_string($data)) {
  157. Dever::error($v['message']);
  158. } elseif ($v['type'] == 'boolean' && !is_bool($data)) {
  159. Dever::error($v['message']);
  160. } elseif ($v['type'] == 'url' && !filter_var($data, FILTER_VALIDATE_URL)) {
  161. Dever::error($v['message']);
  162. } elseif ($v['type'] == 'email' && !filter_var($data, FILTER_VALIDATE_EMAIL)) {
  163. Dever::error($v['message']);
  164. } elseif ($v['type'] == 'enum' && isset($v['enum']) && !in_array($data, $v['enum'])) {
  165. Dever::error($v['message']);
  166. }
  167. }
  168. if (isset($v['len']) && $v['len'] && strlen($data) > $v['len']) {
  169. Dever::error($v['message']);
  170. }
  171. if (isset($v['min']) && $v['min'] && strlen($data) < $v['min']) {
  172. Dever::error($v['message']);
  173. }
  174. if (isset($v['max']) && $v['max'] && strlen($data) > $v['max']) {
  175. Dever::error($v['message']);
  176. }
  177. }
  178. }
  179. }
  180. }
  181. private function setData($setting, &$data, $field, $type, $disable)
  182. {
  183. $result = array();
  184. foreach ($setting as $k => $v) {
  185. if (!is_array($v)) {
  186. if (is_numeric($k)) {
  187. $k = $v;
  188. $v = $type;
  189. }
  190. if ($k == 'id') {
  191. $v = array('name' => 'ID', 'type' => $v);
  192. } elseif ($k == 'cdate') {
  193. $v = array('name' => '创建时间', 'type' => $v);
  194. } elseif(isset($this->db->config['struct'][$k])) {
  195. $v = array('name' => $this->db->config['struct'][$k]['name'], 'type' => $v);
  196. } else {
  197. $v = array('name' => $v);
  198. }
  199. } else {
  200. if (isset($v['only'])) {
  201. if ($v['only'] == 'edit' && !$this->id) {
  202. continue;
  203. } elseif ($v['only'] == 'add' && $this->id) {
  204. continue;
  205. }
  206. }
  207. }
  208. if ($field) {
  209. if (is_string($field) && (strstr($field, '{') || strstr($field, '%7B'))) {
  210. $field = htmlspecialchars_decode($field);
  211. $field = Dever::json_decode($field);
  212. }
  213. if (is_array($field)) {
  214. if (isset($field['param'])) {
  215. if (isset($field['param']['set'])) {
  216. $field = array_merge($field, $field['param']['set']);
  217. }
  218. if (isset($field['param']['search'])) {
  219. $field = array_merge($field, $field['param']['search']);
  220. }
  221. } elseif (isset($field['field']) && !Dever::check($field['field'], $k)) {
  222. continue;
  223. }
  224. if (isset($field[$k])) {
  225. $v['default'] = $field[$k];
  226. $v['type'] = 'hidden';
  227. }
  228. } elseif (!Dever::check($field, $k)) {
  229. continue;
  230. }
  231. }
  232. $info = $this->setField($data, $k, $v, $field, $type, $disable);
  233. if ($info) {
  234. $result[] = $info;
  235. }
  236. }
  237. return $result;
  238. }
  239. private function setField(&$data, $key, $value, $field, $type = 'show', $disable = false)
  240. {
  241. $value['key'] = $key;
  242. $this->setName($value);
  243. # 对每个字段进行权限设置
  244. if (isset($value['func']) && $value['func']) {
  245. $func = $this->getFunc('field_' . $value['key'], '字段-' . $value['name'], 200);
  246. if (!$func) {
  247. return false;
  248. }
  249. }
  250. if (strpos($key, '/') && $this->key == 'update') {
  251. $this->setType($value, 'array');
  252. $this->setShow($value);
  253. if (strpos($key, '#')) {
  254. $key = str_replace('#', '', $key);
  255. }
  256. $sku = $value['type'] == 'sku' ? true : false;
  257. $value['value'] = $this->getOther($key, $value['where'], $this->info);
  258. $update = new \Manage\Api\Page\Update($key, false);
  259. $value['option'] = array();
  260. $value['content'] = $update->get($value['value'], $value['option'], $sku);
  261. $data[] = $value;
  262. return $value['name'];
  263. } else {
  264. $this->setType($value, $type);
  265. $this->setDisable($value, $disable);
  266. if ($this->key == 'update') {
  267. # 一般为更新页面需要的参数
  268. $this->setShow($value);
  269. $this->setRules($value);
  270. } elseif (isset($value['remote'])) {
  271. $value['remote'] = Dever::url($value['remote']);
  272. }
  273. if ($type == 'show') {
  274. if (!isset($value['tip'])) {
  275. $value['tip'] = false;
  276. }
  277. $in = array('switch', 'select', 'input');
  278. if (in_array($value['type'], $in)) {
  279. $value['func'] = $this->getFunc('list_edit_' . $value['key'], '列表更新-' . $value['name'], 104);
  280. if (!$value['func']) {
  281. $value['type'] = 'show';
  282. if (isset($value['show'])) {
  283. unset($value['show']);
  284. }
  285. }
  286. }
  287. if (isset($value['child'])) {
  288. $child = array();
  289. $this->setData($value['child'], $child, $field, $type, $disable);
  290. $value['child'] = $child;
  291. } else {
  292. $this->field[$key] = $value;
  293. }
  294. }
  295. $this->setForm($value);
  296. $data[] = $value;
  297. return $value['name'];
  298. }
  299. }
  300. private function setShow(&$value)
  301. {
  302. if ($value['type'] == 'hidden') {
  303. $value['show'] = false;
  304. } elseif (!isset($value['show'])) {
  305. $value['show'] = true;
  306. }
  307. }
  308. private function setName(&$value)
  309. {
  310. if (empty($value['name']) && isset($this->db->config['struct'][$value['key']])) {
  311. $value['name'] = $this->db->config['struct'][$value['key']]['name'];
  312. }
  313. if (empty($value['placeholder']) && isset($value['name'])) {
  314. $value['placeholder'] = $value['name'];
  315. }
  316. }
  317. private function setType(&$value, $type)
  318. {
  319. if (empty($value['type'])) {
  320. $value['type'] = $type;
  321. }
  322. if (strpos($value['type'], '(')) {
  323. $value['type'] = $type;
  324. }
  325. if (empty($value['width'])) {
  326. $value['width'] = 'auto';
  327. }
  328. if (isset($value['upload']) && Dever::project('upload')) {
  329. $value['url'] = Dever::url('upload/save.act', array('id' => $value['upload']));
  330. $value['config'] = Dever::load('save', 'upload')->get($value['upload']);
  331. $value['set'] = Dever::url('image/manage.set', array('id' => $value['upload'], 'wh' => $value['wh'] ?? '500*500'));
  332. if (isset($value['multiple']) && $value['multiple']) {
  333. $value['limit'] = 10;
  334. } else {
  335. $value['limit'] = 1;
  336. }
  337. }
  338. if (isset($value['editorMenu'])) {
  339. if (isset($value['editorMenu']['uploadImage'])) {
  340. if (!is_array($value['editorMenu']['uploadImage'])) {
  341. $value['editorMenu']['uploadImage'] = array('upload' => $value['editorMenu']['uploadImage']);
  342. }
  343. $value['editorMenu']['uploadImage']['server'] = Dever::url('upload/save.wangEditor', array('id' => $value['editorMenu']['uploadImage']['upload']));
  344. if (empty($value['editorMenu']['uploadImage']['fieldName'])) {
  345. $value['editorMenu']['uploadImage']['fieldName'] = 'file';
  346. }
  347. }
  348. if (isset($value['editorMenu']['uploadVideo'])) {
  349. if (!is_array($value['editorMenu']['uploadVideo'])) {
  350. $value['editorMenu']['uploadVideo'] = array('upload' => $value['editorMenu']['uploadVideo']);
  351. }
  352. $value['editorMenu']['uploadVideo']['server'] = Dever::url('upload/save.wangEditor', array('id' => $value['editorMenu']['uploadVideo']['upload']));
  353. if (empty($value['editorMenu']['uploadImage']['fieldName'])) {
  354. $value['editorMenu']['uploadImage']['fieldName'] = 'file';
  355. }
  356. }
  357. }
  358. if (isset($value['date_type']) && $value['date_type'] == 'datetimerange' && empty($value['default_time'])) {
  359. $value['default_time'] = array(\Dever\Helper\Date::mktime(date('Y-m-d 00:00:00'))*1000, \Dever\Helper\Date::mktime(date('Y-m-d 23:59:59'))*1000);
  360. }
  361. }
  362. private function setDisable(&$value, $disable)
  363. {
  364. if (isset($value['disable'])) {
  365. $disable = $value['disable'];
  366. }
  367. $value['disable'] = $disable;
  368. }
  369. private function setForm(&$value)
  370. {
  371. $value['value'] = Dever::input('search')[$value['key']] ?? '';
  372. if (!$value['value']) {
  373. if (isset($value['default']) && !strstr($value['default'], '{')) {
  374. $value['value'] = $value['default'];
  375. } elseif ($this->key == 'update' && isset($this->db->config['struct'][$value['key']]['default'])) {
  376. $value['value'] = $this->db->config['struct'][$value['key']]['default'];
  377. }
  378. }
  379. if (isset($value['option']) && $value['option']) {
  380. $this->db->config['struct'][$value['key']]['value'] = $value['option'];
  381. }
  382. if ($option = $this->db->value($value['key'])) {
  383. if ($value['type'] == 'checkbox') {
  384. $value['value'] = $value['value'] ? explode(',', $value['value']) : array();
  385. }
  386. $value['option'] = $option;
  387. if ($value['type'] == 'text') {
  388. $value['type'] = 'select';
  389. }
  390. if (!is_array($value['value']) && $value['value']) {
  391. $value['value'] = (float) $value['value'];
  392. }
  393. }
  394. }
  395. private function setRules(&$value)
  396. {
  397. if (isset($value['rules']) && $value['rules']) {
  398. if (!is_array($value['rules'])) {
  399. $value['rules'] = array
  400. (
  401. array
  402. (
  403. 'required' => true,
  404. 'trigger' => 'blur',
  405. 'message' => $value['name'] . '不能为空',
  406. ),
  407. );
  408. }
  409. foreach ($value['rules'] as $k => $v) {
  410. if (isset($v['only'])) {
  411. if ($v['only'] == 'edit' && !$this->id) {
  412. unset($value['rules'][$k]);
  413. break;
  414. } elseif ($v['only'] == 'add' && $this->id) {
  415. unset($value['rules'][$k]);
  416. break;
  417. }
  418. }
  419. }
  420. if (!isset($value['rules'][0])) {
  421. $value['rules'] = array_values($value['rules']);
  422. }
  423. }
  424. }
  425. protected function input($key, $value = '')
  426. {
  427. return $this->input ? Dever::input($key) : $value;
  428. }
  429. public function button($key = 'button', $data = array())
  430. {
  431. $result = array();
  432. if (!isset($this->config[$key])) {
  433. $num = 0;
  434. if (isset($this->db->config['manage']['update']['field'])) {
  435. $num = count($this->db->config['manage']['update']['field']);
  436. } elseif (isset($this->db->config['struct']) && $this->db->config['struct']) {
  437. $num = count($this->db->config['struct']);
  438. }
  439. $fast = 'fast';
  440. if ($num > 8) {
  441. $fast = '';
  442. }
  443. if ($key == 'button') {
  444. $this->config[$key] = array('新增' => $fast . 'add');
  445. } else {
  446. $this->config[$key] = array('编辑' => $fast . 'edit', '删除' => 'recycle');
  447. }
  448. }
  449. $sort = 1;
  450. foreach ($this->config[$key] as $k => $v) {
  451. $d = '';
  452. $p = '';
  453. $i = '';
  454. if (is_array($v)) {
  455. if (isset($v[3]) && $data) {
  456. $d = $v[3];
  457. parse_str($d, $t);
  458. $state = true;
  459. foreach ($t as $k1 => $v1) {
  460. if (!isset($data[$k1]) || (isset($data[$k1]) && $data[$k1] != $v1)) {
  461. $state = false;
  462. }
  463. }
  464. if (!$state) {
  465. continue;
  466. }
  467. }
  468. if (isset($v[2])) {
  469. $i = $v[2];
  470. }
  471. if (isset($v[1])) {
  472. $p = $v[1];
  473. }
  474. $v = $v[0];
  475. }
  476. if (strstr($v, 'add')) {
  477. $icon = 'Plus';
  478. $button = 'primary';
  479. } elseif (strstr($v, 'edit')) {
  480. $icon = 'Edit';
  481. $button = 'primary';
  482. } elseif (strstr($v, 'view')) {
  483. $icon = 'View';
  484. $button = '';
  485. } elseif ($v == 'delete') {
  486. if ($key == 'button') {
  487. if (isset($this->config['layout'])) {
  488. continue;
  489. }
  490. $this->config['selection'] = true;
  491. }
  492. $icon = 'Delete';
  493. $button = 'danger';
  494. } elseif ($v == 'recycle') {
  495. if ($key == 'button') {
  496. if (isset($this->config['layout'])) {
  497. continue;
  498. }
  499. $this->config['selection'] = true;
  500. }
  501. $icon = 'Delete';
  502. $button = 'danger';
  503. } elseif ($v == 'oper') {
  504. if ($key == 'button') {
  505. if (isset($this->config['layout'])) {
  506. continue;
  507. }
  508. $this->config['selection'] = true;
  509. }
  510. $icon = 'Notification';
  511. $button = 'warning';
  512. } elseif ($v == 'api') {
  513. if ($key == 'button') {
  514. if (isset($this->config['layout'])) {
  515. continue;
  516. }
  517. $this->config['selection'] = true;
  518. }
  519. $p = Dever::url($p);
  520. $icon = 'Notification';
  521. $button = 'warning';
  522. } elseif ($v == 'link') {
  523. $icon = 'Link';
  524. $button = 'success';
  525. } elseif ($v == 'route') {
  526. $icon = 'Link';
  527. $button = 'success';
  528. } elseif ($v == 'recover') {
  529. $icon = 'CirclePlus';
  530. $button = 'info';
  531. } else {
  532. continue;
  533. }
  534. # 权限验证
  535. $sort++;
  536. $func = $this->getFunc($v, $k, $sort, $p);
  537. if ($this->menu && $this->menu['show'] == 1 && !$func) {
  538. continue;
  539. }
  540. if ($i) {
  541. $icon = $i;
  542. }
  543. $result[] = array
  544. (
  545. 'name' => $k,
  546. 'type' => $v,
  547. 'param' => $p,
  548. 'icon' => $icon,
  549. 'button' => $button,
  550. 'func' => $func,
  551. );
  552. if (!$this->recycler && $v == 'recycle') {
  553. $this->recycler = true;
  554. }
  555. }
  556. return $result;
  557. }
  558. }