123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?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 get($col)
- {
- return Dever::input(['set', 'field'])[$col] ?? 0;
- }
- 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 = 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()
- {
-
- $auth = $this->get('authorization');
- if ($auth) {
- $auth = Str::decode($auth);
- $info = Secure::checkLogin($auth);
- if ($info) {
- return $info['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 = [], $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 = [];
- if (strpos($info['partition'], '.')) {
- $temp = explode('.', $info['partition']);
- $result = $this->partition($result, $temp[0], $info['system_key'], $value);
- if ($module && isset($info['data_id']) && $info['data_id']) {
- if ($temp[0] == $temp[1]) {
- $value .= '/' . $info['module_id'] . '_' . $info['data_id'];
- $result = $this->partition($result, $temp[0], $info['system_key'], $value);
- } else {
- $result = $this->partition($result, $temp[1], $info['system_key'], $info['module_id'] . '_' . $info['data_id']);
- }
- }
- } else {
- $result = $this->partition($result, $info['partition'], $info['system_key'], $value);
- }
- if ($field) {
- $result['field'] = Dever::call($field);
- }
- return $result;
- }
- return false;
- }
-
- private function partition(&$result, $type, $key, $value)
- {
- if ($type == 'field') {
- $result[$type] = [
- 'type' => 'key',
- 'field' => $key,
- 'value' => $value,
- ];
- } elseif ($type == 'where') {
- $result[$type] = [
- $key => $value
- ];
- } else {
- $result[$type] = $value;
- }
- return $result;
- }
-
- public function getToken()
- {
- $extend = $this->extend();
- if ($extend) {
- return implode('-', array_values($extend));
- }
- return '';
- }
-
- public function setAuth($system, $module_id, $info_id, $data_id = '')
- {
- if (is_string($system)) {
- $system = Dever::db('system', 'manage')->find(['key' => $system]);
- }
- if (is_string($module_id)) {
- $module_id = Dever::db('system_module', 'manage')->column(['key' => $module_id], 'id');
- }
- $token = $this->token(-1, '', $system['partition'], $system['key'], $system['id'], $info_id, $module_id, $data_id);
- return \Dever\Route::$data['authorization'] = Secure::encode($token['token']);
- }
-
- public function token($uid, $mobile, $partition, $system_key, $system_id, $info_id, $module_id, $data_id)
- {
- $extend['partition'] = $partition;
- $extend['system_key'] = $system_key;
- $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);
- }
-
- public function hash($password, $salt)
- {
- return hash('sha256', $password . $salt);
- }
-
- public function updateKey($db, $data)
- {
- if ($data['name'] && !$data['key']) {
- if (Dever::project('pinyin')) {
- $where = [];
- if (isset($data['id']) && $data['id']) {
- $where['id'] = ['!=', $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', 'isset', '联动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;
- }
-
- public function db($load)
- {
- $menu = [];
- $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(['key' => $app]);
- if ($parent) {
- $menu = Dever::db('menu', 'manage')->find(['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 [$db, $menu];
- }
-
- public function project()
- {
- $result = [];
- $app = \Dever\Project::read();
- foreach ($app as $k => $v) {
- $result[] = [
- 'id' => $k,
- 'name' => $v['lang'] ?? $k,
- ];
- }
- return $result;
- }
- }
|