Common.php 17 KB

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