Auth.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. namespace store\Admin;
  3. use Dever;
  4. use Dever\Routing\Uri;
  5. use Dever\Session\Oper;
  6. use Dever\String\Encrypt;
  7. class Auth
  8. {
  9. /**
  10. * 后台的session名
  11. *
  12. * @var string
  13. */
  14. private $name;
  15. /**
  16. * save
  17. *
  18. * @var Dever\Plad\Save
  19. */
  20. private $save;
  21. /**
  22. * __construct
  23. *
  24. * @return mixed
  25. */
  26. public function __construct()
  27. {
  28. $this->save = new Oper(false, 'session');
  29. $this->name = 'store';
  30. }
  31. /**
  32. * 获取当前登录的管理员信息
  33. *
  34. * @return mixed
  35. */
  36. public function info()
  37. {
  38. $info = $this->save->get($this->name);
  39. return $info;
  40. }
  41. public function check()
  42. {
  43. $admin = $this->info();
  44. if (!$admin) {
  45. return $this->location_login();
  46. }
  47. return $admin;
  48. }
  49. /**
  50. * 只获取中间的内容部分
  51. *
  52. * @return mixed
  53. */
  54. public function loading()
  55. {
  56. $set = Dever::input('loading');
  57. if ($set) {
  58. Dever::config('base')->url = 'loading=' . $set;
  59. }
  60. return $set;
  61. }
  62. public function config()
  63. {
  64. $config = Dever::db('main/factory_config')->find();
  65. $config['refer'] = Dever::input('refer');
  66. $config['login_url'] = Dever::url("store/admin/auth.login");
  67. $config['mcode_url'] = Dever::url("store/admin/auth.getMcode?json=1");
  68. return $config;
  69. }
  70. /**
  71. * 获取当前登录的管理员信息
  72. *
  73. * @return mixed
  74. */
  75. public function manage()
  76. {
  77. $admin = $this->info();
  78. if ($admin && $admin['id'] == 1) {
  79. return '';
  80. }
  81. return 'display:none;';
  82. }
  83. /**
  84. * 获取手机随机验证码
  85. * @return mixed
  86. */
  87. public function getMcode_api()
  88. {
  89. $mobile = $this->checkMobile();
  90. $user = Dever::db('store/member')->find(array('mobile' => $mobile));
  91. if (!$user) {
  92. Dever::alert('该手机号还未注册');
  93. }
  94. $code = Dever::load('passport/reg')->mcode($mobile);
  95. $msg = '验证码已发送至您的手机,请注意查收,十分钟之内有效';
  96. $debug = Dever::config('base', 'project')->mobileCode['debug'];
  97. if ($debug) {
  98. $msg .= '::' . $code;
  99. }
  100. return $msg;
  101. }
  102. /**
  103. * 退出登录
  104. *
  105. * @return mixed
  106. */
  107. public function quit_api()
  108. {
  109. if ($this->info()) {
  110. $this->save->un($this->name);
  111. }
  112. Dever::location('login');
  113. }
  114. /**
  115. * 登录
  116. *
  117. * @return mixed
  118. */
  119. public function login_api()
  120. {
  121. $where['mobile'] = $this->checkMobile();
  122. $this->checkMcode($where['mobile']);
  123. $user = Dever::db('store/member')->find($where);
  124. if (!$user) {
  125. Dever::alert('登录失败,手机号错误,请重新输入');
  126. }
  127. if ($user) {
  128. $this->save($user);
  129. $refer = Dever::input('refer');
  130. if ($refer) {
  131. $refer = Encrypt::decode($refer);
  132. Dever::out($refer);
  133. } else {
  134. Dever::out(Dever::url('home'));
  135. }
  136. } else {
  137. Dever::alert('登录失败');
  138. }
  139. }
  140. /**
  141. * 验证手机号
  142. * @return mixed
  143. */
  144. private function checkMobile()
  145. {
  146. $rule = Dever::rule('mobile');
  147. $mobile = Dever::input('mobile');
  148. if (!$mobile) {
  149. Dever::alert('手机号不能为空');
  150. }
  151. if (!preg_match($rule, $mobile)) {
  152. Dever::alert('手机号码格式错误');
  153. }
  154. return $mobile;
  155. }
  156. /**
  157. * 验证验证码
  158. * @return mixed
  159. */
  160. private function checkMcode($mobile)
  161. {
  162. $code = Dever::input('mcode');
  163. if (!$code) {
  164. Dever::alert('请输入验证码');
  165. }
  166. $code = Dever::load('passport/reg')->mcode($mobile, $code);
  167. if (!$code) {
  168. Dever::alert('验证码输入错误');
  169. }
  170. }
  171. public function save(&$user)
  172. {
  173. $this->save->add($this->name, $user, 3600 * 24 * 7);
  174. }
  175. /**
  176. * cookie 处理cookie
  177. *
  178. * @return array
  179. */
  180. public function cookie()
  181. {
  182. $cookie = $_COOKIE;
  183. $array = array();
  184. # 判断手机版
  185. $mobile = Dever::mobile();
  186. if ((isset($cookie['left_menu']) && $cookie['left_menu'] == 'close') || $mobile) {
  187. $array['wrapper'] = 'enlarged forced';
  188. } else {
  189. $array['wrapper'] = 'forced';
  190. }
  191. return $array;
  192. }
  193. /**
  194. * menu 对一些固定的菜单,生成其链接
  195. *
  196. * @return array
  197. */
  198. public function link()
  199. {
  200. return array
  201. (
  202. 'project' => Dever::url('project/list?menu=manage&table=manage_project'),
  203. 'log' => Dever::url('log/list?menu=manage&table=manage_log'),
  204. 'logout' => Dever::url('admin/auth.quit'),
  205. 'index' => Dever::url('home'),
  206. 'console' => Dever::url('console'),
  207. );
  208. }
  209. /**
  210. * location_login
  211. *
  212. * @return mixed
  213. */
  214. public function location_login()
  215. {
  216. $refer = Encrypt::encode(Dever::url());
  217. return Dever::location('store/login?refer=' . $refer);
  218. }
  219. # 编辑员工
  220. public function edit_api()
  221. {
  222. $admin = $this->info();
  223. if (!$admin) {
  224. Dever::alert('您没有权限修改');
  225. }
  226. $id = $admin['id'];
  227. $name = Dever::input('name');
  228. $mobile = Dever::input('mobile');
  229. if ($name) {
  230. $update['name'] = $name;
  231. } else {
  232. Dever::alert('姓名不能为空');
  233. }
  234. if ($mobile) {
  235. $update['mobile'] = $mobile;
  236. $check = Dever::db('store/member')->find(array('mobile' => $mobile, 'store_id' => $admin['store_id']));
  237. if ($check && $id && $id != $check['id']) {
  238. Dever::alert('电话已存在');
  239. } elseif ($check && !$id) {
  240. Dever::alert('电话已存在');
  241. }
  242. } else {
  243. Dever::alert('电话不能为空');
  244. }
  245. $update['store_id'] = $admin['store_id'];
  246. if ($id) {
  247. $update['where_id'] = $id;
  248. Dever::db('store/member')->update($update);
  249. } else {
  250. $id = Dever::db('store/member')->insert($update);
  251. }
  252. $user = Dever::db('store/member')->find($id);
  253. $this->save($user);
  254. return '操作成功';
  255. }
  256. public function menu()
  257. {
  258. $info = $this->info();
  259. if ($info && Dever::project('message')) {
  260. $this->msg = Dever::load('message/lib/data')->num($info['store_id'], 3);
  261. } else {
  262. $this->msg = 0;
  263. }
  264. $data = array
  265. (
  266. 'console' => array
  267. (
  268. 'icon' => 'home',
  269. 'name' => '概况',
  270. 'link' => Dever::url('console', 'store'),
  271. ),
  272. 'order' => array
  273. (
  274. 'icon' => 'app',
  275. 'name' => '订单处理',
  276. 'child' => array
  277. (
  278. 'order' => array
  279. (
  280. 'name' => '门店订货单',
  281. 'link' => Dever::url('order', 'store'),
  282. ),
  283. 'factory_order' => array
  284. (
  285. 'name' => '入库订单确认',
  286. 'link' => Dever::url('factory_order', 'store'),
  287. ),
  288. 'ku' => array
  289. (
  290. 'name' => '库存清单',
  291. 'link' => Dever::url('goods', 'store'),
  292. ),
  293. 'out' => array
  294. (
  295. 'name' => '出库单管理',
  296. 'link' => Dever::url('out', 'store'),
  297. ),
  298. ),
  299. ),
  300. 'stat' => array
  301. (
  302. 'icon' => 'date',
  303. 'name' => '结算对账',
  304. 'child' => array
  305. (
  306. 'stat' => array
  307. (
  308. 'name' => '对账单',
  309. 'link' => Dever::url('stat', 'store'),
  310. ),
  311. ),
  312. ),
  313. 'set' => array
  314. (
  315. 'icon' => 'set',
  316. 'name' => '基础设置',
  317. 'child' => array
  318. (
  319. 'set' => array
  320. (
  321. 'name' => '企业资质',
  322. 'link' => Dever::url('set', 'store'),
  323. ),
  324. 'msg' => array
  325. (
  326. 'name' => '消息通知',
  327. 'link' => Dever::url('msg', 'store'),
  328. ),
  329. 'user' => array
  330. (
  331. 'name' => '账号管理',
  332. 'link' => Dever::url('user', 'store'),
  333. ),
  334. 'help' => array
  335. (
  336. 'name' => '帮助文档',
  337. 'link' => Dever::url('help', 'store'),
  338. ),
  339. ),
  340. ),
  341. );
  342. return $data;
  343. }
  344. /**
  345. * menu 生成左侧的菜单 此处应加上权限来显示是否管理员有该菜单的权限
  346. *
  347. * @return array
  348. */
  349. public function getMenu($state = false)
  350. {
  351. $data = $this->menu();
  352. $html = '';
  353. foreach ($data as $k => $v) {
  354. if (!isset($v['key'])) {
  355. $v['key'] = $k;
  356. }
  357. $v['icon'] = $v['icon'] ? "layui-icon layui-icon-" . $v['icon'] : "layui-icon layui-icon-home";
  358. $cur = 'layui-nav-itemed';
  359. if ($k == 'console') {
  360. $cur = 'layui-this';
  361. }
  362. $html .= '<li data-name="'.$v['key'].'" class="layui-nav-item menu_group '.$cur.'">';
  363. if (isset($v['child']) && $v['child']) {
  364. $link = 'href="javascript:;"';
  365. } else {
  366. $link = 'lay-href="'.$v['link'].'"';
  367. }
  368. $html .= '<a '.$link.' lay-tips="'.$v['name'].'" lay-direction="2">';
  369. $html .= '<i class="'.$v['icon'].'"></i><cite>'.$v['name'].'</cite></a>';
  370. $html .= $this->getMenuChild($v);
  371. $html .= '</li>';
  372. }
  373. $html .= '';
  374. return $html;
  375. }
  376. public function getMenuChild($v)
  377. {
  378. $html = '';
  379. if (isset($v['child']) && $v['child']) {
  380. $html .= '<dl class="layui-nav-child">';
  381. foreach ($v['child'] as $k1 => $v1) {
  382. if (!isset($v1['key'])) {
  383. $v1['key'] = $k1;
  384. }
  385. $html .= '<dd data-name="'.$v1['key'].'">';
  386. if (isset($v1['child']) && $v1['child']) {
  387. $html .= '<a href="javascript:;">'.$v1['name'].'</a><dl class="layui-nav-child">';
  388. foreach ($v1['child'] as $k2 => $v2) {
  389. if (!isset($v2['key'])) {
  390. $v2['key'] = $k2;
  391. }
  392. $html .= '<dd data-name="'.$v2['key'].'"><a lay-href="'.$v2['link'].'">'.$v2['name'].'</a></dd>';
  393. }
  394. $html .= '</dl>';
  395. } else {
  396. if ($k1 == 'msg' && $this->msg) {
  397. $v1['name'] .= '<span class="layui-badge">'.$this->msg.'</span>';
  398. }
  399. $html .= '<a lay-href="'.$v1['link'].'">'.$v1['name'].'</a>';
  400. }
  401. $html .= '</dd>';
  402. }
  403. $html .= '</dl>';
  404. }
  405. return $html;
  406. }
  407. }