Common.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. <?php namespace Manage\Lib;
  2. use Dever;
  3. use Dever\Helper\Str;
  4. use Dever\Helper\Env;
  5. use Dever\Helper\Secure;
  6. use Dever\Helper\Date;
  7. class Common
  8. {
  9. public function info()
  10. {
  11. $auth = $this->auth();
  12. $system = Dever::db('system', 'manage')->find($auth['extend']['system_id']);
  13. return Dever::db($system['user_table'])->find($auth['uid']);
  14. }
  15. public function auth()
  16. {
  17. $auth = Dever::input('authorization');
  18. if ($auth) {
  19. $auth = \Dever\Helper\Str::decode($auth);
  20. }
  21. if (!$auth) {
  22. $auth = Env::header('authorization');
  23. }
  24. if ($auth) {
  25. $auth = str_replace('Bearer ', '', $auth);
  26. Dever::session('auth', $auth);
  27. $info = Secure::checkLogin($auth);
  28. return $info;
  29. }
  30. return false;
  31. }
  32. # 获取当前的扩展数据
  33. public function extend()
  34. {
  35. $info = $this->auth();
  36. if (!$info) {
  37. $auth = Dever::session('auth');
  38. if (!$auth) {
  39. return false;
  40. }
  41. $info = Secure::checkLogin($auth);
  42. }
  43. if ($info && isset($info['extend'])) {
  44. return $info['extend'];
  45. }
  46. return false;
  47. }
  48. # 获取页面类
  49. public function page($load, $config = array(), $key = 'list', $input = true)
  50. {
  51. $page = new Page($key, $load, $input, $config);
  52. return $page;
  53. }
  54. # 获取当前使用的系统 一般为数据库隔离使用
  55. public function system($info = false, $module = true, $field = false)
  56. {
  57. if (!$info) {
  58. $info = $this->extend();
  59. }
  60. if ($info && isset($info['info_id']) && isset($info['partition'])) {
  61. # 这里后续增加从数据库中获取
  62. $value = $info['system_id'] . '_' . $info['info_id'];
  63. $result = array();
  64. if (strpos($info['partition'], '.')) {
  65. $temp = explode('.', $info['partition']);
  66. $result = $this->partition($result, $temp[0], $info['system_key'], $value);
  67. if ($module && isset($info['data_id']) && $info['data_id']) {
  68. if ($temp[0] == $temp[1]) {
  69. $value .= '/' . $info['module_id'] . '_' . $info['data_id'];
  70. $result = $this->partition($result, $temp[0], $info['system_key'], $value);
  71. } else {
  72. $result = $this->partition($result, $temp[1], $info['system_key'], $info['module_id'] . '_' . $info['data_id']);
  73. }
  74. }
  75. } else {
  76. $result = $this->partition($result, $info['partition'], $info['system_key'], $value);
  77. }
  78. if ($field) {
  79. $result['field'] = Dever::call($field);
  80. }
  81. return $result;
  82. }
  83. return false;
  84. }
  85. # 设置数据隔离
  86. private function partition(&$result, $type, $key, $value)
  87. {
  88. if ($type == 'field') {
  89. $result[$type] = array
  90. (
  91. 'type' => 'key',
  92. 'field' => $key,
  93. 'value' => $value,
  94. );
  95. } elseif ($type == 'where') {
  96. $result[$type] = array
  97. (
  98. $key => $value
  99. );
  100. } else {
  101. $result[$type] = $value;
  102. }
  103. return $result;
  104. }
  105. # 获取token需要用到的key
  106. public function getToken()
  107. {
  108. $extend = $this->extend();
  109. if ($extend) {
  110. return implode('-', array_values($extend));
  111. }
  112. return '';
  113. }
  114. # 将token设置到route权限中,方便后续读取
  115. public function setAuth($partition, $system_key, $system_id, $info_id, $module_id, $data_id)
  116. {
  117. $token = Dever::load('common', 'manage')->token(-1, '', $partition, $system_key, $system_id, $info_id, $module_id, $data_id);
  118. \Dever\Route::$data['authorization'] = Secure::encode($token['token']);
  119. }
  120. # 生成token
  121. public function token($uid, $mobile, $partition, $system_key, $system_id, $info_id, $module_id, $data_id)
  122. {
  123. $extend['partition'] = $partition;
  124. $extend['system_key'] = $system_key;
  125. $extend['system_id'] = $system_id;
  126. $extend['info_id'] = $info_id;
  127. $extend['module_id'] = $module_id;
  128. $extend['data_id'] = $data_id;
  129. if ($uid && $uid > 0) {
  130. $select['uid'] = $uid;
  131. $select['system_id'] = $system_id;
  132. $select['info_id'] = $info_id;
  133. $info = Dever::db('system_user', 'manage')->find($select);
  134. $select += $extend;
  135. if (!$info) {
  136. Dever::db('system_user', 'manage')->insert($select);
  137. } else {
  138. Dever::db('system_user', 'manage')->update($info['id'], $select);
  139. }
  140. }
  141. return array('token' => Secure::login($uid, $extend));
  142. }
  143. # 生成密码
  144. public function createPwd($password)
  145. {
  146. $data['salt'] = Str::salt(8);
  147. $data['password'] = $this->hash($password, $data['salt']);
  148. return $data;
  149. }
  150. # 生成时间
  151. public function crateDate($date)
  152. {
  153. return Date::mktime($date);
  154. }
  155. # hash加密
  156. public function hash($password, $salt)
  157. {
  158. return hash('sha256', $password . $salt);
  159. }
  160. # 自动更新key
  161. public function updateKey($db, $data)
  162. {
  163. if ($data['name'] && !$data['key']) {
  164. if (Dever::project('pinyin')) {
  165. $where = array();
  166. if (isset($data['id']) && $data['id']) {
  167. $where['id'] = array('!=', $data['id']);
  168. }
  169. $data['key'] = Dever::load('convert', 'pinyin')->getPinyin($data['name']);
  170. # 检查是否存在
  171. $where['key'] = $data['key'];
  172. $info = $db->find($where);
  173. if ($info) {
  174. $data['key'] .= '-' . date('YmdHis');
  175. }
  176. }
  177. }
  178. return $data;
  179. }
  180. # 设置联动
  181. public function cascader($total, $func)
  182. {
  183. $total = Dever::input('total', 'is_numeric', '联动总数', $total);
  184. $level = Dever::input('level', 'is_numeric', '联动级别', 1);
  185. $parent = Dever::input('parent', 'is_numeric', '联动ID', 0);
  186. if ($parent < 0) {
  187. Dever::error('error');
  188. }
  189. $data = $func($level, $parent);
  190. if ($level >= $total) {
  191. foreach ($data as &$v) {
  192. $v['leaf'] = true;
  193. }
  194. }
  195. $result['total'] = $total;
  196. $result['list'] = $data;
  197. return $result;
  198. }
  199. # 根据load获取db
  200. public function db($load)
  201. {
  202. $menu = array();
  203. $load = explode('/', ltrim($load, '/'));
  204. if (isset($load[2])) {
  205. $app = $load[1];
  206. $table = $load[2];
  207. } else {
  208. $app = $load[0];
  209. $table = $load[1];
  210. }
  211. $parent = Dever::db('menu', 'manage')->find(array('key' => $app));
  212. if ($parent) {
  213. $menu = Dever::db('menu', 'manage')->find(array('parent_id' => $parent['id'], 'key' => $table));
  214. if ($menu) {
  215. $app = $menu['app'];
  216. }
  217. }
  218. $set = Dever::project($app);
  219. $manage = $set['path'] . 'manage/'.$table.'.php';
  220. if (is_file($manage)) {
  221. $manage = include $manage;
  222. if ($source = Dever::issets($manage, 'source')) {
  223. if (strpos($source, '/')) {
  224. $source = explode('/', $source);
  225. $app = $source[0];
  226. $table = $source[1];
  227. } else {
  228. $table = $source;
  229. }
  230. }
  231. }
  232. $db = Dever::db($table, $app);
  233. $db->config['manage'] = $manage;
  234. return array($db, $menu);
  235. }
  236. # 获取项目
  237. public function project()
  238. {
  239. $result = array();
  240. $app = \Dever\Project::read();
  241. foreach ($app as $k => $v) {
  242. $result[] = array
  243. (
  244. 'id' => $k,
  245. 'name' => $v['lang'] ?? $k,
  246. );
  247. }
  248. return $result;
  249. }
  250. # 仅为测试用
  251. public function out($data)
  252. {
  253. $result = array();
  254. $result['head'] = array('id', '姓名', '时间');
  255. $result['body'] = array();
  256. foreach ($data['body'] as $k => $v) {
  257. $result['body'][$k] = array($v['id'], $v['name'], $v['cdate']);
  258. }
  259. return $result;
  260. }
  261. # 仅为测试用,展示表格更多内容
  262. public function show($data)
  263. {
  264. # 返回类型:string字符串,table表格,card卡片,desc描述,list列表
  265. $result['type'] = 'string';
  266. $result['content'] = 'ddddd';
  267. $result['type'] = 'table';
  268. $result['head'] = array('id' => 'id', 'name' => '姓名');
  269. $result['body'] = array
  270. (
  271. array('id' => '1', 'name' => 'test'),
  272. array('id' => '2', 'name' => 'test2'),
  273. );
  274. $result['type'] = 'card';
  275. $result['content'][] = array
  276. (
  277. 'title' => '订单信息',
  278. 'shadow' => 'never',//always | never | hover
  279. 'content' => array
  280. (
  281. '1111',
  282. '2222',
  283. ),
  284. );
  285. $result['type'] = 'list';
  286. $result['content'] = array
  287. (
  288. array('日志类型', 'test'),
  289. array('姓名', 'test1'),
  290. );
  291. return $result;
  292. }
  293. # 仅为测试用,展示详情
  294. public function view($page)
  295. {
  296. # 这里获取基本信息
  297. //print_r($page->info);die;
  298. $info[] = array
  299. (
  300. # 类型,desc描述 table表格,表格有head和body即可
  301. 'type' => 'desc',
  302. 'name' => '基本信息',
  303. # 每行展示数量
  304. 'column' => 4,
  305. # 是否有边框
  306. 'border' => true,
  307. # 排列方向:horizontal横向 vertical纵向
  308. 'direction' => 'vertical',
  309. # 右侧按钮
  310. 'button' => array
  311. (
  312. array
  313. (
  314. 'name' => '编辑',
  315. # fastedit、fastadd、oper、api、link、route,参数与list里的data_button一致,多了一个load,可以单独设置路由
  316. 'type' => 'fastedit',
  317. 'path' => 'platform/role',
  318. # 增加权限,第三个参数是排序,建议大一些
  319. //'func' => $page->getFunc('view_fastedit', '详情页-编辑角色', 1000),
  320. # 这里是按钮用到的参数数据
  321. 'row' => array
  322. (
  323. 'id' => 1,
  324. ),
  325. ),
  326. ),
  327. # 具体内容
  328. 'content' => array
  329. (
  330. array
  331. (
  332. 'name' => '标题',
  333. # 类型,text普通文本,tag标签,link链接,image图片 progress进度条 stat统计 timeline时间线 table表格
  334. 'type' => 'text',
  335. 'content' => '内容',
  336. # 样式primary success warning danger info exception
  337. 'style' => 'primary',
  338. ),
  339. array
  340. (
  341. 'name' => '标题',
  342. 'type' => 'tag',
  343. 'content' => '内容',
  344. 'style' => 'warning',
  345. ),
  346. array
  347. (
  348. 'name' => '标题',
  349. 'type' => 'link',
  350. 'content' => '内容',
  351. ),
  352. array
  353. (
  354. 'name' => '图片',
  355. 'type' => 'image',
  356. 'content' => 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
  357. # 'fill', 'contain', 'cover', 'none', 'scale-down'
  358. 'fit' => 'fill',
  359. ),
  360. array
  361. (
  362. 'name' => '进度条',
  363. 'type' => 'progress',
  364. 'content' => '10',
  365. 'style' => 'exception',
  366. 'width' => '20',
  367. 'inside' => true,
  368. # line dashboard 仪表盘 circle 圆形
  369. 'show' => 'line',
  370. # 开启条纹
  371. 'striped' => true,
  372. # 开启动画
  373. 'indeterminate' => true,
  374. ),
  375. array
  376. (
  377. 'name' => '统计',
  378. 'type' => 'stat',
  379. 'content' => array
  380. (
  381. array
  382. (
  383. # 一共24
  384. 'span' => '6',
  385. 'name' => '测试',
  386. 'value' => '1000',
  387. ),
  388. array
  389. (
  390. 'span' => '6',
  391. 'name' => '测试1',
  392. 'value' => '1000',
  393. ),
  394. array
  395. (
  396. 'span' => '6',
  397. 'name' => '测试2',
  398. 'value' => '1000',
  399. ),
  400. array
  401. (
  402. 'span' => '6',
  403. 'name' => '测试2',
  404. 'value' => '1000',
  405. ),
  406. ),
  407. ),
  408. array
  409. (
  410. 'name' => '时间线',
  411. 'type' => 'timeline',
  412. 'content' => array
  413. (
  414. array
  415. (
  416. 'time' => '2020-10-11',
  417. 'name' => '测试',
  418. 'color' => '#0bbd87',
  419. 'size' => 'large',
  420. 'type' => 'primary',
  421. 'hollow' => true,
  422. ),
  423. array
  424. (
  425. 'time' => '2020-10-11',
  426. 'name' => '测试',
  427. ),
  428. array
  429. (
  430. 'time' => '2020-10-11',
  431. 'name' => '测试',
  432. ),
  433. array
  434. (
  435. 'time' => '2020-10-11',
  436. 'name' => '测试',
  437. ),
  438. ),
  439. ),
  440. array
  441. (
  442. 'name' => '表格',
  443. 'type' => 'table',
  444. 'border' => true,
  445. 'height' => '200',
  446. 'head' => array
  447. (
  448. array
  449. (
  450. 'key' => 'name',
  451. 'name' => '姓名',
  452. 'fixed' => 'fixed',
  453. ),
  454. array
  455. (
  456. 'key' => 'desc',
  457. 'name' => '描述',
  458. 'fixed' => 'fixed',
  459. ),
  460. ),
  461. 'button' => array
  462. (
  463. array
  464. (
  465. 'name' => '编辑',
  466. 'type' => 'fastedit',
  467. 'load' => 'platform/role',
  468. ),
  469. ),
  470. 'body' => array
  471. (
  472. array
  473. (
  474. 'id' => 1,
  475. 'name' => 'test',
  476. 'desc' => 'dfdf',
  477. ),
  478. ),
  479. ),
  480. ),
  481. );
  482. $info[] = array
  483. (
  484. 'type' => 'table',
  485. 'name' => '表格信息',
  486. 'border' => true,
  487. 'height' => '200',
  488. 'head' => array
  489. (
  490. array
  491. (
  492. 'key' => 'name',
  493. 'name' => '姓名',
  494. 'fixed' => 'fixed',
  495. ),
  496. array
  497. (
  498. 'key' => 'desc',
  499. 'name' => '描述',
  500. 'fixed' => 'fixed',
  501. ),
  502. ),
  503. 'button' => array
  504. (
  505. array
  506. (
  507. 'name' => '编辑',
  508. 'type' => 'fastedit',
  509. 'load' => 'platform/role',
  510. # 增加权限,第三个参数是排序,建议大一些
  511. 'func' => $page->getFunc('view_fastedit', '详情页-编辑角色', 1000),
  512. ),
  513. ),
  514. 'body' => array
  515. (
  516. array
  517. (
  518. 'id' => 1,
  519. 'name' => 'test',
  520. 'desc' => 'dfdf',
  521. ),
  522. ),
  523. );
  524. $tab = array
  525. (
  526. 'active' => 'table1',
  527. 'content' => array
  528. (
  529. 'table1' => array
  530. (
  531. # 这里跟desc一样
  532. 'name' => '标题',
  533. 'type' => 'text',
  534. 'content' => '内容',
  535. 'style' => 'primary',
  536. ),
  537. 'tab2' => array
  538. (
  539. 'name' => '表格',
  540. 'type' => 'table',
  541. 'border' => true,
  542. 'height' => '200',
  543. 'head' => array
  544. (
  545. array
  546. (
  547. 'key' => 'name',
  548. 'name' => '姓名',
  549. 'fixed' => 'fixed',
  550. ),
  551. array
  552. (
  553. 'key' => 'desc',
  554. 'name' => '描述',
  555. 'fixed' => 'fixed',
  556. ),
  557. ),
  558. 'button' => array
  559. (
  560. array
  561. (
  562. 'name' => '编辑',
  563. 'type' => 'fastedit',
  564. 'load' => 'platform/role',
  565. ),
  566. ),
  567. 'body' => array
  568. (
  569. array
  570. (
  571. 'id' => 1,
  572. 'name' => 'test',
  573. 'desc' => 'dfdf',
  574. ),
  575. ),
  576. ),
  577. )
  578. );
  579. return array('info' => $info, 'tab' => $tab);
  580. }
  581. public function stat($where)
  582. {
  583. return array
  584. (
  585. array
  586. (
  587. # 一共24
  588. 'span' => '8',
  589. 'name' => '测试',
  590. 'value' => '1000',
  591. ),
  592. array
  593. (
  594. 'span' => '8',
  595. 'name' => '测试1',
  596. 'value' => '1000',
  597. ),
  598. array
  599. (
  600. 'span' => '8',
  601. 'name' => '测试2',
  602. 'value' => '1000',
  603. ),
  604. );
  605. }
  606. }