Common.php 16 KB

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