Login.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: TABLE ME
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-25 17:23
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\system\controller;
  13. use app\BaseViewController;
  14. use app\model\api\OrderInfo;
  15. use app\model\system\Order;
  16. use app\model\system\Product;
  17. use app\model\system\SystemAdmin;
  18. use app\model\system\SystemMenu;
  19. use app\Request;
  20. use library\exceptions\GeneralException;
  21. use library\lib\hupun;
  22. use library\lib\weixina;
  23. use library\services\UtilService;
  24. use library\utils\Captcha;
  25. use library\utils\Qiniu;
  26. use think\facade\Db;
  27. class Login extends BaseViewController
  28. {
  29. /**
  30. * 用户登录端
  31. * @param Request $request
  32. */
  33. public function index(Request $request)
  34. {
  35. [$account, $pwd, $imgcode] = UtilService::getMore([
  36. ['account','','empty','请输入登录账户'],
  37. ['pwd','','empty','请输入登录密码'],
  38. ['imgcode','','empty','验证码不能为空'],
  39. ],$request,true);
  40. if (!(new Captcha)->check($imgcode)) {
  41. return app('json')->fail('验证码错误,请重新输入');
  42. }
  43. //获取登录
  44. $adminInfo = SystemAdmin::login($account,$pwd);
  45. if(empty($adminInfo)) {
  46. return app('json')->fail(SystemAdmin::getErrorInfo('用户名错误,请重新输入'));
  47. }
  48. //生成令牌
  49. $token = SystemAdmin::createToken($adminInfo, 'admin');
  50. if(empty($token)) {
  51. return app('json')->fail(SystemAdmin::getErrorInfo());
  52. }
  53. //获取管理菜单
  54. $menuMenu = new SystemMenu();
  55. $menus = $menuMenu->getRoute($adminInfo->role_id);
  56. return app('json')->success([
  57. 'token' => $token['token'],
  58. 'expires_time' => $token['params']['exp'],
  59. 'menus' => $menus,
  60. 'user_info' => [
  61. 'id' => $adminInfo->getData('id'),
  62. 'username' => $adminInfo->getData('username'),
  63. 'name' => $adminInfo->getData('name'),
  64. 'avatar' => $adminInfo->getData('avatar')
  65. ],
  66. ]);
  67. }
  68. /**
  69. * 用户发生退出
  70. */
  71. public function logut(Request $request){
  72. echo 'a';
  73. var_dump($request->post());exit;
  74. }
  75. /**
  76. * 验证码
  77. * @return \app\adminapi\controller\Login|\think\Response
  78. */
  79. public function captcha()
  80. {
  81. return (new Captcha())->create();
  82. }
  83. /**
  84. * 绑定数据
  85. */
  86. public function system_bind(Request $request){
  87. $token = $request->get('token');
  88. if(empty($token)) {
  89. $this->assign('error','token 错误,请重新扫码绑定!');
  90. return $this->display('binderror');
  91. }
  92. $token = str_replace(" ","+",$token);
  93. $str = crypto_decrypt(base64_decode($token),'md5_token');
  94. if(empty($str)) {
  95. $this->assign('error','token 错误,请重新扫码绑定!');
  96. return $this->display('binderror');
  97. }
  98. $strAr = explode('|',$str);
  99. if(count($strAr) != 2) {
  100. $this->assign('error','数据出错!');
  101. return $this->display('binderror');
  102. }
  103. $w = $this->weixinLogin();
  104. if(!$w[1]) {
  105. return $w[0];
  106. }
  107. $this->assign('user',$w[0]);
  108. $this->assign('token',$token);
  109. return $this->display('bindqrcode');
  110. }
  111. public function system_bind_data(Request $request){
  112. $token = $request->post('token');
  113. if(empty($token)) {
  114. return app('json')->fail('token 错误,请重新扫码绑定!');
  115. }
  116. $token = str_replace(" ","+",$token);
  117. $str = crypto_decrypt(base64_decode($token),'md5_token');
  118. if(empty($str)) {
  119. return app('json')->fail('token 错误,请重新扫码绑定!');
  120. }
  121. $w = $this->weixinLogin();
  122. if(!$w[1]) {
  123. return $w[0];
  124. }
  125. $userInfo = $w[0];
  126. $count = Db::name("weixin_push_user")
  127. ->where('sassid',0)
  128. ->where('openid',$userInfo['openid'])
  129. ->count();
  130. if($count <= 0) {
  131. $d['type'] = 'system';
  132. $d['sassid'] = 0;
  133. $d['uid'] = 0;
  134. $d['time'] = time();
  135. $d['avatar'] = $userInfo['headimgurl'];
  136. $d['nickname'] = $userInfo['nickname'];
  137. $d['openid'] = $userInfo['openid'];
  138. Db::name("weixin_push_user")->insert($d);
  139. }
  140. return app('json')->success('操作成功');
  141. }
  142. public function bindsuccess(){
  143. return $this->display();
  144. }
  145. /**
  146. * 微信登录板顶
  147. */
  148. private function weixinLogin() {
  149. $weixinUser = cookie('weix_userinfo');
  150. if(!empty($weixinUser)) return [unserialize($weixinUser),true];
  151. $domain = \request()->url();
  152. cookie('w_url',$domain);
  153. $weixinA = new weixina;
  154. return [$weixinA->oauth('login'),false];
  155. }
  156. }