Base.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\FuncNotFoundException;
  4. use think\exception\ValidateException;
  5. use think\facade\Request;
  6. use think\facade\Cache;
  7. use app\model\UsersSessions;
  8. use app\model\Users;
  9. use app\model\Store;
  10. use app\model\TextReplace;
  11. use app\model\Operatingcity;
  12. use app\model\Tuanzhang;
  13. class Base extends \app\BaseController
  14. {
  15. protected $userInfo = [];
  16. protected $sid = '';
  17. protected $ocid = '';
  18. protected $tzid = '';
  19. protected $console = '';
  20. protected $actionurl = '';
  21. protected function initialize()
  22. {
  23. $controller = $this->request->controller();
  24. $action = $this->request->action();
  25. $app = app('http')->getName();
  26. $url = "{$app}/{$controller}/{$action}";
  27. $url = strtolower($url);
  28. $this->actionurl = $url;
  29. if (!in_array($url, array_map('strtolower', config('my.nocheck')))) {
  30. $this->checkTokenAuth();
  31. }
  32. }
  33. //设置token
  34. protected function setToken($data)
  35. {
  36. $weid = $data['weid'];
  37. $data = serialize($data);
  38. $token = md5(uniqid());
  39. //登录的时候把token写入数据表
  40. $tokenInfo = UsersSessions::where('token', $token)->find();
  41. if (empty($tokenInfo)) {
  42. UsersSessions::create([
  43. 'weid' => $weid,
  44. 'token' => $token,
  45. 'ptype' => 'pc',
  46. 'ip' => getRealIP(),
  47. 'expire_time' => time(),
  48. 'data' => $data,
  49. 'status' => 1
  50. ]);
  51. } else {
  52. UsersSessions::where('token', $token)->update([
  53. 'weid' => $weid,
  54. 'token' => $token,
  55. 'ptype' => 'pc',
  56. 'ip' => getRealIP(),
  57. 'expire_time' => time(),
  58. 'data' => $data,
  59. 'status' => 1
  60. ]);
  61. }
  62. return $token;
  63. }
  64. //设置token
  65. protected function getToken()
  66. {
  67. global $_W;
  68. $Authorization = Request::header('Authorization');
  69. $tokenarr = explode('|', $Authorization);
  70. $token = $tokenarr[0];
  71. if (is_array($tokenarr)) {
  72. foreach ($tokenarr as $vo) {
  73. $tmp = explode('=', $vo);
  74. if (is_array($tmp)) {
  75. $arr[$tmp[0]] = $tmp[1];
  76. }
  77. }
  78. }
  79. $this->sid = $arr['sid'];
  80. $this->ocid = $arr['ocid'];
  81. $this->tzid = $arr['tzid'];
  82. $this->console = $arr['console'];
  83. $_W['i'] = $arr['i'];
  84. return $token;
  85. }
  86. //检测token
  87. protected function checkTokenAuth()
  88. {
  89. global $_W;
  90. $token = $this->getToken();
  91. if (!$token) {
  92. abort(101, 'token不能为空');
  93. }
  94. $tokenInfo = UsersSessions::where('token', $token)->find();
  95. if (empty($tokenInfo)) {
  96. abort(101, 'token不存在');
  97. } else {
  98. $tokenInfo = $tokenInfo->toArray();
  99. }
  100. if (!$tokenInfo['status']) {
  101. abort(101, '状态被禁用');
  102. }
  103. if (!$tokenInfo['dev_status']) {
  104. abort(101, '你已下线,账户在其它设备登录!');
  105. }
  106. if (($tokenInfo['expire_time'] - config('my.token_expire_time')) > time()) {
  107. abort(101, '登录状态已过期,请重新登录');
  108. }
  109. $this->userInfo = iunserializer($tokenInfo['data']);
  110. if ($this->console == 1 && (int) $this->userInfo['weid'] > 0) {
  111. abort(101, '你没有控制台的权限');
  112. }
  113. if (!empty($this->userInfo['sid'])) {
  114. $this->sid = $this->userInfo['sid'];
  115. $Store = Store::find($this->sid);
  116. if ($Store && $Store->status == 1) {
  117. } else {
  118. abort(101, '你的商铺被禁用');
  119. }
  120. }
  121. if (!empty($this->userInfo['ocid'])) {
  122. $this->ocid = $this->userInfo['ocid'];
  123. $Operatingcity = Operatingcity::find($this->ocid);
  124. if ($Operatingcity && $Operatingcity->status == 1) {
  125. } else {
  126. abort(101, '你的城市代理被禁用');
  127. }
  128. }
  129. if (!empty($this->userInfo['tzid'])) {
  130. $this->tzid = $this->userInfo['tzid'];
  131. $Tuanzhang = Tuanzhang::find($this->tzid);
  132. if ($Tuanzhang && $Tuanzhang->status == 1) {
  133. } else {
  134. abort(101, '你的帐号被禁用');
  135. }
  136. }
  137. if ((int) $this->userInfo['weid'] > 0) {
  138. $_W['uniacid'] = $this->userInfo['weid'];
  139. } else {
  140. if (!empty($_W['i'])) {
  141. if (!empty($this->userInfo['id'] && (int) $this->userInfo['weid'] == 0)) {
  142. Users::where('id', $this->userInfo['id'])->update(['lastweid' => $_W['i']]);
  143. $this->userInfo['lastweid'] = $_W['i'];
  144. }
  145. }
  146. $_W['uniacid'] = $this->userInfo['lastweid'];
  147. if ($this->console == 1) {
  148. $_W['console'] = $this->console;
  149. $_W['uniacid'] = 0;
  150. }
  151. }
  152. if (file_exists($this->getRoot() . '/data/test.lock')) {
  153. if ($this->userInfo['username'] != 'admin' && in_array($this->actionurl, array_map('strtolower', config('my.testnoupdate')))) {
  154. throw new ValidateException('演示站不能修改核心设置!');
  155. }
  156. }
  157. $_W['w7copyright'] = $this->userInfo['w7copyright'];
  158. event('DoLog', $this->userInfo['username']); //写入操作日志
  159. }
  160. //获取所有菜单
  161. protected function getTotalMenus()
  162. {
  163. $menu = $this->getBaseMenus();
  164. $order_array = array_column($menu, 'sort'); //数组排序
  165. array_multisort($order_array, SORT_ASC, $menu);
  166. return $menu;
  167. }
  168. //返回当前应用的菜单列表
  169. protected function getBaseMenus()
  170. {
  171. $where = [];
  172. $where[] = ['type', 'in', [0, 1]];
  173. $where[] = ['status', '=', 1];
  174. $query = \app\model\AdminMenu::where($where);
  175. if (!empty($this->sid)) {
  176. $query->where('is_store', 1);
  177. } elseif (!empty($this->ocid)) {
  178. $query->where('is_city', 1);
  179. } elseif (!empty($this->tzid)) {
  180. $query->where('is_tuanzhang', 1);
  181. } elseif ($this->console == 1) {
  182. $query->where('is_console', 1);
  183. } else {
  184. $query->where('is_admin', 1);
  185. }
  186. if (config('database.app_name') == config('my.app_v2')) {
  187. $query->where('is_v2', 1);
  188. $version = 'v2';
  189. }
  190. if (config('database.app_name') == config('my.app_v3')) {
  191. $query->where('is_v3', 1);
  192. $version = 'v3';
  193. }
  194. if (config('database.app_name') == config('my.app_v6')) {
  195. $query->where('is_v6', 1);
  196. $version = 'v6';
  197. }
  198. if (!empty($this->userInfo['w7copyright'])) {
  199. $query->where('w7_hidden', 0);
  200. }
  201. $list = $query->order('sort asc')->select()->toArray();
  202. if ($list) {
  203. foreach ($list as $key => $val) {
  204. //$menus[$key]['name'] = $val['path'];
  205. $menus[$key]['name'] = $val['title'];
  206. $menus[$key]['pid'] = $val['pid'];
  207. $menus[$key]['id'] = $val['id'];
  208. $menus[$key]['title'] = $val['title'];
  209. $menus[$key]['sort'] = $val['sort'];
  210. $menus[$key]['icon'] = $val['icon'] ? $val['icon'] : 'el-icon-menu';
  211. $menus[$key]['path'] = $val['path'];
  212. $menus[$key]['pages_path'] = $val['pages_path'];
  213. if (empty($menus[$key]['pages_path'])) {
  214. $menus[$key]['pages_path'] = '';
  215. }
  216. //vue3
  217. $menus[$key]['component'] = str_replace('.vue', '', $val['pages_path']);
  218. $menus[$key]['paths'] = $val['path'];
  219. $menus[$key]['perms'] = $val['path'];
  220. $menus[$key]['selected'] = $val['selected'];
  221. $menus[$key]['params'] = $val['params'];
  222. $menus[$key]['is_show'] = $val['is_show'];
  223. $menus[$key]['is_cache'] = $val['is_cache'];
  224. if ($val['type'] == 0) {
  225. $menus[$key]['type'] = 'M';
  226. } elseif ($val['type'] == 1) {
  227. $menus[$key]['type'] = 'C';
  228. unset($menus[$key]['children']);
  229. } elseif ($val['type'] == 2) {
  230. $menus[$key]['type'] = 'A';
  231. }
  232. }
  233. $retmenus = _generateListTree($menus, 0, ['id', 'pid']);
  234. return $retmenus;
  235. }
  236. }
  237. function del($model)
  238. {
  239. $idx = $this->request->post('id', '', 'serach_in');
  240. if (!$idx) throw new ValidateException('参数错误');
  241. if (!is_array($idx)) {
  242. $idx = explode(',', $idx);
  243. }
  244. $model->destroy(['id' => $idx], true);
  245. return $this->json(['msg' => '操作成功']);
  246. }
  247. protected function json($result)
  248. {
  249. if (empty($result['code'])) {
  250. $result['code'] = 2000;
  251. }
  252. if (is_array($result['data'])) {
  253. if ($result['data']['current_page']) {
  254. $result['data']['lists'] = $result['data']['data'];
  255. $result['data']['count'] = $result['data']['total'];
  256. $result['data']['page_no'] = $result['data']['current_page'];
  257. $result['data']['page_size'] = $result['data']['per_page'];
  258. }
  259. }
  260. $result = TextReplace::setreplace($result);
  261. return json($result);
  262. }
  263. //获取要加载的组件
  264. protected function getComponents($menu)
  265. {
  266. $components = [];
  267. foreach ($menu as $v) {
  268. $components[] = [
  269. 'name' => $v['name'],
  270. 'path' => $v['path'],
  271. 'meta' => ['title' => $v['title']],
  272. 'pages_path' => $v['pages_path']
  273. ];
  274. if ($v['children']) {
  275. $components = array_merge($components, $this->getComponents($v['children']));
  276. }
  277. }
  278. return $components;
  279. }
  280. public function getRoot()
  281. {
  282. return dirname(dirname(dirname(__DIR__)));
  283. }
  284. public function __call($method, $args)
  285. {
  286. throw new FuncNotFoundException('方法不存在', $method);
  287. }
  288. }