Page.php 26 KB

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