Common.php 19 KB

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