Common.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. # 获取后台传入的数据
  10. public function get($col)
  11. {
  12. return Dever::input(['set', 'field'])[$col] ?? 0;
  13. }
  14. public function info()
  15. {
  16. $auth = $this->auth();
  17. $system = Dever::db('system', 'manage')->find($auth['extend']['system_id']);
  18. return Dever::db($system['user_table'])->find($auth['uid']);
  19. }
  20. public function auth()
  21. {
  22. $auth = Dever::input('authorization');
  23. if ($auth) {
  24. $auth = Str::decode($auth);
  25. }
  26. if (!$auth) {
  27. $auth = Env::header('authorization');
  28. }
  29. if ($auth) {
  30. $auth = str_replace('Bearer ', '', $auth);
  31. Dever::session('auth', $auth);
  32. $info = Secure::checkLogin($auth);
  33. return $info;
  34. }
  35. return false;
  36. }
  37. # 获取当前的扩展数据
  38. public function extend()
  39. {
  40. # 先从query的set中获取,这个不影响用户登录
  41. $auth = $this->get('authorization');
  42. if ($auth) {
  43. $auth = Str::decode($auth);
  44. $info = Secure::checkLogin($auth);
  45. if ($info) {
  46. return $info['extend'];
  47. }
  48. }
  49. # 从登录里获取
  50. $info = $this->auth();
  51. if (!$info) {
  52. $auth = Dever::session('auth');
  53. if (!$auth) {
  54. return false;
  55. }
  56. $info = Secure::checkLogin($auth);
  57. }
  58. if ($info && isset($info['extend'])) {
  59. return $info['extend'];
  60. }
  61. return false;
  62. }
  63. # 获取页面类
  64. public function page($load, $config = [], $key = 'list', $input = true)
  65. {
  66. $page = new Page($key, $load, $input, $config);
  67. return $page;
  68. }
  69. # 获取当前使用的系统 一般为数据库隔离使用
  70. public function system($info = false, $module = true, $field = false)
  71. {
  72. if (!$info) {
  73. # 单独的数据库隔离,不影响当前登录状态
  74. $info = $this->extend();
  75. }
  76. if ($info && isset($info['info_id']) && isset($info['partition'])) {
  77. # 这里后续增加从数据库中获取
  78. $value = $info['system_id'] . '_' . $info['info_id'];
  79. $result = [];
  80. if (strpos($info['partition'], '.')) {
  81. $temp = explode('.', $info['partition']);
  82. $result = $this->partition($result, $temp[0], $info['system_key'], $value);
  83. if ($module && isset($info['data_id']) && $info['data_id']) {
  84. if ($temp[0] == $temp[1]) {
  85. $value .= '/' . $info['module_id'] . '_' . $info['data_id'];
  86. $result = $this->partition($result, $temp[0], $info['system_key'], $value);
  87. } else {
  88. $result = $this->partition($result, $temp[1], $info['system_key'], $info['module_id'] . '_' . $info['data_id']);
  89. }
  90. }
  91. } else {
  92. $result = $this->partition($result, $info['partition'], $info['system_key'], $value);
  93. }
  94. if ($field) {
  95. $result['field'] = Dever::call($field);
  96. }
  97. return $result;
  98. }
  99. return false;
  100. }
  101. # 设置数据隔离
  102. private function partition(&$result, $type, $key, $value)
  103. {
  104. if ($type == 'field') {
  105. $result[$type] = [
  106. 'type' => 'key',
  107. 'field' => $key,
  108. 'value' => $value,
  109. ];
  110. } elseif ($type == 'where') {
  111. $result[$type] = [
  112. $key => $value
  113. ];
  114. } else {
  115. $result[$type] = $value;
  116. }
  117. return $result;
  118. }
  119. # 获取token需要用到的key
  120. public function getToken()
  121. {
  122. $extend = $this->extend();
  123. if ($extend) {
  124. return implode('-', array_values($extend));
  125. }
  126. return '';
  127. }
  128. # 将token设置到route权限中,方便后续读取
  129. public function setAuth($system, $module_id, $info_id, $data_id = '')
  130. {
  131. if (is_string($system)) {
  132. $system = Dever::db('system', 'manage')->find(['key' => $system]);
  133. }
  134. if (is_string($module_id)) {
  135. $module_id = Dever::db('system_module', 'manage')->column(['key' => $module_id], 'id');
  136. }
  137. $token = $this->token(-1, '', $system['partition'], $system['key'], $system['id'], $info_id, $module_id, $data_id);
  138. return \Dever\Route::$data['authorization'] = Secure::encode($token['token']);
  139. }
  140. # 生成token
  141. public function token($uid, $mobile, $partition, $system_key, $system_id, $info_id, $module_id, $data_id)
  142. {
  143. $extend['partition'] = $partition;
  144. $extend['system_key'] = $system_key;
  145. $extend['system_id'] = $system_id;
  146. $extend['info_id'] = $info_id;
  147. $extend['module_id'] = $module_id;
  148. $extend['data_id'] = $data_id;
  149. if ($uid && $uid > 0) {
  150. $select['uid'] = $uid;
  151. $select['system_id'] = $system_id;
  152. $select['info_id'] = $info_id;
  153. $info = Dever::db('system_user', 'manage')->find($select);
  154. $select += $extend;
  155. if (!$info) {
  156. Dever::db('system_user', 'manage')->insert($select);
  157. } else {
  158. Dever::db('system_user', 'manage')->update($info['id'], $select);
  159. }
  160. }
  161. return array('token' => Secure::login($uid, $extend));
  162. }
  163. # 生成密码
  164. public function createPwd($password)
  165. {
  166. $data['salt'] = Str::salt(8);
  167. $data['password'] = $this->hash($password, $data['salt']);
  168. return $data;
  169. }
  170. # 生成时间
  171. public function crateDate($date)
  172. {
  173. return Date::mktime($date);
  174. }
  175. # hash加密
  176. public function hash($password, $salt)
  177. {
  178. return hash('sha256', $password . $salt);
  179. }
  180. # 自动更新key
  181. public function updateKey($db, $data)
  182. {
  183. if ($data['name'] && !$data['key']) {
  184. if (Dever::project('pinyin')) {
  185. $where = [];
  186. if (isset($data['id']) && $data['id']) {
  187. $where['id'] = ['!=', $data['id']];
  188. }
  189. $data['key'] = Dever::load('convert', 'pinyin')->getPinyin($data['name']);
  190. # 检查是否存在
  191. $where['key'] = $data['key'];
  192. $info = $db->find($where);
  193. if ($info) {
  194. $data['key'] .= '-' . date('YmdHis');
  195. }
  196. }
  197. }
  198. return $data;
  199. }
  200. # 设置联动
  201. public function cascader($total, $func)
  202. {
  203. $total = Dever::input('total', 'is_numeric', '联动总数', $total);
  204. $level = Dever::input('level', 'is_numeric', '联动级别', 1);
  205. $parent = Dever::input('parent', 'isset', '联动ID', 0);
  206. if ($parent < 0) {
  207. Dever::error('error');
  208. }
  209. $data = $func($level, $parent);
  210. if ($level >= $total) {
  211. foreach ($data as &$v) {
  212. $v['leaf'] = true;
  213. }
  214. }
  215. $result['total'] = $total;
  216. $result['list'] = $data;
  217. return $result;
  218. }
  219. # 根据load获取db
  220. public function db($load)
  221. {
  222. $menu = [];
  223. $load = explode('/', ltrim($load, '/'));
  224. if (isset($load[2])) {
  225. $app = $load[1];
  226. $table = $load[2];
  227. } else {
  228. $app = $load[0];
  229. $table = $load[1];
  230. }
  231. $parent = Dever::db('menu', 'manage')->find(['key' => $app]);
  232. if ($parent) {
  233. $menu = Dever::db('menu', 'manage')->find(['parent_id' => $parent['id'], 'key' => $table]);
  234. if ($menu) {
  235. $app = $menu['app'];
  236. }
  237. }
  238. $set = Dever::project($app);
  239. $manage = $set['path'] . 'manage/'.$table.'.php';
  240. if (is_file($manage)) {
  241. $manage = include $manage;
  242. if ($source = Dever::issets($manage, 'source')) {
  243. if (strpos($source, '/')) {
  244. $source = explode('/', $source);
  245. $app = $source[0];
  246. $table = $source[1];
  247. } else {
  248. $table = $source;
  249. }
  250. }
  251. }
  252. $db = Dever::db($table, $app);
  253. $db->config['manage'] = $manage;
  254. return [$db, $menu];
  255. }
  256. # 获取项目
  257. public function project()
  258. {
  259. $result = [];
  260. $app = \Dever\Project::read();
  261. foreach ($app as $k => $v) {
  262. $result[] = [
  263. 'id' => $k,
  264. 'name' => $v['lang'] ?? $k,
  265. ];
  266. }
  267. return $result;
  268. }
  269. }