Common.php 19 KB

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