Page.php 25 KB

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