Page.php 25 KB

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