LoginController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\love\controller;
  3. use app\love\model\ActiveApplyModel;
  4. use app\love\model\ActiveModel;
  5. use app\love\model\UserMatingModel;
  6. use app\love\model\UserModel;
  7. use cmf\controller\HomeBaseController;
  8. use think\facade\Session;
  9. class LoginController extends HomeBaseController
  10. {
  11. /**
  12. * 登录
  13. */
  14. public function index()
  15. {
  16. $userId = cmf_get_current_user_id();
  17. $active_id = $this->request->param('active_id', 0);
  18. if (!empty($active_id)) {
  19. Session::set('active_id', $active_id);
  20. }
  21. if (!empty($userId)) {
  22. $user = UserModel::get($userId);
  23. if (!empty($user)) {
  24. if ($user['user_status'] == 0) {
  25. $this->redirect(cmf_url('love/register/ban'));
  26. }
  27. //完善资料
  28. if ($user['is_complete'] == 2) {
  29. if (empty($user['idcard'])) {
  30. $this->redirect(cmf_url('love/register/idcard'));
  31. } else {
  32. $this->redirect(cmf_url('love/register/intro'));
  33. }
  34. }
  35. if ($active_id) {
  36. $this->redirect(url('love/active/detail') . "?id={$active_id}");
  37. } else {
  38. $this->redirect(url('/'));
  39. }
  40. }
  41. }
  42. $this->redirect('https://www.jucai.gov.cn/api/auth/wechat_auth?url=' . urlencode(url('love/login/wechatBack', '', true, true)));
  43. }
  44. /**
  45. * 微信回调
  46. */
  47. public function wechatBack()
  48. {
  49. $param = $this->request->param();
  50. $open_id = $param['openid'];
  51. //登录
  52. $user = UserModel::get(['open_id' => $open_id]);
  53. if (empty($user)) {
  54. $user = UserModel::create([
  55. 'user_type' => 2,
  56. 'open_id' => $open_id,
  57. // 'username' => $param['nickname'],
  58. // 'avatar' => $param['headimgurl'],
  59. 'create_time' => time(),
  60. ]);
  61. UserMatingModel::create(['user_id' => $user['id']]);
  62. //活动
  63. $active_id = Session::pull('active_id');
  64. if (!empty($active_id)) {
  65. $active = ActiveModel::get($active_id);
  66. if (!empty($active)) {
  67. ActiveApplyModel::create([
  68. 'active_id' => $active_id,
  69. 'user_id' => $user['id'],
  70. 'create_time' => time(),
  71. 'check_status' => 2,
  72. 'check_time' => time(),
  73. ]);
  74. }
  75. }
  76. }
  77. session('user', $user->toArray());
  78. $data = [
  79. 'last_login_time' => time(),
  80. 'last_login_ip' => get_client_ip(0, true),
  81. ];
  82. UserModel::update($data, ['id' => $user['id']]);
  83. $token = cmf_generate_user_token($user["id"], 'mobile');
  84. if (!empty($token)) {
  85. session('token', $token);
  86. }
  87. $domain = Session::get('domain');
  88. $this->redirect($domain ?: '/');
  89. }
  90. public function login1()
  91. {
  92. $user = UserModel::get(3);
  93. session('user', $user->toArray());
  94. $token = cmf_generate_user_token(3, 'mobile');
  95. if (!empty($token)) {
  96. session('token', $token);
  97. }
  98. return 'OK';
  99. }
  100. /**
  101. * 退出登录
  102. */
  103. public function logout()
  104. {
  105. session("user", null);//只有前台用户退出
  106. return redirect($this->request->root() . "/");
  107. }
  108. }