Page.php 23 KB

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