Page.php 23 KB

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