123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610 |
- <?php namespace Manage\Lib;
- use Dever;
- use Dever\Helper\Str;
- use Dever\Helper\Env;
- use Dever\Helper\Secure;
- use Dever\Helper\Date;
- class Common
- {
- public function info()
- {
- $auth = $this->auth();
- $system = Dever::db('system', 'manage')->find($auth['extend']['system_id']);
- return Dever::db($system['user_table'])->find($auth['uid']);
- }
- public function auth()
- {
- $auth = Dever::input('authorization');
- if ($auth) {
- $auth = \Dever\Helper\Str::decode($auth);
- }
- if (!$auth) {
- $auth = Env::header('authorization');
- }
- if ($auth) {
- $auth = str_replace('Bearer ', '', $auth);
- Dever::session('auth', $auth);
- $info = Secure::checkLogin($auth);
- return $info;
- }
- return false;
- }
- # 获取当前的扩展数据
- public function extend()
- {
- $info = $this->auth();
- if (!$info) {
- $auth = Dever::session('auth');
- if (!$auth) {
- return false;
- }
- $info = Secure::checkLogin($auth);
- }
- if ($info && isset($info['extend'])) {
- return $info['extend'];
- }
- return false;
- }
- # 获取页面类
- public function page($load, $config = array(), $key = 'list', $input = true)
- {
- $page = new Page($key, $load, $input, $config);
- return $page;
- }
- # 获取当前使用的系统 一般为数据库隔离使用
- public function system($info = false, $module = true, $field = false)
- {
- if (!$info) {
- $info = $this->extend();
- }
- if ($info && isset($info['info_id']) && isset($info['partition'])) {
- # 这里后续增加从数据库中获取
- $value = $info['system_id'] . '_' . $info['info_id'];
- $result = array();
- if (strpos($info['partition'], '.')) {
- $temp = explode('.', $info['partition']);
- $result = array($temp[0] => $value);
- if ($module && isset($info['data_id']) && $info['data_id']) {
- if ($temp[0] == $temp[1]) {
- $result[$temp[0]] .= '/' . $info['module_id'] . '_' . $info['data_id'];
- } else {
- $result[$temp[1]] = $info['module_id'] . '_' . $info['data_id'];
- }
- }
- } else {
- $result = array($info['partition'] => $value);
- }
- if ($field) {
- $result['field'] = Dever::call($field);
- }
- return $result;
- }
- return false;
- }
- # 获取token需要用到的key
- public function getToken()
- {
- $extend = $this->extend();
- if ($extend) {
- return implode('-', array_values($extend));
- }
- return '';
- }
- # 将token设置到route权限中,方便后续读取
- public function setAuth($partition, $system_id, $info_id, $module_id, $data_id)
- {
- $token = Dever::load('common', 'manage')->token(-1, '', $partition, $system_id, $info_id, $module_id, $data_id);
- \Dever\Route::$data['authorization'] = Secure::encode($token['token']);
- }
- # 生成token
- public function token($uid, $mobile, $partition, $system_id, $info_id, $module_id, $data_id)
- {
- $extend['partition'] = $partition;
- $extend['system_id'] = $system_id;
- $extend['info_id'] = $info_id;
- $extend['module_id'] = $module_id;
- $extend['data_id'] = $data_id;
- if ($uid && $uid > 0) {
- $select['uid'] = $uid;
- $select['system_id'] = $system_id;
- $select['info_id'] = $info_id;
- $info = Dever::db('system_user', 'manage')->find($select);
- $select += $extend;
- if (!$info) {
- Dever::db('system_user', 'manage')->insert($select);
- } else {
- Dever::db('system_user', 'manage')->update($info['id'], $select);
- }
- }
- return array('token' => Secure::login($uid, $extend));
- }
- # 生成密码
- public function createPwd($password)
- {
- $data['salt'] = Str::salt(8);
- $data['password'] = $this->hash($password, $data['salt']);
- return $data;
- }
- # 生成时间
- public function crateDate($date)
- {
- return Date::mktime($date);
- }
- # hash加密
- public function hash($password, $salt)
- {
- return hash('sha256', $password . $salt);
- }
- # 自动更新key
- public function updateKey($db, $data)
- {
- if ($data['name'] && !$data['key']) {
- if (Dever::project('pinyin')) {
- $where = array();
- if (isset($data['id']) && $data['id']) {
- $where['id'] = array('!=', $data['id']);
- }
- $data['key'] = Dever::load('convert', 'pinyin')->getPinyin($data['name']);
- # 检查是否存在
- $where['key'] = $data['key'];
- $info = $db->find($where);
- if ($info) {
- $data['key'] .= '-' . date('YmdHis');
- }
- }
- }
- return $data;
- }
- # 设置联动
- public function cascader($total, $func)
- {
- $total = Dever::input('total', 'is_numeric', '联动总数', $total);
- $level = Dever::input('level', 'is_numeric', '联动级别', 1);
- $parent = Dever::input('parent', 'is_numeric', '联动ID', 0);
- if ($parent < 0) {
- Dever::error('error');
- }
- $data = $func($level, $parent);
- if ($level >= $total) {
- foreach ($data as &$v) {
- $v['leaf'] = true;
- }
- }
- $result['total'] = $total;
- $result['list'] = $data;
- return $result;
- }
- # 根据load获取db
- public function db($load)
- {
- $menu = array();
- $load = explode('/', ltrim($load, '/'));
- if (isset($load[2])) {
- $app = $load[1];
- $table = $load[2];
- } else {
- $app = $load[0];
- $table = $load[1];
- }
- $parent = Dever::db('menu', 'manage')->find(array('key' => $app));
- if ($parent) {
- $menu = Dever::db('menu', 'manage')->find(array('parent_id' => $parent['id'], 'key' => $table));
- if ($menu) {
- $app = $menu['app'];
- }
- }
- $set = Dever::project($app);
- $manage = $set['path'] . 'manage/'.$table.'.php';
- if (is_file($manage)) {
- $manage = include $manage;
- if ($source = Dever::issets($manage, 'source')) {
- if (strpos($source, '/')) {
- $source = explode('/', $source);
- $app = $source[0];
- $table = $source[1];
- } else {
- $table = $source;
- }
- }
- }
- $db = Dever::db($table, $app);
- $db->config['manage'] = $manage;
- return array($db, $menu);
- }
- # 获取项目
- public function project()
- {
- $result = array();
- $app = \Dever\Project::read();
- foreach ($app as $k => $v) {
- $result[] = array
- (
- 'id' => $k,
- 'name' => $v['lang'] ?? $k,
- );
- }
- return $result;
- }
- # 仅为测试用
- public function out($data)
- {
- $result = array();
- $result['head'] = array('id', '姓名', '时间');
- $result['body'] = array();
- foreach ($data['body'] as $k => $v) {
- $result['body'][$k] = array($v['id'], $v['name'], $v['cdate']);
- }
- return $result;
- }
- # 仅为测试用,展示表格更多内容
- public function show($data)
- {
- # 返回类型:string字符串,table表格,card卡片,desc描述,list列表
- $result['type'] = 'string';
- $result['content'] = 'ddddd';
- $result['type'] = 'table';
- $result['head'] = array('id' => 'id', 'name' => '姓名');
- $result['body'] = array
- (
- array('id' => '1', 'name' => 'test'),
- array('id' => '2', 'name' => 'test2'),
- );
- $result['type'] = 'card';
- $result['content'][] = array
- (
- 'title' => '订单信息',
- 'shadow' => 'never',//always | never | hover
- 'content' => array
- (
- '1111',
- '2222',
- ),
- );
- $result['type'] = 'list';
- $result['content'] = array
- (
- array('日志类型', 'test'),
- array('姓名', 'test1'),
- );
- return $result;
- }
- # 仅为测试用,展示详情
- public function view($page)
- {
- # 这里获取基本信息
- //print_r($page->info);die;
- $info[] = array
- (
- # 类型,desc描述 table表格,表格有head和body即可
- 'type' => 'desc',
- 'name' => '基本信息',
- # 每行展示数量
- 'column' => 4,
- # 是否有边框
- 'border' => true,
- # 排列方向:horizontal横向 vertical纵向
- 'direction' => 'vertical',
- # 右侧按钮
- 'button' => array
- (
- array
- (
- 'name' => '编辑',
- # fastedit、fastadd、oper、api、link、route,参数与list里的data_button一致,多了一个load,可以单独设置路由
- 'type' => 'fastedit',
- 'path' => 'platform/role',
- # 增加权限,第三个参数是排序,建议大一些
- //'func' => $page->getFunc('view_fastedit', '详情页-编辑角色', 1000),
- # 这里是按钮用到的参数数据
- 'row' => array
- (
- 'id' => 1,
- ),
- ),
- ),
- # 具体内容
- 'content' => array
- (
- array
- (
- 'name' => '标题',
- # 类型,text普通文本,tag标签,link链接,image图片 progress进度条 stat统计 timeline时间线 table表格
- 'type' => 'text',
- 'content' => '内容',
- # 样式primary success warning danger info exception
- 'style' => 'primary',
- ),
- array
- (
- 'name' => '标题',
- 'type' => 'tag',
- 'content' => '内容',
- 'style' => 'warning',
- ),
- array
- (
- 'name' => '标题',
- 'type' => 'link',
- 'content' => '内容',
- ),
- array
- (
- 'name' => '图片',
- 'type' => 'image',
- 'content' => 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
- # 'fill', 'contain', 'cover', 'none', 'scale-down'
- 'fit' => 'fill',
- ),
- array
- (
- 'name' => '进度条',
- 'type' => 'progress',
- 'content' => '10',
- 'style' => 'exception',
- 'width' => '20',
- 'inside' => true,
- # line dashboard 仪表盘 circle 圆形
- 'show' => 'line',
- # 开启条纹
- 'striped' => true,
- # 开启动画
- 'indeterminate' => true,
- ),
- array
- (
- 'name' => '统计',
- 'type' => 'stat',
- 'content' => array
- (
- array
- (
- # 一共24
- 'span' => '6',
- 'name' => '测试',
- 'value' => '1000',
- ),
- array
- (
- 'span' => '6',
- 'name' => '测试1',
- 'value' => '1000',
- ),
- array
- (
- 'span' => '6',
- 'name' => '测试2',
- 'value' => '1000',
- ),
- array
- (
- 'span' => '6',
- 'name' => '测试2',
- 'value' => '1000',
- ),
- ),
- ),
- array
- (
- 'name' => '时间线',
- 'type' => 'timeline',
- 'content' => array
- (
- array
- (
- 'time' => '2020-10-11',
- 'name' => '测试',
- 'color' => '#0bbd87',
- 'size' => 'large',
- 'type' => 'primary',
- 'hollow' => true,
- ),
- array
- (
- 'time' => '2020-10-11',
- 'name' => '测试',
- ),
- array
- (
- 'time' => '2020-10-11',
- 'name' => '测试',
- ),
- array
- (
- 'time' => '2020-10-11',
- 'name' => '测试',
- ),
- ),
- ),
- array
- (
- 'name' => '表格',
- 'type' => 'table',
- 'border' => true,
- 'height' => '200',
- 'head' => array
- (
- array
- (
- 'key' => 'name',
- 'name' => '姓名',
- 'fixed' => 'fixed',
- ),
- array
- (
- 'key' => 'desc',
- 'name' => '描述',
- 'fixed' => 'fixed',
- ),
- ),
- 'button' => array
- (
- array
- (
- 'name' => '编辑',
- 'type' => 'fastedit',
- 'load' => 'platform/role',
- ),
- ),
- 'body' => array
- (
- array
- (
- 'id' => 1,
- 'name' => 'test',
- 'desc' => 'dfdf',
- ),
- ),
- ),
- ),
- );
- $info[] = array
- (
- 'type' => 'table',
- 'name' => '表格信息',
- 'border' => true,
- 'height' => '200',
- 'head' => array
- (
- array
- (
- 'key' => 'name',
- 'name' => '姓名',
- 'fixed' => 'fixed',
- ),
- array
- (
- 'key' => 'desc',
- 'name' => '描述',
- 'fixed' => 'fixed',
- ),
- ),
- 'button' => array
- (
- array
- (
- 'name' => '编辑',
- 'type' => 'fastedit',
- 'load' => 'platform/role',
- # 增加权限,第三个参数是排序,建议大一些
- 'func' => $page->getFunc('view_fastedit', '详情页-编辑角色', 1000),
- ),
- ),
- 'body' => array
- (
- array
- (
- 'id' => 1,
- 'name' => 'test',
- 'desc' => 'dfdf',
- ),
- ),
- );
- $tab = array
- (
- 'active' => 'table1',
- 'content' => array
- (
- 'table1' => array
- (
- # 这里跟desc一样
- 'name' => '标题',
- 'type' => 'text',
- 'content' => '内容',
- 'style' => 'primary',
- ),
- 'tab2' => array
- (
- 'name' => '表格',
- 'type' => 'table',
- 'border' => true,
- 'height' => '200',
- 'head' => array
- (
- array
- (
- 'key' => 'name',
- 'name' => '姓名',
- 'fixed' => 'fixed',
- ),
- array
- (
- 'key' => 'desc',
- 'name' => '描述',
- 'fixed' => 'fixed',
- ),
- ),
- 'button' => array
- (
- array
- (
- 'name' => '编辑',
- 'type' => 'fastedit',
- 'load' => 'platform/role',
- ),
- ),
- 'body' => array
- (
- array
- (
- 'id' => 1,
- 'name' => 'test',
- 'desc' => 'dfdf',
- ),
- ),
- ),
- )
- );
- return array('info' => $info, 'tab' => $tab);
- }
- public function stat($where)
- {
- return array
- (
- array
- (
- # 一共24
- 'span' => '8',
- 'name' => '测试',
- 'value' => '1000',
- ),
- array
- (
- 'span' => '8',
- 'name' => '测试1',
- 'value' => '1000',
- ),
- array
- (
- 'span' => '8',
- 'name' => '测试2',
- 'value' => '1000',
- ),
- );
- }
- }
|