Page.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. $upload['cate_id'] = 1;
  370. $upload['group_key'] = $this->db->config['table'] . '-' . $value['key'];
  371. $upload['group_name'] = $this->db->config['name'];
  372. $upload['user_token'] = Dever::load('common', 'manage')->getToken();
  373. $upload['user_table'] = $this->user['table'];
  374. $upload['user_id'] = $this->user['id'];
  375. if (is_array($value['upload'])) {
  376. $upload += $value['upload'];
  377. } else {
  378. $upload += array('id' => $value['upload']);
  379. }
  380. if (empty($upload['id'])) {
  381. Dever::error('上传配置错误');
  382. }
  383. $value['config'] = Dever::load('save', 'upload')->get($upload['id']);
  384. $value['yun'] = false;
  385. if ($value['config']['method'] == 2) {
  386. $value['yun'] = true;
  387. }
  388. $value['url'] = Dever::url('upload/save.act', $upload);
  389. $upload['wh'] = $value['wh'] ?? '500*500';
  390. $value['set'] = Dever::url('image/manage.set', $upload);
  391. if (isset($value['multiple']) && $value['multiple']) {
  392. $value['limit'] = 10;
  393. } else {
  394. $value['limit'] = 1;
  395. }
  396. }
  397. if (isset($value['editorMenu'])) {
  398. if (isset($value['editorMenu']['uploadImage'])) {
  399. if (!is_array($value['editorMenu']['uploadImage'])) {
  400. $value['editorMenu']['uploadImage'] = array('upload' => $value['editorMenu']['uploadImage']);
  401. }
  402. $value['editorMenu']['uploadImage']['server'] = Dever::url('upload/save.wangEditor', array('id' => $value['editorMenu']['uploadImage']['upload']));
  403. if (empty($value['editorMenu']['uploadImage']['fieldName'])) {
  404. $value['editorMenu']['uploadImage']['fieldName'] = 'file';
  405. }
  406. }
  407. if (isset($value['editorMenu']['uploadVideo'])) {
  408. if (!is_array($value['editorMenu']['uploadVideo'])) {
  409. $value['editorMenu']['uploadVideo'] = array('upload' => $value['editorMenu']['uploadVideo']);
  410. }
  411. $value['editorMenu']['uploadVideo']['server'] = Dever::url('upload/save.wangEditor', array('id' => $value['editorMenu']['uploadVideo']['upload']));
  412. if (empty($value['editorMenu']['uploadImage']['fieldName'])) {
  413. $value['editorMenu']['uploadImage']['fieldName'] = 'file';
  414. }
  415. }
  416. }
  417. if (isset($value['date_type']) && $value['date_type'] == 'datetimerange' && empty($value['default_time'])) {
  418. $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);
  419. }
  420. }
  421. private function setDisable(&$value, $disable)
  422. {
  423. if (isset($value['disable'])) {
  424. $disable = $value['disable'];
  425. }
  426. $value['disable'] = $disable;
  427. }
  428. private function setForm(&$value)
  429. {
  430. if (empty($value['value'])) {
  431. $value['value'] = Dever::input('search')[$value['key']] ?? '';
  432. }
  433. if (is_array($value['value'])) {
  434. foreach ($value['value'] as &$v) {
  435. $v = (float) $v;
  436. }
  437. }
  438. if (!$value['value']) {
  439. if (isset($value['default']) && !strstr($value['default'], '{')) {
  440. $value['value'] = $value['default'];
  441. } elseif ($this->key == 'update' && isset($this->db->config['struct'][$value['key']]['default'])) {
  442. $value['value'] = $this->db->config['struct'][$value['key']]['default'];
  443. }
  444. }
  445. if (isset($value['option']) && $value['option']) {
  446. $this->db->config['struct'][$value['key']]['value'] = $value['option'];
  447. }
  448. if ($option = $this->db->value($value['key'])) {
  449. if ($value['type'] == 'checkbox') {
  450. $value['value'] = $value['value'] ? explode(',', $value['value']) : array();
  451. }
  452. $value['option'] = $option;
  453. if ($value['type'] == 'text') {
  454. $value['type'] = 'select';
  455. }
  456. if (!is_array($value['value']) && $value['value']) {
  457. $value['value'] = (float) $value['value'];
  458. }
  459. }
  460. }
  461. private function setRules(&$value)
  462. {
  463. if (isset($value['rules']) && $value['rules']) {
  464. if (!is_array($value['rules'])) {
  465. $value['rules'] = array
  466. (
  467. array
  468. (
  469. 'required' => true,
  470. 'trigger' => 'blur',
  471. 'message' => $value['name'] . '不能为空',
  472. ),
  473. );
  474. }
  475. foreach ($value['rules'] as $k => $v) {
  476. if (isset($v['only'])) {
  477. if ($v['only'] == 'edit' && !$this->id) {
  478. unset($value['rules'][$k]);
  479. break;
  480. } elseif ($v['only'] == 'add' && $this->id) {
  481. unset($value['rules'][$k]);
  482. break;
  483. }
  484. }
  485. }
  486. if (!isset($value['rules'][0])) {
  487. $value['rules'] = array_values($value['rules']);
  488. }
  489. }
  490. }
  491. protected function input($key, $value = '')
  492. {
  493. return $this->input ? Dever::input($key) : $value;
  494. }
  495. public function button($key = 'button', $data = array())
  496. {
  497. $result = array();
  498. if (!isset($this->config[$key])) {
  499. $num = 0;
  500. if (isset($this->db->config['manage']['update']['field'])) {
  501. $num = count($this->db->config['manage']['update']['field']);
  502. } elseif (isset($this->db->config['struct']) && $this->db->config['struct']) {
  503. $num = count($this->db->config['struct']);
  504. }
  505. $fast = 'fast';
  506. if ($num > 8) {
  507. $fast = '';
  508. }
  509. if ($key == 'button') {
  510. $this->config[$key] = array('新增' => $fast . 'add');
  511. } else {
  512. $this->config[$key] = array('编辑' => $fast . 'edit', '删除' => 'recycle');
  513. }
  514. }
  515. $sort = 1;
  516. foreach ($this->config[$key] as $k => $v) {
  517. $d = '';
  518. $p = '';
  519. $i = '';
  520. if (is_array($v)) {
  521. if (isset($v[3]) && $data) {
  522. $d = $v[3];
  523. parse_str($d, $t);
  524. $state = true;
  525. foreach ($t as $k1 => $v1) {
  526. if (!isset($data[$k1]) || (isset($data[$k1]) && $data[$k1] != $v1)) {
  527. $state = false;
  528. }
  529. }
  530. if (!$state) {
  531. continue;
  532. }
  533. }
  534. if (isset($v[2])) {
  535. $i = $v[2];
  536. }
  537. if (isset($v[1])) {
  538. $p = $v[1];
  539. }
  540. $v = $v[0];
  541. }
  542. if (strstr($v, 'add')) {
  543. $icon = 'Plus';
  544. $button = 'primary';
  545. } elseif (strstr($v, 'edit')) {
  546. $icon = 'Edit';
  547. $button = 'primary';
  548. } elseif (strstr($v, 'view')) {
  549. $icon = 'View';
  550. $button = '';
  551. } elseif ($v == 'delete') {
  552. if ($key == 'button') {
  553. if (isset($this->config['layout'])) {
  554. continue;
  555. }
  556. $this->config['selection'] = true;
  557. }
  558. $icon = 'Delete';
  559. $button = 'danger';
  560. } elseif ($v == 'recycle') {
  561. if ($key == 'button') {
  562. if (isset($this->config['layout'])) {
  563. continue;
  564. }
  565. $this->config['selection'] = true;
  566. }
  567. $icon = 'Delete';
  568. $button = 'danger';
  569. } elseif ($v == 'oper') {
  570. if ($key == 'button') {
  571. if (isset($this->config['layout'])) {
  572. continue;
  573. }
  574. $this->config['selection'] = true;
  575. }
  576. $icon = 'Notification';
  577. $button = 'warning';
  578. } elseif ($v == 'api') {
  579. if ($key == 'button') {
  580. if (isset($this->config['layout'])) {
  581. continue;
  582. }
  583. $this->config['selection'] = true;
  584. }
  585. $p = Dever::url($p);
  586. $icon = 'Notification';
  587. $button = 'warning';
  588. } elseif ($v == 'link') {
  589. $icon = 'Link';
  590. $button = 'success';
  591. } elseif ($v == 'route') {
  592. $icon = 'Link';
  593. $button = 'success';
  594. } elseif ($v == 'recover') {
  595. $icon = 'CirclePlus';
  596. $button = 'info';
  597. } else {
  598. continue;
  599. }
  600. # 权限验证
  601. $sort++;
  602. $func = $this->getFunc($v, $k, $sort, $p);
  603. if ($this->menu && $this->menu['show'] == 1 && !$func) {
  604. continue;
  605. }
  606. if ($i) {
  607. $icon = $i;
  608. }
  609. $result[] = array
  610. (
  611. 'name' => $k,
  612. 'type' => $v,
  613. 'param' => $p,
  614. 'icon' => $icon,
  615. 'button' => $button,
  616. 'func' => $func,
  617. );
  618. if (!$this->recycler && $v == 'recycle') {
  619. $this->recycler = true;
  620. }
  621. }
  622. return $result;
  623. }
  624. }