Login.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace app\mobile\controller;
  3. use app\mobile\MobileBaseController;
  4. class Login extends MobileBaseController
  5. {
  6. public function login()
  7. {
  8. halt('https://www.jucai.gov.cn/api/auth/wechat_auth?url=' . urlencode(url('/mobile/login/wechatBack')));
  9. return redirect('https://www.jucai.gov.cn/api/auth/wechat_auth?url=' . urlencode(url('/mobile/login/wechatBack')));
  10. }
  11. /**
  12. * 微信回调
  13. */
  14. public function wechatBack()
  15. {
  16. $param = input('param.');
  17. halt($param);
  18. $open_id = $param['openid'];
  19. //登录
  20. $user = UserModel::where(['openid' => $open_id])->find();
  21. if (empty($user)) {
  22. $user = UserModel::create([
  23. 'nickname' => $param['nickname'],
  24. 'avatar' => $param['headimgurl'],
  25. 'openid' => $open_id,
  26. 'unionid' => $param['unionid'],
  27. 'logintime' => time(),
  28. 'loginip' => request()->ip(),
  29. ]);
  30. } else {
  31. $user->logintime = time();
  32. $user->loginip = request()->ip();
  33. $user->save();
  34. }
  35. session('mobile.user.id', $user['id']);
  36. $back_url = '/';
  37. if (session('?back_url')) {
  38. $back_url = session('back_url');
  39. session('back_url', null);
  40. }
  41. return redirect($back_url);
  42. }
  43. public function login1()
  44. {
  45. session('mobile.user.id', 1);
  46. $back_url = '/';
  47. if (session('?back_url')) {
  48. $back_url = session('back_url');
  49. session('back_url', null);
  50. }
  51. return redirect($back_url);
  52. }
  53. public function logout()
  54. {
  55. session('mobile.user.id', null);
  56. return '退出';
  57. }
  58. }