Base.php 8.4 KB

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