Page.php 23 KB

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